2013-03-11 95 views
1

我想使用iText填充文本字段的顏色。我試過setfieldproperty,它不適用於bgcolor或填充顏色屬性。我所尋找的是文本字段的屬性設置,因爲它會被覆蓋現有的文本或圖像使用iText填充文本字段背景

我已經試過夫婦在結束的情況下..

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

我只是想更改一個文本字段背景的顏色

當您手動編輯文檔中的文本字段時。屬性的外觀選項卡。它有填充顏色和邊框顏色 我能夠做邊框顏色.. 我似乎無法做內碼的填充顏色屬性的屬性..

+0

我已添加上面的代碼 – theApeman 2013-03-11 16:58:33

+0

bordercolor選項工作... – theApeman 2013-03-11 16:59:21

+0

確定更新。我試圖打印和展平文件..它不保存邊框顏色...有沒有辦法做到這一點? – theApeman 2013-03-11 17:43:54

回答

2

的背景色被存儲在一個顯示窗口小部件(PDF規格12.5.6.19)

下面是以兩種不同方式設置背景顏色的示例代碼。第一個塊創建一個全新的PDF,並直接向其添加一個表單字段。以這種方式完成後,您只需在FormField上設置MKBackgroundColor屬性即可完成設置。第二個塊編輯第一個塊的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 
+0

謝謝克里斯......我會在稍後嘗試..這看起來像我正在尋找的 – theApeman 2013-03-12 14:25:03

+0

這工作......現在唯一的事情是文本旋轉..我需要它在270. – theApeman 2013-03-13 14:20:43

+0

你應該能夠去掉'NewMK'的所有實例並執行'W.GetAsDict(PdfName.MK).Put(PdfName.BG,New PdfArray({ColorBase.R,ColorBase.G,ColorBase.B}))'''''。這應該保留小部件的現有旋轉。 – 2013-03-13 14:33:15