Unable to fill formfields in a PDF using iTextSharp - c#

I tried to fill my pdf using iTextsharp library.I took reference from this tutorial fill pdf using iTextsharp. But when I tried this example with my pdf file it showed nothing. When I read my pdf file in pdf reader it contains null.
string pdfTemplate = #"c:\authform.pdf";
// create a new PDF reader based on the PDF template document
PdfReader pdfReader = new PdfReader(pdfTemplate);
In this I get null when I read my pdf template.Is there some special format in which the pdf template should be so that it can work with this example.

If you check the tutorial there are two examples.
one is to read fields from existing pdf.
second one is to print data to pdf.
The following is the way to print data to pdf.
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
// set form pdfFormFields
// The first worksheet and W-4 form
pdfFormFields.SetField("f1_01(0)", "1");
pdfStamper.Close();

Related

How to get binary data of PDF with out generating

I have my PDF code and existing PDF layout where I am adding the data and creating the new pdf file, but I am not able to create my new pdf file or not able to download.
I need to read the newly generate PDF file and need to written the binary data
Please review below code
MemoryStream pdfms = new MemoryStream();
PdfReader reader;
reader = new PdfReader(HttpContext.Current.Server.MapPath("20171010_BillTemplate.pdf"));
PdfStamper formFiller = new PdfStamper(reader, pdfms);
AcroFields pdfBillingFields = formFiller.AcroFields;
pdfBillingFields.SetField("CT_Mail_Block",MailBlock.ToUpper());// some data
pdfBillingFields.SetField("Cash_Only", Cash_Only);
formFiller.FormFlattening = true;
formFiller.Writer.CloseStream = false;
reader = new PdfReader(pdfms); // giving error
formFiller.Close();
pdfms.Dispose();
What I need is with or with out Creating newly created file I need to get the binary data of file and send in return.
I had implemented this code in webapi
Reader is giving error as
PDF header signature not found.
Please say is this a right way to get the binary data from above code?

iTextSharp | Embed xlsx file with export button in PDF in C#

I want to attachment excel file in PDF with click_button, on click event it generate excel that contain PDF data.
Attaching a code that I'm using for this process, due to lack in knowledge of iTextSharp was not able to embed the file. I'm not using any kind of web service. Please help me out.
Thanks in Advance.
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create));
PdfFileSpecification fs = PdfFileSpecification.FileEmbedded(stamper.Writer, null, "test.xlxs", null);
stamper.AddFileAttachment("Some Test File",fs);
stamper.Close();

Generating PDF in windows forms using XML reader

My windows form contains a textbox in which we need to enter html tags,One button to generate PDF.
And we need to load the textbox content into XML Reader and process each element of XML recursively then we need to generate a PDF file.
The PDF file must contain the data i.e;
for example if I entered tag in the text box in the pdf file it must display a table.
I am very new to Windows forms and XML also can any one help me to complete this task
You would need to use a library to create PDF files. iTextSharp is a common library which can help. Take a look at this library and samples, you would be able to create PDF files easily from your application
https://sourceforge.net/projects/itextsharp/
iText is a PDF library that allows you to CREATE, ADAPT, INSPECT and MAINTAIN documents in the Portable Document Format (PDF):
iTextSharp is the .NET port.
Got Answer with this simple code
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\MySamplePDF.pdf", FileMode.Create));
document.Open();
iTextSharp.text.html.simpleparser.HTMLWorker hw =
new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.Parse(new StringReader(htmlText));
document.Close();
but my problem is the path I want to select the path dynamically.Can any one help me how to set the path dynamically in the above code.
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\MySamplePDF.pdf", FileMode.Create));
Changed this code by using a savefile dialog box
SaveFileDialog svg = new SaveFileDialog();
svg.ShowDialog();
PdfWriter.GetInstance(document, new FileStream(svg.FileName + ".pdf", FileMode.Create));

Replacing an image

I am trying to add or replace an image in an existing PDF file using iTextSharp. The file has 3 layers which are required by the printing company. The content on these layers cannot be merged.
Thus far I have tried many of the code examples (most don't seem to be in C# and can't find the conversion from java). The closest example is:
PdfReader reader = new PdfReader(this.FrontPDFFile);
PdfStamper stamper = new PdfStamper(reader, new FileStream(this.OutputDirectory, FileMode.Create));
var pdfContentBuffer = stamper.GetOverContent(1);
// get image from our api
System.Drawing.Image image = GenerateQRCode("GUOIO", 1000, 1000);
// convert to itextsharp image and insert
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(image, iTextSharp.text.BaseColor.WHITE);
img.SetAbsolutePosition(100, 100);
img.ScaleToFit(100, 100);
pdfContentBuffer.AddImage(img, true);
stamper.Close();
This generates the pdf with the image on, however opening in Illustrator it is not shown, this is likely to be something to do with layers (I am told). Anyone got any ideas?

iTextSharp - How to generate a RTF document in the ClipBoard instead of a file

I would like to generate a PDF or RTF document using iTextSharp library that can be copied to the clipboard, using the same code I use to generate the document on a file (FileStream).
This way my application would give the user two options: generate to a file or to the clipboard.
Basically every iTextSharp document is attached to a System.IO.Stream.
Document doc = new Document(PageSize.A4);
RtfWriter2.GetInstance(doc, stream);
Usually we save the document to a file, using FileStream. To use the same code to paste the document in the Clipboard, we use a MemoryStream instead.
MemoryStream stream = new MemoryStream();
Document doc = new Document(PageSize.A4);
RtfWriter2.GetInstance(doc, stream);
// (...) document content
doc.Close();
string rtfText = ASCIIEncoding.ASCII.GetString(stream.GetBuffer());
stream.Close();
Clipboard.SetText(rtfText, TextDataFormat.Rtf);
I only had problems with Images: It seems that iTextSharp exports images saving the bytes of the image after the \bin tag. Some libraries put the binary content encoded as hex characters. When I paste (from memory) in Word, the images won't appear, but if I load from a file, everything is OK. Any suggestions?

Categories