Example usage for com.itextpdf.text Document setPageSize

List of usage examples for com.itextpdf.text Document setPageSize

Introduction

In this page you can find the example usage for com.itextpdf.text Document setPageSize.

Prototype


public boolean setPageSize(Rectangle pageSize) 

Source Link

Document

Sets the pagesize.

Usage

From source file:voedselbank.OverzichtScherm.java

private void exporteerKnopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exporteerKnopActionPerformed

    try {/*from   w ww. j  a  v a 2s . c  om*/
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle("Kies een locatie");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);

        if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
            Document doc = new Document();

            Rectangle rect = new Rectangle(PageSize.A4.rotate());
            PdfWriter.getInstance(doc, new FileOutputStream(chooser.getSelectedFile() + "/overzicht.pdf"));

            doc.setPageSize(rect);
            doc.open();

            PdfPTable pdfTable = new PdfPTable(overzichtTabel.getColumnCount());
            pdfTable.setWidthPercentage(108);
            pdfTable.setHorizontalAlignment(Element.ALIGN_CENTER);

            //adding table headers
            for (int i = 0; i < overzichtTabel.getColumnCount(); i++) {
                PdfPCell head = new PdfPCell(new Paragraph(overzichtTabel.getColumnName(i)));
                head.setHorizontalAlignment(Element.ALIGN_CENTER);
                head.setBackgroundColor(new BaseColor(236, 116, 4));
                pdfTable.addCell(head);
            }

            //extracting data from the JTable and inserting it to PdfPTable
            for (int rows = 0; rows < overzichtTabel.getRowCount(); rows++) {
                for (int cols = 0; cols < overzichtTabel.getColumnCount(); cols++) {
                    pdfTable.addCell(overzichtTabel.getModel().getValueAt(rows, cols).toString());
                }
            }

            doc.add(pdfTable);
            doc.close();
            System.out.println("Done");
        } else {
            System.out.println("U heeft niks geselecteerd.");
        }
    } catch (FileNotFoundException | DocumentException e) {
        e.printStackTrace();
    }
}