Storing a PdfContentByte state ?
- From: "Mark Storer" <MStorer (at) cardiff.com>
- Date: Wed, 17 May 2006 17:03:03 -0700
That code looks kosher to me. It may be that you're running into a bug in Acrobat's imaging model. If I'm following things correctly, your output will look something like:
---
/foo 8 Tf %%cb.setFontAndSize()
q %%cb.saveState()
<stuff>
Q %%cb.restoreState();
BT %%cb.beginText();
<more stuff>
ET %%cb.endText();
---
That "Tf" (set the font and size) operator is really only valid inside a beginText/endText block. Given your later comments, it sounds like this will work and be valid:
public void outerMethod(PdfContentByte cb, BaseFont font) {
cb.beginText();
cb.setFontAndSize(font.getBaseFont(), 8);
cb.endText();
//the rest is left alone
...
}
Poking around in the PDF Reference I don't see anything specifically stating that text state commands like 'Tf' (aka setFontAndSize()) are illegal outside BT/ET. Chapter 5 covers text as a whole, and BT/ET starts being definied at 5.3, though it is mentioned earlier. All the samples presented wrap all text-related operations with BT/ET. 5.3 mentions several things that are only valid within BT/ET, but these are confined to commands introduced in that section. No mention of text state commands is made, for or against.
BTW: Just about every PdfContentByte call directly translates to a PDF graphic. A call to a PdfContentByte function writes various parameters and commands into the internal byte array. Some of them do additional work for you (handling resources like fonts and images for you), others are provided for convenience (there's no 'circle' command, so they must be simulated with a collection of curveTo's). There's no storing of calls to be written later...
...But how those parameters and commands are interpretted by the application drawing them is another matter entirely.
You'll also notice that I've been typing "parameters and commands". PDF uses "reverse polish notation": param param command.
So addition (which doesn't happen in PDF, but makes for a universally understood example) looks like "2 2 +" instead of "2 + 2". It turns out that this notation is relatively easy to build in a computer.
--Mark Storer
Senior Software Engineer
Cardiff Software
#include <disclaimer>
typedef std::Disclaimer<Cardiff> DisCard;
> -----Original Message-----
> From: itext-questions-admin (at) lists.sourceforge.net
> [mailto:itext-questions-admin (at) lists.sourceforge.net]On Behalf
> Of Darryl
> Miles
> Sent: Wednesday, May 17, 2006 5:09 AM
> To: itext-questions (at) lists.sourceforge.net
> Subject: Re: [iText-questions] Storing a PdfContentByte state ?
>
>
> Paulo Soares wrote:
> > PdfContentByte.saveState() saves everything including the
> font, matrix,
> > colors, etc.
>
> Hmm... ok, I get unexpected output (when viewed from Acrobat
> 7.0.7) if I
> don't explicitly reset the PdfContentByte.setFontAndSize() at
> a point I
> expected restoreState() to have reset the font back to what
> is was before.
>
>
> My innerMethod function is actually an implementation of
> SMALL CAPS for
> lowercased characters of the passed string, but I have
> simplified it in
> this example.
>
>
> Code goes something like:
>
>
> public void outerMethod(PdfContentByte cb, BaseFont font) {
> cb.setFontAndSize(font.getBaseFont(), 8);
>
> innerMethod(cb, font.getBaseFont(),
> PdfContentByte.ALIGN_RIGHT, 8, 6,
> "abcDEF", colOneIndentX, thisLineY, 0);
> cb.setFontAndSize(font.getBaseFont(), 8); // Restore font
> info from
> calling #innerMethod() this is the call I'm not expecting to
> need to make
>
> cb.beginText();
> // This is the text that which ends up in the wrong place,
> by around
> 6pts east when a 6pt/smallSize font got selected inside innerMethod
> cb.showTextAligned(PdfContentByte.ALIGN_LEFT, "Some Data",
> colTwoIndentX, thisLineY, 0);
> cb.endText();
> }
>
>
> public void innerMethod(PdfContentByte cb, BaseFont baseFont,
> int align,
> float bigSize, float smallSize, String s, float x, float y, float a) {
> cb.saveState();
> cb.beginText();
> float fontSize = bigSize;
> if(Ctype.islower(s.charAt(0)))
> fontSize = smallSize;
> float charSpacing = 1.0f;
> cb.moveText(x, y); // Simplyfied example of ALIGN_LEFT
> cb.setFontAndSize(baseFont, fontSize);
> cb.setCharacterSpacing(charSpacing);
> cb.showText(s.charAt(0));
> cb.endText();
> cb.restoreState();
> }
>
>
> Maybe there is somewhere I can attach a test case if anyone is
> interested. For now I just make the extra call to
> cb.setFontAndSize()
> after any call to innerMethod().
>
>
>
> Adding:
>
> cb.beginText();
> cb.endText();
>
> after the first cb.setFontAndSize() also fixes the output to what I
> expect. I can't do saveState() after beginText() I get an
> error/warning
> in acrobat.
>
> Maybe a "setfont" isn't called until cb.beginText(), which means the
> graphics state is not setup. I'm not sure what iText's goals are
> aligning with PDF/PS syntax or being programtically intuitive.
>
>
> Thanks for your clarification,
>
> Darryl
>
>
> -------------------------------------------------------
> Using Tomcat but need to do more? Need to support web
> services, security?
> Get stuff done quickly with pre-integrated technology to make
> your job easier
> Download IBM WebSphere Application Server v.1.0.1 based on
> Apache Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&
dat=121642
_______________________________________________
iText-questions mailing list
iText-questions (at) lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
_______________________________________________
iText-questions mailing list
iText-questions (at) lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions