Using iText to fill text field background - itext

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..

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

Related

How to generate PDF in Hebrew? currently the PDF is generated empty

I'm using iTextSharp 5.5.13 and when i try to generate the PDF with Hebrew it comes out empty.
this is my code: I'm i doing something wrong?
public byte[] GenerateIvhunPdf(FinalIvhunSolution ivhun)
{
byte[] pdfBytes;
using (var mem = new MemoryStream())
{
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(document, mem);
writer.PageEvent = new MyHeaderNFooter();
document.Open();
var font = new
Font(BaseFont.CreateFont("C:\\Downloads\\fonts\\Rubik-Light.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), 14);
Paragraph p = new Paragraph("פסקת פתיחה")
{
Alignment = Element.ALIGN_RIGHT
};
PdfPTable table = new PdfPTable(2)
{
RunDirection = PdfWriter.RUN_DIRECTION_RTL
};
PdfPCell cell = new PdfPCell(new Phrase("מזהה", font));
cell.BackgroundColor = BaseColor.BLACK;
table.AddCell(cell);
document.Add(p);
document.Add(table);
document.Close();
pdfBytes = mem.ToArray();
}
return pdfBytes;
}
The PDF comes out blank
I changed a few details of your code, and now I get this:
My changes:
Embedding the font
As I don't have Rubik installed on my system, I have to embed the font into the PDF to have a chance to see anything. Thus, I replaced BaseFont.NOT_EMBEDDED by BaseFont.EMBEDDED when creating the var font:
var font = new Font(BaseFont.CreateFont("Rubik-Light.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 14);
Making the Paragraph kind of work
You create the Paragraph p without specifying a font. Thus, a default font with default encoding is used. The default encoding is WinAnsiEncoding which is Latin1-like, so no Hebrew characters can be represented. I added your Rubik font instance to the Paragraph p creation:
Paragraph p = new Paragraph("פסקת פתיחה", font)
{
Alignment = Element.ALIGN_RIGHT
};
Et voilà, the writing appears.
iText developers often have communicated that in iText 5.x and earlier right-to-left scripts are only supported properly in certain contexts, e.g. in tables, but not in others like paragraphs immediately added to the document. As your Paragraph p is added immediately to the Document document, its letters appear in the wrong order in the output.
Making the PdfPTable work
You defined the PdfPTable table to have two columns (new PdfPTable(2)) but then you added only one cell. Thus, table contains not even a single complete row. iText, therefore, draws nothing when it is added to the document.
I changed the definition of table to have a single column only:
PdfPTable table = new PdfPTable(1)
{
RunDirection = PdfWriter.RUN_DIRECTION_RTL
};
Furthermore, I commented out the line setting the cell background to black because usually it is difficult to read black on black:
PdfPCell cell = new PdfPCell(new Phrase("מזהה", font));
//cell.BackgroundColor = BaseColor.BLACK;
table.AddCell(cell);
And again the writing appears.
Properly downloading the font
Another possible obstacle is that when downloading the font from the URL you gave — https://fonts.google.com/selection?selection.family=Rubik — one can see in the customization tab of the selection drawer that by default only Latin characters are included in the download, in particular not Hebrew ones:
I tested with a font file I downloaded with all language options enabled:

How to move text written in Type 3 Font from one pdf to other pdf?

I have a pdf which include text written in Type 3 Font.
I want to get some text from it and write it into other pdf in exactly same shape.
I am using itext. Please give me a tip.
edit: I attached my code.
DocumentFont f = renderInfo.getFont();
String str = renderInfo.getText();
x = renderInfo.getBaseline().getStartPoint().get(Vector.I1);
In this code, I want to write str into x value position.
In Type 3 Font, is it work?
You can copy parts of one page to a new one using code like this:
InputStream resourceStream = getClass().getResourceAsStream("from.pdf");
PdfReader reader = new PdfReader(new FileOutputStream("from.pdf"));
Rectangle pagesize = reader.getPageSizeWithRotation(1);
Document document = new Document(pagesize);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("areaOfFrom.pdf"));
document.open();
PdfContentByte content = writer.getDirectContent();
PdfImportedPage page = writer.getImportedPage(reader, 1);
content.saveState();
content.rectangle(0, 350, 360, 475);
content.clip();
content.newPath();
content.addTemplate(page, 0, 0);
content.restoreState();
document.close();
reader.close();
This turns your
into
Unfortunately, though, that hidden content is merely... hidden... but it is still there. You can especially mark the lines with that hidden text and try to copy&paste them.
If you want to completely remove that hidden text (or start out by merely copying the desired text), you have to inspect the content of the imported page and filter it. I'm afraid iText does not yet explicitly support something like that. It can be done using the iText lowlevel API but it is quite some work.

Add itextsharp barcode image to PdfPCell

I'm trying generate barcodes from a text string and then PDF the results using iTextSharp. Right now, I have the code below:
// Create a Document object
var pdfToCreate = new Document(PageSize.A4, 0, 0, 0, 0);
// Create a new PdfWrite object, writing the output to a MemoryStream
var outputStream = new MemoryStream();
var pdfWriter = PdfWriter.GetInstance(pdfToCreate, outputStream);
PdfContentByte cb = new PdfContentByte(pdfWriter);
// Open the Document for writing
pdfToCreate.Open();
PdfPTable BarCodeTable = new PdfPTable(3);
// Create barcode
Barcode128 code128 = new Barcode128();
code128.CodeType = Barcode.CODE128_UCC;
code128.Code = "00100370006756555316";
// Generate barcode image
iTextSharp.text.Image image128 = code128.CreateImageWithBarcode(cb, null, null);
// Add image to table cell
BarCodeTable.AddCell(image128);
// Add table to document
pdfToCreate.Add(BarCodeTable);
pdfToCreate.Close();
When I run this and try to open the PDF programmatically, I receive an error saying "The document has no pages."
However when I use:
pdfToCreate.Add(image128);
(instead of adding to a table, I add it to a cell), the barcode shows up (though it's floating off the document).
I'd like to send the barcodes to a table so that I can format the document easier. The final product will be 20-60 barcodes read from a database. Any idea on where I'm going wrong?
You are telling the PdfPTable constructor to create a three column table but only providing one of the columns:
PdfPTable BarCodeTable = new PdfPTable(3);
iTextSharp by default ignores incomplete rows. You can either change the constructor to match your column count, add the extra cells or call BarCodeTable.CompleteRow() which will fill in the blanks using the default cell template.

Using PDF itextSharp it is possible to put an image on top of text while creating the pdf document

I attempted several ways to do this, but still cannot get it.
It appears iTextSharp requires a 2 pass situation so that an image appears on top of the text.
So I am attempting to do this using memory streams, but I keep getting errors.
Public Function createDoc(ByRef reqResponse As HttpResponse) As Boolean
Dim m As System.IO.MemoryStream = New System.IO.MemoryStream()
Dim document As Document = New Document()
Dim writer As PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(document, m)
document.Open()
document.Add(New Paragraph(DateTime.Now.ToString()))
document.Add(New Paragraph(DateTime.Now.ToString()))
document.Add(New Paragraph(DateTime.Now.ToString()))
document.Add(New Paragraph(DateTime.Now.ToString()))
document.Add(New Paragraph(DateTime.Now.ToString()))
document.Add(New Paragraph(DateTime.Now.ToString()))
document.Add(New Paragraph(DateTime.Now.ToString()))
document.Close()
writer.Flush()
writer.Flush()
'yes; I get the pdf if this is the last statement
'reqResponse.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length)
'this statment does not work it says the stream is closed
'm.Position = 0
Dim Reader As PdfReader = New PdfReader(m)
'Dim rm As MemoryStream = New MemoryStream(m.GetBuffer(), 0, m.GetBuffer().Length)
Dim PdfStamper As PdfStamper = New PdfStamper(Reader, reqResponse.OutputStream)
Dim cb As iTextSharp.text.pdf.PdfContentByte = Nothing
cb = PdfStamper.GetOverContent(1)
Dim locMyImage As System.Drawing.Image = System.Drawing.Image.FromStream(zproProduceWhiteImageToCovertBarCodeNumbers())
Dim BImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(locMyImage, iTextSharp.text.BaseColor.CYAN)
Dim overContent As PdfContentByte = PdfStamper.GetOverContent(1)
BImage.SetAbsolutePosition(5, 5)
overContent.AddImage(BImage)
PdfStamper.FormFlattening = True
PdfStamper.Close()
'rm.Flush()
'rm.Close()
'Dim data As Byte() = rm.ToArray()
'reqResponse.Clear()
'Dim finalMs As MemoryStream = New MemoryStream(data)
'reqResponse.ContentType = "application/pdf"
'reqResponse.AddHeader("content-disposition", "attachment;filename=labtest.pdf")
'reqResponse.Buffer = True
'finalMs.WriteTo(reqResponse.OutputStream)
'reqResponse.End()
'Dim data As Byte() = rm.ToArray()
'reqResponse.OutputStream.Write(data, 0, data.Length)
''Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
''Response.OutputStream.Flush();
''Response.OutputStream.Close();
''Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest()
Return True
End Function
reference:
Put text on top of an image?
seach engine reference:
whiteout text on a pdf document by using a image which is the same color as the background pdf
image overlap with itextpdf
itextsharp image on top of the text whiteout
itextsharp place picture on top of text
itextpdf image on top
thanks,
Doug Lubey of Louisiana
You can do this pretty easily. The Document object is a helper object that abstracts away many of the internals of the PDF model and for the most part assumes that you want to flow content and that text would go above images. If you want to get around this you can talk directly the PdfWriter object instead. It has two properties, DirectContent and DirectContentUnder that both have methods named AddImage() that you can use to set an absolute position on an image. DirectContent is above existing content and DirectContentUnder is below it. See the code for an example:
You appear to be doing this on the web so you'll need to adapt this to whatever stream you are using but that should be pretty easy.
One note, NEVER call GetBuffer() on a MemoryStream, ALWAYS use ToArray(). The former method includes uninitialized bytes that will give you potentially corrupt PDFs.
''//File that we are creating
Dim OutputFile As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf")
''//Image to place
Dim SampleImage As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "SampleImage.jpg")
''//Standard PDF creation setup
Using FS As New FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None)
Using Doc As New Document(PageSize.LETTER)
Using writer = PdfWriter.GetInstance(Doc, FS)
''//Open the document for writing
Doc.Open()
''//Add a simple paragraph
Doc.Add(New Paragraph("Hello world"))
''//Create an image object
Dim Img = iTextSharp.text.Image.GetInstance(SampleImage)
''//Give it an absolute position in the top left corner of the document (remembering that 0,0 is bottom left, not top left)
Img.SetAbsolutePosition(0, Doc.PageSize.Height - Img.Height)
''//Add it directly to the raw pdfwriter instead of the document helper. DirectContent is above and DirectContentUnder is below
writer.DirectContent.AddImage(Img)
''//Close the document
Doc.Close()
End Using
End Using
End Using

How to add a form field to an existing pdf with itextsharp?

How to add a form field to an existing pdf with itextsharp?
I have an existing pdf document, I'd like to add form fields to it without creating a copy and writing out a new document.
After further review, the ruling on the field is overturned. Turns out if you form flatten the stamper the fields do not show on the resulting document (because they lack 'appearance' settings). BTW, form flattening prevents further edits of a form field. Now we can add appearance to the form, however, an easier way is to use the TextField class and not worry about explicitly setting up 'appearance' objects.
public void ABetterWayToAddFormFieldToExistingPDF( )
{
PdfReader reader = new PdfReader(#"c:\existing.pdf");
FileStream out = new FileStream(#"C:\existingPlusFields.pdf", FileMode.Create, FileAccess.Write);
PdfStamper stamp = new PdfStamper(reader, out);
TextField field = new TextField(stamp.Writer, new iTextSharp.text.Rectangle(40, 500, 360, 530), "some_text");
// add the field here, the second param is the page you want it on
stamp.AddAnnotation(field.GetTextField(), 1);
stamp.FormFlattening = true; // lock fields and prevent further edits.
stamp.Close();
}
I struggled with this for awhile so figured I'd post the Question & Answer
Using the PdfStamper itext class is the key. (I guess this does make a copy but it's much cleaner than using the itext PdfCopy classes).
public void AddFormFieldToExistingPDF( )
{
PdfReader reader = new PdfReader(#"c:\existing.pdf");
FileStream out = new FileStream(#"C:\existingPlusFields.pdf", FileMode.Create, FileAccess.Write);
PdfStamper stamp = new PdfStamper(reader, out);
PdfFormField field = PdfFormField.CreateTextField(stamp.Writer, false, false, 50);
// set a field w/some position and size
field.SetWidget(new iTextSharp.text.Rectangle(40, 500, 360, 530),
PdfAnnotation.HIGHLIGHT_INVERT);
field.SetFieldFlags(PdfAnnotation.FLAGS_PRINT);
field.FieldName = "some_field";
// add the field here, the second param is the page you want it on
stamp.AddAnnotation(field, 1);
stamp.Close();
}
Using pdfStamper you can complete it.
PdfStamper Stamper= new PdfStamper(new PdfReader(sourcefile), File.Create(NewOutputFile));
TextField moreText = new TextField(Stamper.Writer,
new iTextSharp.text.Rectangle(20, 20, 590, 780), "moreText");
moreText.Visibility = TextField.VISIBLE_BUT_DOES_NOT_PRINT;
moreText.Text = "Use this space for any additional information";
moreText.Options = (TextField.MULTILINE);
PdfFormField Fieldtxt = moreText.GetTextField();
Stamper.AddAnnotation(Fieldtxt, n);