如何使用iTextSharp绘制轮廓颜色为红色、内部颜色为灰色的水印文本

发布于 2024-12-11 11:21:20 字数 43 浏览 0 评论 0原文

如何使用iTextSharp绘制轮廓颜色为红色、内部颜色为灰色的水印文本

How to draw watermark text whose outline color is red and inner color is gray using iTextSharp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不…忘初心 2024-12-18 11:21:20

如果您使用水印,我假设您正在谈论 PdfStamper。如果是这样,当您使用 GetOverContent()GetUnderContent() 获得原始 PdfContentByte 时,您只需要几个属性设置。

  • PdfContentByte.SetLineWidth(single) - 设置描边粗细
  • PdfContentByte.SetColorFill(BaseColor.GRAY) - 设置填充颜色。您还可以使用任何其他颜色方法,例如 SetRGBColorFill()SetCMYKColorFill()
  • PdfContentByte.SetColorStroke(BaseColor.RED) - 设置描边颜色
  • PdfContentByte.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE) - 您想要绘制文本的标志带有填充和描边

下面是一个完整的 WinForms 应用程序,目标是 iTextSharp 5.1.1.0,它将所有内容组合在一起。您应该能够相当轻松地将其移至 ASP.Net,并在需要时将其转换为 C#。

Option Explicit On
Option Strict On

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

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ''//Our sample files
        Dim InputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf")
        Dim OutputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test_W_Watermark.pdf")

        ''//Create our input file to watermark later, nothing special here
        Using FS As New FileStream(InputFile, FileMode.Create, FileAccess.Write, FileShare.Read)
            Using Doc As New Document(PageSize.LETTER)
                Using W = PdfWriter.GetInstance(Doc, FS)
                    Doc.Open()

                    Doc.Add(New Paragraph("This is a test"))

                    Doc.Close()
                End Using
            End Using
        End Using


        ''//Watermark the file that we create above

        ''//Bind a reader to our input file
        Dim R As New PdfReader(InputFile)
        ''//Create our output file stream
        Using FS As New FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.Read)
            ''//Bind a stamper to our output file stream
            Using stamper As New PdfStamper(R, FS)
                ''//Grab the raw content byte to draw with
                Dim cb = stamper.GetOverContent(1)

                ''//Flag that we are starting text commands
                cb.BeginText()

                ''//Set the stroke width
                cb.SetLineWidth(2)

                ''//Set the fill (inner) color for the font
                cb.SetColorFill(BaseColor.GRAY)

                ''//Set the stroke (outer) color for the font
                cb.SetColorStroke(BaseColor.RED)

                ''//Flag that when drawing text the system should use both a fill and a stroke
                cb.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE)

                ''//Set a font to draw with
                cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED), 50)

                ''//Tell the system to start drawing at the center of the first page
                cb.SetTextMatrix(R.GetPageSize(1).Width / 2, R.GetPageSize(1).Height / 2)

                ''//Draw the actual text
                cb.ShowText("Hello")

                ''//Flag that we are done drawing text
                cb.EndText()
            End Using
        End Using
        Me.Close()
    End Sub
End Class

If you are using a watermark I'm assuming that you are talking about a PdfStamper. If so, once you've got a raw PdfContentByte using either GetOverContent() or GetUnderContent() there's just a couple of properties that you need to set.

  • PdfContentByte.SetLineWidth(single) - set the stroke thickness
  • PdfContentByte.SetColorFill(BaseColor.GRAY) - set the fill color. You can also use any of the other color methods such as SetRGBColorFill() or SetCMYKColorFill()
  • PdfContentByte.SetColorStroke(BaseColor.RED) - set the stroke color
  • PdfContentByte.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE) - flag that you want text to be draw with both a fill and a stroke

Below is a full working WinForms app targetting iTextSharp 5.1.1.0 that puts it all together. You should be able to move this to ASP.Net fairly easily as well as convert it to C# if needed.

Option Explicit On
Option Strict On

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

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ''//Our sample files
        Dim InputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf")
        Dim OutputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test_W_Watermark.pdf")

        ''//Create our input file to watermark later, nothing special here
        Using FS As New FileStream(InputFile, FileMode.Create, FileAccess.Write, FileShare.Read)
            Using Doc As New Document(PageSize.LETTER)
                Using W = PdfWriter.GetInstance(Doc, FS)
                    Doc.Open()

                    Doc.Add(New Paragraph("This is a test"))

                    Doc.Close()
                End Using
            End Using
        End Using


        ''//Watermark the file that we create above

        ''//Bind a reader to our input file
        Dim R As New PdfReader(InputFile)
        ''//Create our output file stream
        Using FS As New FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.Read)
            ''//Bind a stamper to our output file stream
            Using stamper As New PdfStamper(R, FS)
                ''//Grab the raw content byte to draw with
                Dim cb = stamper.GetOverContent(1)

                ''//Flag that we are starting text commands
                cb.BeginText()

                ''//Set the stroke width
                cb.SetLineWidth(2)

                ''//Set the fill (inner) color for the font
                cb.SetColorFill(BaseColor.GRAY)

                ''//Set the stroke (outer) color for the font
                cb.SetColorStroke(BaseColor.RED)

                ''//Flag that when drawing text the system should use both a fill and a stroke
                cb.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE)

                ''//Set a font to draw with
                cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED), 50)

                ''//Tell the system to start drawing at the center of the first page
                cb.SetTextMatrix(R.GetPageSize(1).Width / 2, R.GetPageSize(1).Height / 2)

                ''//Draw the actual text
                cb.ShowText("Hello")

                ''//Flag that we are done drawing text
                cb.EndText()
            End Using
        End Using
        Me.Close()
    End Sub
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文