Hi All,
I am getting an unbalanced state save/restore exception. My application
creates a multi-page pdf document. I first insert images in each page and then
use the Graphics2D object that I get from the PdfContentByte object to draw
text over the inserted images. A more detailed explanation of what I am doing
is later in my email.
2008-07-17 10:27:12,016 [HMB Q-16] ERROR products.packager.CPackager -
CPackager: retry=false (context: AccountOrderOid=2377803 DBNode=SFOSTG02)
java.lang.RuntimeException: Unbalanced save/restore state operators.
at com.lowagie.text.pdf.PdfContentByte.restoreState(Unknown Source)
at com.lowagie.text.pdf.PdfGraphics2D.setClip(Unknown Source)
at
com.snapfish.render.core.CSnapTextLayer.render_impl(CSnapTextLayer.java:533)
Looking at the source code I see-
public void setClip(Shape s) {
cb.restoreState();
cb.saveState();
if (s != null)
s = transform.createTransformedShape(s);
if (s == null) {
clip = null;
}
else {
clip = new Area(s);
followPath(s, CLIP);
}
paintFill = paintStroke = null;
currentFillGState = currentStrokeGState = 255;
oldStroke = strokeOne;
}
Shouldn't the restoreState() and saveState() calls be reversed? Is this masking
a bug somewhere else?
Here is a code snippet to describe what I am doing. Btw this is purely
explanatory. The Exception gets thrown where you see " //------->EXCEPTION
THROWN HERE"
public void doTestRenderTextInPdf() throws CSnapError, Exception
{
// step 1: creation of a document-object
//Rectangle pageSize = new Rectangle(216f, 720f);
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
String strDataDirName ="foo/bar";
PdfWriter writer=PdfWriter.getInstance(document,
new CFileOutputStream(new CFile
(new URL("file:///" + strDataDirName +
"Images.pdf")
)));
PdfContentByte cb = null;
float scaleX=0;
float scaleY=0;
for(int i=0; i < 1; i++)
{
sLogger.debug("building page=" + (i+1));
CFile file = new CFile(new URL("file:///" +
strDataDirName + props[i][9]));
CSnapPixelMap pixMap= CSnapCodec.getImage(file,
CSnapCodec.SUN_CODEC);
if(0==i){
scaleX=pixMap.getWidth();
scaleY=pixMap.getHeight();
System.out.println("scaleX=" + scaleX +
";scaleY=" + scaleY);
document.setPageSize(new
Rectangle(scaleX, scaleY));
document.open();
}
cb = writer.getDirectContent();
Image img = Image.getInstance(cb,
pixMap.toBufferedImage(),1);
img.setAbsolutePosition(0, 0);
img.scaleToFit(scaleX, scaleY);
document.add(img);
Graphics2D g2 =cb.createGraphics(scaleX,
scaleY);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_DITHERING,
RenderingHints.VALUE_DITHER_ENABLE);
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_PURE);
g2.transform(userToDevice);
g2.setClip(new Rectangle(this.originalBox));
//------->EXCEPTION THROWN HERE
g2.setComposite(AlphaComposite.SrcOver);
// fill the background
g2.setColor(bgColor);
g2.fillRect(0,0,originalBox.width,originalBox.height);
FontMetrics metrics =
calculateFontAndLineBreaks(g2);
g2.setFont(theFont);
for (Iterator iter =
textLines.iterator(); iter.hasNext();lineCount++) {
String txt = (String) iter.next();
int w = metrics.stringWidth(txt);
if (txt != null && txt.length() > 0) {
g2.setColor(textColor);
int penX;
if (textAlign == ALIGN_TOPLEFT || textAlign
== ALIGN_CENTERLEFT || textAlign == ALIGN_BOTTOMLEFT) {
penX = 0; // left-justify
} else if (textAlign == ALIGN_TOPRIGHT ||
textAlign == ALIGN_CENTERRIGHT || textAlign == ALIGN_BOTTOMRIGHT) {
penX =
(int)Math.round(originalBox.getWidth() - w); // right-justify
} else {
penX =
(int)Math.round((originalBox.getWidth() - w) / 2); //center-justify
}
sLogger.debug("txt=" + txt + ",penX=" +
penX + "penY=" + penY);
if (outlineSize > OUTLINE_THRESHOLD) {
g2.setStroke(new
BasicStroke(outlineSize));
TextLayout tl = new TextLayout(txt,
theFont, g2.getFontRenderContext());
AffineTransform at = new
AffineTransform();
at.translate(penX, penY);
Shape sh = tl.getOutline(at);
g2.fill(sh);
g2.setColor(outlineColor);
g2.draw(sh);
} else {
/*
* the reason that we use drawString()
here
* and fill up above is that the outline
* that is retrieved doesn't exactly
* match the shape that is drawn with
* drawString(). I suspect this is
because
* of font hinting but I haven't looked
into
* it. drawString(), I assume, produces
* slightly more correct results, so I'm
* using it here when the effects of
drawing
* a slightly off outline are not a
problem.
*/
g2.drawString(txt, penX, penY);
}
}
g2.dispose();
document.newPage();
}
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
document.close();
}
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Do you like iText?
Buy the iText book: http://www.1t3xt.com/docs/book.php
Or leave a tip: https://tipit.to/itexttipjar