diff --git a/cardcreator/src/tutorial/chapter01/SimpleTable.java b/cardcreator/src/tutorial/chapter01/SimpleTable.java index 865f87e..0a013d8 100644 --- a/cardcreator/src/tutorial/chapter01/SimpleTable.java +++ b/cardcreator/src/tutorial/chapter01/SimpleTable.java @@ -3,15 +3,27 @@ package tutorial.chapter01; import java.io.File; import com.itextpdf.barcodes.BarcodeQRCode; +import com.itextpdf.kernel.geom.PageSize; import com.itextpdf.kernel.pdf.PdfDocument; +import com.itextpdf.kernel.pdf.PdfNumber; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; +import com.itextpdf.layout.element.Cell; +import com.itextpdf.layout.element.IElement; import com.itextpdf.layout.element.Image; +import com.itextpdf.layout.element.Paragraph; import com.itextpdf.layout.element.Table; +import com.itextpdf.layout.property.TextAlignment; +import com.itextpdf.layout.property.UnitValue; +import com.itextpdf.layout.property.VerticalAlignment; public class SimpleTable { public static final String DEST = "results/simple_table.pdf"; - + public static final PdfNumber INVERTEDPORTRAIT = new PdfNumber(180); + public static final PdfNumber LANDSCAPE = new PdfNumber(90); + public static final PdfNumber PORTRAIT = new PdfNumber(0); + public static final PdfNumber SEASCAPE = new PdfNumber(270); + public static void main(String[] args) throws Exception { File file = new File(DEST); file.getParentFile().mkdirs(); @@ -20,18 +32,41 @@ public class SimpleTable { protected void manipulatePdf(String dest) throws Exception { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); + pdfDoc.setDefaultPageSize(PageSize.A4.rotate()); + Document doc = new Document(pdfDoc); - - Table table = new Table(9); + doc.setMargins(0, 0, 0, 0); + + UnitValue[] colWidths = new UnitValue[9]; + for (int i = 0; i < colWidths.length; i++) + colWidths[i] = new UnitValue(UnitValue.PERCENT, 11.10f); + + Table table = new Table(colWidths); + table.useAllAvailableWidth(); + for (int i = 0; i < 17; i++) { table.addCell("hi"); } BarcodeQRCode barcodeQRCode = new BarcodeQRCode("spotify:album:1X2HXE2lAqdmD8VjiBX8Tn"); - table.addCell(new Image(barcodeQRCode.createFormXObject(pdfDoc))); + Cell cell = new Cell(); + cell.setVerticalAlignment(VerticalAlignment.MIDDLE); + cell.setHeight(297.637795276f); // mm / 25.4 = in | in * 72 = pt + + Paragraph p = new Paragraph(); + p.setTextAlignment(TextAlignment.CENTER); + Image qrcode = new Image(barcodeQRCode.createFormXObject(pdfDoc)); + qrcode.setWidth(10/25.4f*72); + p.add(qrcode); + cell.add(p); + table.addCell(cell); + +// table.addCell(new Image(barcodeQRCode.createFormXObject(pdfDoc))); + + table.getCell(1, 1).setVerticalAlignment(VerticalAlignment.MIDDLE); doc.add(table); - + doc.close(); } } \ No newline at end of file