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

Asp.net 如何使用iTextSharp绘制轮廓颜色为红色,内部颜色为灰色的水印文本,asp.net,pdf,itextsharp,Asp.net,Pdf,Itextsharp,如何使用iTextSharp绘制轮廓颜色为红色、内部颜色为灰色的水印文本如果您使用的是水印,我假设您所说的是PdfStamper。如果是这样,一旦您使用GetOverContent()或GetUnderContent()获得了原始的PdfContentByte,您只需要设置几个属性 PdfContentByte.SetLineWidth(单个)-设置笔划厚度 PdfContentByte.SetColorFill(BaseColor.GRAY)-设置填充颜色。您还可以使用任何其他颜色方法,例如

如何使用iTextSharp绘制轮廓颜色为红色、内部颜色为灰色的水印文本如果您使用的是水印,我假设您所说的是
PdfStamper
。如果是这样,一旦您使用
GetOverContent()
GetUnderContent()
获得了原始的
PdfContentByte
,您只需要设置几个属性

  • PdfContentByte.SetLineWidth(单个)
    -设置笔划厚度
  • PdfContentByte.SetColorFill(BaseColor.GRAY)
    -设置填充颜色。您还可以使用任何其他颜色方法,例如
    SetRGBColorFill()
    SetCMYKColorFill()
  • PdfContentByte.SetColorStroke(BaseColor.RED)
    -设置笔划颜色
  • PdfContentByte.SetTextRenderingMode(PdfContentByte.TEXT\u RENDER\u MODE\u FILL\u 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

请不要在寻求帮助时发短信“Very Urjent”/“Immediate”等。这很有效。非常感谢你。我缺少PdfContentByte.SetTextRenderingMode(PdfContentByte.TEXT\u RENDER\u MODE\u FILL\u STROKE)