Example usage for com.lowagie.text.pdf PdfWriter getDirectContent

List of usage examples for com.lowagie.text.pdf PdfWriter getDirectContent

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfWriter getDirectContent.

Prototype


public PdfContentByte getDirectContent() 

Source Link

Document

Use this method to get the direct content for this document.

Usage

From source file:tufts.vue.PresentationNotes.java

License:Educational Community License

public static void createAudienceNotes(File file) {
    //page size notes:
    //martin-top,left,right,bottom = 36
    //widht :612//w  w  w . j a  v a 2 s .co m
    //height : 792
    //usable space 540 x 720
    // step 1: creation of a document-object
    Document document = new Document(PageSize.LETTER);

    try {
        GUI.activateWaitCursor();
        // step 2:
        // we create a writer that listens to the document
        // and directs a PDF-stream to a file            
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        //  writer.setDefaultColorspace(PdfName.DEFAULTRGB, null);
        // writer.setStrictImageSequence(true);

        // step 3: we open the document
        document.open();

        // PdfPTable table;
        // PdfPCell cell;            
        int entryCount = 0;
        int entryOnPage = 0;
        int currentIndex = VUE.getActivePathway().getIndex();

        VUE.getActivePathway().setIndex(-1);

        for (LWPathway.Entry entry : VUE.getActivePathway().getEntries()) {

            final LWSlide slide = entry.produceSlide();
            final LWComponent toDraw = (slide == null ? entry.node : slide);

            entryCount++;
            //String label = entry.getLabel();
            PdfContentByte cb = writer.getDirectContent();
            //cb.cr
            PdfTemplate tp = cb.createTemplate(SlideSizeX, SlideSizeY);
            Point2D.Float offset = new Point2D.Float();
            // center vertically only if landscape mode
            //if (format.getOrientation() == PageFormat.LANDSCAPE)
            //TODO: allow horizontal centering, but not vertical centering (handle in computeZoomFit)

            Rectangle2D bounds = null;

            bounds = slide.getBounds();

            Dimension page = null;

            page = new Dimension(SlideSizeX, 172);

            //PdfTemplate tp = cb.createTemplate(document.getPageSize().width()-80, document.getPageSize().height()-80);
            double scale = ZoomTool.computeZoomFit(page, 5, bounds, offset, true);
            PdfGraphics2D g2d = (PdfGraphics2D) tp.createGraphics(SlideSizeX, SlideSizeY, getFontMapper(),
                    false, 60.0f);
            DrawContext dc = new DrawContext(g2d, scale, -offset.x, -offset.y, null, // frame would be the PageFormat offset & size rectangle
                    entry.isMapView() ? entry.getFocal() : slide, false); // todo: absolute links shouldn't be spec'd here

            dc.setClipOptimized(false);
            dc.setPrintQuality();

            toDraw.drawFit(dc, 0);

            g2d.dispose();
            //document.add(Image.getInstance(tp));

            if (entryOnPage == 0) {
                drawSequenceNumber(writer, 36, 739, entryCount);
                cb.addTemplate(tp, 56, 583);
                cb.addTemplate(drawLines(writer), 296, 565);

            }
            if (entryOnPage == 1) {
                drawSequenceNumber(writer, 36, 559, entryCount);
                cb.addTemplate(tp, 56, 403);
                cb.addTemplate(drawLines(writer), 296, 385);
            }
            if (entryOnPage == 2) {
                drawSequenceNumber(writer, 36, 375, entryCount);
                cb.addTemplate(tp, 56, 219);
                cb.addTemplate(drawLines(writer), 296, 201);
            }
            if (entryOnPage == 3) {
                drawSequenceNumber(writer, 36, 192, entryCount);
                cb.addTemplate(tp, 56, 36);
                cb.addTemplate(drawLines(writer), 296, 18);
            }

            entryOnPage++;
            if (entryCount % 4 == 0) {
                document.newPage();
                entryOnPage = 0;
            }
        }
        VUE.getActivePathway().setIndex(currentIndex);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    } finally {
        GUI.clearWaitCursor();
    }

    // step 5: we close the document
    document.close();
}

