使用iText填充文本字段背景 [英] Using iText to fill text field background
问题描述
我正在尝试使用iText填充文本字段的颜色。我尝试过setfieldproperty,它不适用于bgcolor或fill color属性。我正在寻找的是文本字段的属性设置,因为它将覆盖现有的文本或图像
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..
推荐答案
背景颜色存储在MK键下的表单域的显示窗口小部件中(PDF Spec 12.5.6.19)
The background color is stored in the display widget of a form field under the MK key (PDF Spec 12.5.6.19)
下面是以两种不同方式设置背景颜色的示例代码。第一个块创建一个全新的PDF并直接向其添加一个表单域。这样完成后,您只需在 FormField
上设置 MKBackgroundColor
属性即可。第二个块编辑第一个块的PDF,获取命名字段,创建一个新字典,为其添加颜色并将其分配给字段的窗口小部件(销毁过程中任何现有的MK条目)。
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
这篇关于使用iText填充文本字段背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!