/** * Creates a PDF file: hello_mirrored_margins.pdf * * @param args no arguments needed */ public static void main(String[] args) throws DocumentException, IOException { // step 1 Document document = new Document(); // step 2 PdfWriter.getInstance(document, new FileOutputStream(RESULT)); // setting page size, margins and mirrered margins document.setPageSize(PageSize.A5); document.setMargins(36, 72, 108, 180); document.setMarginMirroringTopBottom(true); // step 3 document.open(); // step 4 document.add( new Paragraph( "The left margin of this odd page is 36pt (0.5 inch); " + "the right margin 72pt (1 inch); " + "the top margin 108pt (1.5 inch); " + "the bottom margin 180pt (2.5 inch).")); Paragraph paragraph = new Paragraph(); paragraph.setAlignment(Element.ALIGN_JUSTIFIED); for (int i = 0; i < 20; i++) { paragraph.add( "Hello World! Hello People! " + "Hello Sky! Hello Sun! Hello Moon! Hello Stars!"); } document.add(paragraph); document.add(new Paragraph("The top margin 180pt (2.5 inch).")); // step 5 document.close(); }
public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); Rectangle one = new Rectangle(70, 140); Rectangle two = new Rectangle(700, 400); document.setPageSize(one); document.setMargins(2, 2, 2, 2); document.open(); Paragraph p = new Paragraph("Hi"); document.add(p); document.setPageSize(two); document.setMargins(20, 20, 20, 20); document.newPage(); document.add(p); document.close(); }
/** * Method to export charts as PDF files using the defined path. * * @param path The filename and absolute path. * @param chart The JFreeChart object. * @param width The width of the PDF file. * @param height The height of the PDF file. * @param mapper The font mapper for the PDF file. * @param title The title of the PDF file. * @throws IOException If writing a PDF file fails. */ @SuppressWarnings("deprecation") public static void exportPdf( String path, JFreeChart chart, int width, int height, FontMapper mapper, String title) throws IOException { File file = new File(path); FileOutputStream pdfStream = new FileOutputStream(file); BufferedOutputStream pdfOutput = new BufferedOutputStream(pdfStream); Rectangle pagesize = new Rectangle(width, height); Document document = new Document(); document.setPageSize(pagesize); document.setMargins(50, 50, 50, 50); document.addAuthor("OMSimulationTool"); document.addSubject(title); try { PdfWriter pdfWriter = PdfWriter.getInstance(document, pdfOutput); document.open(); PdfContentByte contentByte = pdfWriter.getDirectContent(); PdfTemplate template = contentByte.createTemplate(width, height); Graphics2D g2D = template.createGraphics(width, height, mapper); Double r2D = new Rectangle2D.Double(0, 0, width, height); chart.draw(g2D, r2D); g2D.dispose(); contentByte.addTemplate(template, 0, 0); } catch (DocumentException de) { JOptionPane.showMessageDialog( null, "Failed to write PDF document.\n" + de.getMessage(), "Failed", JOptionPane.ERROR_MESSAGE); de.printStackTrace(); } document.close(); }
/** * Constructor. Takes in a string representing where the file should be written to disk. * * @param fname * @throws DocumentException * @throws FileNotFoundException */ public BarcodePdf(String fname) throws FileNotFoundException, DocumentException { _d = new Document(); _writer = PdfWriter.getInstance(_d, new FileOutputStream(fname)); _d.setMargins(25, 25, 25, 25); _d.open(); _open = true; _rawContent = _writer.getDirectContent(); _table = new PdfPTable(4); _table.setHorizontalAlignment(Element.ALIGN_CENTER); _itemsAdded = 0; }
private Document newDocument() { Document document = new Document(); document.setPageSize(PageSize.LETTER); document.setMargins(72, 72, 72, 72); return document; }