Hi.
All I did was this:
I changed the public static void main (String[] arg) method to public
ByteArrayOutputStream mainExecute().
public ByteArrayOutputStream mainExecute() {
System.out.println("Chapter 14: Romeo and Juliet");
System.out.println("-> Creates a PDF file that converts a play in
XML");
System.out.println(" to PDF and adds extra stuff using page
events.");
System.out.println("-> jars needed: iText.jar");
System.out
.println("-> resources needed: romeo_juliet.xml and
tagmap.xml");
System.out.println("-> resulting PDF: romeo_juliet.pdf");
RomeoJulietBaos rj = new RomeoJulietBaos();
// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 80, 50, 30, 65);
here I add the baos
ByteArrayOutputStream baos=new ByteArrayOutputStream();
try {
// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
call the getInstance with baos parameter:
PdfWriter writer = PdfWriter.getInstance(document,baos);
// create add the event handler
MyPageEvents events = rj.new MyPageEvents();
writer.setPageEvent(events);
// step 3: we create a parser and set the document handler
SAXParser parser = SAXParserFactory.newInstance
().newSAXParser();
// step 4: we parse the document
For the example I just defined fixed path to the resources:
RomeoJulietMap tagmap = rj.new RomeoJulietMap(
"C:/work/chapter14/resources/tagmap.xml");
parser.parse("C:/work/chapter14/resources/romeo_juliet.xml",
rj.new MyHandler(document, tagmap));
int end_play = writer.getPageNumber();
events.template.beginText();
events.template.setFontAndSize(events.bf, 8);
events.template.showText(String.valueOf(end_play));
events.template.endText();
document.newPage();
writer.setPageEvent(null);
Speaker speaker;
for (Iterator i = events.speakers.iterator(); i.hasNext();) {
speaker = (Speaker) i.next();
document.add(new Paragraph(speaker.getName() + ": "
+ speaker.getOccurrance() + " speech blocks"));
}
int end_doc = writer.getPageNumber();
int[] reorder = new int[end_doc];
for (int i = 0; i < reorder.length; i++) {
reorder[i] = i + end_play + 1;
if (reorder[i] > end_doc)
reorder[i] -= end_doc;
}
document.newPage();
writer.reorderPages(reorder);
} catch (Exception e) {
e.printStackTrace();
}
document.close();
here I return the baos back to the calling class:
return baos;
}
The rest of the R&J class has been left as is, since all inner classes and
methods are needed.
As for the population, its done each time the document.add() command is
called.
You should buy the Book, if you don't have it. I think it's a real asset.
P.S: By passing the baos to the PdfWriter you automaticaly create a
reference between the document and the baos. So no explicit calls to the
baos are needed to populate it.
2007/5/21, Jim Hurricane <[EMAIL PROTECTED]>:
Thanks Iliadis.
Can you tell me the point where the Baos actually gets populated?
I'm not sure what point in the R&J code acutally runs iText. It's not
clearly commented where this happens.
I have all the Servlet code working and ready to go so that part is OK.
I've modifed the R&J code somewhat (though the inner classes are still
there).
My createNewDocument() is unchanged from the example.
public void createNewDocument()
{
document = new Document(PageSize.A4, 80, 50, 30, 65);
}
In step 2, I have:
writer = PdfWriter.getInstance(document, baos);
>From your reply below, it sounds like you're returning the baos after
step 4
as commented in the code. However, although I've been populating things
like
your email, my baos is empty after the
parser.parse(...) code in step 4.
I'm assuming you're using the tagmap files and such too.
Thanks in advance.
- M
>From: "Iliadis Yannis" <[EMAIL PROTECTED]>
>Reply-To: Post all your questions about iText here
><[email protected]>
>To: "Post all your questions about iText here"
><[email protected]>
>Subject: Re: [iText-questions] BAOS in one step?
>Date: Mon, 21 May 2007 14:37:33 +0300
>
>I did the following and it worked for me.
>
>I created a test class which has a main method (just for testing
purpose):
>
> public static void main(String[] args) {
> ByteArrayOutputStream bs;
> RomeoJulietBaos rmBaos=new RomeoJulietBaos();
>
> bs=rmBaos.mainExecute();
> System.out.println(bs);
>
> }
>
>My second step was to alter the RomeoJuliet.java code like this:
>
>I changed the main method to mainExecute with ByteArrayOutputStream as
>return type:
>
>public ByteArrayOutputStream mainExecute() {
>
>inside I added the following:
>
> ByteArrayOutputStream baos=new ByteArrayOutputStream();
> try {
> // step 2:
> // we create a writer that listens to the document
> // and directs a XML-stream to a file
> PdfWriter writer = PdfWriter.getInstance(document,baos);
>
>Instead of a FileOutPutStream, I pass the baos as parameter to the
>PdfWriter.
>
>The last step was to return the baos to the caller class:
>
> document.close();
> return bs;
> }
>
>This worked fine and the result was printed to the console.
>
>As I recall your original post, you mentioned that you want to display
the
>output in a browser. So you only need to change my test class to a
servlet
>with the proper response commands (see the link I send you last time), to
>open the pdf.
_________________________________________________________________
PC Magazine's 2007 editors' choice for best Web mail—award-winning Windows
Live Hotmail.
http://imagine-windowslive.com/hotmail/?locale=en-us&ocid=TXT_TAGHM_migration_HM_mini_pcmag_0507
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/