2012-03-05 172 views

回答

1

做到這一點,最簡單的方法可能是使用頁面事件。要使用頁面事件,你需要繼承PdfPageEventHelper

Public Class CustomPageEventHandler 
    Inherits iTextSharp.text.pdf.PdfPageEventHelper 
End Class 

父類有一堆,你可以重寫方法,就是那個你可能要爲OnStartPage()。通過覆蓋這個,您的自定義代碼將被調用到文檔中的每個頁面。

Public Class CustomPageEventHandler 
    Inherits iTextSharp.text.pdf.PdfPageEventHelper 
    Public Overrides Sub OnStartPage(writer As iTextSharp.text.pdf.PdfWriter, document As iTextSharp.text.Document) 
     ''//Anything you do here will be done on every page 
    End Sub 
End Class 

自定義頁面的事件處理程序綁定到名爲writer你只是做一個PdfWriter對象:

writer.PageEvent = New CustomPageEventHandler() 

至於在OnStartPage()方法做什麼你有一對夫婦的選擇。不幸的是,使用Document對象本身可能會遇到問題。這個對象是爲了處理更高層次的概念而提供的抽象,但並不總是提供一種在特定的x,y座標上做事情的方式。在這裏使用它與文本只會導致文本流動奇怪。

相反,您需要使用原始PdfContentByte對象,您可以從writerDirectContent屬性中獲得該對象。

Dim cb = writer.DirectContent 

一旦你有了,你有兩個選擇,我會告訴你兩個。首先是使用一個ColumnText對象,你幾乎可以認爲它是一個表格。實際上,大多數人將它用作單排單列表。 ColumnText對象很不錯,因爲它允許您使用更高級別的抽象(如Paragraph),並且它會自動處理換行符等內容。

Dim CT As New ColumnText(writer.DirectContent) 
CT.SetSimpleColumn(llx, lly, urx, ury) ''//Coordinates to draw text to 
CT.AddElement(New Paragraph("This goes in the margin")) 
CT.Go() 

第二種選擇是隻畫自己的文字。這最大的缺點是你不能像Paragraph這樣的更高層次的抽象。因此,如果您繪製一段非常長的文本,它不會打破它併爲您開始一個新行,它將繼續在可見文檔之外進行繪製。

Dim cb = writer.DirectContent 
cb.BeginText() 
cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.TIMES_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED), 8) 
cb.SetColorFill(BaseColor.RED) 
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "right margin", x, y, 0) 
cb.EndText() 

把所有一起,下面是一個完整的工作VB.Net 2010 WinForm的應用定位iTextSharp的5.1.2.0,顯示了所有上述的。有關具體詳情,請參閱代碼中的註釋。

Option Strict On 
Option Explicit On 

Imports System.IO 
Imports iTextSharp.text 
Imports iTextSharp.text.pdf 

Public Class Form1 

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
     ''//Test file that we'll create 
     Dim TestFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TestFile.pdf") 
     ''//Standard PDF setup, change as needed for your stream type 
     Using FS As New FileStream(TestFile, FileMode.Create, FileAccess.Write, FileShare.None) 
      Using Doc As New Document(PageSize.LETTER) 
       Using writer = PdfWriter.GetInstance(Doc, FS) 
        ''//Bind our custom event handler to our writer object 
        writer.PageEvent = New CustomPageEventHandler() 
        Doc.Open() 

        ''//Add text on page 1 
        Doc.Add(New Paragraph("Page 1")) 
        ''//Add a new page 
        Doc.NewPage() 
        ''//Add text on page 2 
        Doc.Add(New Paragraph("Page 2")) 

        Doc.Close() 
       End Using 
      End Using 
     End Using 

     Me.Close() 
    End Sub 
    Public Class CustomPageEventHandler 
     Inherits iTextSharp.text.pdf.PdfPageEventHelper 
     ''//This will get called whenever a new page gets added to the document 
     Public Overrides Sub OnStartPage(writer As iTextSharp.text.pdf.PdfWriter, document As iTextSharp.text.Document) 
      ''//Pre-calculate the Y coordinates for use later 
      Dim yBot = document.PageSize.Height - 300 
      Dim yTop = yBot + 200 

      ''//For the left margin we'll use the ColumnText object 
      Dim CT As New ColumnText(writer.DirectContent) 
      ''//Create a single column object bound to our margin and using the y coordinates from above 
      CT.SetSimpleColumn(0, yBot, document.LeftMargin, yTop) 
      ''//Add a simple paragraph 
      CT.AddElement(New Paragraph("This goes in the margin")) 
      ''//Draw the text 
      CT.Go() 


      ''//For the right margin we'll draw the text manually 
      ''//Grab the raw canvas 
      Dim cb = writer.DirectContent 
      ''//Store the current graphics state so that we can restore it later 
      cb.SaveState() 

      ''//Flag that we're begining text 
      cb.BeginText() 
      ''//Set a font and size to draw with 
      cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.TIMES_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED), 8) 
      ''//Set a color for the text 
      cb.SetColorFill(BaseColor.RED) 
      ''//Draw the text at a specific x,y coordinate. NOTE: These commands assume do not break text for you automatically 
      cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "right margin", document.PageSize.Width - document.RightMargin, yTop, 0) 
      ''//Flag that we're doing with our text 
      cb.EndText() 

      ''//Restore the graphics state to whatever it was before we started messing with it 
      cb.RestoreState() 


     End Sub 
    End Class 
End Class