Vb.net iTextSharp向水印文本添加背景色

Vb.net iTextSharp向水印文本添加背景色,vb.net,itextsharp,Vb.net,Itextsharp,我正在将水印文本添加到我创建的类库中的PDF中。我在下面发布的代码运行良好,但是水印有时很难读取,因为它与PDF上的内容重叠。如何在水印文本周围添加白色背景色?我基本上希望水印文本被包围在文本大小的白色矩形内。谢谢 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

我正在将水印文本添加到我创建的类库中的PDF中。我在下面发布的代码运行良好,但是水印有时很难读取,因为它与PDF上的内容重叠。如何在水印文本周围添加白色背景色?我基本上希望水印文本被包围在文本大小的白色矩形内。谢谢

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
Public函数AddWatermarkText(ByVal tempDirectory作为字符串)作为字符串
'如果不需要添加水印,只需返回PDF的完整路径即可。
如果Me.Document.RevRank 0或lse Me.Document.ReleaseDate为空,则返回Me.FullPath
将读卡器调暗为iTextSharp.text.pdf.PdfReader=无
根据iTextSharp.text.pdf.PdfStamper将压模调暗=无
Dim gstate作为新的iTextSharp.text.pdf.PdfGState()
Dim overContent As iTextSharp.text.pdf.PdfContentByte=无
Dim rect As iTextSharp.text.Rectangle=无
暗淡水印字体为iTextSharp.text.pdf.BaseFont=无
Dim folderGuid作为Guid=Guid.NewGuid()
Dim outputFile As String=tempDirectory&System.IO.Path.directoryseportorchar&folderGuid.ToString()&System.IO.Path.directoryseportorchar_
&Me.Document.Prefix&Me.Document.BaseNumber&Me.Document.Revision&“.pdf”
'创建临时目录以放置新PDF。
如果不存在My.Computer.FileSystem.directory(tempDirectory),则存在My.Computer.FileSystem.CreateDirectory(tempDirectory)
My.Computer.FileSystem.CreateDirectory(tempDirectory&System.IO.Path.directoryseportorchar&folderGuid.ToString())
reader=New iTextSharp.text.pdf.PdfReader(Me.FullPath)
rect=reader.getPageSizeWebRotation(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
'将水印添加到文档中的每一页。
对于reader.NumberOfPages()
过多内容=母版。GetOverContent(i)
心满意足
.SaveState()
.SetGState(gstate)
.SetColorFill(iTextSharp.text.BaseColor.BLUE)
.Fill()
.BeginText()
.SetFontAndSize(水印字体,8)
.SetTextMatrix(30,30)
如果Me.Document.RevRank=0,并且Me.Document.ReleaseDate也不是空的,那么
.ShowTextAligned(iTextSharp.text.Element.ALIGN_LEFT,UCase(String.Format)(“受控文档–此副本是截至{0}的最新版本”_
,Date.Now.ToString(“ddMMMyyyy”)),10,矩形高度-15,0)
如果结束
.Fill()
.EndText()
1.房地产
以
下一个
母版关闭()
reader.Close()
返回输出文件
端函数

我通常喜欢那些你可以随意插入的代码,但不幸的是,你的代码有点过于领域特定,无法提供直接的答案(我们不得不猜测大量的
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()

对不起,我正在查看这篇文章,不小心我的鼠标滑过并点击了这个答案上的向下投票按钮。我不是故意的。事实上,我发现这个答案非常有用。现在,我想把它投回去,但它不让我这么做。它说,“在有人编辑此答案之前,您的投票是锁定的。”