Example usage for com.lowagie.text Document close

List of usage examples for com.lowagie.text Document close

Introduction

In this page you can find the example usage for com.lowagie.text Document close.

Prototype

boolean close

To view the source code for com.lowagie.text Document close.

Click Source Link

Document

Has the document already been closed?

Usage

From source file:jp.ac.utokyo.rcast.karkinos.summary.TextToGraph.java

License:Apache License

/**
 * @param args//from   w  ww.j  av  a  2  s.  c o m
 */
public static void main(String[] args) {

    // String s =
    // "/GLUSTER_DIST/data/users/ueda/ICGC/bcmtest/TCGA-MR-A520-01A-11D-A25V-10.vs-10_cnvAllelicDepth.txt";
    // String s2 =
    // "/GLUSTER_DIST/data/users/ueda/ICGC/bcmtest/TCGA-MR-A520-01A-11D-A25V-10.vs-10_cnvdepth.txt";
    // String sout = "/GLUSTER_DIST/data/users/ueda/ICGC/bcmtest/out.pdf";

    String s = "/GLUSTER_DIST/data/users/ueda/ICGC/bcmtest/HCC-JP-468-T.vs-N_cnvAllelicDepth.txt";
    String s2 = "/GLUSTER_DIST/data/users/ueda/ICGC/bcmtest/HCC-JP-468-T.vs-N_cnvdepth.txt";
    String sout = "/GLUSTER_DIST/data/users/ueda/ICGC/bcmtest/468out.pdf";

    Document document = null;
    PdfWriter writer = null;
    //
    try {

        int width = 1200;
        int hight = 500;
        int size = 1;
        FileOutputStream fileOutputStream = new FileOutputStream(sout);
        document = new Document();

        writer = PdfWriter.getInstance(document, fileOutputStream);
        document.open();

        JFreeChart chart0 = getChart(new File(s2), 0);
        JFreeChart chart1 = getChart(new File(s2), 1);
        JFreeChart chart2 = getChart(new File(s2), 2);
        JFreeChart chart3 = getChart(new File(s2), 3);
        JFreeChart chart4 = getChart(new File(s), 4);
        JFreeChart chart5 = getChart(new File(s), 5);
        JFreeChart chart6 = getChart(new File(s), 6);

        BufferedImage bufferedImage = chart0.createBufferedImage(width * size, hight * size);
        Image image = Image.getInstance(writer, bufferedImage, 1.0f);
        image.scalePercent(20);
        document.add(image);

        BufferedImage bufferedImage1 = chart1.createBufferedImage(width * size, hight * size);
        Image image1 = Image.getInstance(writer, bufferedImage1, 1.0f);
        image1.scalePercent(20);
        document.add(image1);

        BufferedImage bufferedImage2 = chart2.createBufferedImage(width * size, hight * size);
        Image image2 = Image.getInstance(writer, bufferedImage2, 1.0f);
        image2.scalePercent(20);
        document.add(image2);

        BufferedImage bufferedImage3 = chart3.createBufferedImage(width * size, hight * size);
        Image image3 = Image.getInstance(writer, bufferedImage3, 1.0f);
        image3.scalePercent(20);
        document.add(image3);

        BufferedImage bufferedImage4 = chart4.createBufferedImage(width * size, hight * size);
        Image image4 = Image.getInstance(writer, bufferedImage4, 1.0f);
        image4.scalePercent(20);
        document.add(image4);

        BufferedImage bufferedImage5 = chart5.createBufferedImage(width * size, hight * size);
        Image image5 = Image.getInstance(writer, bufferedImage5, 1.0f);
        image5.scalePercent(20);
        document.add(image5);

        BufferedImage bufferedImage6 = chart6.createBufferedImage(width * size, hight * size);
        Image image6 = Image.getInstance(writer, bufferedImage6, 1.0f);
        image6.scalePercent(20);
        document.add(image6);

        document.close();
        document = null;
        writer.close();
        writer = null;

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // release resources
        if (null != document) {
            try {
                document.close();
            } catch (Exception ex) {
            }
        }
        if (null != writer) {
            try {
                writer.close();
            } catch (Exception ex) {
            }
        }
    }
}

From source file:jPDFmelange.MelangeJFrame.java

License:Open Source License

