The margin only take effect in the next page. You must set them before opening the document.
----- Original Message ----- From: "Söderström Programvaruverkstad" <[EMAIL PROTECTED]> To: <itext-questions@lists.sourceforge.net> Sent: Saturday, May 07, 2005 7:59 PM Subject: [iText-questions] PdfPTable: second page very different from first (now with code) Hi again, Using iText (v 1.2.3) for an app that builds a table in two columns. It extends into some 10 pages. It sets the doc margins, the table width percentage, etc, once and only once. The problem is that the first page comes out as expected, while subsequent pages have different margins. Why? There is way too much code, so I condensed the pertinent part to a stand-alone class. Basically it creates the document, sets its margins, and creates the table. Cells are added, each one created from a Phrase. The Phrase is created by adding Chunks with a Font, including Chunk.NEWLINE where appropriate. Finally, empty cells are added to fill up the last row, and then the table is added to the document. /Hakan package se.soderstrom.freg.rullan; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.io.IOException; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Stack; import com.lowagie.text.*; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; import se.soderstrom.freg.m.*; import se.soderstrom.yoddl.rt.DbObject; /** * PDF report generation. */ public class TestReport { // Millimeter unit private static final float MM_UNIT = 72.0f/25.4f; // Centimeter unit private static final float CM_UNIT = 72.0f/2.54f; /** * Cell layout. */ private CellLayout cellLayout; /** * List where each element is a TextTemplate instance corresponding * to a column. */ private List colTempl; /** * Default font for the report. */ private Font defaultFont; /** * The report as an iText document. */ private Document doc; /** * Number of columns across the page. */ private int nColumns; /** * Number of columns for each record. */ private int rColumns; /** * The report output stream. */ private OutputStream os; /** * The table that makes up the major part of the report. */ private PdfPTable mainTable; /** * Total cells written. */ private int totalCells = 0; /** * The factor to multiply all measurements with to get points. */ private float unit; /** * The current PDF writer. */ private PdfWriter writer; /** * Initialize a report. * * Creates a document, sets header and footer. * Initializes various report generating resources. * Sets up a table for the core of the contents. */ public TestReport(OutputStream os) throws DocumentException { this.os = os; // Initialize PDF output. doc = new Document(); writer = PdfWriter.getInstance(doc, os); // Determine the report unit. unit = MM_UNIT; defaultFont = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12.0f, Font.NORMAL); // // XXX Set up header and footer information before opening the document. // // Initialize the document. doc.open(); doc.setPageSize(PageSize.A4); setMargins(doc); // Initialize the table. nColumns = 2; rColumns = 1; mainTable = new PdfPTable(nColumns); mainTable.setSplitRows(false); mainTable.setWidthPercentage(100); float[] widths = new float[nColumns]; for (int idx = 0; idx < widths.length; idx++) widths[idx] = 100.0f/(float)nColumns; mainTable.setWidths(widths); // Extract cell layout. cellLayout = new CellLayout(); // Extract column templates. colTempl = new LinkedList(); colTempl.add("Single column, dummy template"); } public static void main(String[] argv) { try { FileOutputStream fos = new FileOutputStream("report.pdf"); TestReport report = new TestReport(fos); for (int idx = 0; idx < 113; idx++) report.writePers(); report.close(); } catch (Exception exc) { System.out.println("EXCEPTION: " + exc); exc.printStackTrace(System.out); } System.out.println("Finished"); } /** * Close the report, i.e. write it out, finalize. */ public void close() throws DocumentException { // Write empty cells until the last row is complete. PdfPCell cell = new PdfPCell(new Phrase()); layoutCell(cell); for (; totalCells % nColumns != 0; totalCells++) mainTable.addCell(cell); doc.add(mainTable); doc.close(); } /** * Write a database object to the report. */ public void writePers() throws Exception { Iterator it = colTempl.iterator(); for (int colidx = 0; colidx < rColumns ; colidx++) { if (it.hasNext()) { Object template = it.next(); Phrase phrase = null; try { phrase = writePersCell(); totalCells++; } catch (Exception exc) { System.out.println(exc); exc.printStackTrace(System.out); } PdfPCell cell = new PdfPCell(phrase); layoutCell(cell); mainTable.addCell(cell); } } } //===================== Private methods ===================== /** * Set common cell properties. */ private void layoutCell(PdfPCell cell) { //cell.setBorder(cellLayout.border); cell.setLeading(cellLayout.leadingFixed, cellLayout.leadingRelative); cell.setPaddingLeft(cellLayout.paddingLeft); cell.setPaddingRight(cellLayout.paddingRight); cell.setPaddingTop(cellLayout.paddingTop); cell.setPaddingBottom(cellLayout.paddingBottom); } private void setMargins(Document doc) { float leftMargin = 1.5f * CM_UNIT; float rightMargin = 1.5f * CM_UNIT; float topMargin = 2.5f * CM_UNIT; float bottomMargin = 2.0f * CM_UNIT; doc.setMargins(leftMargin, rightMargin, topMargin, bottomMargin); } /** * Write a cell. * * @return the phrase to be inserted into the cell. */ private Phrase writePersCell() throws Exception { Phrase ph = new Phrase(); Font font = defaultFont; ph.add(new Chunk("1234", FontFactory.getFont(FontFactory.HELVETICA, 7.0f, Font.NORMAL))); ph.add(Chunk.NEWLINE); ph.add(new Chunk("Xxxxxxxxxxxx", defaultFont)); ph.add(new Chunk(", ", defaultFont)); ph.add(new Chunk("Xxxxxx", defaultFont)); ph.add(Chunk.NEWLINE); ph.add(new Chunk("21, Yyy Yyyyyyyyyy", defaultFont)); ph.add(Chunk.NEWLINE); Font bold = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12.0f, Font.BOLD); ph.add(new Chunk("123 45", bold)); ph.add(new Chunk(" ", bold)); ph.add(new Chunk("Zzzzzzzzz", bold)); return ph; } /** * Cell layout values. */ private class CellLayout { public int border; public float leadingFixed; public float leadingRelative; public float paddingLeft; public float paddingRight; public float paddingTop; public float paddingBottom; public CellLayout() { border = 0; leadingFixed = 16.0f; leadingRelative = 0.0f; paddingLeft = 1.0f * MM_UNIT; paddingRight = 3.0f * MM_UNIT; paddingTop = 1.5f * MM_UNIT; paddingBottom = 2.5f * MM_UNIT; } } } ------------------------------------------------------- This SF.Net email is sponsored by: NEC IT Guy Games. Get your fingers limbered up and give it your best shot. 4 great events, 4 opportunities to win big! Highest score wins.NEC IT Guy Games. Play to win an NEC 61 plasma display. Visit http://www.necitguy.com/?r=20 _______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/itext-questions ------------------------------------------------------- This SF.Net email is sponsored by: NEC IT Guy Games. Get your fingers limbered up and give it your best shot. 4 great events, 4 opportunities to win big! Highest score wins.NEC IT Guy Games. Play to win an NEC 61 plasma display. Visit http://www.necitguy.com/?r=20 _______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/itext-questions