1

I am trying to use @aaronbartell example, to place the text at required (absolute) position in the PDF document. please give me some direction, thanks.

Example:

private static void absText(String text, int x, int y) {
  try {
    PdfContentByte cb = writer.getDirectContent();
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    cb.saveState();
    cb.beginText();
    cb.moveText(x, y);
    cb.setFontAndSize(bf, 12);
    cb.showText(text);
    cb.endText();
    cb.restoreState();
  } catch (DocumentException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }
}
Community
  • 1
  • 1
Java.beginner
  • 871
  • 2
  • 19
  • 37
  • 2
    Well yes - where are you *expecting* the `writer` variable to come from? I think that answer is expecting there to already be an appropriate writer available... – Jon Skeet Dec 16 '14 at 09:41

1 Answers1

2

If you use iText, you create a PDF document in 5 steps"

  1. Create a Document instance
  2. Create a PdfWriter instance
  3. Open the document
  4. Add content
  5. Close the document

In your question, you do not create the PdfWriter instance (unless it's a global variable). In your comment, you do not open the document (you've skipped step 3 and this step is essential in the document creation process).

Take the code from your comment, and add the following line at the appropriate place:

document.open();

The appropriate place is after the line where you create the PdfWriter instance and before you start using the writer instance.

Update

In your comment, you are now sharing some code that contains a logical error.

Your main method pdfGeneration() (probably) contains the five steps in the creating process:

  1. You create a Document instance
  2. You create a PdfWriter instance that writes bytes to a file My First PDF Doc.pdf
  3. You open the document
  4. You call a method setPara() that is supposed to add content
  5. You close the document (not visible in your code)

The logical error can be found in the setPara() method. In this method, you repeat the five steps. You create a new Document instance (there's no need to do this) and you create a new PdfWriter instance that creates a new file My First PDF Doc.pdf. This throws an exception, because that file is already in use!

You should change setPara() like this:

public void setPara(PdfContentByte canvas, Phrase p, float x, float y) {
    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, x, y, 0);
}

You should call this method from your main method like this:

setPara(writer.getDirectContent(), new Phrase(text), x, y);

Of course: as the setPara() method is little more than a reduced version of the showTextAligned() method that already exists in iText, you may want to use that method directly. For instance: use this in your main method, instead of introducing a setPara() method:

Phrase phrase = new Phrase("Some text", new Font());
PdfContentByte canvas = writer.getDirectContent();
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 20, 20, 0);
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks @Bruno Lowagie again, when I followed your steps, this time no error but the file is [corrupt](http://i.imgur.com/0lzmWaM.png). – Java.beginner Dec 16 '14 at 10:31
  • Can you share the PDF instead of the screen shot? The error message you show is very typical if you forget to close the document. Did you close the document? – Bruno Lowagie Dec 16 '14 at 10:33
  • Thanks @Bruno Lowagie, PDF [screen shot](http://i.imgur.com/uOpxRch.png), the actual code where I am calling the [function](http://i.imgur.com/2H4AqNr.png) – Java.beginner Dec 16 '14 at 10:42
  • There is a serious logical error in your code: you are damaging the file you are creating! I'll update my answer. – Bruno Lowagie Dec 16 '14 at 10:44
  • You don't need to write your own method to do this, the `showTextAligned()` method in the `ColumnText` class takes care of your requirement (as shown in my updated answer). – Bruno Lowagie Dec 16 '14 at 10:55
  • Thanks @Bruno Lowagie, I am very sorry to waste you time, I made it more complicated. Now it works [beautifully](http://i.imgur.com/VetqIfK.png), Thank you so much. – Java.beginner Dec 16 '14 at 11:15
  • Yes indeed, you (@Bruno Lowagie) solved it very very quickly! I think! need to point this answer to the other [questions](http://stackoverflow.com/questions/1625455/itext-positioning-text-absolutely) as well. – Java.beginner Dec 16 '14 at 11:21