Post a complete working sample with the wrong behavior. I can't reproduce the problem with only what you have provided. I did this program and it works.
import java.io.*; import com.lowagie.text.*; import com.lowagie.text.pdf.*; import java.awt.*; public class g2_landscape { public static void main(String[] args) { Document document = new Document(PageSize.A4, 50, 50, 50, 50); Document.compress = false; try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:\\g2_landscape.pdf")); document.open(); PdfContentByte cb = writer.getDirectContent(); Graphics2D g2 = cb.createGraphics(100, 100); g2.drawLine(0, 0, 100, 100); g2.dispose(); document.setPageSize(PageSize.A4.rotate()); document.newPage(); g2 = cb.createGraphics(100, 100); g2.drawLine(0, 0, 100, 100); g2.dispose(); document.close(); System.out.println("Finished."); } catch (Exception de) { de.printStackTrace(); } } } Best Regards, > -----Original Message----- > From: Erwin Achermann [SMTP:[EMAIL PROTECTED]] > Sent: Monday, April 22, 2002 17:05 > To: jim moore; [EMAIL PROTECTED] > Subject: RE: [iText-questions] PdfGraphics2D: unbalanced save/Restore > Exception > > Hi Jim, > you're right they were mismatched but still after fixing that i get wrong > behaviour. > It seems like the the PdfGraphics2D is doing some chaching or > optimization, concerning the save/resore graphicsState. > > // set pageSize of first page before the document > opens!! > float width = > (float)pageable.getPageFormat(0).getPaper().getWidth(); > float height = > (float)pageable.getPageFormat(0).getPaper().getHeight(); > theDocument.setPageSize(new Rectangle(width, > height)); > // g.setPageSize(pageable.getPageFormat(0)); // this > was my old implementation > theDocument.open(); > g = > pdfWriter.getDirectContent().createGraphics(width, height, fm); > > for (int p = 0; p < pages ; ++p) { > Printable thePage = > pageable.getPrintable(p); > try { > // print! > thePage.print(g, > pageable.getPageFormat(p), p); > > System.out.println("PdfJob.createPdf: page " + p + " printed"); > if (p < pages-1) { > // next page, please! on its > proper PageSize > // > g.setPageSize(pageable.getPageFormat(p+1)); > width = > (float)pageable.getPageFormat(p+1).getPaper().getWidth(); > height = > (float)pageable.getPageFormat(p+1).getPaper().getHeight(); > theDocument.setPageSize(new > Rectangle(width, height)); > } // end of if (p < pages-1) > theDocument.newPage(); > g.dispose(); > g = > pdfWriter.getDirectContent().createGraphics(width, height, fm); > } catch ( PrinterException e) { > e.printStackTrace(); > } > catch(DocumentException de) { > de.printStackTrace(); > } > } // end of for (int p = 0; p < pages; p++) > g.dispose(); > // write the document and clear all references > end(); > > The idea is to have a PdfGraphicsObject per page. I therefore issue a > g.dispose() before each new GraphicsObject is created, and one g.dispose() > after the last page (outside of the the loop) create-dispose pairs match > now. But still the paper orientation is not correctly set to the > respective page orientation. In my onwn old PdfGraphics implementation I > had the following method: > > public void setPageSize(PageFormat pf) { > Paper p = pf.getPaper(); > System.out.println(" Paper: w:"+p.getWidth()+", > h:"+p.getHeight()); > System.out.println(" PageFormat: w:"+pf.getWidth()+" > h:"+pf.getHeight() > + " Orient: > "+pf.getOrientation()); > System.out.println(" DocTop: "+doc.top()); > System.out.println(" Doc.Page.Height: > "+doc.getPageSize().height()); > > doc.setPageSize(new Rectangle((float)pf.getWidth(), > (float)pf.getHeight())); > > // do not use the imageable numbers here. We are drawing > // everthing where we are asked to. Our user (caller) must > // ensure that margins are respected. > int llx = 0; // > (int)pf.getImageableX(); > int lly = 0; // > (int)pf.getImageableY(); > int w = (int)pf.getWidth(); // > (int)pf.getImageableWidth(); > int h = (int)pf.getHeight(); // > (int)pf.getImageableHeight(); > System.out.println(" PageSize: w:"+w+", h:"+h); > > setClip(llx, lly, w, h); > } > > > This Method is not aware of the save/restore issues at all but worked > nicely. Since it seems not so trivial, i would suggest a method along this > line for the new PdfGraphics2D class too, taking the save/restore > resposibility off the users shoulders. What do you think? > > Cheers > Erwin > > > > -----Original Message----- > > From: jim moore [mailto:[EMAIL PROTECTED]] > > Sent: Monday, April 22, 2002 5:14 PM > > To: Erwin Achermann; [EMAIL PROTECTED] > > Subject: Re: [iText-questions] PdfGraphics2D: unbalanced save/Restore > > Exception > > > > > > On first glance it looks like your PdfGraphics2D creates and > > disposes are > > mismatched. You create one before the for loop. If there are > > more pages, you > > dispose it and create a new one. This new one never gets > > disposed though. I > > think this code below should fix it: > > > > /** > > * <code>createPdf</code> prints a set of pages into the PDF > > * file. > > */ > > public void createPdf() { > > // make an instance of a PdfGraphics Object for the document > > DefaultFontMapper fm = new DefaultFontMapper(); > > java.awt.Graphics2D g = null; > > > > // don't do anything if pageable is null > > if (pageable != null) { > > // read all fonts recognized be iText in system font path > > fm.insertDirectory(sun.awt.font.NativeFontWrapper.getFontPath(true)); > > > > System.out.println("PdfJob.createPdf: started"); > > int pages = pageable.getNumberOfPages(); > > System.out.println("PdfJob.createPdf: pages to print: "+pages+"\n"); > > > > // set pageSize of first page before the document opens!! > > float width = (float)pageable.getPageFormat(0).getPaper().getWidth(); > > float height = > > (float)pageable.getPageFormat(0).getPaper().getHeight(); > > theDocument.setPageSize(new Rectangle(width, height)); > > > > // g.setPageSize(pageable.getPageFormat(0)); > > theDocument.open(); > > > > for (int p = 0; p < pages ; ++p) { > > //moved the create into the for loop > > g = pdfWriter.getDirectContent().createGraphics(width, > > height, fm); > > Printable thePage = pageable.getPrintable(p); > > try { > > // print! > > thePage.print(g, pageable.getPageFormat(p), p); > > System.out.println("PdfJob.createPdf: page " + p + " > > printed"); > > if (p < pages-1) { > > // next page, please! on its proper PageSize > > width = > > (float)pageable.getPageFormat(p+1).getPaper().getWidth(); > > height = > > (float)pageable.getPageFormat(p+1).getPaper().getHeight(); > > theDocument.setPageSize(new Rectangle(width, height)); > > } // end of if (p < pages-1) > > > > > > theDocument.newPage(); > > } catch ( PrinterException e) { > > e.printStackTrace(); > > } > > catch(DocumentException de) { > > de.printStackTrace(); > > } > > > > g.dispose(); //dispose it here when you are done with it > > } // end of for (int p = 0; p < pages; p++) > > // write the document and clear all references > > end(); // = theDocument.close() > > } // end of if (pageable) > > } // end createPdf > > > > > > _______________________________________________ > > iText-questions mailing list > > [EMAIL PROTECTED] > > https://lists.sourceforge.net/lists/listinfo/itext-questions > > > > _______________________________________________ > iText-questions mailing list > [EMAIL PROTECTED] > https://lists.sourceforge.net/lists/listinfo/itext-questions _______________________________________________ iText-questions mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/itext-questions