Example usage for com.itextpdf.text Document addCreationDate

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

Introduction

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

Prototype


public boolean addCreationDate() 

Source Link

Document

Adds the current date and time to a Document.

Usage

From source file:Modelo.RelatoriosBEAN.java

public static void relatorioAluguelJogoPeriodo(String s, String dI, String dF)
        throws FileNotFoundException, DocumentException {
    String path = System.getProperty("user.home") + FRMEmitirRelatorios.local + ".pdf";
    Document document = new Document(PageSize.LETTER);
    PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();//from   w ww.j  a v  a 2s. c om
    document.addAuthor("TechSales");
    document.addCreator("TechSales");
    document.addSubject("TechSales - Sistema de gerenciamento de locadoras");
    document.addCreationDate();
    document.addTitle("TechSales");

    HTMLWorker htmlWorker = new HTMLWorker(document);

    String frase;
    try {
        frase = "<html>" + "<head> <meta charset=\"utf-8\"> </head>" + "<body>" + "<br/>"
                + "<h1 align=center>Relatrio de Jogos alugados</h1>" + "<h3 align=center>Perodo de " + dI
                + " a " + dF + "</h3>" + "<br/><br/>"
                + "<table BORDER RULES=rows border=0 style=\"  width:100%; \">\n" + "<tr> "
                + "<th align=center><strong>Cdigo do jogo</strong></th> "
                + "<th align=center><strong>Nome do jogo</strong></th> "
                + "<th align=center><strong>Nmero de alugueis</strong></th> "
                + "<th align=center><strong>Valor total dos alugueis</strong></th> " + "</tr> ";
        for (JogoBEAN j : jDAO.aluguelPorPeriodo(s)) {
            frase += "<tr align=center><td>" + j.getJoCodigo() + "</td><td>" + j.getJoNome() + "</td><td>"
                    + j.getJoQtd() + "</td><td>" + j.getJoPrecoPadrao() + "</td></tr>";
        }
        frase += "</table>";
        frase += "</body></html>";
        htmlWorker.parse(new StringReader(frase));
        document.close();
        if (Desktop.isDesktopSupported()) {
            try {
                Desktop.getDesktop().open(new File(path));
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Modelo.RelatoriosBEAN.java

public static void relatorioMontanteCliente() throws FileNotFoundException, DocumentException {
    String path = System.getProperty("user.home") + FRMEmitirRelatorios.local + ".pdf";
    Document document = new Document(PageSize.A4.rotate());
    PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();// ww w  . ja v  a  2 s . c om
    document.addAuthor("TechSales");
    document.addCreator("TechSales");
    document.addSubject("TechSales - Sistema de gerenciamento de locadoras");
    document.addCreationDate();
    document.addTitle("TechSales");

    HTMLWorker htmlWorker = new HTMLWorker(document);

    String frase;
    try {
        frase = "<html>" + "<head> <meta charset=\"utf-8\"> </head>" + "<body>" + "<br/>"
                + "<h1 align=center>Relatrio de montante por cliente </h1>" + "<br/><br/>"
                + "<table BORDER RULES=rows border=0 style=\"  width:100%; \">\n" + "<tr> "
                + "<th align=center><strong>Cdigo do cliente</strong></th> "
                + "<th align=center><strong>Nome do cliente</strong></th> "
                + "<th align=center><strong>Nmero de compras</strong></th> "
                + "<th align=center><strong>Valor total das compras</strong></th> "
                + "<th align=center><strong>Quantidade de alugueis</strong></th> "
                + "<th align=center><strong>Valor total de alugueis</strong></th> "
                + "<th align=center><strong>Montante Total</strong></th> " + "</tr> ";
        for (MontanteBEAN j : jDAO.montanteCliente()) {
            frase += "<tr align=center><td>" + j.getFunCodigo() + "</td><td>" + j.getFunNome() + "</td><td>"
                    + j.getnVendas() + "</td><td>" + j.getValorVendas() + "</td><td>" + j.getnAluguel()
                    + "</td><td>" + j.getValorAluguel() + "</td><td>" + j.getValorTotal() + "</td></tr>";
        }
        frase += "</table>";
        frase += "</body></html>";
        htmlWorker.parse(new StringReader(frase));
        document.close();
        if (Desktop.isDesktopSupported()) {
            try {
                Desktop.getDesktop().open(new File(path));
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Modelo.RelatoriosBEAN.java

public static void notaDaVenda(VendaBEAN v) throws FileNotFoundException, DocumentException {
    String path = System.getProperty("user.home") + FRMEmitirRelatorios.local + ".pdf";
    Document document = new Document(PageSize.LETTER);
    PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();/*from w w w  .jav  a  2s. c o m*/
    document.addAuthor("TechSales");
    document.addCreator("TechSales");
    document.addSubject("TechSales - Sistema de gerenciamento de locadoras");
    document.addCreationDate();
    document.addTitle("TechSales");

    HTMLWorker htmlWorker = new HTMLWorker(document);

    String frase;
    try {
        frase = "<html>" + "<head> <meta charset=\"utf-8\"> </head>" + "<body>"
                + "<h1 align=center>Nota de Compra</h1>" + "<br/><br/>"
                + "<table BORDER RULES=rows border=0 style=\"  width:100%; \">\n" + "<tr> "
                + "<th align=center><strong>Cdigo</strong></th> "
                + "<th align=center><strong>Cliente</strong></th> "
                + "<th align=center><strong>Data</strong></th> "
                + "<th align=center><strong>Total</strong></th> "
                + "<th align=center><strong>Entrada</strong></th> "
                + "<th align=center><strong>Parcelas</strong></th> "
                + "<th align=center><strong>Vendedor</strong></th> " + "</tr> ";

        frase += "<tr align=center><td>" + v.getVendaCodigo() + "</td><td>" + v.getCliente().getCliCodigo()
                + "</td><td>" + v.getVendaData() + "</td><td>" + "</td><td>" + v.getVendaValorTotal()
                + "</td><td>" + "</td><td>" + v.getVendaEntrada() + "</td><td>" + "</td><td>"
                + v.getVendaNparcelas() + "</td><td>" + "</td><td>" + v.getVendedor().getVendedorCodigo()
                + "</td></tr>" + "</table>";

        frase += "<table BORDER RULES=rows border=0 style=\"  width:100%; \">\n" + "<br/><br/>" + "<tr> "
                + "<th align=center><strong>Parcela</strong></th> "
                + "<th align=center><strong>Data</strong></th> "
                + "<th align=center><strong>Valor</strong></th> "
                + "<th align=center><strong>Situao</strong></th> " + "</tr> ";

        for (VendaAPrazoBEAN vap : cVap.listarALL()) {
            if (vap.getVenda().getVendaCodigo() == v.getVendaCodigo()) {
                frase += "<tr align=center><td>" + vap.getVapNumParcela() + "</td><td>" + vap.getVapData()
                        + "</td><td>" + vap.getVapValorParcela() + "</td><td>" + vap.getVapSituacao()
                        + "</td></tr>";
            }
        }
        frase += "</table>";
        frase += "</body></html>";
        htmlWorker.parse(new StringReader(frase));
        document.close();
        if (Desktop.isDesktopSupported()) {
            try {
                Desktop.getDesktop().open(new File(path));
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Modelo.RelatoriosBEAN.java

public static void notaDaDev(DevolucaoBEAN l) throws FileNotFoundException, DocumentException {
    String path = System.getProperty("user.home") + FRMEmitirRelatorios.local + ".pdf";
    Document document = new Document(PageSize.A4.rotate());
    PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();//from   w w  w  .  j  a v  a 2 s  . c om
    document.addAuthor("TechSales");
    document.addCreator("TechSales");
    document.addSubject("TechSales - Sistema de gerenciamento de locadoras");
    document.addCreationDate();
    document.addTitle("TechSales");

    HTMLWorker htmlWorker = new HTMLWorker(document);

    String frase;
    try {
        frase = "<html>" + "<head> <meta charset=\"utf-8\"> </head>" + "<body>"
                + "<h1 align=center>Nota de devoluo</h1>" + "<br/><br/>"
                + "<table BORDER RULES=rows border=0 style=\"  width:100%; \">\n" + "<tr> "
                + "<th align=center><strong>Cdigo</strong></th> "
                + "<th align=center><strong>Cliente</strong></th> "
                + "<th align=center><strong>Data Devoluo</strong></th> "
                + "<th align=center><strong>Multa</strong></th> "
                + "<th align=center><strong>Valor Total</strong></th> "
                + "<th align=center><strong>Vendedor</strong></th> " + "</tr> ";

        frase += "<tr align=center><td>" + l.getDevCodigo() + "</td><td>"
                + l.getLocacao().getCliente().getCliNome() + "</td><td>" + l.getDevData() + "</td><td>"
                + l.getDevMulta() + "</td><td>" + l.getDevValor() + "</td><td>"
                + l.getLocacao().getVendedor().getFuncionario().getFunNome() + "</td></tr>" + "</table>";

        frase += "<table BORDER RULES=rows border=0 style=\"  width:100%; \">\n" + "<br/><br/>" + "<tr> "
                + "<th align=center><strong>Cdigo do Jogo</strong></th> "
                + "<th align=center><strong>Nome</strong></th> "
                + "<th align=center><strong>Quantidade Locao</strong></th> " + "</tr> ";
        for (JogoLocacaoBEAN jl : cJl.listarALL()) {
            if (jl.getChaveComposta().getLocacao().getLocCodigo() == l.getLocacao().getLocCodigo()) {
                frase += "<tr align=center><td>" + jl.getChaveComposta().getJogo().getJoCodigo() + "</td><td>"
                        + jl.getChaveComposta().getJogo().getJoNome() + "</td><td>" + jl.getJlQtd()
                        + "</td></tr>";
            }
        }
        frase += "</table>";
        frase += "</body></html>";
        htmlWorker.parse(new StringReader(frase));
        document.close();
        if (Desktop.isDesktopSupported()) {
            try {
                Desktop.getDesktop().open(new File(path));
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Modelo.RelatoriosBEAN.java

public static void notaDeConta(ContaBEAN v) throws FileNotFoundException, DocumentException {
    String path = System.getProperty("user.home") + FRMEmitirRelatorios.local + ".pdf";
    Document document = new Document(PageSize.LETTER);
    PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();//  w w  w . j  a v  a 2  s  .co m
    document.addAuthor("TechSales");
    document.addCreator("TechSales");
    document.addSubject("TechSales - Sistema de gerenciamento de locadoras");
    document.addCreationDate();
    document.addTitle("TechSales");

    HTMLWorker htmlWorker = new HTMLWorker(document);

    String frase;
    try {
        frase = "<html>" + "<head> <meta charset=\"utf-8\"> </head>" + "<body>"
                + "<h1 align=center>Nota de Conta</h1>" + "<br/><br/>"
                + "<table BORDER RULES=rows border=0 style=\"  width:100%; \">\n" + "<tr> "
                + "<th align=center><strong>Cdigo</strong></th> "
                + "<th align=center><strong>Fornecedor</strong></th> "
                + "<th align=center><strong>Data</strong></th> "
                + "<th align=center><strong>Total</strong></th> "
                + "<th align=center><strong>Entrada</strong></th> "
                + "<th align=center><strong>Parcelas</strong></th> " + "</tr> ";

        frase += "<tr align=center><td>" + v.getConCodigo() + "</td><td>"
                + v.getFornecedor().getForNomeEmpresa() + "</td><td>" + v.getConData() + "</td><td>"
                + v.getConValorTotal() + "</td><td>" + v.getConEntrada() + "</td><td>" + v.getConNparcelas()
                + "</td></tr>" + "</table>";

        frase += "<table BORDER RULES=rows border=0 style=\"  width:100%; \">\n" + "<br/><br/>" + "<tr> "
                + "<th align=center><strong>Parcela</strong></th> "
                + "<th align=center><strong>Data</strong></th> "
                + "<th align=center><strong>Valor</strong></th> "
                + "<th align=center><strong>Situao</strong></th> " + "</tr> ";

        for (ContaAPrazoBEAN vap : cCap.listarALL()) {
            if (vap.getConta().getConCodigo() == v.getConCodigo()) {
                frase += "<tr align=center><td>" + vap.getCapNumParcela() + "</td><td>" + vap.getCapData()
                        + "</td><td>" + vap.getCapValorParcela() + "</td><td>" + vap.getCapSituacao()
                        + "</td></tr>";
            }
        }
        frase += "</table>";
        frase += "</body></html>";
        htmlWorker.parse(new StringReader(frase));
        document.close();
        if (Desktop.isDesktopSupported()) {
            try {
                Desktop.getDesktop().open(new File(path));
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Modelo.RelatoriosBEAN.java

public static void notaDeCompra(NotaDecompraBEAN n) throws FileNotFoundException, DocumentException {
    String path = System.getProperty("user.home") + FRMEmitirRelatorios.local + ".pdf";
    Document document = new Document(PageSize.A4.rotate());
    PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();/*from   w ww  .j a  v a2s  .c  o m*/
    document.addAuthor("TechSales");
    document.addCreator("TechSales");
    document.addSubject("TechSales - Sistema de gerenciamento de locadoras");
    document.addCreationDate();
    document.addTitle("TechSales");

    HTMLWorker htmlWorker = new HTMLWorker(document);

    String frase;
    try {
        frase = "<html>" + "<head> <meta charset=\"utf-8\"> </head>" + "<body>" + "<br/>"
                + "<h1 align=center>Relatrio de Jogos Comprados </h1>" + "<br/><br/>"
                + "<table BORDER RULES=rows border=0 style=\"  width:100%; \">\n" + "<tr> "
                + "<th align=center><strong>Cdigo</strong></th> "
                + "<th align=center><strong>Custo Unitrio</strong></th> "
                + "<th align=center><strong>Data</strong></th> "
                + "<th align=center><strong>Valor da Entrada</strong></th> "
                + "<th align=center><strong>Nmero de parcelas</strong></th> "
                + "<th align=center><strong>Preo Total</strong></th> "
                + "<th align=center><strong>Quantidade Compradra</strong></th> "
                + "<th align=center><strong>Caixa</strong></th> "
                + "<th align=center><strong>Jogo</strong></th> " + "</tr> ";

        frase += "<tr align=center><td>" + n.getNdcCodigo() + "</td><td>" + n.getNdcCustoUnitario()
                + "</td><td>" + n.getNdcData() + "</td><td>" + n.getNdcEntrada() + "</td><td>"
                + n.getNdcParcelas() + "</td><td>" + n.getNdcPrecoTotal() + "</td><td>" + n.getNdcQtdComprada()
                + "</td><td>" + n.getCaixa().getCaixaCodigo() + "</td><td>" + n.getJogo().getJoNome()
                + "</td></tr>" + "</table>";
        ;

        frase += "<table BORDER RULES=rows border=0 style=\"  width:100%; \">\n" + "<br/><br/>" + "<tr> "
                + "<th align=center><strong>Parcela</strong></th> "
                + "<th align=center><strong>Data</strong></th> "
                + "<th align=center><strong>Valor</strong></th> "
                + "<th align=center><strong>Situao</strong></th> " + "</tr> ";

        for (NotaDeCompraPrazoBEAN nap : cNdcap.listarALL()) {
            if (nap.getNota().getNdcCodigo() == n.getNdcCodigo()) {
                frase += "<tr align=center><td>" + nap.getNapNumParcela() + "</td><td>" + nap.getNapData()
                        + "</td><td>" + nap.getNapValor() + "</td><td>" + nap.getNapSituacao() + "</td></tr>";
            }
        }

        frase += "</table>";
        frase += "</body></html>";
        htmlWorker.parse(new StringReader(frase));
        document.close();
        if (Desktop.isDesktopSupported()) {
            try {
                Desktop.getDesktop().open(new File(path));
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.FilterLogic.imaging.ToPDF.java

License:Apache License

/**
 * Iterate through properties and set each value for PDF document.
 *//*from   ww w .ja  v a  2 s.co  m*/
private void setDocumentProperties(Document pdf) {

    for (Enumeration e = this.documentProperties.keys(); e.hasMoreElements();) {
        String name = e.nextElement().toString().toUpperCase();
        String value = this.documentProperties.getProperty(name);

        if (name.equals("TITLE"))
            pdf.addTitle(value);

        if (name.equals("AUTHOR"))
            pdf.addAuthor(value);

        //            if(name.equals("CREATOR"))
        //                pdf.addCreator(value);

        if (name.equals("CREATIONDATE"))
            pdf.addCreationDate();

        if (name.equals("SUBJECT"))
            pdf.addSubject(value);

        if (name.equals("KEYWORDS"))
            pdf.addKeywords(value);
    }

}

From source file:nz.ac.waikato.cms.doc.OverlayFilename.java

License:Open Source License

/**
 * Performs the overlay./*from w ww .jav a2s  .  com*/
 *
 * @param input   the input file/dir
 * @param output   the output file/dir
 * @param vpos   the vertical position
 * @param hpos   the horizontal position
 * @param stripPath   whether to strip the path
 * @param stripExt   whether to strip the extension
 * @param pages   the array of pages (1-based) to add the overlay to, null for all
 * @param evenPages   whether to enforce even pages in the document
 * @return      true if successfully overlay
 */
public boolean overlay(File input, File output, int vpos, int hpos, boolean stripPath, boolean stripExt,
        int[] pages, boolean evenPages) {
    PdfReader reader;
    PdfStamper stamper;
    FileOutputStream fos;
    PdfContentByte canvas;
    int i;
    String text;
    int numPages;
    File tmpFile;
    Document document;
    PdfWriter writer;
    PdfImportedPage page;
    PdfContentByte cb;

    reader = null;
    stamper = null;
    fos = null;
    numPages = -1;
    try {
        reader = new PdfReader(input.getAbsolutePath());
        fos = new FileOutputStream(output.getAbsolutePath());
        stamper = new PdfStamper(reader, fos);
        numPages = reader.getNumberOfPages();
        if (pages == null) {
            pages = new int[reader.getNumberOfPages()];
            for (i = 0; i < pages.length; i++)
                pages[i] = i + 1;
        }

        if (stripPath)
            text = input.getName();
        else
            text = input.getAbsolutePath();
        if (stripExt)
            text = text.replaceFirst("\\.[pP][dD][fF]$", "");

        for (i = 0; i < pages.length; i++) {
            canvas = stamper.getOverContent(pages[i]);
            ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Paragraph(text), hpos, vpos, 0.0f);
        }
    } catch (Exception e) {
        System.err.println("Failed to process " + input + ":");
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (stamper != null)
                stamper.close();
        } catch (Exception e) {
            // ignored
        }
        try {
            if (reader != null)
                reader.close();
        } catch (Exception e) {
            // ignored
        }
        try {
            if (fos != null) {
                fos.flush();
                fos.close();
            }
        } catch (Exception e) {
            // ignored
        }
    }

    // enforce even pages?
    if (evenPages && (numPages > 0) && (numPages % 2 == 1)) {
        reader = null;
        fos = null;
        writer = null;
        tmpFile = new File(output.getAbsolutePath() + "tmp");
        try {
            if (!output.renameTo(tmpFile)) {
                System.err.println("Failed to rename '" + output + "' to '" + tmpFile + "'!");
                return false;
            }
            reader = new PdfReader(tmpFile.getAbsolutePath());
            document = new Document(reader.getPageSize(1));
            fos = new FileOutputStream(output.getAbsoluteFile());
            writer = PdfWriter.getInstance(document, fos);
            document.open();
            document.addCreationDate();
            document.addAuthor(System.getProperty("user.name"));
            cb = writer.getDirectContent();
            for (i = 0; i < reader.getNumberOfPages(); i++) {
                page = writer.getImportedPage(reader, i + 1);
                document.newPage();
                cb.addTemplate(page, 0, 0);
            }
            document.newPage();
            document.add(new Paragraph(" ")); // fake content
            document.close();
        } catch (Exception e) {
            System.err.println("Failed to process " + tmpFile + ":");
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (fos != null) {
                    fos.flush();
                    fos.close();
                }
            } catch (Exception e) {
                // ignored
            }
            try {
                if (reader != null)
                    reader.close();
            } catch (Exception e) {
                // ignored
            }
            try {
                if (writer != null)
                    writer.close();
            } catch (Exception e) {
                // ignored
            }
            if (tmpFile.exists()) {
                try {
                    tmpFile.delete();
                } catch (Exception e) {
                    // ignored
                }
            }
        }
    }

    return true;
}

From source file:org.cidte.sii.negocio.PDFWriter.java

public void writePDF(ArrayList<Writable> list, String directorio, String nombre, java.awt.Image image)
        throws DocumentException, FileNotFoundException, BadElementException, IOException {

    Document doc = new Document();
    PdfWriter docWriter;//from  w  w  w  .j  a v  a 2s  .c om

    // special font sizes
    Font bfBold12 = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
    Font bf12 = new Font(Font.FontFamily.TIMES_ROMAN, 12);

    // file path
    String path = directorio + nombre + ".pdf";
    docWriter = PdfWriter.getInstance(doc, new FileOutputStream(new File(path)));

    // document header attributes
    doc.addAuthor("sii");
    doc.addCreationDate();
    doc.addProducer();
    doc.addCreator("sii");
    doc.addTitle(nombre);
    doc.setPageSize(PageSize.LETTER);

    // open document
    doc.open();

    Image img = Image.getInstance(image, null);
    img.setAlignment(Element.ALIGN_LEFT);
    doc.add(img);

    // create a paragraph
    Paragraph paragraph = new Paragraph("iText  is a library that allows you to create and "
            + "manipulate PDF documents. It enables developers looking to enhance web and other "
            + "applications with dynamic PDF document generation and/or manipulation.");

    // create PDF table with the given widths
    PdfPTable table = new PdfPTable(list.get(0).getNames().length);
    // set table width a percentage of the page width
    table.setWidthPercentage(100);
    table.setSpacingBefore(10f); // Space before table
    table.setSpacingAfter(10f); // Space after table

    // insert column headings
    String[] headings = list.get(0).getNames();
    for (String heading : headings) {
        insertCell(table, heading, Element.ALIGN_CENTER, 1, bfBold12);
    }
    table.setHeaderRows(1);

    // insert the data
    for (int i = 0; i < list.size(); i++) {
        Writable w = list.get(i);
        Object[] arr = w.getAsArray();
        for (int j = 0; j < arr.length; j++) {
            // arr[j]
            insertCell(table, arr[j].toString(), Element.ALIGN_LEFT, 1, bf12);
        }
    }

    // insert an empty row
    // insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12);
    // add the PDF table to the paragraph
    paragraph.add(table);
    // add the paragraph to the document
    doc.add(paragraph);

    // close the document
    doc.close();

    // close the writer
    docWriter.close();

}

From source file:org.jfree.chart.swt.ChartPdf.java

License:Open Source License

public static void saveChartAsPDF(File file, JFreeChart chart, int width, int height)
        throws DocumentException, FileNotFoundException, IOException {
    if (chart != null) {
        boolean success = false;
        String old = null;/*from   w  ww . jav a  2 s. co  m*/
        File oldFile = null;
        boolean append = file.exists();
        if (append) {
            old = file.getAbsolutePath() + ".old"; //$NON-NLS-1$
            oldFile = new File(old);
            oldFile.delete();
            file.renameTo(oldFile);
        }
        try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
            // convert chart to PDF with iText:
            Rectangle pagesize = new Rectangle(width, height);
            if (append) {
                PdfReader reader = new PdfReader(old);
                PdfStamper stamper = new PdfStamper(reader, out);
                try {
                    int n = reader.getNumberOfPages() + 1;
                    stamper.insertPage(n, pagesize);
                    PdfContentByte overContent = stamper.getOverContent(n);
                    writeChart(chart, width, height, overContent);
                    ColumnText ct = new ColumnText(overContent);
                    ct.setSimpleColumn(width - 50, 50, width - 12, height, 150, Element.ALIGN_RIGHT);
                    Paragraph paragraph = new Paragraph(String.valueOf(n),
                            new Font(FontFamily.HELVETICA, 9, Font.NORMAL, BaseColor.DARK_GRAY));
                    paragraph.setAlignment(Element.ALIGN_RIGHT);
                    ct.addElement(paragraph);
                    ct.go();
                    success = true;
                } finally {
                    stamper.close();
                    reader.close();
                    oldFile.delete();
                }
            } else {
                Document document = new Document(pagesize, 50, 50, 50, 50);
                document.addCreationDate();
                document.addCreator(Constants.APPLICATION_NAME);
                document.addAuthor(System.getProperty("user.name")); //$NON-NLS-1$
                try {
                    PdfWriter writer = PdfWriter.getInstance(document, out);
                    document.open();
                    writeChart(chart, width, height, writer.getDirectContent());
                    success = true;
                } finally {
                    document.close();
                }
            }
        }
        if (!success) {
            file.delete();
            if (oldFile != null)
                oldFile.renameTo(file);
        }
    }
}