2011-12-29 50 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ボタンをクリックしました。私は意味しなかった。実際、私はこの答えが非常に有用であることを発見しました。今、私はそれをupvoteしようとしているが、それは私をさせません。 「あなたの投票は、誰かがこの回答を編集するまでロックされています。 –

関連する問題