Ok, I think I understand that. I'm still confused on if getBottom/Top/BottomMargin/TopMargin are expected to return the dimensions that are different then where doc.add() will "respect".
Look at page 1, 3, and 4 of this sample. I used ColumnText to add a phrase at what I thought was the bottom. It really stumped me, because my table was respecting the new margins so I expected when I said to place some text at doc.bottom(), it would show up where the table ended...and it didn't. I'm just wondering if I'm doing something wrong in the way I'm changing margins. Is there a better way to accomplish this task? If not, I have to remember that the getTop/Bottom/etc may not return the (what I considered) expected locations. It seems strange that doc.add() would use vertical space between point A and B, but doc.top() and doc.bottom() could return a different A and B location. Is the rule "once you change margins, top/bottom/left/right may not return what you expect"? Thanks for always helping, Jason import java.io.File; import java.io.FileOutputStream; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.Element; import com.itextpdf.text.PageSize; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.ColumnText; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfPageEventHelper; import com.itextpdf.text.pdf.PdfWriter; public class TestChangeMargins extends PdfPageEventHelper { public static final Rectangle PAGE_SIZE = PageSize.LETTER; public static final float LEFT = 36; public static final float RIGHT = 36; public static final float TOP = 90; public static final float BOTTOM = 36; public static final float BIG_BOTTOM = 250; public static void main(String[] args) { try { new TestChangeMargins(); } catch (Exception e) { e.printStackTrace(); } } private Document doc; private PdfContentByte canvas; private boolean changeMargins = false; public TestChangeMargins() throws Exception { doc = new Document(PAGE_SIZE, LEFT, RIGHT, 200, BOTTOM); PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(new File( "/home/jberk/desktop/test.pdf"))); writer.setPageEvent(this); doc.open(); canvas = writer.getDirectContent(); addTable(); newPageWithNewMargins(); addTable(); doc.close(); } private void addTable() throws Exception { PdfPTable table = new PdfPTable(1); table.setTotalWidth(144); table.setLockedWidth(true); for (int i = 1; i <= 115; i++) { table.addCell(new Phrase("row: " + i)); } doc.add(table); } private void newPageWithNewMargins() { // change the margins for this next page...subsequent pages should use the standard margins changeMargins = true; doc.newPage(); // move to next page which should have new margins } @Override public void onStartPage(PdfWriter writer, Document document) { changeMargins = false; } @Override public void onEndPage(PdfWriter writer, Document document) { if (changeMargins) { // if this flag is set, use these special margins just for this page /* * notice what DOCUMENT object is used below... in either case, the table is rendered * correctly...respecting the assumed margins, but the reported margins in either case * are wrong. * * If I use document, the top and bottom margins are 200 and 36 on every page but * document.add adds the table like we wanted...with a tall page 1 header and a tall * page 4 footer * * If I use doc, then page 3 shows a bottom margin of 250, yet the table prints past * that point. Then on page 4, the bottom margin is back to 36, but the table stops at * row 29, which is the bottom margin as marked on page 3. */ // document.setMargins(LEFT, RIGHT, TOP, BIG_BOTTOM); doc.setMargins(LEFT, RIGHT, TOP, BIG_BOTTOM); } else { // after page 1, we should use these standard margins // document.setMargins(LEFT, RIGHT, TOP, BOTTOM); doc.setMargins(LEFT, RIGHT, TOP, BOTTOM); } displayMargins(); } private void displayMargins() { ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("doc.topMargin() = " + doc.topMargin()), doc.left(), doc.top(), 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("doc.top() = " + doc.top()), doc.right(), doc.top(), 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("doc.bottomMargin() = " + doc.bottomMargin()), doc.left(), doc.bottom(), 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("doc.bottom() = " + doc.bottom()), doc.right(), doc.bottom(), 0); canvas.setColorStroke(BaseColor.RED); canvas.moveTo(doc.left(), doc.bottom()); canvas.lineTo(doc.left(), doc.top()); canvas.lineTo(doc.right(), doc.top()); canvas.lineTo(doc.right(), doc.bottom()); canvas.lineTo(doc.left(), doc.bottom()); canvas.stroke(); } } -----Original Message----- From: 1T3XT BVBA [mailto:i...@1t3xt.info] Sent: Thursday, July 28, 2011 4:24 AM To: Post all your questions about iText here Subject: Re: [iText-questions] Document getBottomMargin() bug On 27/07/2011 18:04, Jason Berk wrote: > I change page margins in the onEndPage event, so the changes will > affect the next page. > > When I add my table to the document via doc.add(myTable), the new > margins are respected and my PDF looks great. What's funny is that if > use doc.bottomMargin(), I get back the wrong size. In other words, > the correct margins are being used by doc.add(), but not returned via > the getters. > > Anybody else ever seen this Whether it's a bug or not... that's debatable. In any case: the reason for this behavior is clear. When you create a Document object, you don't indicate that you want to produce PDF. In the past, you could create a Document object to create RTF, HTML, PDF... It isn't up until you create a PdfWriter that you're actually creating a PDF document. You aren't limited to creating just one PDF document from one Document object. You can create different PdfWriter instances for the same document. For instance: to simultaneously create one PDF that is stored on disk and one PDF that is sent to a browser. (Warning: don't use ColumnText when you do this; don't use direct content either.) You are using page events. Page events are linked to one (and only one) PdfWriter instance. You have access to a Document object in the event's methods. HOWEVER (as documented in the book), this Document object IS NOT the original Document object you created. Instead it's an instance of the PdfDocument class that is created for INTERNAL use. As explained in the book, you should use this PdfDocument object for READ-ONLY purposes. You shouldn't add any content to this object, nor should you change the margins! If you do, you'll experience strange side-effects. Hope this helps. ------------------------------------------------------------------------ ------ Got Input? Slashdot Needs You. Take our quick survey online. Come on, we don't ask for help often. Plus, you'll get a chance to win $100 to spend on ThinkGeek. http://p.sf.net/sfu/slashdot-survey _______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/itext-questions iText(R) is a registered trademark of 1T3XT BVBA. Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/ Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php This is a transmission from Purdue Federal Credit Union (Purdue Federal) and is intended solely for its authorized recipient(s), and may contain information that is confidential and or legally privileged. If you are not an addressee, or the employee or agent responsible for delivering it to an addressee, you are hereby notified that any use, dissemination, distribution, publication or copying of the information contained in this email is strictly prohibited. If you have received this transmission in error, please notify us by telephoning (765)497-3328 or returning the email. You are then instructed to delete the information from your computer. Thank you for your cooperation. ------------------------------------------------------------------------------ Got Input? Slashdot Needs You. Take our quick survey online. Come on, we don't ask for help often. Plus, you'll get a chance to win $100 to spend on ThinkGeek. http://p.sf.net/sfu/slashdot-survey _______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/itext-questions iText(R) is a registered trademark of 1T3XT BVBA. Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/ Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php