Example usage for com.itextpdf.text PageSize A4

List of usage examples for com.itextpdf.text PageSize A4

Introduction

In this page you can find the example usage for com.itextpdf.text PageSize A4.

Prototype

Rectangle A4

To view the source code for com.itextpdf.text PageSize A4.

Click Source Link

Document

This is the a4 format

Usage

From source file:de.fau.amos.ChartToPDF.java

License:Open Source License

/**
 * Converts Chart into PDF//  www . j  a  v a2 s .c o m
 * 
 * @param chart JFreeChart from CharRenderer that will be displayed in the PDF file
 * @param fileName Name of created PDF-file
 */
public void convertChart(JFreeChart chart, String fileName) {
    if (chart != null) {

        //set page size
        float width = PageSize.A4.getWidth();
        float height = PageSize.A4.getHeight();

        //creating a new pdf document
        Document document = new Document(new Rectangle(width, height));
        PdfWriter writer;
        try {
            writer = PdfWriter.getInstance(document,
                    new FileOutputStream(new File(System.getProperty("userdir.location"), fileName + ".pdf")));
        } catch (DocumentException e) {
            e.printStackTrace();
            return;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return;
        }

        document.addAuthor("Green Energy Cockpit");
        document.open();

        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate tp = cb.createTemplate(width, height);

        //sets the pdf page
        Graphics2D g2D = new PdfGraphics2D(tp, width, height);
        Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);

        //draws the passed JFreeChart into the PDF file
        chart.draw(g2D, r2D);

        g2D.dispose();
        cb.addTemplate(tp, 0, 0);

        document.close();

        writer.flush();
        writer.close();
    }
}

From source file:de.tf.capmath.puzzle.pdf.PdfUtil.java

License:Open Source License

public static void image2Pdf(File pdf, BufferedImage image) throws IOException, DocumentException {
    logger.info(String.format("Writing PDF to %s", pdf.getAbsolutePath()));
    Document document = new Document(PageSize.A4, 5, 5, 5, 5);
    PdfWriter writer = null;/*from ww  w . j av  a  2s . c  om*/
    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(pdf));
        document.open();
        PdfContentByte pdfCB = new PdfContentByte(writer);
        Image img = Image.getInstance(pdfCB, image, 1);
        document.add(img);
    } finally {
        document.close();
        writer.close();
    }
}

From source file:digilib.pdf.PDFStreamWorker.java

License:Open Source License

/**
 * @throws DocumentException//from   w ww.  j av a  2 s.c o  m
 * @throws InterruptedException
 * @throws ExecutionException
 * @throws IOException
 */
protected OutputStream renderPDF()
        throws DocumentException, InterruptedException, ExecutionException, IOException {
    // create document object
    doc = new Document(PageSize.A4, 0, 0, 0, 0);
    PdfWriter docwriter = null;

    long start_time = System.currentTimeMillis();

    docwriter = PdfWriter.getInstance(doc, outstream);

    setPDFProperties(doc);

    doc.open();

    addTitlePage(doc);

    logger.debug("PDF: " + outstream + " doc.open()ed (" + (System.currentTimeMillis() - start_time) + "ms)");

    NumRange pgs = job_info.getPages();

    for (int p : pgs) {
        logger.debug("PDF: adding Image " + p + " to " + outstream);
        // create ImageJobInformation
        ImageJobDescription iji = ImageJobDescription.getInstance(job_info, job_info.getDlConfig());
        iji.setValue("pn", p);
        addImage(doc, iji);
        logger.debug("PDF: done adding Image " + p + " to " + outstream);
    }

    logger.debug("PDF: done adding all Images to " + outstream);

    doc.close();
    logger.debug("PDF: " + outstream + " doc.close() (" + (System.currentTimeMillis() - start_time) + "ms)");
    docwriter.flush();
    docwriter.close();
    return outstream;
}

From source file:digilib.pdf.PDFStreamWorker.java

License:Open Source License

/**
 * adds an image to the document.// ww w .ja  va  2 s .  c om
 * 
 * @param doc
 * @param iji
 * @return
 * @throws InterruptedException
 * @throws ExecutionException
 * @throws IOException
 * @throws DocumentException
 */
public Document addImage(Document doc, ImageJobDescription iji)
        throws InterruptedException, ExecutionException, IOException, DocumentException {
    // create image worker
    ImageWorker job = new ImageWorker(dlConfig, iji);
    // submit
    Future<DocuImage> jobTicket = imageJobCenter.submit(job);
    // wait for result
    DocuImage img = jobTicket.get();
    // scale the image
    Image pdfimg = Image.getInstance(img.getAwtImage(), null);
    float docW = PageSize.A4.getWidth() - 2 * PageSize.A4.getBorder();
    float docH = PageSize.A4.getHeight() - 2 * PageSize.A4.getBorder();
    // TODO: do we really scale this again?
    pdfimg.scaleToFit(docW, docH);
    // add to PDF
    doc.add(pdfimg);
    return doc;
}

From source file:domain.Pdfmaker.java

public void makePdf() {
    Document doc = new Document();
    doc.setPageSize(PageSize.A4);
    try {/*from   w ww . jav  a2  s  . c o m*/
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(test.getTitle() + ".pdf"));
        writer.setPdfVersion(PdfWriter.VERSION_1_7);
        doc.open();
        Font f = new Font(Font.FontFamily.TIMES_ROMAN, 15, Font.BOLD, new BaseColor(0, 0, 0));
        Paragraph title = new Paragraph("TEST TITLE", f);
        title.setAlignment(Element.ALIGN_CENTER);
        doc.addCreator("Arne De Bremme");
        //make table
        PdfPTable table = new PdfPTable(4);
        table.setTotalWidth(550f);
        table.setLockedWidth(true);
        table.setHeaderRows(1);

        Paragraph p = new Paragraph(test.getTitle());
        p.setAlignment(Element.ALIGN_CENTER);
        PdfPCell cell = new PdfPCell(p);
        cell.setGrayFill(0.7f);
        cell.setColspan(4);
        table.addCell(cell);
        table.addCell(test.getClassGroup().getGroupName());
        PdfPCell cell2 = new PdfPCell(new Paragraph(test.getDescription()));
        cell2.setColspan(2);
        cell2.setRowspan(2);
        table.addCell(cell2);
        PdfPCell cell3 = new PdfPCell(new Paragraph("DD " + test.getStartDate().getDay() + "/"
                + test.getStartDate().getMonth() + "/" + test.getStartDate().getYear()));
        cell3.setRowspan(2);
        table.addCell(cell3);
        table.addCell(new Paragraph("Test " + test.getTestId()));
        doc.add(table);
        doc.add(Chunk.NEWLINE);
        for (Exercise e : ex) {
            Paragraph par = new Paragraph();
            par.add("Vraag " + counter);
            par.add(Chunk.TABBING);
            par.add(Chunk.TABBING);
            par.add(Chunk.TABBING);
            par.add(Chunk.TABBING);
            par.add(e.getPunten() + "/");
            doc.add(par);
            doc.add(new Paragraph("Klimatogram " + e.getClimateChart().getLocation() + " " + e.getPunten()));
            Robot r = new Robot();
            Desktop.getDesktop().browse(new URI(
                    "http://climatechart.azurewebsites.net/ClimateChart/ShowExercises?selectedYear=1&continentId="
                            + e.getClimateChart().getCountry().getContinent().getId() + "&countryId="
                            + e.getClimateChart().getCountry().getContinent().getId() + "&climateId="
                            + e.getClimateChart().getId()));
            TimeUnit.SECONDS.sleep(6);
            BufferedImage bi = r.createScreenCapture(
                    new Rectangle(Toolkit.getDefaultToolkit().getScreenSize().width * 7 / 10,
                            Toolkit.getDefaultToolkit().getScreenSize().width * 8 / 14));
            File file = new File("screenCapture" + counter + ".jpg");
            ImageIO.write(bi, "jpg", file);
            Image img = Image.getInstance("screenCapture.jpg");
            img.scaleAbsolute(400, 300);
            doc.add(img);
            doc.add(Chunk.NEWLINE);
            doc.add(new Paragraph(e.getNaam()));
            doc.add(Chunk.NEXTPAGE);
            counter++;

        }

        doc.close();
        File filz = new File(test.getTitle() + ".pdf");
        Desktop.getDesktop().open(filz);
    } catch (Exception e) {

    }
}

From source file:dsy_form.Pg_1Controller.java

@FXML
private void doPrinting(ActionEvent event) {
    print_btn.setVisible(false);/*from   w w w  . java 2  s  . c  o  m*/
    nextB.setVisible(false);
    Scene scene = print_btn.getScene();
    WritableImage snapshot = scene.snapshot(null);
    BufferedImage image = SwingFXUtils.fromFXImage(snapshot, null);
    File f = new File("test2.png");
    try {
        ImageIO.write(image, "png", f);
    } catch (IOException ex) {

    }
    Document document = new Document(PageSize.A4, 0, 0, 0, 0);
    try {
        PdfWriter.getInstance(document, new FileOutputStream("Image.pdf"));
        document.open();
        // BufferedImage img = ImageIO.read(new File("test1.png"));
        //File f = new File("test1.png");
        Image image1 = Image.getInstance("test2.png");
        document.add(image1);

        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        InputStream is = new BufferedInputStream(new FileInputStream("Image.pdf"));
        DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
        PrintService service = PrintServiceLookup.lookupDefaultPrintService();
        DocPrintJob printJob = service.createPrintJob();
        printJob.addPrintJobListener(new Pg_1Controller.JobCompleteMonitor());
        Doc doc = new SimpleDoc(is, flavor, null);
        PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
        //attributes.add(new Destination(new java.net.URI("file:/home/jayesh/NetBeansProjects/myFile.ps")));
        printJob.print(doc, attributes);
        //while(jobRunning)
        //{
        //    Thread.sleep(1000);
        //}
        is.close();
    } catch (Exception ex) {
    } finally {
        System.out.println("exiting");

    }
    System.out.println("Done");
    try {
        con1();
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/pro", username, password);
        String query = "insert into log values(?,?)";
        PreparedStatement ps = con.prepareStatement(query);
        ps.setString(1, getCurUser());
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/YYYY HH:mm:ss");
        ps.setString(2, "Printed Direct Second Year Form Page1 at " + df.format(date));
        ps.executeUpdate();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    print_btn.setVisible(true);
    nextB.setVisible(true);
}

From source file:dsy_form.Pg_2Controller.java

@FXML
private void doPrinting(ActionEvent event) {
    print_btn.setVisible(false);/*from   www. j a  v a 2  s  . co  m*/
    back_btn.setVisible(false);
    nextB.setVisible(false);
    Scene scene = print_btn.getScene();
    WritableImage snapshot = scene.snapshot(null);
    BufferedImage image = SwingFXUtils.fromFXImage(snapshot, null);
    File f = new File("test2.png");
    try {
        ImageIO.write(image, "png", f);
    } catch (IOException ex) {

    }
    Document document = new Document(PageSize.A4, 0, 0, 0, 0);
    try {
        PdfWriter.getInstance(document, new FileOutputStream("Image.pdf"));
        document.open();
        // BufferedImage img = ImageIO.read(new File("test1.png"));
        //File f = new File("test1.png");
        Image image1 = Image.getInstance("test2.png");
        document.add(image1);

        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        InputStream is = new BufferedInputStream(new FileInputStream("Image.pdf"));
        DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
        PrintService service = PrintServiceLookup.lookupDefaultPrintService();
        DocPrintJob printJob = service.createPrintJob();
        printJob.addPrintJobListener(new Pg_2Controller.JobCompleteMonitor());
        Doc doc = new SimpleDoc(is, flavor, null);
        PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
        //attributes.add(new Destination(new java.net.URI("file:/home/jayesh/NetBeansProjects/myFile.ps")));
        printJob.print(doc, attributes);
        //while(jobRunning)
        //{
        //    Thread.sleep(1000);
        //}
        is.close();
    } catch (Exception ex) {
    } finally {
        System.out.println("exiting");

    }
    System.out.println("Done");
    try {
        con1();
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/pro", username, password);
        String query = "insert into log values(?,?)";
        PreparedStatement ps = con.prepareStatement(query);
        ps.setString(1, getCurUser());
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/YYYY HH:mm:ss");
        ps.setString(2, "Printed Direct Second Year Form Page2 at " + df.format(date));
        ps.executeUpdate();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    print_btn.setVisible(true);
    back_btn.setVisible(true);
    nextB.setVisible(true);
}

From source file:dsy_form.Pg_3Controller.java

@FXML
private void doPrinting(ActionEvent event) {
    print_btn.setVisible(false);//from   www  . java2 s.co m
    back_btn.setVisible(false);
    Scene scene = print_btn.getScene();
    WritableImage snapshot = scene.snapshot(null);
    BufferedImage image = SwingFXUtils.fromFXImage(snapshot, null);
    File f = new File("test2.png");
    try {
        ImageIO.write(image, "png", f);
    } catch (IOException ex) {

    }
    Document document = new Document(PageSize.A4, 0, 0, 0, 0);
    try {
        PdfWriter.getInstance(document, new FileOutputStream("Image.pdf"));
        document.open();
        // BufferedImage img = ImageIO.read(new File("test1.png"));
        //File f = new File("test1.png");
        Image image1 = Image.getInstance("test2.png");
        document.add(image1);

        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        InputStream is = new BufferedInputStream(new FileInputStream("Image.pdf"));
        DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
        PrintService service = PrintServiceLookup.lookupDefaultPrintService();
        DocPrintJob printJob = service.createPrintJob();
        printJob.addPrintJobListener(new Pg_3Controller.JobCompleteMonitor());
        Doc doc = new SimpleDoc(is, flavor, null);
        PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
        //attributes.add(new Destination(new java.net.URI("file:/home/jayesh/NetBeansProjects/myFile.ps")));
        printJob.print(doc, attributes);
        //while(jobRunning)
        //{
        //    Thread.sleep(1000);
        //}
        is.close();
    } catch (Exception ex) {
    } finally {
        System.out.println("exiting");

    }
    System.out.println("Done");
    try {
        con1();
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/pro", username, password);
        String query = "insert into log values(?,?)";
        PreparedStatement ps = con.prepareStatement(query);
        ps.setString(1, getCurUser());
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/YYYY HH:mm:ss");
        ps.setString(2, "Printed Direct Second Year Form Page3 at " + df.format(date));
        ps.executeUpdate();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    print_btn.setVisible(true);
    back_btn.setVisible(true);
}

From source file:EditorKits.DocumentExporter.java

public static void ConvertToPDF(String htmlString, String output) {

    String out = "";
    out = htmlString.substring(0, htmlString.indexOf("<body>")).replaceAll("<br>", "");
    out += htmlString.substring(htmlString.indexOf("<body>"), htmlString.indexOf("</body>"));
    out += htmlString.substring(htmlString.indexOf("</body>"), htmlString.length()).replaceAll("<br>", "");
    out = out.replaceAll("<br>", "<br/>");
    try {/*from  www.  j a  va 2s  .  c o  m*/
        Document document = new Document(PageSize.A4);
        PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(output));
        document.open();
        document.addCreationDate();

        XMLWorkerHelper worker = XMLWorkerHelper.getInstance();

        worker.parseXHtml(pdfWriter, document, new StringReader(out));
        document.close();
        System.out.println("Done.");
    } catch (FileNotFoundException | DocumentException e) {
    } catch (IOException ex) {
        Logger.getLogger(DocumentExporter.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:edu.avans.ivh5.shared.util.generateInvoicePDF.java

public static String getInvoicePDF(Invoice invoice) {
    try {/*from  w w  w  . ja  v a2  s  . com*/
        Document document = new Document(PageSize.A4);
        Document treatmentPage = new Document(PageSize.A4);
        Document clientPage = new Document(PageSize.A4);
        Document insuranceCompanyPage = new Document(PageSize.A4);
        String fileName = UUID.randomUUID().toString();
        FileOutputStream stream = new FileOutputStream(fileName + ".pdf");
        PdfWriter.getInstance(document, stream);
        document.open();

        addMetaData(document);
        addTitlePage(document, invoice);

        document.close();
        return fileName;

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}