From source file:tufts.vue.PresentationNotes.java

License:Educational Community License

public static void createSpeakerNotes1PerPage(File file) {

    // step 1: creation of a document-object

    //This is a bit of a mess but because of hte bugs with drawing the slides
    //the easy way, we have no other choice but to render them directly onto the pdf
    //which makes it hard to use tables for stuff like formatting text...so we'll render
    // a blank table cell then render the image into it.
    Document document = new Document();

    try {//from ww  w. java  2 s  .co  m
        GUI.activateWaitCursor();
        // step 2:
        // we create a writer that listens to the document
        // and directs a PDF-stream to a file            
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));

        // step 3: we open the document
        document.open();

        PdfPTable table;
        PdfPCell cell;
        int currentIndex = VUE.getActivePathway().getIndex();

        VUE.getActivePathway().setIndex(-1);

        for (LWPathway.Entry entry : VUE.getActivePathway().getEntries()) {

            final LWSlide slide = entry.produceSlide();
            final LWComponent toDraw = (slide == null ? entry.node : slide);
            final String notes = entry.getNotes();
            //String label = entry.getLabel();

            PdfContentByte cb = writer.getDirectContent();

            Point2D.Float offset = new Point2D.Float();

            Rectangle2D bounds = null;

            //if (!entry.isMapView())
            bounds = slide.getBounds();
            //else 
            //bounds = entry.getFocal().getBounds();

            Dimension page = null;

            page = new Dimension(432, 324);

            PdfTemplate tp = cb.createTemplate(432, 324);
            double scale = ZoomTool.computeZoomFit(page, 5, bounds, offset, true);
            PdfGraphics2D g2d = (PdfGraphics2D) tp.createGraphics(432, 324, getFontMapper(), false, 60.0f);
            DrawContext dc = new DrawContext(g2d, scale, -offset.x, -offset.y, null, // frame would be the PageFormat offset & size rectangle
                    entry.isMapView() ? entry.getFocal() : slide, false); // todo: absolute links shouldn't be spec'd here

            dc.setClipOptimized(false);
            dc.setPrintQuality();
            /*if (!entry.isMapView())                   
               slide.drawZero(dc);
            else
            {                
               entry.getFocal().draw(dc);
            }*/
            toDraw.drawFit(dc, 0);

            g2d.dispose();

            cb.addTemplate(tp, 80, 482);

            //Paragraph p = new Paragraph();
            //p.setExtraParagraphSpace(330);
            // p.setSpacingBefore(330f);
            //  p.setAlignment(Element.ALIGN_CENTER);

            Paragraph phrase = new Paragraph(notes);
            //phrase.setExtraParagraphSpace(340f);
            phrase.setSpacingBefore(320f);
            phrase.setKeepTogether(true);
            //cell = new PdfPCell(phrase);
            //cell.setBorder(0);
            //         table = new PdfPTable(new float[]{ 1 });
            //        table.setWidthPercentage(100.0f);
            //        table.getDefaultCell().setBorder(0);
            //table.getDefaultCell().setPaddingTop(30);

            //PdfPCell c2 = new PdfPCell();
            //c2.setFixedHeight(340); //slides are 540x405
            //c2.setBorder(0);
            //table.addCell(c2);                
            //table.addCell(cell);
            //table.setKeepTogether(false);
            //cell.setVerticalAlignment(PdfPCell.ALIGN_TOP);

            //p.add(table);
            //System.out.println("CELL HEIGHT : " + cell.getHeight());
            //Section s1 = new Section();
            //ColumnText chunk2 = new ColumnText(cb);
            //chunk2.setText(phrase);
            //chunk2.setSi
            //chunk2.setSimpleColumn(phrase,70, 330, document.getPageSize().width()-70,document.getPageSize().height()-70,15, Element.ALIGN_LEFT);
            // chunk2.go();
            //PdfChunk chunk2 = new PdfChunk);
            Paragraph p2 = new Paragraph(" ");
            p2.setKeepTogether(false);
            phrase.setKeepTogether(false);
            // p2.setExtraParagraphSpace(230f);
            document.add(p2);
            document.add(phrase);
            document.newPage();
        }

        VUE.getActivePathway().setIndex(currentIndex);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    } finally {
        GUI.clearWaitCursor();
    }

    // step 5: we close the document
    document.close();
}

From source file:tufts.vue.PresentationNotes.java

License:Educational Community License

public static void createSpeakerNotes4PerPage(File file) {
    //page size notes:
    //martin-top,left,right,bottom = 36
    //widht :612/* w  w  w  . j a v  a 2s  .c  om*/
    //height : 792
    //usable space 540 x 720
    // step 1: creation of a document-object

    Document document = new Document(PageSize.LETTER);

    try {
        GUI.activateWaitCursor();
        // step 2:
        // we create a writer that listens to the document
        // and directs a PDF-stream to a file            
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        //  writer.setDefaultColorspace(PdfName.DEFAULTRGB, null);
        // writer.setStrictImageSequence(true);

        // step 3: we open the document
        document.open();

        PdfPTable table;
        PdfPCell cell;
        int entryCount = 0;
        int entryOnPage = 0;
        int currentIndex = VUE.getActivePathway().getIndex();

        VUE.getActivePathway().setIndex(-1);

        for (LWPathway.Entry entry : VUE.getActivePathway().getEntries()) {

            final LWSlide slide = entry.produceSlide();
            final LWComponent toDraw = (slide == null ? entry.node : slide);
            final String notes = entry.getNotes();
            entryCount++;

            table = new PdfPTable(new float[] { 1, 1 });
            table.getDefaultCell().setBorder(0);
            //table.getDefaultCell().setPaddingBottom(50.0f);
            table.setSpacingAfter(20.0f);
            Paragraph p = new Paragraph();

            p.setAlignment(Element.ALIGN_CENTER);

            Phrase phrase = new Phrase(notes);
            Font f = phrase.getFont();
            f.setSize(8.0f);
            p.setFont(f);
            cell = new PdfPCell(phrase);
            cell.setBorder(0);

            PdfPCell i2 = new PdfPCell();
            i2.setFixedHeight(172);
            i2.setBorder(0);

            //Render the table then throw the images on
            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate tp = cb.createTemplate(SlideSizeX, SlideSizeY);

            Point2D.Float offset = new Point2D.Float();
            // center vertically only if landscape mode
            //if (format.getOrientation() == PageFormat.LANDSCAPE)
            //TODO: allow horizontal centering, but not vertical centering (handle in computeZoomFit)

            Rectangle2D bounds = null;
            //if (!entry.isMapView())
            bounds = slide.getBounds();
            //else 
            //bounds = entry.getFocal().getBounds();

            Dimension page = null;

            page = new Dimension(SlideSizeX, 172);

            //PdfTemplate tp = cb.createTemplate(document.getPageSize().width()-80, document.getPageSize().height()-80);
            double scale = ZoomTool.computeZoomFit(page, 5, bounds, offset, true);
            PdfGraphics2D g2d = (PdfGraphics2D) tp.createGraphics(SlideSizeX, SlideSizeY, getFontMapper(),
                    false, 60.0f);
            DrawContext dc = new DrawContext(g2d, scale, -offset.x, -offset.y, null, // frame would be the PageFormat offset & size rectangle
                    entry.isMapView() ? entry.getFocal() : slide, false); // todo: absolute links shouldn't be spec'd here

            dc.setClipOptimized(false);
            dc.setPrintQuality();
            /*if (!entry.isMapView())                   
               slide.drawZero(dc);
            else
            {                
               entry.getFocal().draw(dc);
            }*/
            toDraw.drawFit(dc, 0);

            g2d.dispose();
            //document.add(Image.getInstance(tp));

            //  int position = cell.
            //     drawSequenceNumber(writer,36,position+203,entryCount);

            //     cb.addTemplate(tp,56, position);                                                           

            Image img = Image.getInstance(tp);
            table.addCell(img);
            table.addCell(cell);
            p.add(table);
            document.add(p);
        }
        VUE.getActivePathway().setIndex(currentIndex);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    } finally {
        GUI.clearWaitCursor();
    }

    // step 5: we close the document
    document.close();

}

From source file:tufts.vue.PresentationNotes.java

License:Educational Community License

public static void createNodeNotes4PerPage(File file) {
    //page size notes:
    //martin-top,left,right,bottom = 36
    //widht :612/*from  w ww  .jav a 2s. co m*/
    //height : 792
    //usable space 540 x 720
    // step 1: creation of a document-object

    Document document = new Document(PageSize.LETTER);

    try {
        GUI.activateWaitCursor();
        // step 2:
        // we create a writer that listens to the document
        // and directs a PDF-stream to a file            
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        //  writer.setDefaultColorspace(PdfName.DEFAULTRGB, null);
        // writer.setStrictImageSequence(true);

        // step 3: we open the document
        document.open();

        PdfPTable table;
        PdfPCell cell;
        int entryCount = 0;
        int entryOnPage = 0;

        Iterator i = VUE.getActiveMap().getAllDescendents(LWComponent.ChildKind.PROPER).iterator();
        while (i.hasNext()) {
            LWComponent component = (LWComponent) i.next();
            if (component instanceof LWNode) {
                final LWNode node = (LWNode) component;

                final String notes = node.getNotes();

                entryCount++;

                table = new PdfPTable(new float[] { 1, 1 });
                table.getDefaultCell().setBorder(0);

                table.setSpacingAfter(20.0f);
                Paragraph p = new Paragraph();

                p.setAlignment(Element.ALIGN_CENTER);

                Phrase phrase = new Phrase(notes);

                Font f = phrase.getFont();
                f.setSize(8.0f);
                p.setFont(f);
                cell = new PdfPCell(phrase);
                cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
                cell.setBorder(0);

                PdfPCell i2 = new PdfPCell();
                i2.setFixedHeight(172);
                i2.setBorder(0);

                //Render the table then throw the images on
                PdfContentByte cb = writer.getDirectContent();
                PdfTemplate tp = cb.createTemplate(SlideSizeX, SlideSizeY);

                Point2D.Float offset = new Point2D.Float();
                //center vertically only if landscape mode
                //if (format.getOrientation() == PageFormat.LANDSCAPE)
                //TODO: allow horizontal centering, but not vertical centering (handle in computeZoomFit)

                Rectangle2D bounds = null;

                bounds = node.getBounds();

                Dimension page = null;

                page = new Dimension(SlideSizeX, 172);

                //   PdfTemplate tp = cb.createTemplate(document.getPageSize().width()-80, document.getPageSize().height()-80);
                double scale = ZoomTool.computeZoomFit(page, 15, bounds, offset, true);
                PdfGraphics2D g2d = (PdfGraphics2D) tp.createGraphics(SlideSizeX, SlideSizeY, getFontMapper(),
                        false, 60.0f);
                DrawContext dc = new DrawContext(g2d, scale, -offset.x, -offset.y, null, // frame would be the PageFormat offset & size rectangle
                        node, false); // todo: absolute links shouldn't be spec'd here

                dc.setClipOptimized(false);
                dc.setPrintQuality();
                node.drawFit(dc, 15);

                g2d.dispose();

                Image img = Image.getInstance(tp);
                table.addCell(img);
                table.addCell(cell);
                p.add(table);
                document.add(p);
            }
        }

    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    } finally {
        GUI.clearWaitCursor();
    }

    // step 5: we close the document
    document.close();

}

From source file:tztBackoffice.Print.java

private void print(File path, JTable table) {
    // het document vormaat wort aangemaakt
    Document document = new Document(PageSize.A4.rotate());
    PdfWriter writer;
    try {//from ww w.ja  v  a  2  s. co m
        // check eidigt op .pdf
        String pathd = "" + paths;
        if (!pathd.endsWith(".pdf")) {
            writer = PdfWriter.getInstance(document, new FileOutputStream(path + ".pdf"));
        } else {
            writer = PdfWriter.getInstance(document, new FileOutputStream(path));
        }
        // het document wordt geopend
        document.open();
        // de writer wordt aangemaakt
        PdfContentByte cb = writer.getDirectContent();
        // de staat wordt opgeslagen
        cb.saveState();
        // graphics worden aangemaakt
        Graphics2D g2 = cb.createGraphics(500, 500);
        // de clip wordt weggeschreven
        Shape oldClip = g2.getClip();
        g2.clipRect(0, 0, 500, 500);

        // de tabel wordt geprint in de graphics
        table.print(g2);
        g2.setClip(oldClip);

        // graphics word gesloten
        g2.dispose();
        cb.restoreState();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.getMessage() + "Er is een fout opgetreden");
    } finally {
        document.close();
    }
}

