Javix (9) [Avatar] Offline
#1
I have an RTF template that I'd like to use to send a standard letter. The below are my questions:
1. Is it possible to convert existing template(s) to PDF template with PDF forms.
2. If not, is it possible to convert existing PDF file/template without PDF forms to a new one with needed PDF forms that I could fill in via GUI interface or from the DB?

Thank you
blowagie (284) [Avatar] Offline
#2
Re: convert existing PDF file without PDF forms to a new one with pdf forms
1. iText doesn't do RTF to PDF conversion, so your first suggestion is not an option.
2. Yes, you can read the PDF with PdfReader and use PdfStamper.addAnnotation() to add the form fields. You will end up with a fillable form. The main problem will be to find out at which coordinates the fields have to be added.
Javix (9) [Avatar] Offline
#3
Re: convert existing PDF file without PDF forms to a new one with pdf forms
Thanks a lot. That's exactly what I expected. It's true, the only problem wil be to calculate coordnates for new forms to fill in.
Javix (9) [Avatar] Offline
#4
Re: convert existing PDF file without PDF forms to a new one with pdf forms
I can't get it works. Can you explain me the order to follow, please.
As far I as I understood, I should deal with 2 files:
1 - original file to read (without acrofields)
2 - new file with acro fields.

But I don't understand the way to go.

PdfReader reader;
PdfStamper stamper;
AcroFields form;
reader = new PdfReader("original.pdf");

How to add new textfields to fill in later from the DB? When should I call PdfWriter to write my changes
I can't find an appropriate example among the ones proposed in downloads.
Thank you very much.
blowagie (284) [Avatar] Offline
#5
Re: convert existing PDF file without PDF forms to a new one with pdf forms
In chapter 2, you learn how to add content to an existing PDF using PdfStamper.
Instead of adding content, you have to use addAnnotation.
Google is your friend if you need an example:
http://www.mail-archive.com/itext-questions@lists.sourceforge.net/msg22068.html
Javix (9) [Avatar] Offline
#6
Re: convert existing PDF file without PDF forms to a new one with pdf forms
I looked the chapter 2.All the examples are enough trivial. The below is just an extracted example (page 69, p.2.3.1):

/* chapter02/HelloWorldStampCopy.java */
RandomAccessFileOrArray letter =
new RandomAccessFileOrArray("HelloLetter.pdf");
reader = new PdfReader(letter, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, baos);
form = stamper.getAcroFields();
form.setField("field", "World,");
stamper.setFormFlattening(true);
stamper.close();

How can I getAcroFields if there is no one yet. So how can I create one in the precised position? I didn't find anything clear neither in the book nor on the site you posted.
Thank u in advance.
Javix (9) [Avatar] Offline
#7
Re: convert existing PDF file without PDF forms to a new one with pdf forms
I tried to do the following:

public static void main(String[] args) {


try {
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
PdfReader reader = new PdfReader(
"E:/software/camser/FormExclusion_fr.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
"E:/software/camser/FormExclusion_fr2.pdf"));
TextField tf = new TextField(stamper.getWriter(), new Rectangle(
167, 785, 340, 800), "name");
tf.setText("USERNAME");
tf.setTextColor(new GrayColor(0.5f));
tf.setFont(bf);
stamper.getWriter().addAnnotation(tf.getTextField());
stamper.close();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}

and I got the following error:

Exception in thread "main" java.lang.RuntimeException: Unsupported in this context. Use PdfStamper.addAnnotation()
at com.lowagie.text.pdf.PdfStamperImp.addAnnotation(Unknown Source)
at mytests.HelloWorldStamperAdvanced.main(HelloWorldStamperAdvanced.java:46)
blowagie (284) [Avatar] Offline
#8
Re: convert existing PDF file without PDF forms to a new one with pdf forms
Do as the exception tells you, and replace:
stamper.getWriter().addAnnotation(tf.getTextField());
with
stamper.addAnnotation(tf.getTextField(), pagenumber);
Javix (9) [Avatar] Offline
#9
Re: convert existing PDF file without PDF forms to a new one with pdf forms
I'm trying to do the same as does "Hello Who" example but by adding my own key-words where needed (like "who" in the example"). The only difference is that I should read the existing original file first and not create it as you do in your example:

Document document = new Document(PageSize.A4);
try {
// step 2:
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(filename));
// step 3: we open the document
document.open();
// step 4:
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
PdfContentByte cb = writer.getDirectContent();
cb.beginText();
cb.setFontAndSize(bf, 12);
cb.moveText(36f, 78smilie;
cb.showText("Hello");
cb.endText();

TextField tf = new TextField(writer, new Rectangle(67, 785, 340,
800), "Who");
tf.setFontSize(12);
tf.setFont(bf);
tf.setText("Who?");
tf.setTextColor(new GrayColor(0.5f));
writer.addAnnotation(tf.getTextField());
Javix (9) [Avatar] Offline
#10
Re: convert existing PDF file without PDF forms to a new one with pdf forms
Yeessss ! It works! Now I should calculate the right position of the text.
THANKS A LOT FOR YOUR HELP !
blowagie (284) [Avatar] Offline
#11
Re: convert existing PDF file without PDF forms to a new one with pdf forms
Well, when using writer.addAnnotation(), the annotation is added on the current page.
When using PdfStamper, there is no current page; you have to use PdfStamper.addAnnotation() with the annotation AND the number of the page where the annotation should be added.
Your earlier attempt didn't make sense: it threw an exception because iText didn't want to guess the page number (even if there's only one page in your existing PDF).
Javix (9) [Avatar] Offline
#12
Re: convert existing PDF file without PDF forms to a new one with pdf forms
Hi Bruno ou plutôt bonjour ?
I have one more question for you. Why (see the below code) when I change the upper right "Y" coordinate of a rectangle oblect, everything changes as well, that is my "text" moves down or up. I suposed that if I don't change lower left "Y" it will not move, not right?

here is the code:

BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
PdfReader reader = new PdfReader(
"E:/software/camser/FormExclusion_fr.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
"E:/software/camser/FormExclusion_fr2.pdf"));
TextField tf = new TextField(stamper.getWriter(), new Rectangle(120,
330, 200, 500), "name");
tf.setText("USERNAME");
tf.setTextColor(new GrayColor(0.5f));
tf.setFont(bf);
stamper.addAnnotation(tf.getTextField(), 1);

stamper.close();
blowagie (284) [Avatar] Offline
#13
Re: convert existing PDF file without PDF forms to a new one with pdf forms
Bonsoir, aujourd'hui j'ai visité le Adobe Business Event (La Hulpe) smilie

As for your question: there's no real concept of a 'baseline' in AcroFields. If you change the upper Y coordinate, the content will indeed move upwards, as the upper border is the reference for defining the baseline of the first line of data.
Javix (9) [Avatar] Offline
#14
Re: convert existing PDF file without PDF forms to a new one with pdf forms
OK, thank you. As far as I've understood, I should change either the low left as well or the low left onl. Any way, it's not so clear as it seems. I'm tryng to use iReport for te the moment. Salut