2013-03-05 53 views
0

我用iTextSharp的庫添加文本到PDF用下面的代碼的所有頁面。(接到鏈接代碼ITextSharp insert text to an existing pdfiTextSharp的包括從輸入文件

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 pages.But由此代碼生成的輸出文件只有第一頁。所以,我怎麼能包括源文件的所有頁面中輸出文件? 代碼的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 

我做了以下更改,但其不working.result文件只是一個1kb的文件。

回答

1

您遺憾地發現不應使用的示例代碼。要操縱現有的PDF,您應該使用PdfStamper,而不是PdfWriter

您的代碼(即使在複製所有頁面後進行更正)也不會複製交互功能(表單,其他註釋...)。而應該立足您對Webified iTextSharp ExampleStampText.cs代碼的iText in Action — 2nd Editionchapter 6解釋說:

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

+1

很好的答案。爲什麼很多人喜歡複製錯誤的示例代碼是一個謎。請注意,可以在此處找到示例:http://tinyurl.com/iiacsCH06將06更改爲00至16之間的任意數字,以獲取其他章節中的示例。 – 2013-03-05 09:28:45

+0

@mkl請在這裏看到我的問題http://stackoverflow.com/questions/15241337/pdf-setting-the-font-color-to-the-text – 2013-03-06 08:19:06

+0

@winman你應該開始接受答案,使透明你認爲有用。 – mkl 2013-03-06 08:34:17