From source file:UI.ImageList.java

private void print() {
    Document document = new Document(PageSize.A4.rotate());
    try {/*from  w w  w .ja va2s. co m*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("jTable.pdf"));

        document.open();
        PdfContentByte cb = writer.getDirectContent();

        cb.saveState();
        Graphics2D g2 = cb.createGraphicsShapes(500, 500);

        Shape oldClip = g2.getClip();
        g2.clipRect(0, 0, 500, 500);

        jTable1.print(g2);
        g2.setClip(oldClip);

        g2.dispose();
        cb.restoreState();
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    document.close();
}

From source file:uk.ac.bbsrc.tgac.miso.core.data.decorator.itext.ITextProjectDecorator.java

License:Open Source License

public void buildReport() throws DocumentException {
    report = new Document();
    PdfWriter writer = PdfWriter.getInstance(report, stream);
    report.open();/* w w w  .  ja  va 2s . c  o m*/
    report.add(new Paragraph("Project Summary"));
    PdfContentByte cb = writer.getDirectContent();
    cb.setLineWidth(2.0f); // Make a bit thicker than 1.0 default
    cb.setGrayStroke(0.9f); // 1 = black, 0 = white
    float x = 72f;
    float y = 200f;
    cb.moveTo(x, y);
    cb.lineTo(x + 72f * 6, y);
    cb.stroke();

    report.add(new Paragraph(project.getAlias()));
    report.add(new Paragraph(project.getDescription()));

    PdfPTable t = new PdfPTable(1);
    t.setHorizontalAlignment(Element.ALIGN_CENTER);
    t.setWidthPercentage(100f); // this would be the 100 from setHorizontalLine
    t.setSpacingAfter(5f);
    t.setSpacingBefore(0f);
    t.getDefaultCell().setUseVariableBorders(true);
    t.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
    t.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
    t.getDefaultCell().setBorder(Rectangle.BOTTOM); // This generates the line
    t.getDefaultCell().setBorderWidth(1f); // this would be the 1 from setHorizontalLine
    t.getDefaultCell().setPadding(0);
    t.addCell("");
    report.add(t);

    x = 72f;
    y = 100f;
    cb.moveTo(x, y);
    cb.lineTo(x + 72f * 6, y);
    cb.stroke();

    if (project.getSamples().size() > 0) {
        report.add(new Paragraph("Samples"));
        for (Sample sample : project.getSamples()) {
            Paragraph sPara = new Paragraph(sample.getAlias(), FontFactory.getFont("Helvetica", 12, Font.BOLD));
            sPara.setIndentationLeft(20);
            report.add(sPara);
            report.add(new Paragraph(sample.getDescription()));
        }
    }

    report.close();
}

From source file:userInterface.HospitalAdminRole.ManagePatientsJPanel.java

private void saveAsPdfBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsPdfBtnActionPerformed
    Document document = new Document(PageSize.A4.rotate());
    String[] headers = new String[] { "Name", "TimeStamp", "Resp Rate", "Heart Rate", "Blood Pressure",
            "Temperature", "Status" };
    String filename = fileNameTxt.getText();
    try {//  w  ww.  j  a v a  2  s  .  c om
        if (!filename.equals("")) {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename + ".pdf"));

            document.open();
            PdfContentByte cb = writer.getDirectContent();

            cb.saveState();
            PdfPTable table = new PdfPTable(headers.length);
            for (int i = 0; i < headers.length; i++) {
                String header = headers[i];
                PdfPCell cell = new PdfPCell();
                cell.setGrayFill(0.9f);
                cell.setPhrase(new Phrase(header.toUpperCase(), new Font(Font.HELVETICA, 8, Font.BOLD)));
                table.addCell(cell);

            }
            table.completeRow();

            table.spacingBefore();
            table.spacingAfter();
            document.add(table);
            Graphics2D g2 = cb.createGraphicsShapes(500, 500);
            //cb.showTextAligned(PdfContentByte.ALIGN_CENTER, g2, 200, 300, 0);

            Shape oldClip = g2.getClip();
            g2.clipRect(0, 0, 700, 500);

            vitalSignjTable.print(g2);
            g2.setClip(oldClip);

            g2.dispose();
            cb.restoreState();
            JOptionPane.showMessageDialog(null, "file saved", "Saved", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "enter the filename", "FileName", JOptionPane.ERROR_MESSAGE);
        }
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    document.close();
}

