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:views.HacerPedido.java

public void generatePDF(String nombreArchivo) throws DocumentException, FileNotFoundException, IOException {
    Document document = new Document();
    PdfWriter writer;

    String username = System.getProperty("user.name");

    //String filepath = "/Users/alejandro/NetBeansProjects/QRGenerator/qrCode.png";
    String filepath = "/Users/" + username + "/Desktop/" + nombreArchivo + ".pdf";

    writer = PdfWriter.getInstance(document, new FileOutputStream(filepath));

    // writer = PdfWriter.getInstance(document, new
    // FileOutputStream("my_jtable_fonts.pdf"));

    document.open();//  w w  w .  j ava2 s  .  co m
    PdfContentByte cb = writer.getDirectContent();

    PdfTemplate tp = cb.createTemplate(500, 500);
    Graphics2D g2;

    g2 = tp.createGraphicsShapes(500, 500);

    // g2 = tp.createGraphics(500, 500);
    tablaPedidos.print(g2);
    g2.dispose();
    //cb.addTemplate(tp, 30, 300);
    cb.addTemplate(tp, 75, 300);

    //            //String archivo = System.getProperty("user.dir")+"/qrCode.png";
    //            String archivo = System.getProperty(filepath);
    //            Desktop d = Desktop.getDesktop();
    //            d.open(new File(archivo));

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

From source file:vjmol.VJMol.java

License:GNU General Public License

protected void saveImage() {
    String strPath = m_txfImage.getText();
    if (strPath == null || strPath.equals(""))
        return;/*  w  ww  .  j a  va 2  s  .co  m*/

    StringTokenizer strTok = new StringTokenizer(strPath);
    if (strTok.hasMoreTokens())
        strPath = strTok.nextToken();

    strPath = new StringBuffer().append(m_strImageDir).append(File.separator).append(strPath).toString();
    try {
        Image image = m_vjmolPanel.getImage();
        File file = new File(strPath);
        FileOutputStream os = new FileOutputStream(strPath);
        String strFormat = (String) m_cmbFormat.getSelectedItem();

        if (strFormat.equals("JPG")) {
            JpegEncoder jc = new JpegEncoder(image, 100, os);
            jc.Compress();
        } else if (strFormat.equals("PPM")) {
            PpmEncoder pc = new PpmEncoder(image, os);
            pc.encode();
        } else if (strFormat.equals("GIF")) {
            GifEncoder gc = new GifEncoder(image, os, true);
            gc.encode();
        } else if (strFormat.equals("PNG")) {
            PngEncoder png = new PngEncoder(image);
            byte[] pngbytes = png.pngEncode();
            os.write(pngbytes);
        } else if (strFormat.equals("BMP")) {
            BMPFile bmp = new BMPFile();
            bmp.saveBitmap(os, image);
        } else if (strFormat.equals("PDF")) {
            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document, os);

            document.open();

            int w = m_vjmolPanel.getWidth();
            int h = m_vjmolPanel.getHeight();
            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate tp = cb.createTemplate(w, h);
            Graphics2D g2 = tp.createGraphics(w, h);
            g2.setStroke(new BasicStroke(0.1f));
            tp.setWidth(w);
            tp.setHeight(h);

            m_vjmolPanel.print(g2);
            g2.dispose();
            cb.addTemplate(tp, 72, 720 - h);
            document.close();
        }

        os.flush();
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}