【问题标题】:ITextSharp include all pages from the input fileITextSharp 包含输入文件中的所有页面
【发布时间】:2013-02-19 12:27:30
【问题描述】:

我通过使用 itextsharp 库使用以下代码将文本添加到 pdf。(从链接 ITextSharp insert text to an existing pdf 获取代码

    Dim reader As New PdfReader(oldFile)
    Dim size As iTextSharp.text.Rectangle = reader.GetPageSizeWithRotation(1)
    Dim document As New iTextSharp.text.Document(size)

    ' open the writer
    Dim fs As New FileStream(newFile, FileMode.Create, FileAccess.Write)
    Dim writer As PdfWriter = PdfWriter.GetInstance(document, fs)
    document.Open()

    ' the pdf content
    Dim cb As PdfContentByte = writer.DirectContent

    ' select the font properties
    Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.ZAPFDINGBATS, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
    'cb.SetColorFill(GrayColor.DARK_GRAY)
    cb.SetFontAndSize(bf, 8)
    cb.BeginText()
    Dim Text As String = "l"
    ' put the alignment and coordinates here
    cb.ShowTextAligned(2, Text, 84, 729, 0)
    cb.EndText()

    Dim bf1 As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
    cb.SetFontAndSize(bf1, 8)
    cb.BeginText()
    Dim text1 As String = "Navaneeth A"
    cb.ShowTextAligned(1, text1, 65, 690, 0)
    cb.EndText()

    ' create the new page and add it to the pdf
    Dim page As PdfImportedPage = writer.GetImportedPage(reader, 1)
    cb.AddTemplate(page, 0, 0)

    ' close the streams and voilá the file should be changed :)
    document.Close()
    fs.Close()
    writer.Close()
    reader.Close()

现在的问题是源 pdf 大约有 5 页。但是此代码生成的输出文件只有第一页。那么如何在输出文件中包含源文件的所有页面? 源pdf链接是http://law.incometaxindia.gov.in/DITTaxmann/IncomeTaxRules/PDF/Ay-2012-2013/SAHAJ2012_14.pdf


 Dim reader As New PdfReader(oldFile)
    Using ms = New MemoryStream()
        Dim stamper As New PdfStamper(reader, ms)
        'Using stamper  'As New PdfStamper(reader, ms)
        stamper.RotateContents = False
        Dim canvas As PdfContentByte = stamper.GetOverContent(1)
        ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, New Phrase("Hello people!"), 36, 540, 0)
        'End Using

        Dim result As Byte() = ms.ToArray()
        File.WriteAllBytes(newFile, result)
        System.Diagnostics.Process.Start(newFile)
    End Using

我做了以下更改,但它不起作用。结果文件只是一个 1kb 文件。

【问题讨论】:

    标签: vb.net pdf adobe itextsharp itext


    【解决方案1】:

    不幸的是,您发现了不应使用的示例代码。要操作现有的 PDF,您应该使用 PdfStamper,而不是 PdfWriter

    您的代码(即使在更正后复制所有页面)不会复制交互式功能(表单、其他注释...)。您应该将您的代码基于Webified iTextSharp Example StampText.cschapter 6iText in Action — 2nd Edition 中解释:

    PdfReader reader = new PdfReader(resource);
    using (var ms = new MemoryStream()) {
      using (PdfStamper stamper = new PdfStamper(reader, ms)) {
        stamper.RotateContents = false;
        PdfContentByte canvas = stamper.GetOverContent(1);
        ColumnText.ShowTextAligned(
          canvas,
          Element.ALIGN_LEFT, 
          new Phrase("Hello people!"), 
          36, 540, 0
        );
      }
      byte[] result = ms.ToArray();
    }    
    

    如果您像这样更改代码,您也可以控制字体和颜色:

    [...]
    Font FONT = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD, new GrayColor(0.75f));
    PdfContentByte canvas = stamper.GetOverContent(1);
    ColumnText.ShowTextAligned(
      canvas,
      Element.ALIGN_LEFT, 
      new Phrase("Hello people!", FONT), 
      36, 540, 0
    );
    [...]
    

    PS如果您由于某些原因不得不使用一些旧的 iTextSharp 版本(您提出的其他问题似乎暗示您使用的是 VB6...),一些细节可能会有所不同。尽管如此,你还是应该改用PdfStamper

    【讨论】:

    • 很好的答案。为什么很多人喜欢复制错误的示例代码是个谜。请注意,可以在这里找到示例:tinyurl.com/iiacsCH06 将 06 更改为 00 到 16 之间的任意数字,以获取其他章节的示例。
    • @mkl 请在这里查看我的问题stackoverflow.com/questions/15241337/…
    • @winman 您应该开始接受答案,以使您认为有用的内容变得透明。
    猜你喜欢
    • 2018-02-21
    • 2023-03-31
    • 2016-04-30
    • 2013-03-06
    • 1970-01-01
    • 2012-09-15
    • 2012-04-03
    • 2019-10-11
    • 2012-09-10
    相关资源
    最近更新 更多