Example usage for com.lowagie.text.pdf PdfContentByte createGraphicsShapes

List of usage examples for com.lowagie.text.pdf PdfContentByte createGraphicsShapes

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfContentByte createGraphicsShapes.

Prototype

public java.awt.Graphics2D createGraphicsShapes(float width, float height) 

Source Link

Document

Gets a Graphics2D to write on.

Usage

From source file:papertoolkit.render.SheetRenderer.java

License:BSD License

/**
 * @param destPDFFile/*w  w  w. ja v  a  2  s  . c  o m*/
 * @param topLayer
 * @param bottomLayer
 */
protected void renderToPDFContentLayers(File destPDFFile, PdfContentByte topLayer, PdfContentByte bottomLayer) {
    mostRecentlyRenderedFile = destPDFFile;

    final Units width = sheet.getWidth();
    final Units height = sheet.getHeight();
    final float wPoints = (float) width.getValueInPoints();
    final float hPoints = (float) height.getValueInPoints();

    // top layer for regions (changed from bottom layer)
    final Graphics2D g2dOver = topLayer.createGraphicsShapes(wPoints, hPoints);
    // now that we have a G2D, we can just use our other G2D rendering method
    renderToG2D(g2dOver);

    // an efficient dispose, because we are not within a Java paint() method
    g2dOver.dispose();

    // should this be moved to regions???
    if (renderActiveRegionsWithPattern) {
        // DebugUtils.println("Rendering Pattern");
        // after rendering everything, we still need to overlay the pattern on top of active
        // regions; This is only for PDF rendering.

        // top layer for pattern
        renderPatternToPDF(topLayer);
    }
}

From source file:questions.graphics2D.ArabicText.java

