Read and Manipulate existing PDF cont.

View: New views
6 Messages — Rating Filter:   Alert me  

Read and Manipulate existing PDF cont.

by Iliadis Yannis :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

First of all I've already ordered the book and I'm just waiting for it (I will receive it probably around January).
In the meantime I'm trying to solve my issue (described in a previous mail) by reading the tutorial and any example I can get my hands on, even getting bits and peaces from the mailing list, but most examples refer to documents created from scratch.


I am sending you 2 sample attachments (both created with Acrobat Designer 7) of what I want to do.

The temp.pdf is the template which I will use to fill in the data. (The name and lastname are acrofields)

The finalOut.pdf is an example of what the final document should look like. (The name and lastname are filled using PdfStamper and Acrofields.setField method, taken from Register.java).

Can you please send me a small code example on how I could create the entire finalOut.pdf using the iText API.


Thanks in advance
Yannis Iliadis

ps: I am also reading the PDF Reference 1.6 guide to get a better understanding of the file format.



temp.pdf (58K) Download Attachment

Re: Read and Manipulate existing PDF cont.

by Bruno Lowagie (iText) :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Iliad Yan wrote:

> I am sending you 2 sample attachments (both created with Acrobat
> Designer 7) of what I want to do.

Note that there is only limited support for
forms created with Designer. You also need
a very recent version of iText.

However, that's not a problem.

Let's get some source code from the book ;-)

// create a reader object
PdfReader reader = new PdfReader("temp.pdf");
// create a stamper object
PdfStamper stamper =
   new PdfStamper(reader, new FileOutputStream( "result.pdf"));
// get the form from stamper (because we are going to fill out fields)
AcroFields form = stamper.getAcroFields();
// first let's see what fields are inside:
HashMap fields = form.getFields();
for (Iterator i = fields.keySet().iterator(); i.hasNext();) {
   System.out.print(i.next());
}
// iText has found two fields:
// form1[0].Page1[0].name[0]
// form1[0].Page1[0].lastname[0]
// now let's fill those fields:
form.setField("form1[0].Page1[0].name[0]", "Doe");
form.setField("form1[0].Page1[0].lastname[0]", "John");
// the next line removes the form
stamper.setFormFlattening(true);
stamper.close();
// and we're done!

FYI: you are talking about AcroFields, but you do have
an XFA form in your PDF. This can be checked like this:

XfaForm xfa = form.getXfa();
System.err.println(xfa.isXfaPresent());

> Can you please send me a small code example on how I could create the
> entire finalOut.pdf using the iText API.

OK, the above example solves half of the problem: we have
added a first and last name (you may want to use setExtraMargin()
because I see that your fields aren't positioned very well; or
you could redesign your form).

Now you want to add subjects and grades to the document.
I suggest that you use PdfPTable (maybe wrapped in a
ColumnText object if you expect that the table will not
fit on one page).

Unfortunately iText doesn't know where it should add the table.
Either you have to take a ruler, measure the paper and calculate the
coordinates where iText can write the table (with writeSelectedRows)
or column (setSimpleColumn).
Or you redesign your form and add an extra field (for instance a
button) that marks the area where iText should draw the table.
You can then get the coordinates with getFieldPositions and
use these coordinates to draw the table or column.

br,
Bruno

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
iText-questions@...
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Re: Read and Manipulate existing PDF cont.

by Iliadis Yannis :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanx for your quick reply.

I will try the XFA form and PdfPTable part to see how they work.

BTW: It seems that Acrobat Designer does't align objects correctly, even if you tell them to (Layout->Align->Left from the Designer's Menu).

2006/11/30, Bruno Lowagie (iText) <bruno@...>:
Iliad Yan wrote:

> I am sending you 2 sample attachments (both created with Acrobat
> Designer 7) of what I want to do.

Note that there is only limited support for
forms created with Designer. You also need
a very recent version of iText.

However, that's not a problem.

Let's get some source code from the book ;-)

// create a reader object
PdfReader reader = new PdfReader("temp.pdf");
// create a stamper object
PdfStamper stamper =
   new PdfStamper(reader, new FileOutputStream( "result.pdf"));
