The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

simoneffe (12) [Avatar] Offline
Hi, I'm Italian and excuse me for my Language,
I creating a Editor of PDF and my problem is to create e PDF from a JTextPane, the text in my TextPane have font color and dimension, and these characteristics must be maintained in the PDF. I have found an example in your book:
com.lowagie.text.Document document1 = new com.lowagie.text.Document();
FileOutputStream fos = new FileOutputStream("editor.pdf");
PdfWriter writer = PdfWriter.getInstance(document1, fos);;
PdfContentByte cb = writer.getDirectContent();
DefaultFontMapper mapper = new DefaultFontMapper();

PdfGraphics2D g2 = (PdfGraphics2D)cb.createGraphics(612, 792, mapper);


}catch (Exception es){
System.err.print( es.getMessage());

...but it does not work, it create editor.pdf, but is empty.
Can you help me?
Thanks you
blowagie (284) [Avatar] Offline
Re: PDF from JTextPane
Based on your code snippet, I can tell what the myPane object is. I fail to see why your code snippet should generate a PDF with content. The example that comes with the book will also create a blank PDF if you don't type anything in the JTextPane. (But that's obvious, isn't it?)
simoneffe (12) [Avatar] Offline
Re: PDF from JTextPane
Thanks so much, I have resolved, however I have others two problems: the first one as: I need to make the contrary, that is from pdf to textPane, how I can make it? Second one: how I make to read the pdf line for line?

Grazie (and excuse me for my language)
blowagie (284) [Avatar] Offline
Re: PDF from JTextPane
1. PDF to JTextPane: you need a PDF viewer:
2. The concept of lines is inexistent in PDF. Characters are added at absolute positions. Not necessarily in the order you might expect.
simoneffe (12) [Avatar] Offline
Re: PDF from JTextPane
...therefore, if I need to manipulate the PDF in my editor it is no possible?
blowagie (284) [Avatar] Offline
Re: PDF from JTextPane
As described in my book: PDF is a One Way Process.
You won't be able to edit existing PDFs in your editor.
Most editors save the contents in another format.
PDF as a format is not designed to be easily editable.
simoneffe (12) [Avatar] Offline
Re: PDF from JTextPane
Hi, I resolved all the questions, but there are others 3 problems.
-The first one: in example of chapter12 (a simple Editor), if the text in JTextPane is more of one page, the PDF's result is composed from a only page, and the rest of the text is lost.
-The second One, in the same example when I create the PDF's file the border of the rectangle are black, how I can them make white? I used setBorderColor(java.awt.Color.WHITE), but it does not work.
-The third one, in the same example, when I press a button, I need that the caret of the mouse, stay on the JPane, without that the user press the Pane again.

Thanks so much Simone
blowagie (284) [Avatar] Offline
Re: PDF from JTextPane
First issue: PDF is a Page Description Language; there's no reflow, there are no scrollbars. There are 2 possibilities:

(1) you create one big PdfTemplate from the PdfGraphics2D and cut it into different pages (reusing one PdfTemplate object on different pages).
(2) you don't serve the end user a canvas that is larger than one page. This way, he understands the concept of pages (the same way you get the concept of pages when using Word in page view). Use one PdfGraphics2D per page.

I prefer (2)

Second issue: look for the following lines:
and remove them.

Third issue: that's a Swing related question. Not an iText related one. I'm not a Swing expert, but I think it's possible to keep track of the cursor position in a JTextPane. The cursor should be reset to that position after the button is clicked.
simoneffe (12) [Avatar] Offline
Re: PDF from JTextPane
good morning, I have resolved all problem, but i don't understand the answer for my first problem, can you explain again?
And last question: I'm manipulating a file .RTF, I convert the content of the file in a string, and I need to replace a particular substring with an other, but "going in a new line".There is a character kind:
, par,
? I write:

string = string + "par";

but it doesn't work.
How I can make it?
blowagie (284) [Avatar] Offline
Re: PDF from JTextPane
Concerning RTF: I can't help you. The book is about PDF. You'll have to post the question to the mailing list.

Concerning the first issue. What is the problem? Compare it with paper.
(1) Either you write text on an roll of paper. While you are writing, you know the width, but not the length. When finished writing, you take your scissors and cut the roll into pages.
Or you write your text on different sheets of paper of which you know the width and the height in advance.

Translated to iText:
(1) you have one PdfGraphics2D wrapped in a PdfTemplate that you distribute over different pages as described in the book.
(2) you have many PdfGraphics2D objects (one per page).
simoneffe (12) [Avatar] Offline
Re: PDF from JTextPane
OK, I understand the example, but how I make to understand if the text in a pane is more of one page, e then I create an other object of Graphics2D? the theory is ok, but
thanks for the patience
blowagie (284) [Avatar] Offline
Re: PDF from JTextPane
So there's no Swing object that allows you to have a text editor with a fixed height. That's a pity. I once wrote a (closed source) editor in Java using nothing but AWT (Swing didn't exist yet). It did just what you needed. I'm not that familiar with Swing. I assumed it would be possible.
simoneffe (12) [Avatar] Offline
Re: PDF from JTextPane
Nothing, I have studied the book, watched the example, but I don't resolve the problem. I have used, a file .RTF in support, to manipolate the PDF, and the RTF, if the pane is more a page, have all the pages. I don't understand how to create more pages and no only one page.
blowagie (284) [Avatar] Offline
Re: PDF from JTextPane
Too bad. But it's not an iText problem, is it?
It's purely a Swing problem: how do you paginate a text pane object.
Once you've got the answer to that question, you can render each
page to a PDF page with PdfGraphics2D.
If there is no answer: you need to render the complete text pane to
one large PdfGraphics2D and then add the same XObject to as many
pages as needed (each time with a different offset).
I remember writing such an example in answer to a question on the
official iText mailing list. It's probably still somewhere in the archives.
simoneffe (12) [Avatar] Offline
Re: PDF from JTextPane
ok, no problem, an other question is possible to convert RTF in PDF? How?
blowagie (284) [Avatar] Offline
Re: PDF from JTextPane