0

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?

enter image description here

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();
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • Those pink boxes look like form fields which are a form of annotation and thus will always be top-most. If so, you can [flatten the PDF](http://stackoverflow.com/a/27067449/231316) (just skip the loop) or you can fill out the form. If you're having trouble with the latter please post both your code and the PDF itself for us to inspect. – Chris Haas Jan 25 '16 at 19:54
  • Thank you. I was able to reach a solution with the answer to Razvan. – F. Baydemir Jan 27 '16 at 06:21

3 Answers3

3

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

serhiyb
  • 4,753
  • 2
  • 15
  • 24
  • I tried that method. It didn’t work. Thanks for helping me. – F. Baydemir Jan 25 '16 at 18:55
  • 2
    @F.Baydemir Using `PdfStamper` is the way to go. Saying *I tried that method. It didn't work.* is not constructive. It *does* work, but if you don't want to accept good advice, then why are you asking a question? – Bruno Lowagie Jan 25 '16 at 21:03
  • @Bruno Lowagie that salmon grid may be a picture. wouldn't be the first time I see a PDF issued by some authority which was originally meant to be filled in with a pencil on paper and they did not mind using AcroFields. Of course the right way to go would be to get a properly designed form first, then proceed with `PdfStamper` but this option is often not available to the poor programmer who is asked to add content programmatically, and so they resort to a position-guessing-and-overlay like here. – Cee McSharpface Jan 27 '16 at 10:59
1

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.

Razvan
  • 80
  • 8
0

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

Community
  • 1
  • 1
Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77