使用iText填充文本字段背景

时间:2013-03-11 16:15:28

标签: itextsharp

我正在尝试使用iText填充文本字段的颜色。我尝试过setfieldproperty,它不适用于bgcolor或fill color属性。我正在寻找的是文本字段的属性设置,因为它将覆盖现有的文本或图像

我最后尝试了几个案例..

    ' Create a new PDF reader based on the PDF template document
    Dim pdfReaderBG As PdfReader = New PdfReader(pdfTemplate) ' Page of Fields
    Dim pdfReaderFG As PdfReader = New PdfReader(pdfExisting) ' Image from CD Image

    'Create the stream for the new PDF Document with the BackGround PDf
    Dim writer As PdfStamper = New PdfStamper(pdfReaderBG, New FileStream("c:\temp\CDs\newMerge.pdf", FileMode.Create))

    'Get all the content of the page
    Dim content_Byte As PdfContentByte = writer.GetUnderContent(1)

    'Then get the Other PDF to overlay the other
    Dim mark_page As PdfImportedPage = writer.GetImportedPage(pdfReaderFG, 1)

    If (mark_page.Width > mark_page.Height) Then 'Check to see if it is in Landscape
        content_Byte.AddTemplate(mark_page, 0, -1, 1, 0, 0, mark_page.Width)
    Else
        'Then add the content to the new page over the Image
        content_Byte.AddTemplate(mark_page, 0, 0)
    End If

    Dim formFields As AcroFields = writer.AcroFields

    formFields.SetFieldProperty("cd28", "borderColor", BaseColor.GREEN, Nothing)
    'content_Byte.te(BaseColor.PINK)

    **formFields.SetFieldProperty("cd28", "backgroundcolor", BaseColor.YELLOW, Nothing)
    'formFields.setfieldproperty("cd28") ' SetFieldProperty("cd28", "bgColor", BaseColor.WHITE, Nothing)**

我只想更改一个文本字段背景的颜色

手动编辑文档中的文本字段时。属性的外观选项卡。它具有填充颜色和边框颜色的属性 我能够做边框颜色.. 我似乎无法在代码中执行填充颜色属性。

1 个答案:

答案 0 :(得分:2)

背景颜色存储在MK键下的表单字段的显示窗口小部件中(PDF Spec 12.5.6.19)

下面是以两种不同方式设置背景颜色的示例代码。第一个块创建一个全新的PDF并直接向其添加一个表单字段。完成此操作后,您只需在MKBackgroundColor上设置FormField属性即可。第二个块编辑第一个块的PDF,获取命名字段,创建一个新字典,为其添加颜色并将其分配给字段的窗口小部件(销毁过程中任何现有的MK条目)。

第一个代码块是更容易的路径。有关详细信息,请参阅代码中的注释。

    ''//File to output
    Dim TestFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf")
    Dim Test2File = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test2.pdf")

    ''//Standard iTextSharp setup, nothing special
    Using FS As New FileStream(TestFile, FileMode.Create, FileAccess.Write, FileShare.None)
        Using Doc As New Document()
            Using writer = PdfWriter.GetInstance(Doc, FS)
                Doc.Open()

                ''//Add a generic paragraph
                Doc.Add(New Paragraph("Hello"))

                ''//Create our text field
                Dim TF As New iTextSharp.text.pdf.TextField(writer, New Rectangle(50, 650, 250, 600), "FirstName")

                ''//Get the raw form field
                Dim FF = TF.GetTextField()

                ''//Sat the background color
                FF.MKBackgroundColor = BaseColor.RED

                ''//Add it to the document
                writer.AddAnnotation(FF)
                Doc.Close()
            End Using
        End Using
    End Using

    ''//Read the file above
    Dim R As New PdfReader(TestFile)
    ''//Create a new output file
    Using FS As New FileStream(Test2File, FileMode.Create, FileAccess.Write, FileShare.None)
        ''//Bind a stamper
        Using stamper As New PdfStamper(R, FS)

            ''//Get all of the fields
            Dim Fields = stamper.AcroFields.Fields

            ''//Get our specific field created above
            Dim FF = stamper.AcroFields.GetFieldItem("FirstName")

            ''//Color to use for the background
            Dim ColorBase = BaseColor.GREEN

            ''//The background color is a part of the display widget's MK property
            ''//This example is going to erase any existing ones and just create a new one
            Dim NewMK As New PdfDictionary(PdfName.MK)
            ''//Put our backgroun and the RGB values into the MK dictionary
            NewMK.Put(PdfName.BG, New PdfArray({ColorBase.R, ColorBase.G, ColorBase.B}))

            ''//Get the actual widget for the field
            Dim W = FF.GetWidget(0)
            ''//Set the MK value
            W.Put(PdfName.MK, NewMK)

            ''//Save and close
            stamper.Close()
        End Using
    End Using