// get the form from stamper (because we are going to fill out fields)
AcroFields form = stamper.getAcroFields();
// first let's see what fields are inside:
HashMap fields = form.getFields();
for (Iterator i = fields.keySet().iterator(); i.hasNext();) {
   System.out.print(i.next());
}
// iText has found two fields:
// form1[0].Page1[0].name[0]
// form1[0].Page1[0].lastname[0]
// now let's fill those fields:
form.setField("form1[0].Page1[0].name[0]", "Doe");
form.setField("form1[0].Page1[0].lastname[0]", "John");
// the next line removes the form
stamper.setFormFlattening(true);
stamper.close();
// and we're done!

FYI: you are talking about AcroFields, but you do have
an XFA form in your PDF. This can be checked like this:

XfaForm xfa = form.getXfa();
System.err.println(xfa.isXfaPresent());

> Can you please send me a small code example on how I could create the
> entire finalOut.pdf using the iText API.

OK, the above example solves half of the problem: we have
added a first and last name (you may want to use setExtraMargin()
because I see that your fields aren't positioned very well; or
you could redesign your form).

Now you want to add subjects and grades to the document.
I suggest that you use PdfPTable (maybe wrapped in a
ColumnText object if you expect that the table will not
fit on one page).

Unfortunately iText doesn't know where it should add the table.
Either you have to take a ruler, measure the paper and calculate the
coordinates where iText can write the table (with writeSelectedRows)
or column (setSimpleColumn).
Or you redesign your form and add an extra field (for instance a
button) that marks the area where iText should draw the table.
You can then get the coordinates with getFieldPositions and
use these coordinates to draw the table or column.

br,
Bruno

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
iText-questions@...
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
iText-questions@...
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Re: Read and Manipulate existing PDF cont.

by Bruno Lowagie (iText) :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Iliad Yan wrote:
> Thanx for your quick reply.
>
> I will try the XFA form and PdfPTable part to see how they work.

If you are redesigning the form:
- use Acrobat WITHOUT Designer; have a look at:
   Tools > Advanced Editing > Text Field Tool
   That way you won't have any XFA stuff in your PDF.
- To avoid formatting problems, you may want to omit
   the header of your table (Subject - Grade) and add
   this header yourself in the PdfPTable.

best regards,
Bruno

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
iText-questions@...
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Re: Read and Manipulate existing PDF cont.

by Iliadis Yannis :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanx again.

> use Acrobat WITHOUT Designer

I will keep that in mind for future documents.

> any XFA stuff in your PDF

What advantage or disadvantage do I have by using XFA forms in PDF.

Yannis Iliadis


2006/11/30, Bruno Lowagie (iText) <bruno@...>:
Iliad Yan wrote:
> Thanx for your quick reply.
>
> I will try the XFA form and PdfPTable part to see how they work.

If you are redesigning the form:
- use Acrobat WITHOUT Designer; have a look at:
   Tools > Advanced Editing > Text Field Tool
   That way you won't have any XFA stuff in your PDF.
- To avoid formatting problems, you may want to omit
   the header of your table (Subject - Grade) and add
   this header yourself in the PdfPTable.

best regards,
Bruno

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net 's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
iText-questions@...
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
iText-questions@...
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Re: Read and Manipulate existing PDF cont.

by Bruno Lowagie (iText) :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Iliad Yan wrote:
> What advantage or disadvantage do I have by using XFA forms in PDF.

It's hard to explain in only a few words.

Advantages of XFA:
- it's XML.
- it's new.
- it offers some flexibility.

Disadvantages of XFA:
- it's XML.
- it's new: not many tools support it
- it's not really PDF

Advantages of AcroForms:
- it's PDF.
- it has been around for a while.
- iText knows how to manipulate AcroForms pretty well,
   and so do some other tools
- you know what you'll get: all fields are fixed

Disadvantages of AcroForms:
- the way Adobe Acrobat and Reader create appearances
   has been changing in between versions, so you don't
   always get exactly what you expect (this is an
   amendment of the fourth advantage).

br,
Bruno

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
iText-questions@...
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/