john Smith
john Smith

Reputation: 1605

iText - export PDF - unbalanced begin end text operators

There are 2 ways of creating PDF files programmatically using iText:

  1. using high-level objects such as Paragraph, Chunk, etc
  2. using low-level functionality. this is dome using PdfContentByte class.

The second way uses beginText and endText before and after each new data insertion.

Does the first need to do it too ? Does it use beginText and endText ?

I'm using the first way, and I'm getting an unbalanced begin end text operators exception.

How could it be ?

Upvotes: 2

Views: 5701

Answers (2)

d1e
d1e

Reputation: 6452

Analysis

If we look into iText API:

PdfContentByte is an object containing the user positioned text and graphic contents of a page. It knows how to apply the proper font encoding.

Chunk is the smallest significant part of text that can be added to a document. Most elements can be divided in one or more Chunks. A chunk is a String with a certain Font. All other layout parameters should be defined in the object to which this chunk of text is added.

PdfContentByte and Chunk (Element) are made for different purposes. They do not have anything in common, nor interfaces, nor superclasses (excluding Object ofc).

Q/A

Does the first why do it too ?

It uses begin to start the writing of text and end to finish the writing of text and make the current font invalid.

does it uses beginText and endText ?

No, Elements like Chunk and Paragraph do not use begin and end. It uses StringBuffer to hold its text value.

why am I getting 'unbalanced begin end text operators' exception?

If you look at PdfContentByte's beginText() and endText() sources, you will see, that if we try to begin text when it is not yet finished, or end text when it is not yet began, we will get this exception.


Make sure you called document.open() before starting adding any element to it and document.close() when finishing the document.

Upvotes: 2

Alexis Pigeon
Alexis Pigeon

Reputation: 7522

When using high-level objects in iText, you don't need to worry with the beginText() and endText() methods. Have a look at this Hello World example.

Upvotes: 0

Related Questions