From source file:view.ViewCodigobarras.java

public void gerarCodigoBarras(ArrayList<String> pListaCodigos) {

    System.out.println("Barcode Linha de Cdigo ");

    // criando um objeto da classe Document
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);

    try {/* w  w  w. java2s. c  o  m*/

        //Aqui comeamos a utilizar as classes do iText: o documento
        //criado acima ser
        //direcionado para um arquivo PDF.
        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream("C://Codigo_Barra_Java_Linha_Codigo.pdf"));

        //abrindo o documento.
        document.open();

        //adicionando um novo paragrafo.
        for (int i = 0; i < pListaCodigos.size(); i++) {
            document.add(new Paragraph("CDIGOS DE BARRA"));

            document.add(new Paragraph("    "));

            document.add(new Paragraph("    "));

            //Comecando a configurar o cod de barras
            PdfContentByte cb = writer.getDirectContent();

            BarcodeEAN codeEAN = new BarcodeEAN();

            //O iText suporta os principais tipos de cdigo de barra, como Barcode39,
            //  Barcode128 (128, 128_UCC, 128_RAW),  BarcodeEAN (EAN13, EAN8, UPCA, UPCE), EANSUP, etc
            if (jcbTipoCodigo.getSelectedItem().toString() == "EAN13") {
                codeEAN.setCodeType(codeEAN.EAN13);
            } else if ("EAN8".equals(jcbTipoCodigo.getSelectedItem().toString())) {
                codeEAN.setCodeType(codeEAN.EAN8);
            } else if ("UPCA".equals(jcbTipoCodigo.getSelectedItem().toString())) {
                codeEAN.setCodeType(codeEAN.UPCA);
            } else if ("UPCE".equals(jcbTipoCodigo.getSelectedItem().toString())) {
                codeEAN.setCodeType(codeEAN.UPCE);
            }

            codeEAN.setCode("1234567890123");

            Image imageEAN = codeEAN.createImageWithBarcode(cb, null, null);

            document.add(new Phrase(new Chunk(imageEAN, 0, 0)));
        }
        //abrir o arquivo
        File file = new File("C://Codigo_Barra_Java_Linha_Codigo.pdf");
        try {
            Desktop.getDesktop().open(file);
        } catch (Exception e) {
            JOptionPane.showConfirmDialog(null, e);
        }
        file.deleteOnExit();

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

    }
    document.close();

}

From source file:views.HacerPedido.java

private void print() {
    Document document = new Document(PageSize.A4.rotate());
    String username = System.getProperty("user.name");

    //String filepath = "/Users/alejandro/NetBeansProjects/QRGenerator/qrCode.png";
    String filepath = "/Users/" + username + "/Desktop/jajasaludosss.pdf";
    try {//w w w .j a  v a 2 s . co  m
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filepath));

        document.open();
        PdfContentByte cb = writer.getDirectContent();

        cb.saveState();
        Graphics2D g2 = cb.createGraphicsShapes(500, 500);

        Shape oldClip = g2.getClip();
        g2.clipRect(0, 0, 500, 500);

        tablaPedidos.print(g2);
        g2.setClip(oldClip);

        g2.dispose();
        cb.restoreState();
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    document.close();
}