2011-12-29 124 views
0

我將水印文本添加到我創建的類庫中的PDF中。我在下面發佈的代碼工作正常,但水印有時難以閱讀,因爲它與PDF上的內容重疊。我會如何着手在水印文本週圍添加白色背景顏色?我基本上想要將水印文本包圍在文本大小的白色矩形內。由於iTextSharp將背景顏色添加到水印文本

Public Function AddWatermarkText(ByVal tempDirectory As String) As String 
    ' Just return the full path of the PDF if we don't need to add a watermark. 
    If Me.Document.RevRank <> 0 OrElse Me.Document.ReleaseDate Is Nothing Then Return Me.FullPath 

    Dim reader As iTextSharp.text.pdf.PdfReader = Nothing 
    Dim stamper As iTextSharp.text.pdf.PdfStamper = Nothing 
    Dim gstate As New iTextSharp.text.pdf.PdfGState() 
    Dim overContent As iTextSharp.text.pdf.PdfContentByte = Nothing 
    Dim rect As iTextSharp.text.Rectangle = Nothing 
    Dim watermarkFont As iTextSharp.text.pdf.BaseFont = Nothing 
    Dim folderGuid As Guid = Guid.NewGuid() 
    Dim outputFile As String = tempDirectory & System.IO.Path.DirectorySeparatorChar & folderGuid.ToString() & System.IO.Path.DirectorySeparatorChar _ 
           & Me.Document.Prefix & Me.Document.BaseNumber & Me.Document.Revision & ".pdf" 

    ' Create the temp directory to place the new PDF in. 
    If Not My.Computer.FileSystem.DirectoryExists(tempDirectory) Then My.Computer.FileSystem.CreateDirectory(tempDirectory) 
    My.Computer.FileSystem.CreateDirectory(tempDirectory & System.IO.Path.DirectorySeparatorChar & folderGuid.ToString()) 

    reader = New iTextSharp.text.pdf.PdfReader(Me.FullPath) 
    rect = reader.GetPageSizeWithRotation(1) 
    stamper = New iTextSharp.text.pdf.PdfStamper(reader, New System.IO.FileStream(outputFile, IO.FileMode.Create)) 
    watermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA_BOLD, _ 
                iTextSharp.text.pdf.BaseFont.CP1252, _ 
                iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED) 
    gstate.FillOpacity = 0.9F 
    gstate.StrokeOpacity = 1.0F 

    ' Add the watermark to each page in the document. 
    For i As Integer = 1 To reader.NumberOfPages() 
     overContent = stamper.GetOverContent(i) 
     With overContent 
      .SaveState() 
      .SetGState(gstate) 
      .SetColorFill(iTextSharp.text.BaseColor.BLUE) 
      .Fill() 
      .BeginText() 
      .SetFontAndSize(watermarkFont, 8) 
      .SetTextMatrix(30, 30) 

      If Me.Document.RevRank = 0 AndAlso Me.Document.ReleaseDate IsNot Nothing Then 
       .ShowTextAligned(iTextSharp.text.Element.ALIGN_LEFT, UCase(String.Format("CONTROLLED DOCUMENT – THIS COPY IS THE LATEST REVISION AS OF {0}" _ 
                         , Date.Now.ToString("ddMMMyyyy"))), 10, rect.Height - 15, 0) 
      End If 

      .Fill() 
      .EndText() 
      .RestoreState() 
     End With 
    Next 

    stamper.Close() 
    reader.Close() 

    Return outputFile 
End Function 

回答

1

我通常喜歡的代碼,你可以在撲通但不幸的是你的代碼是有點過於特定領域提供一個直接的答案(很多Me.*,我們必須在猜測),但我仍然可以通過一些代碼重構來讓你在那裏。

要做你想做的事你必須測量你正在繪製的字符串,然後畫一個矩形到這些尺寸。 PDF規範沒有文本的「背景顏色」概念,並且任何使其看起來像它的實現都只是爲您繪製矩形。所以,第一個(是的,你可以突出顯示文本但是這是一個註釋是不同的。)

我要拉的東西出來到變量,使我們可以重新使用,並調整它們更容易:

''//Text to measure and draw 
Dim myText As String = UCase(String.Format("CONTROLLED DOCUMENT – THIS COPY IS THE LATEST REVISION AS OF {0}", Date.Now.ToString("ddMMMyyyy"))) 
''//Font size to measure and draw with 
Dim TextFontSize As Integer = 8 
''//Original X,Y positions that we were drawing the text at 
Dim TextX As Single = 10 
Dim TextY As Single = rect.Height - 15 

下一頁我們需要計算寬度和高度。前者很容易,但後者要求我們首先獲得文本的上升和下降,然後計算差異。

''//Calculate the width 
Dim TextWidth As Single = watermarkFont.GetWidthPoint(myText, TextFontSize) 
''//Calculate the ascent and decent 
Dim TextAscent As Single = watermarkFont.GetAscentPoint(myText, TextFontSize) 
Dim TextDescent As Single = watermarkFont.GetDescentPoint(myText, TextFontSize) 
''//The height is the difference between the two 
Dim TextHeight As Single = TextAscent - TextDescent 

(注:我不知道,如果GetWidthPoint(),根據需要與多行文本GetAscentPoint()GetDescentPoint()工作。)

,那麼你可能希望有框和文本之間的一些填充:

''//Amount of padding around the text when drawing the box 
Dim TextPadding As Single = 2 

最後,在你的地方設置並繪製你想先繪製矩形文本:

''//Set a background color 
.SetColorFill(BaseColor.YELLOW) 
''//Create a rectangle 
.Rectangle(TextX - TextPadding, TextY - TextPadding, TextWidth + (TextPadding * 2), TextHeight + (TextPadding * 2)) 
''//Fill it 
.Fill() 
+0

對不起,我在查看這篇文章,並錯誤地將鼠標移過去,點擊此答案的downvote按鈕。我不是故意的。事實上,我發現這個答案非常有幫助。現在,我正試圖恢復它,但它不會讓我。它說,「你的投票被鎖定,直到有人編輯這個答案。」 – 2014-06-03 04:05:29