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); // only page 1 top margin is 200 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 next page. // Subsequent pages should revert back to standard sizes /* * 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() { // the only doc object we have access to here is "doc" and not "document" from the event handler // this more closely resembles my code, where the class adding content doesn't have access to the event handler ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("topMargin() = " + doc.topMargin()), doc.left(), doc.top(), 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("bottomMargin() = " + doc.bottomMargin()), doc.left(), 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: Jason Berk [mailto:jb...@purduefed.com] Sent: Wednesday, July 27, 2011 12:04 PM To: Post all your questions about iText here Subject: [iText-questions] Document getBottomMargin() bug I will send up a sample to illustrate, but here's the just of it. 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? Jason 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 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