Got it solved. Apparently the problem had to do with mixing the itext/pdf classes with the awt Graphics2D or FontMapper classes. In my (much larger) running code, I've moved away from the awt classes entirely and things work fine...so far.
- Chuck > -----Original Message----- > From: Chuck Bueche [mailto:chu...@craniac.com] > Sent: Monday, August 26, 2013 4:31 PM > To: itext-questions@lists.sourceforge.net > Subject: [iText-questions] Long paragraph causing > beginText/endText imbalance > > Hi folks, > > I'm getting a beginText/endText imbalance that I think I've > narrowed down to > NoNewLineParagraph or related functions. I'm using version 5.4.3 of > iTextPDF and XMLWorker to generate a PDF file from an HTML > file. JRE 6. If > the length of a paragraph forces a newPage, it looks like > things aren't(?) > getting cleaned up properly. > > I've included below a sample application that reproduces the > failure, an > input file, and a stack trace on an exception that occurs > when the first > newPage() is encountered. That exception is later ignored. The real > problem is the beginText/endText imbalance when > Document.close() is called > at the end. > > As you'll see, I don't issue any beginText/endText. It's all > handled by the > XMLWorkerHelper. > > If you delete one character in the input file, everything is fine. > Coincidentally you also get exactly one complete page on the pdf file, > instead of running into the following page. > > Am I missing something simple here? > > Any help appreciated. > > - Chuck > > > (Aside, are the files I've included below better sent as > attachments, rather > than in-line?) > > ******************* > Here's a test application which reproduces the problem: > > import java.io.ByteArrayInputStream; > import java.io.FileInputStream; > import java.io.FileOutputStream; > import java.io.IOException; > import java.io.InputStreamReader; > import java.io.BufferedReader; > import java.io.OutputStream; > > import java.awt.RenderingHints; > > import com.itextpdf.awt.DefaultFontMapper; > import com.itextpdf.awt.PdfGraphics2D; > import com.itextpdf.text.Document; > import com.itextpdf.text.DocumentException; > import com.itextpdf.text.FontFactory; > import com.itextpdf.text.PageSize; > import com.itextpdf.text.pdf.PdfContentByte; > import com.itextpdf.text.pdf.PdfWriter; > import com.itextpdf.tool.xml.XMLWorkerHelper; > > > public class EndTextFailure { > private DefaultFontMapper fontMapper; > private PdfGraphics2D g2d; > private PdfWriter writer; > private XMLWorkerHelper helper; > > > > //------------------------------------------------------------ > -------------- > - > // EndTextFailure() > > //------------------------------------------------------------ > -------------- > - > public EndTextFailure() { > fontMapper = new DefaultFontMapper(); > FontFactory.registerDirectories(); > } > > > > //------------------------------------------------------------ > -------------- > - > // doit() > > //------------------------------------------------------------ > -------------- > - > public void doit(String inFilename, String outFilename) { > Document doc = null; > helper = XMLWorkerHelper.getInstance(); > try { > OutputStream output = new FileOutputStream(outFilename); > > doc = new Document(PageSize.LETTER); > doc.addTitle("This is the Title"); > writer = PdfWriter.getInstance(doc, output); > > doc.open(); > PdfContentByte cb = writer.getDirectContent(); > g2d = new PdfGraphics2D(cb, 612, 792, fontMapper); > drawFile(doc, inFilename); > g2d.dispose(); > } catch (IOException e) { > e.printStackTrace(); > } catch (DocumentException e) { > e.printStackTrace(); > } > > if(doc != null) { > doc.close(); > } > } > > > > //------------------------------------------------------------ > -------------- > - > // drawFile() > // > // Draws this.test into a Document. > > //------------------------------------------------------------ > -------------- > - > private void drawFile(Document doc, String inFilename) > throws DocumentException { > > g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, > RenderingHints.VALUE_FRACTIONALMETRICS_ON); > > String htmlFile = getFileContents(inFilename); > try { > helper.parseXHtml(writer, doc, new > ByteArrayInputStream(htmlFile.getBytes())); > } catch (IOException e) { > e.printStackTrace(); > } > } > > > > //------------------------------------------------------------ > -------------- > - > // getFileContents() > > //------------------------------------------------------------ > -------------- > - > private String getFileContents(String inFilename) { > String html = ""; > BufferedReader reader = null; > > try { > FileInputStream inStream = new FileInputStream(inFilename); > reader = new BufferedReader(new InputStreamReader(inStream)); > > String inStr; > boolean bDone = false; > while(!bDone) { > inStr = reader.readLine(); > if(inStr != null) { > inStr = inStr.trim(); > > if(inStr.length() != 0) { > html += inStr; > } > } else { > bDone = true; > } > } > > if(reader != null) { > reader.close(); > } > } catch(IOException e) { > e.printStackTrace(); > } > > return(html); > } > > > > //------------------------------------------------------------ > -------------- > - > // main() > > //------------------------------------------------------------ > -------------- > - > public static void main(String[] args) throws IOException { > boolean bError = false; > if(args.length != 2) { > bError = true; > } > > if(!bError) { > EndTextFailure obj = new EndTextFailure(); > obj.doit(args[0], args[1]); > } else { > System.out.println("Usage: EndTextFailure HTMLFILE"); > } > } > } > > > ******************* > Here's an input file that fails: > > <HTML> > <HEAD> > <TITLE> > </TITLE> > </HEAD> > <BODY> > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 678901234567890123456789 > 01234567890123456789012345678901234567890123456789012345678901 > 23456789012345 > 67890 > </BODY> > </HTML> > > > ******************* > Here's a stack trace of the first suspicious exception that > gets generated, > as the file is parsed: > > EndTextFailure [Java Application] > EndTextFailure at localhost:57314 > Thread [main] (Suspended) > PdfContentByte.sanityCheck() line: 3496 > PdfContentByte.reset(boolean) line: 1465 > PdfContentByte.reset() line: 1453 > PdfWriter.resetContent() line: 759 > PdfDocument.initPage() line: 1123 > PdfDocument.newPage() line: 982 > PdfDocument.carriageReturn() line: 1211 > PdfDocument.add(Element) line: 463 > > NoNewLineParagraph(Phrase).process(ElementListener) > line: 218 > PdfDocument.add(Element) line: 512 > Document.add(Element) line: 278 > PdfWriterPipeline.write(WorkerContext, > ProcessObject) line: 144 > PdfWriterPipeline.content(WorkerContext, Tag, > String, ProcessObject) line: 184 > XMLWorker.text(String) line: 163 > XMLParser.callText() line: 382 > XMLParser.endElement() line: 393 > ClosingTagState.process(char) line: 70 > XMLParser.parseWithReader(Reader) line: 235 > XMLParser.parse(Reader) line: 213 > XMLParser.parse(InputStream) line: 174 > XMLWorkerHelper.parseXHtml(PdfWriter, Document, > InputStream, InputStream, Charset, FontProvider) line: 223 > XMLWorkerHelper.parseXHtml(PdfWriter, Document, > InputStream) line: 185 > EndTextFailure.drawFile(Document, > String) line: 83 > > EndTextFailure.doit(String, String) line: 56 > EndTextFailure.main(String[]) line: 138 > C:\Program Files (x86)\Java\jre6\bin\javaw.exe (Aug 26, > 2013 3:43:23 > PM) > > > > > > -------------------------------------------------------------- > ---------------- > Introducing Performance Central, a new site from SourceForge and > AppDynamics. Performance Central is your source for news, insights, > analysis and resources for efficient Application Performance > Management. > Visit us today! > http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/414 0/ostg.clktrk > _______________________________________________ > iText-questions mailing list > iText-questions@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/itext-questions > > iText(R) is a registered trademark of 1T3XT BVBA. > Many questions posted to this list can (and will) be answered > with a reference to the iText book: http://www.itextpdf.com/book/ > Please check the keywords list before you ask for examples: > http://itextpdf.com/themes/keywords.php > ------------------------------------------------------------------------------ Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more! Discover the easy way to master current and previous Microsoft technologies and advance your career. Get an incredible 1,500+ hours of step-by-step tutorial videos with LearnDevNow. Subscribe today and save! http://pubads.g.doubleclick.net/gampad/clk?id=58040911&iu=/4140/ostg.clktrk _______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/itext-questions iText(R) is a registered trademark of 1T3XT BVBA. Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/ Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php