/** 
 *  Main save method./*from  w w w  .  j a  va  2 s.co m*/
 *  <p> 
 *  Saves all elements of the main list represented with its {@link MelangeJFrame#listContentMain content} 
 *  to the specified file.
 *  
 *   @param fileName name of file to save.
 *   @throws IOException on File IO error.
 *   @throws DocumentException on itext PDF error. 
 */
private void saveFile(String fileName) throws IOException, DocumentException {
    File file = new File(fileName);
    File tmpfile = File.createTempFile("Mixer", null, file.getParentFile());
    String bakFileName = fileName.substring(0, fileName.lastIndexOf('.')).concat(".bak");
    File bakfile = new File(bakFileName);

    //
    // prevent writing to a PDF that is blocked by the renderer.
    //
    jPanePreview.closePdfFile();

    // itext usage
    System.out.println("Writing new content to <" + tmpfile.getName() + ">");
    PdfReader reader = null;
    PdfDictionary dict = null;
    Document pdfDoc = new Document();
    PdfCopy writer = new PdfCopy(pdfDoc, new FileOutputStream(tmpfile));
    pdfDoc.open();
    PageNode node = null;
    for (int i = 0; i < listContentMain.size(); i++) {
        node = (PageNode) listContentMain.get(i);
        if (node.password == null)
            reader = new PdfReader(node.filename);
        else
            reader = new PdfReader(node.filename, node.password.getBytes());
        dict = reader.getPageN(node.pagenumber);
        dict.put(PdfName.ROTATE, new PdfNumber(node.rotation));
        writer.addPage(writer.getImportedPage(reader, node.pagenumber));
        reader.close(); // close input file
        System.out
                .println("Page " + node.pagenumber + "  File:" + node.filename + "  Rotation:" + node.rotation);
    }

    //
    // save page mode and layout preferences
    //
    if (jCheckBoxEnablePDFViewerPrefs.isSelected()) {

        String key = jPanelViewPrefs.getKeyPageMode();
        writer.setViewerPreferences(PageMode.get(key));

        key = jPanelViewPrefs.getKeyPageLayout();
        writer.setViewerPreferences(PageLayout.get(key));

        if (jPanelViewPrefs.jCheckBoxHideToolbar.isSelected())
            writer.addViewerPreference(PdfName.HIDETOOLBAR, PdfBoolean.PDFTRUE);
        if (jPanelViewPrefs.jCheckBoxHideMenubar.isSelected())
            writer.addViewerPreference(PdfName.HIDEMENUBAR, PdfBoolean.PDFTRUE);
        if (jPanelViewPrefs.jCheckBoxHideWindowUI.isSelected())
            writer.addViewerPreference(PdfName.HIDEWINDOWUI, PdfBoolean.PDFTRUE);
        if (jPanelViewPrefs.jCheckBoxFitWindow.isSelected())
            writer.addViewerPreference(PdfName.FITWINDOW, PdfBoolean.PDFTRUE);
        if (jPanelViewPrefs.jCheckBoxCenterWindow.isSelected())
            writer.addViewerPreference(PdfName.CENTERWINDOW, PdfBoolean.PDFTRUE);
        if (jPanelViewPrefs.jCheckBoxDisplayDocTitle.isSelected())
            writer.addViewerPreference(PdfName.DISPLAYDOCTITLE, PdfBoolean.PDFTRUE);
    }

    pdfDoc.close(); // close Helper Class
    writer.close(); // close output file

    // save old file to a XXX.bak file
    if (bakfile.exists())
        bakfile.delete();
    if (file.renameTo(bakfile.getCanonicalFile())) {
        System.out.println("Orginal File is saved in <" + bakfile.getName() + ">");
    }

    // move new content to original file name
    file = new File(fileName);
    if (tmpfile.renameTo(file))
        System.out.println("<" + tmpfile.getName() + "> is copied to <" + file.getName() + "> ");
    else {
        JOptionPane.showMessageDialog(MelangeJFrame.this,
                messages.getString("canNotWriteFile") + file.getName() + messages.getString("trySaveAs"),
                messages.getString("warning"), JOptionPane.WARNING_MESSAGE);
        System.out.println(
                messages.getString("canNotWriteFile") + file.getName() + messages.getString("trySaveAs"));
    }

}

From source file:keel.GraphInterKeel.datacf.visualizeData.VisualizePanelCharts2D.java

License:Open Source License

private void topdfjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_topdfjButtonActionPerformed
    // Save chart as a PDF file
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle("Save chart");
    KeelFileFilter fileFilter = new KeelFileFilter();
    fileFilter.addExtension("pdf");
    fileFilter.setFilterName("PDF images (.pdf)");
    chooser.setFileFilter(fileFilter);/*w w  w.  ja  v  a2  s .c o m*/
    chooser.setCurrentDirectory(Path.getFilePath());
    int opcion = chooser.showSaveDialog(this);
    Path.setFilePath(chooser.getCurrentDirectory());
    if (opcion == JFileChooser.APPROVE_OPTION) {
        String nombre = chooser.getSelectedFile().getAbsolutePath();
        if (!nombre.toLowerCase().endsWith(".pdf")) {
            // Add correct extension
            nombre += ".pdf";
        }
        File tmp = new File(nombre);
        if (!tmp.exists() || JOptionPane.showConfirmDialog(this,
                "File " + nombre + " already exists. Do you want to replace it?", "Confirm",
                JOptionPane.YES_NO_OPTION, 3) == JOptionPane.YES_OPTION) {
            try {
                Document document = new Document();
                PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(nombre));
                document.addAuthor("KEEL");
                document.addSubject("Attribute comparison");
                document.open();
                PdfContentByte cb = writer.getDirectContent();
                PdfTemplate tp = cb.createTemplate(550, 412);
                Graphics2D g2 = tp.createGraphics(550, 412, new DefaultFontMapper());
                Rectangle2D r2D = new Rectangle2D.Double(0, 0, 550, 412);
                chart2.setBackgroundPaint(Color.white);
                chart2.draw(g2, r2D);
                g2.dispose();
                cb.addTemplate(tp, 20, 350);
                document.close();
            } catch (Exception exc) {
            }
        }
    }
}