public static void main(String[] args) {
    Document document = new Document(PageSize.A4);
    try {/*w w  w.j a v  a 2 s. com*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        String text1 = "\u0634\u0627\u062f\u062c\u0645\u0647\u0648\u0631";
        String text2 = "\u0634";
        java.awt.Font font = new java.awt.Font("arial", 0, 12);
        PdfContentByte cb = writer.getDirectContent();

        java.awt.Graphics2D g2Shapes = cb.createGraphicsShapes(PageSize.A4.getWidth(), PageSize.A4.getHeight());
        g2Shapes.setFont(font);
        g2Shapes.drawString("text1, expected to render RTL", 50, 100);
        g2Shapes.drawString(text1, 50, 120);
        g2Shapes.drawString("text2, expected to match right-most glyph above", 50, 140);
        g2Shapes.drawString(text2, 50, 160);
        g2Shapes.dispose();

        ColumnText text = new ColumnText(cb);
        Font f = new Font(
                BaseFont.createFont("c://windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED),
                12);
        text.setSimpleColumn(50, 620, 545, 50);
        text.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
        text.setText(new Phrase(text1, f));
        text.go();
        text.setText(new Phrase(text2, f));
        text.go();

        FontMapper arialuni = new FontMapper() {
            public BaseFont awtToPdf(java.awt.Font font) {
                try {
                    return BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H,
                            BaseFont.EMBEDDED);
                } catch (DocumentException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

            public java.awt.Font pdfToAwt(BaseFont font, int size) {
                return null;
            }

        };
        java.awt.Graphics2D g = cb.createGraphics(PageSize.A4.getWidth(), PageSize.A4.getHeight(), arialuni);
        g.setFont(null);
        g.drawString("text1, not expected to render RTL", 50, 180);
        g.drawString(text1, 50, 200);
        g.drawString("text2, not expected to match right-most glyph above", 50, 220);
        g.drawString(text2, 50, 240);
        g.drawString("to your right you see what it SHOULD look like:", 50, 260);
        g.drawString("If it doesn't, the problem is in the JDK, it's not an iText problem.", 50, 280);
        g.dispose();
        document.close();
    } catch (Exception de) {
        de.printStackTrace();
    }
}

From source file:questions.graphics2D.SplitCanvas.java

public static void main(String[] args) {
    Document document = new Document();
    try {/*from   w  ww  .j a v a  2 s.co  m*/
        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 {/*ww  w  . j  a va 2  s.  com*/
        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:songscribe.publisher.publisheractions.ExportPDFAction.java

License:Open Source License

public void actionPerformed(ActionEvent e) {
    if (publisher.isBookNull()) {
        return;//from   w w  w.ja  v  a 2  s.  c o  m
    }

    if (pfd.showDialog()) {
        File saveFile = pfd.getFile();

        if (!saveFile.getName().toLowerCase().endsWith(".pdf")) {
            saveFile = new File(saveFile.getAbsolutePath() + ".pdf");
        }

        if (saveFile.exists()) {
            int answ = JOptionPane.showConfirmDialog(publisher,
                    "The file " + saveFile.getName() + " already exists. Do you want to overwrite it?",
                    publisher.PROG_NAME, JOptionPane.YES_NO_OPTION);
            if (answ == JOptionPane.NO_OPTION) {
                return;
            }
        }

        float resolution = 72f / MusicSheet.RESOLUTION;
        Book book = publisher.getBook();
        Document document = new Document(new com.lowagie.text.Rectangle(book.getPageSize().x * resolution,
                book.getPageSize().y * resolution, book.getPageSize().width * resolution,
                book.getPageSize().height * resolution), 0, 0, 0, 0);

        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(saveFile));
            document.addCreator(publisher.PROG_NAME);
            document.open();
            PdfContentByte cb = writer.getDirectContent();

            for (ListIterator<Page> it = book.pageIterator(); it.hasNext();) {
                Graphics2D g2 = cb.createGraphicsShapes(book.getPageSize().width * resolution,
                        book.getPageSize().height * resolution);
                g2.scale(resolution, resolution);
                it.next().paint(g2, it.nextIndex() - 1, false, 0, book.getPageSize().height);
                g2.dispose();

                if (it.hasNext()) {
                    document.newPage();
                }
            }

            document.close();
            Utilities.openExportFile(publisher, saveFile);
        } catch (DocumentException e1) {
            publisher.showErrorMessage("An unexprected error occured and could not export into PDF.");
            logger.error("PDF save", e1);
        } catch (FileNotFoundException e1) {
            publisher.showErrorMessage(MainFrame.COULD_NOT_SAVE_MESSAGE);
            logger.error("PDF save", e1);
        }
    }
}

From source file:songscribe.ui.mainframeactions.ExportPDFAction.java

License:Open Source License

public static void createPDF(Data data, File outputFile, Boolean isGUI) {
    float resolution = 72f / MusicSheet.RESOLUTION;
    float paperWidth = data.paperWidth * resolution;
    float paperHeight = data.paperHeight * resolution;
    MainFrame mainFrame = data.mainFrame;
    Document document = new Document(new Rectangle(0, 0, paperWidth, paperHeight), 0, 0, 0, 0);
    document.addCreator(mainFrame.PROG_NAME);
    document.addTitle(mainFrame.getMusicSheet().getComposition().getSongTitle());

    // Scale to fit
    int sheetWidth = mainFrame.getMusicSheet().getSheetWidth();
    int sheetHeight = mainFrame.getMusicSheet().getSheetHeight();
    double horizontalMargin = (data.leftInnerMargin + data.rightOuterMargin) * resolution;
    double horizontalScale = (paperWidth - horizontalMargin) / sheetWidth;
    double verticalMargin = (data.topMargin + data.bottomMargin) * resolution;
    double verticalScale = (paperHeight - verticalMargin) / sheetHeight;
    double scale;
    double leftMargin = data.leftInnerMargin * resolution;

    if (horizontalScale < verticalScale) {
        scale = horizontalScale;/*  ww  w .j  av  a2s.com*/
    } else {
        // If scaling vertically, the horizontal margin will be larger than
        // what is specified in Data. So we calculate the total margin available,
        // then give the left margin the same fraction of the total margin
        // it would have had before scaling.
        scale = verticalScale;
        double scaledMargin = paperWidth - (sheetWidth * scale);
        double leftMarginFactor = (double) data.leftInnerMargin
                / (double) (data.leftInnerMargin + data.rightOuterMargin);
        leftMargin = scaledMargin * leftMarginFactor;
    }

    try {
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        Graphics2D g2 = cb.createGraphicsShapes(paperWidth, paperHeight);
        g2.translate(leftMargin, data.topMargin * resolution);
        mainFrame.getMusicSheet().getBestDrawer().drawMusicSheet(g2, false, scale);
        g2.dispose();
        document.close();

        if (isGUI) {
            Utilities.openExportFile(mainFrame, outputFile);
        }
    } catch (DocumentException e1) {
        if (isGUI) {
            mainFrame.showErrorMessage("An unexpected error occurred and could not export as PDF.");
        }

        logger.error("PDF save", e1);
    } catch (FileNotFoundException e1) {
        if (isGUI) {
            mainFrame.showErrorMessage(MainFrame.COULD_NOT_SAVE_MESSAGE);
        }

        logger.error("PDF save", e1);
    }
}

From source file:UI.ImageList.java

private void print() {
    Document document = new Document(PageSize.A4.rotate());
    try {/*from   w  w w. j  ava2 s  . c o 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: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 w w.j  a v a2  s . c o  m
        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: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 {/*from  w ww . j  av  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();
}