Reputation: 3
I want to fill draft form with itexsharp on PDF. But When I run code It doesn’t work. The data hides under PDF Format. You can see below the detail code. How can I fix it?
I think Squares which in the picture might be image format.
string oldFile ="~\Document\oldFile.pdf";
string newFile ="~\Document\newFile.pdf";
PdfReader reader = new PdfReader(oldFile);
Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
PdfContentByte cb = writer.DirectContent;
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetColorFill(BaseColor.DARK_GRAY); cb.SetFontAndSize(bf, 8);
cb.BeginText();
string text = TextBox1.Text;
cb.ShowTextAligned(1, text, 350, 710, 0); // or cb.SetTextMatrix(1, text, 350, 710, 0);
cb.EndText();
cb.BeginText();
text = TextBox1.Text;
cb.ShowTextAligned(2, text, 520, 640, 0);
cb.EndText();
cb.BeginText();
text = "Signature";
cb.ShowTextAligned(3, text, 100, 200, 0);
cb.EndText();
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
document.Close();
fs.Close();
writer.Close();
reader.Close();
Upvotes: 0
Views: 444
Reputation: 90
Try to put
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
before adding your own text. You write the text on the page, then you overlap your original pdf.
Upvotes: 1
Reputation: 8725
This article How to bring added images using iTextSharp in C# to the forefront suggests that z-order in PDF is determined by the order of objects added to the content. So import the source page first, then draw your texts, that should solve the problem.
The squares might as well be actual form fields. If that is the case, you can open the PDF with Acrobat to find out their names (or enumerate them through iTextSharp coding if you don't have Acrobat available) and use the PdfStamper class to actually "fill them in".
Upvotes: 0
Reputation: 4833
If this is editable form (the one which you can fill with Adobe Reader) then you should look at PdfStamper and AcroFields
Here is a good article about it http://www.codeproject.com/Articles/23112/Fill-in-PDF-Form-Fields-using-the-Open-Source-iTex
Upvotes: 3