List of usage examples for com.lowagie.text Document setPageSize
public boolean setPageSize(Rectangle pageSize)
From source file:ppro.contoller.ExportController.java
public void postProcessPDF(Object document) throws IOException, DocumentException { final Document pdf = (Document) document; pdf.setPageSize(PageSize.A4.rotate()); }
From source file:questions.graphics2D.SplitCanvas.java
public static void main(String[] args) { Document document = new Document(); try {/* w ww. jav a 2 s .c om*/ document.setPageSize(new Rectangle(100, 100)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); document.open(); // create the canvas for the complete drawing: PdfContentByte directContent = writer.getDirectContentUnder(); PdfTemplate canvas = directContent.createTemplate(200, 200); Graphics2D g2d = canvas.createGraphicsShapes(200, 200); // draw to the complete drawing to the canvas: g2d.setPaint(new Color(150, 150, 255)); g2d.setStroke(new BasicStroke(10.0f)); g2d.drawArc(50, 50, 100, 100, 0, 360); g2d.dispose(); // wrap the canvas inside an image: Image img = Image.getInstance(canvas); // distribute the image over 4 pages: img.setAbsolutePosition(0, -100); document.add(img); g2d = directContent.createGraphicsShapes(100, 100); g2d.setPaint(new Color(255, 150, 150)); g2d.setStroke(new BasicStroke(5.0f)); g2d.drawLine(25, 25, 25, 100); g2d.drawLine(25, 25, 100, 25); g2d.dispose(); document.newPage(); img.setAbsolutePosition(-100, -100); document.add(img); g2d = directContent.createGraphicsShapes(100, 100); g2d.setPaint(new Color(255, 150, 150)); g2d.setStroke(new BasicStroke(5.0f)); g2d.drawLine(0, 25, 75, 25); g2d.drawLine(75, 25, 75, 100); g2d.dispose(); document.newPage(); img.setAbsolutePosition(0, 0); document.add(img); g2d = directContent.createGraphicsShapes(100, 100); g2d.setPaint(new Color(255, 150, 150)); g2d.setStroke(new BasicStroke(5.0f)); g2d.drawLine(25, 0, 25, 75); g2d.drawLine(25, 75, 100, 75); g2d.dispose(); document.newPage(); img.setAbsolutePosition(-100, 0); document.add(img); g2d = directContent.createGraphicsShapes(100, 100); g2d.setPaint(new Color(255, 150, 150)); g2d.setStroke(new BasicStroke(5.0f)); g2d.drawLine(0, 75, 75, 75); g2d.drawLine(75, 0, 75, 75); g2d.dispose(); } catch (DocumentException de) { de.printStackTrace(); return; } catch (IOException ioe) { ioe.printStackTrace(); return; } document.close(); }
From source file:questions.graphics2D.SplitCanvasDifficult.java
public static void main(String[] args) { Document document = new Document(); try {/* w w w .j av a 2s . c om*/ document.setPageSize(new Rectangle(100, 100)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); document.open(); // create the canvas for the complete drawing: PdfContentByte directContent = writer.getDirectContentUnder(); Graphics2D g2d; // distribute the image over 4 pages: g2d = directContent.createGraphicsShapes(100, 100); g2d.setPaint(new Color(255, 150, 150)); g2d.setStroke(new BasicStroke(5.0f)); g2d.drawLine(25, 25, 25, 100); g2d.drawLine(25, 25, 100, 25); g2d.dispose(); document.newPage(); g2d = directContent.createGraphicsShapes(100, 100); g2d.setPaint(new Color(255, 150, 150)); g2d.setStroke(new BasicStroke(5.0f)); g2d.drawLine(0, 25, 75, 25); g2d.drawLine(75, 25, 75, 100); g2d.dispose(); document.newPage(); g2d = directContent.createGraphicsShapes(100, 100); g2d.setPaint(new Color(255, 150, 150)); g2d.setStroke(new BasicStroke(5.0f)); g2d.drawLine(25, 0, 25, 75); g2d.drawLine(25, 75, 100, 75); g2d.dispose(); document.newPage(); g2d = directContent.createGraphicsShapes(100, 100); g2d.setPaint(new Color(255, 150, 150)); g2d.setStroke(new BasicStroke(5.0f)); g2d.drawLine(0, 75, 75, 75); g2d.drawLine(75, 0, 75, 75); g2d.dispose(); } catch (DocumentException de) { de.printStackTrace(); return; } catch (IOException ioe) { ioe.printStackTrace(); return; } document.close(); }
From source file:questions.graphics2D.SplitCanvasEasy.java
public static void main(String[] args) { Document document = new Document(); try {//ww w . j a v a 2 s .c om document.setPageSize(new Rectangle(100, 100)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); document.open(); // create the canvas for the complete drawing: PdfContentByte directContent = writer.getDirectContentUnder(); PdfTemplate canvas = directContent.createTemplate(200, 200); Graphics2D g2d = canvas.createGraphicsShapes(200, 200); // draw to the complete drawing to the canvas: g2d.setPaint(new Color(150, 150, 255)); g2d.setStroke(new BasicStroke(10.0f)); g2d.drawArc(50, 50, 100, 100, 0, 360); g2d.dispose(); // wrap the canvas inside an image: Image img = Image.getInstance(canvas); // distribute the image over 4 pages: img.setAbsolutePosition(0, -100); document.add(img); document.newPage(); img.setAbsolutePosition(-100, -100); document.add(img); document.newPage(); img.setAbsolutePosition(0, 0); document.add(img); document.newPage(); img.setAbsolutePosition(-100, 0); document.add(img); } catch (DocumentException de) { de.printStackTrace(); return; } catch (IOException ioe) { ioe.printStackTrace(); return; } document.close(); }
From source file:questions.images.MakingImageTransparent.java
public static void main(String args[]) { try {//from ww w .jav a 2 s . c om Rectangle rect; // GIF Image Image gif = Image.getInstance(GIF); gif.setAbsolutePosition(0, 0); gif.setTransparency(new int[] { 0x5B, 0x5D }); rect = new Rectangle(gif.getScaledWidth(), gif.getScaledHeight()); rect.setBackgroundColor(Color.YELLOW); Document document = new Document(rect); PdfWriter.getInstance(document, new FileOutputStream(RESULT)); document.open(); document.add(gif); // JPEG Image java.awt.Image awtImage = Toolkit.getDefaultToolkit().createImage(JPG); Image jpg = Image.getInstance(awtImage, null, true); jpg.setTransparency(new int[] { 0xF0, 0xFF }); jpg.setAbsolutePosition(0, 0); rect = new Rectangle(jpg.getScaledWidth(), jpg.getScaledHeight()); rect.setBackgroundColor(Color.YELLOW); document.setPageSize(rect); document.newPage(); document.add(jpg); document.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:questions.importpages.HelloWorldImportedPages.java
/** * Generates a PDF file with bookmarks./*www . j a v a 2 s . com*/ * * @param filename * the filename of the PDF file. */ private static void createPdf(String filename) { // we create a document with multiple pages and bookmarks Document document = new Document(PageSize.A4); try { PdfWriter.getInstance(document, new FileOutputStream(filename)); document.open(); document.add(new Paragraph( "In this document, we are going to say hello to different beings in different languages.")); document.newPage(); Paragraph hello = new Paragraph( "(English:) hello, " + "(Esperanto:) he, alo, saluton, (Latin:) heu, ave, " + "(French:) all\u00f4, (Italian:) ciao, (German:) hallo, he, heda, holla, " + "(Portuguese:) al\u00f4, ol\u00e1, hei, psiu, bom d\u00eda, (Dutch:) hallo, dag, " + "(Spanish:) ola, eh, (Catalan:) au, bah, eh, ep, " + "(Swedish:) hej, hejsan(Danish:) hallo, dav, davs, goddag, hej, " + "(Norwegian:) hei; morn, (Papiamento:) halo; hallo; k\u00ed tal, " + "(Faeroese:) hall\u00f3, hoyr, (Turkish:) alo, merhaba, (Albanian:) tungjatjeta"); Chapter universe = new Chapter("To the Universe:", 1); Section section; section = universe.addSection("to the World:"); section.add(hello); section = universe.addSection("to the Sun:"); section.add(hello); section = universe.addSection("to the Moon:"); section.add(hello); section = universe.addSection("to the Stars:"); section.add(hello); document.add(universe); Chapter people = new Chapter("To the People:", 2); section = people.addSection("to mothers and fathers:"); section.add(hello); section = people.addSection("to brothers and sisters:"); section.add(hello); section = people.addSection("to wives and husbands:"); section.add(hello); section = people.addSection("to sons and daughters:"); section.add(hello); section = people.addSection("to complete strangers:"); section.add(hello); document.add(people); document.setPageSize(PageSize.A4.rotate()); Chapter animals = new Chapter("To the Animals:", 3); section = animals.addSection("to cats and dogs:"); section.add(hello); section = animals.addSection("to birds and bees:"); section.add(hello); section = animals.addSection("to farm animals and wild animals:"); section.add(hello); section = animals.addSection("to bugs and beatles:"); section.add(hello); section = animals.addSection("to fish and shellfish:"); section.add(hello); document.add(animals); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } document.close(); }
From source file:ro.cs.om.utils.organigram.OrganigramUtils.java
License:Open Source License
/** * Creates the PDF file containing a JPEG Picture representing our Organigram. * It then writes this PDF File in an OutputStream. * Particular the OutputStream it's ServletOutputStream. * @author dan.damian /*from w ww .j a va2 s. c om*/ */ public void createPDFOrganigram(OutputStream os, OrgTree tree) throws Exception { //The PDF Document Document document = new Document(); //Organigram dimensions int height = calculateOrganigramHeight(tree); int width = calculateOrganigramWidth(tree); //Setting Document's dimensions document.setPageSize(new Rectangle(width + 200, height + 200)); PdfWriter.getInstance(document, os); document.open(); // Retrieving the Organization Organisation org = BLOrganisation.getInstance().get(tree.getRoot().getOrganizationId()); document.add(new Paragraph(org.getName())); // Create an Image BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // Get the Image's Graphics, and draw. Graphics2D g = img.createGraphics(); g.setColor(Color.white); g.fillRect(0, 0, width, height); //Start drawing the Organigram with one node, it's root... g.setColor(LINE_COLOR); String themeCode = ((UserAuth) SecurityContextHolder.getContext().getAuthentication().getPrincipal()) .getThemeCode(); String orgPicLocation = ORGANISATION_NODE_PICTURE_LOCATION.replace(REPLACE_THEME, themeCode); String deptPicLocation = DEPARTMENT_NODE_PICTURE_LOCATION.replace(REPLACE_THEME, themeCode); logger.debug("Org = " + ORGANISATION_NODE_PICTURE_LOCATION + ", " + orgPicLocation); logger.debug("Dept = " + DEPARTMENT_NODE_PICTURE_LOCATION + ", " + deptPicLocation); //Location of the Image file that will represent an Organization Box String nodeOrgImage = FileUtils.getInstance().getRealPathForResource(orgPicLocation); //Location of the Image file that will represent a Department Box String nodeDeptImage = FileUtils.getInstance().getRealPathForResource(deptPicLocation); logger.debug("nodeOrgImage = " + nodeOrgImage); logger.debug("nodeDeptImage = " + nodeDeptImage); drawNode(g, tree.getRoot(), nodeOrgImage, nodeDeptImage, true); //Add the Image to PDF com.lowagie.text.Image image = com.lowagie.text.Image .getInstance(ImageUtils.getInstance().bufferedImageToByteArray(img)); document.add(image); //Close the Document document.close(); }
From source file:util.PDFconverter.java
public static void createPDF(String[] header, String[][] data, String path, String tittle, float[] columnWidths) { try {/*w ww . j a va2s . c o m*/ Document doc = new Document(); PdfWriter.getInstance(doc, new FileOutputStream(path)); doc.open(); doc.setPageSize(PageSize.A4); doc.setMargins(10, 10, 10, 10); Font litle = new Font(Font.COURIER, 7, Font.NORMAL); Font norm = new Font(Font.TIMES_ROMAN, 8, Font.NORMAL); Font normBold = new Font(Font.TIMES_ROMAN, 8, Font.BOLD); Font TitleFont = new Font(Font.TIMES_ROMAN, 12, Font.BOLD); doc.add(Chunk.NEWLINE); Paragraph judul = new Paragraph(tittle, TitleFont); judul.setAlignment(Element.ALIGN_CENTER); doc.add(judul); // Paragraph tgl = new Paragraph("tanggal " + tanggal + "\n", TitleFont); // tgl.setAlignment(Element.ALIGN_CENTER); // doc.add(tgl); doc.add(Chunk.NEWLINE); PdfPTable table = new PdfPTable(header.length); table.setWidthPercentage(100f); for (String head : header) { table.addCell(new PdfPCell(new Phrase(head, normBold))); } for (String[] obj : data) { for (int i = 0; i < header.length; i++) { table.addCell(new PdfPCell(new Phrase(obj[i], norm))); } } //float[] columnWidths = new float[] {10f, 20f, 30f, 10f}; table.setWidths(columnWidths); doc.add(table); // Paragraph stamp = new Paragraph(new Chunk("this report has generated with QCMS by " + System.getProperty("user.name") + " on " + new Date(), litle)); // stamp.setAlignment(Element.ALIGN_BOTTOM); // stamp.setAlignment(Element.ALIGN_CENTER); // doc.add(stamp); // // Paragraph tanda = new Paragraph(new Chunk("Mengetahui,", norm)); // tanda.setSpacingBefore(100); // tanda.setAlignment(Element.ALIGN_RIGHT); // tanda.setAlignment(Element.ALIGN_BOTTOM); // doc.add(tanda); // // Paragraph nama = new Paragraph(new Chunk("MANAGER Dept.RnQ", norm)); // nama.setSpacingBefore(30); // nama.setAlignment(Element.ALIGN_RIGHT); // nama.setAlignment(Element.ALIGN_BOTTOM); // doc.add(nama); doc.close(); } catch (DocumentException | FileNotFoundException ex) { Logger.getLogger(PDFconverter.class.getName()).log(Level.SEVERE, null, ex); } }