【问题标题】:iTextSharp PdfWriter Rotating page layout when it shouldnt beiTextSharp PdfWriter 不应该旋转页面布局
【发布时间】:2014-06-17 10:29:46
【问题描述】:

我有一个程序,它采用 pdf 并使用 Itextsharp 和 PdfWriter 将文本打印到第一页。目前,这对于我必须输入文本的每个 pdf 都按预期工作。但是,当源 pdf 的布局为横向时,作者在将文本输入到 pdf 的第一页后将布局旋转为纵向。我找不到关于为什么在 pdf 上输入文本后默认布局更改为纵向的文档。这种旋转导致信息最终在右侧被切断,因为原始布局是横向的。

我查看了涉及 PdfStamper 的其他答案,但在处理现有代码以配合我正在做的事情时遇到了麻烦。我对 C#、pdf 操作和 iTextSharp 编程相当陌生。 pdf 上可突出显示的文本的最终目标。

//Adds white invisible text to the pdf document that is highlightable
public static void stamp(string pdfName, string filePath, string textToStamp)
{
    //Make a Temporary copy of the original to manipulate
    string tempPath = @"C:\TemporaryFilePath\" + pdfName + "";
    File.Copy(filePath, tempPath);
    //Make a new reader using the copied source file
    PdfReader reader = new PdfReader(tempPath);
    using (Document document = new Document())
    {
        using (PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create)))
        {
            document.Open();
            int numofpages = reader.NumberOfPages;
            for (int p = 1; p <= numofpages; p++)
            {
                //create the ContentByte to give the text a position on the first page
                PdfContentByte cb = writer.DirectContent;
                //Get the page to work with
                PdfImportedPage page = writer.GetImportedPage(reader, p);

                document.NewPage();
                cb.AddTemplate(page, 0, 20);
                var FontColour = new BaseColor(255, 255, 255);
                var MyFont = FontFactory.GetFont("Times New Roman", 12, FontColour);
                //Gets the first page and sends the text to the upper left corner
                if (p == 1)
                {
                    if (TextToStamp!= null)
                    {
                        document.Add(new Paragraph("Hello World", MyFont));
                    }
                }
                document.Close();
            }
        }
        reader.Close();
        File.Delete(tempPath);
    }

任何您想添加的 cmets 或建议,请随意!谢谢

【问题讨论】:

  • 你可能不应该使用PdfWriter。当您说您已阅读文档时,您的意思是您已阅读manning.com/lowagie2/samplechapter6.pdf 如果是,请解释该章中没有明确的内容,以便我在编写第三版时确保正确解释。 (请注意,您的要求远未明确:您想突出显示现有文本?您将如何检索该文本的坐标?)
  • 抱歉要求含糊不清。目的只是在第一页的左上角区域添加文本。压模难以理解的是如何限制添加到特定页面的信息,但仍将其余页面保留在 PDF 中。除非您建议我使用阅读器仅获取第一页,然后将其重新加入文档中。

标签: c# pdf itextsharp pdf-writer


【解决方案1】:

在阅读Bruno所指的http://manning.com/lowagie2/samplechapter6.pdf时,特别注意6.3.1,其中解释了如何使用PdfStamper在某个页面的某个位置添加文字。它还显示了两种横向页面之间的区别:旋转页面和宽度 > 高度的页面。

完整的代码示例可以在这里找到:http://www.itextpdf.com/examples/iia.php?id=117

【讨论】:

    【解决方案2】:

    使用 Bruno 发布的信息,我想出了这个解决方案。无论布局是什么,这都允许在页面上标记信息,并对其进行少量定制。

    public static void AddText(string pdfName, string filePath, string textToStamp, float? x = null, float? y = null)
    {
        //x and y are used to position the text and allow multiple different templates to use the same method
        //Designate the Temporary source to be used
        string tempPath = @"C:\TemporaryFilePath\" + pdfName + "";
        //Copy to file to the source path
        File.Copy(filePath, tempPath);
        PdfReader reader = new PdfReader(tempPath);
        iTextSharp.text.Rectangle pageSize = reader.GetPageSizeWithRotation(1);
        //Convert the pageHeight into a float
        int pageHeight = Convert.ToInt32(pageSize.Height);
        PdfStamper stamper = new PdfStamper(reader, new FileStream(filePath, FileMode.Create));
    
        PdfContentByte canvas = stamper.GetOverContent(1);
        //Set a default value if x and y have no value 
        if (x.HasValue == false)
        {
            x = 35;
        }
        if (y.HasValue == false)
        {
            y = 30;
        }
        //choose the font type 
        var FontColour = new BaseColor(255, 255, 255);
        var MyFont = FontFactory.GetFont("Times New Roman", 10, FontColour);
        ColumnText.ShowTextAligned
                    (canvas, Element.ALIGN_LEFT, new Phrase("Hello World", MyFont), (float)x, pageHeight - (float)y, 0);
    
        stamper.Close();
        reader.Close();
        File.Delete(tempPath);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 2013-10-17
      相关资源
      最近更新 更多