From source file:knop.psfj.exporter.PDFExporter.java

License:Open Source License

/**
 * Write single file report.//from   w w  w .  j  a  v a2s.c o  m
 *
 * @param frameList the frame list
 * @param path the path
 * @param page the page
 * @return the runnable
 */
public Runnable writeSingleFileReport(final BeadFrameList frameList, final String path, int page) {
    return new Runnable() {
        public void run() {

            Document document = null;
            PdfWriter writer = null;

            if (FileUtils.folderExists(path))
                FileUtils.deleteFile(path);

            try {
                document = new Document();

                writer = PdfWriter.getInstance(document, new FileOutputStream(path));
                document.open();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            if (writer == null || document == null)
                return;

            for (BeadFrame frame : frameList) {

                //System.out.println("profile : " + frame);
                fillReport(document, writer, frame, frame.getId());
                document.newPage();
                incrementCount();
            }
            document.close();
            writer.close();
        }
    };

}

From source file:knop.psfj.exporter.PDFExporter.java

License:Open Source License

/**
 * Export bead image list./*from   w  w w . j av a  2s . co m*/
 *
 * @param imageList the image list
 * @param path the path
 */
public void exportBeadImageList(BeadFrameList imageList, String path) {
    Document document = new Document();
    try {

        int i = 0;
        int max = imageList.size();

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(path)));
        document.open();
        for (BeadFrame frame : imageList) {
            fillReport(document, writer, frame, frame.getId());

            setChanged();
            Integer progress = new Integer(100 * i++ / max);
            if (progress == 0)
                progress = 1;
            notifyObservers(new Message(this, "progress changed",
                    String.format("Generating bead PDF reports : %d/%d", i, max), progress));
            document.newPage();
        }

        document.close();
        writer.close();

        sendFinishMessage("Done.");

    } catch (FileNotFoundException e) {

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

        e.printStackTrace();
        setChanged();
        notifyObservers(new Message("this", "error", "Error when generating PDF."));
    }
}

From source file:lmcpointofsalessystem.PDFCustomers.java

