Oops. Rectangle.rotate doesn't quite work the way I thought it did. Here's
a fix:
for( int i = reader.getPageRotation(x); i > 0; i-= 90) {
newPageRect = newPageRect.rotate();
}
On Wed, Apr 9, 2008 at 10:18 AM, Mark Storer <[EMAIL PROTECTED]> wrote:
> PdfContentByte cb = writer.getDirectContent();
> //IIRC, the directContent is from the current page, so you'll be drawing
> all your
> // importedPages onto the first page.
>
> for (InputStream input : inputStreams) {
> PdfReader reader = new PdfReader(input);
> for (int x=1; x<= reader.getNumberOfPages(); x++) {
> doc.newPage();
> PdfImportedPage page = writer.getImportedPage(reader, x);
>
> //determine if the page needs to be rotated
> int rotation = reader.getPageRotation(x);
> if (rotation == 90 || rotation == 270)
> {
> cb.addTemplate(page, 0,-1f, 1f, 0, 0,
> reader.getPageSizeWithRotation(x).getHeight());
> } else {
> cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
> }
> Incidentally, this really isn't the way to go with rotated pages. You're
> rotating 90 & 270 the same, and ignoring the never-actually-seen-one freak
> occurrence of a page rotate 180.
>
> IIRC, it'd be more like:
> float theta = degToRad( rotation );
> float cosTheta = cos( theta );
> float sineTheta = sine( theta );
> float xOffset = cosTheta * origWid + signTheta * origHei;
> float yOffset = cosTheta * origHei - signTheta * origWid;
>
> cb.addTemplate( page, cosTheta, -1.0f * cosTheta, sineTheta, cosTheta,
> xOffset, yOffset );
>
> I'm none to sure about the x & y offsets. I hand-executed this stuff for
> 0, 90,180,270... IN THEORY, this is correct. You could probably goodle up a
> more authoritative version.
>
> Oh, and you need to modify the page sizes to match your output sizes:
> And you're assuming that the page's bounding boxes are [ 0 0 Wid Hei ].
> That's not always the case either. You'd need to throw the actual page
> origin (coordinates of the lower left corner) into the X & Y offsets you
> calculate.
>
> OR you could do it the easy way. Set up the new page to have exactly the
> same rotation and bBox as the source page. Then the PDF viewer (Reader,
> ghostView, whatever) will do the work for you:
>
> PdfImportedPage page = writer.getImportedPage( reader, x );
> Rectangle newPageRect = reader.getPageSize( x );
> // YUCK. You can't directly set a Rectangle's rotation. Here's a hack
> that'll get you there
> for( int i = reader.getPageRotation(x); i > 0; i-= 90) {
> newPageRect.rotate();
> }
>
> doc.setPageSize( newPageRect );
> doc.newPage();
> cb = writer.getDirectContent();
> cb.addTemplate( page );
>
>
> I'm not sure any of that will fix the bug you're facing, but it sure will
> fix some bugs you haven't run into yet.
>
> --
> --Mark Storer
> PDF Guy
>
--
--Mark Storer
Professional Geek
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
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