Using iText to fill text field background

*爱你&永不变心* 提交于 2019-12-08 08:49:21

问题


I am trying to Use iText to fill the color of a text field. I have tried setfieldproperty and it does not work with bgcolor or fill color attribute. What I am looking for is the property of the text field to set as it will be overlayed existing text or image

I have tried the couple of cases at the end..

    ' 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)**

I just want to change the color of one text field background

When you manually edit the Text Field within the Document. The appearance tab of the properties. It has the property for Fill color and border color I am able to do the border color.. I cannot seem to do the fill color property within code..


回答1:


The background color is stored in the display widget of a form field under the MK key (PDF Spec 12.5.6.19)

Below is sample code that sets the background color in two different ways. The first block creates a brand new PDF and adds a form field directly to it. When done this way you can just set the MKBackgroundColor property on the FormField and you're all set. The second block edits the first block's PDF, gets the named field, creates a new dictionary, adds the color to it and assigns it to the field's widget (destroying any existing MK entries in the process).

The first block of code is the much easier route. See the comments in the code for more information.

    ''//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


来源:https://stackoverflow.com/questions/15343400/using-itext-to-fill-text-field-background

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!