public PDFCustomers(String tblName) throws IOException {
    try {//w  w  w.  j  av  a2s  .  c  o  m
        LMCPointofSalesSystem connect = new LMCPointofSalesSystem();
        con = connect.con;
        st = connect.s;
        Document document = new Document(PageSize.A4.rotate(), 0, 0, 0, 0);

        PdfWriter.getInstance(document, new FileOutputStream(tblName + ".pdf"));
        document.open();
        Image image = Image.getInstance("src/lmcpointofsalessystem/Assets/header3.png");
        document.add(image);
        document.setMarginMirroring(true);

        PdfPTable newheaderTable;
        newheaderTable = getDatas(tblName);
        newheaderTable.setHorizontalAlignment(Element.ALIGN_CENTER);
        document.add(newheaderTable);
        document.close();

    } catch (FileNotFoundException ex) {
        Logger.getLogger(PDFCustomers.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(PDFCustomers.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:lmcpointofsalessystem.PDFCustomersEmployee.java

public PDFCustomersEmployee(String tblName) throws IOException {
    try {/*from w w w  . j  av  a2s  .com*/
        LMCPointofSalesSystem connect = new LMCPointofSalesSystem();
        con = connect.con;
        st = connect.s;
        Document document = new Document(PageSize.A4.rotate(), 0, 0, 0, 0);

        PdfWriter.getInstance(document, new FileOutputStream(tblName + ".pdf"));
        document.open();
        Image image = Image.getInstance("src/lmcpointofsalessystem/Assets/header3.png");
        document.add(image);
        document.setMarginMirroring(true);

        PdfPTable newheaderTable;
        newheaderTable = getDatas(tblName);
        newheaderTable.setHorizontalAlignment(Element.ALIGN_CENTER);
        document.add(newheaderTable);
        document.close();

    } catch (FileNotFoundException ex) {
        Logger.getLogger(PDFCustomersEmployee.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(PDFCustomersEmployee.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:lmcpointofsalessystem.PDFOrders.java

public PDFOrders(String tblName) throws IOException {
    try {/*w  w  w  . j a va 2 s. co  m*/
        LMCPointofSalesSystem connect = new LMCPointofSalesSystem();
        con = connect.con;
        st = connect.s;
        Document document = new Document(PageSize.A4.rotate(), 0, 0, 0, 0);

        PdfWriter.getInstance(document, new FileOutputStream(tblName + ".pdf"));
        document.open();
        Image image = Image.getInstance("src/lmcpointofsalessystem/Assets/header3.png");
        document.add(image);
        document.setMarginMirroring(true);

        PdfPTable newheaderTable;
        newheaderTable = getDatas(tblName);
        newheaderTable.setHorizontalAlignment(Element.ALIGN_CENTER);
        document.add(newheaderTable);
        document.close();

    } catch (FileNotFoundException ex) {
        Logger.getLogger(PDFEmployees.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(PDFEmployees.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:lt.bsprendimai.ddesk.pdf.PDFTicket.java

License:Apache License

public byte[] writePage() throws Exception {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    try {// ww  w  .  j a  va2  s.c  o m
        logo = Image.getInstance(root.getAbsolutePath() + "/logo.gif");
        logo.setAlignment(Image.ALIGN_LEFT);
    } catch (Exception exc) {
        exc.printStackTrace();
    }

    baseArial = BaseFont.createFont(root.getAbsolutePath() + "/objects/arial.ttf", BaseFont.CP1257,
            BaseFont.EMBEDDED);

    Document doc = new Document(PageSize.A4, 56.5354331f, 22.6771654f, 28.3464567f, 25.0f);
    PdfWriter.getInstance(doc, bout);

    doc.open();

    logo.setAbsolutePosition(28.3464567f, doc.getPageSize().height() - logo.height() - 25.0f);
    doc.add(logo);
    Paragraph pg = new Paragraph(sdf.format(new Date()), new Font(baseArial, 10));
    pg.setAlignment(Element.ALIGN_RIGHT);
    doc.add(pg);
    doc.add(new Phrase("\n\n\n"));

    writeClientInfo(doc);
    writeTicketInfo(doc);
    writeJobInfo(doc);
    writeWorkerInfo(doc);

    doc.close();

    return bout.toByteArray();
}

From source file:lucee.commons.io.IOUtil.java

License:Open Source License

public static void closeEL(Document doc) {
    try {/*  w w w  . j  av a2  s .  c o  m*/
        if (doc != null)
            doc.close();
    } catch (Throwable e) {
    }
}