Search in sources :

Example 1 with DocWriter

use of com.lowagie.text.DocWriter in project activityinfo by bedatadriven.

the class ItextReportRenderer method render.

@Override
public void render(ReportElement element, OutputStream os) throws IOException {
    try {
        Document document = new Document();
        DocWriter writer = createWriter(document, os);
        document.open();
        renderFooter(document);
        if (element instanceof Report) {
            renderReport(writer, document, element);
        } else {
            renderElement(writer, document, element);
        }
        document.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Document(com.lowagie.text.Document) DocWriter(com.lowagie.text.DocWriter) IOException(java.io.IOException) DocumentException(com.lowagie.text.DocumentException)

Example 2 with DocWriter

use of com.lowagie.text.DocWriter in project mdw-designer by CenturyLinkCloud.

the class ExportHelper method exportProcess.

/**
 * Export a process
 *
 * @param filename
 *            the file name (including path) where the document will be
 *            generated
 * @param format
 *            can be docx, pdf, rtf, html and bpmn
 * @param canvas
 *            for printing process images
 * @param graph
 *            the process to be printed.
 */
public void exportProcess(String filename, String format, Graph process, DesignerCanvas canvas) throws Exception {
    initialize(false);
    String oldNodeIdType = process.getNodeIdType();
    try {
        process.setNodeIdType(nodeIdType);
        options.add(SECTION_NUMBER);
        if (format.equals(DOCX)) {
            DocxBuilder builder = printProcessDocx(process, canvas);
            builder.save(new java.io.File(filename));
            return;
        } else if (format.equals(HTML)) {
            StringBuilder sb = printPrologHtml("Process " + process.getName());
            printProcessHtml(sb, canvas, 0, process, filename);
            printEpilogHtml(sb, filename);
            return;
        } else if (format.equals(JPG) || format.equals(PNG)) {
            byte[] imgBytes = printImage(-1f, canvas, process.getGraphSize(), format.equals(JPG) ? "jpeg" : "png");
            try (OutputStream os = new FileOutputStream(new File(filename))) {
                os.write(imgBytes);
                return;
            } catch (Exception ex) {
                ex.printStackTrace();
                throw ex;
            }
        } else if (format.equals(BPMN2)) {
            new BPMNHelper().exportProcess(process.getProcessVO(), filename);
        } else {
            // itext processor
            Document document = new Document();
            try {
                DocWriter writer = null;
                if (format.equals(RTF)) {
                    writer = RtfWriter2.getInstance(document, new FileOutputStream(filename));
                } else if (format.equals(PDF)) {
                    writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
                }
                document.open();
                document.setPageSize(PageSize.LETTER);
                Rectangle pageSize = document.getPageSize();
                Chapter chapter = printOneProcessPdf(writer, canvas, format, 1, process, filename, pageSize);
                document.add(chapter);
            } catch (Exception ex) {
                ex.printStackTrace();
                throw ex;
            } finally {
                // step 5: we close the document
                document.close();
            }
        }
    } finally {
        process.setNodeIdType(oldNodeIdType);
    }
}
Also used : DocxBuilder(com.centurylink.mdw.designer.utils.DocxBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Rectangle(com.lowagie.text.Rectangle) Chapter(com.lowagie.text.Chapter) BPMNHelper(com.centurylink.mdw.common.utilities.bpmn.BPMNHelper) HTMLDocument(javax.swing.text.html.HTMLDocument) Document(com.lowagie.text.Document) DocWriter(com.lowagie.text.DocWriter) File(java.io.File) BadLocationException(javax.swing.text.BadLocationException) BadElementException(com.lowagie.text.BadElementException) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 3 with DocWriter

use of com.lowagie.text.DocWriter in project mdw-designer by CenturyLinkCloud.

the class ExportHelper method exportProcesses.

/**
 * Export multiple processes
 *
 * @param filename
 *            the file name (including path) where the document will be
 *            generated
 * @param type
 *            can be pdf, rtf and html
 * @param flowchart
 *            the designer page (for using its canvas and report errors)
 * @param graphs
 *            the list of processes to be printed.
 * @param options
 *            options for printing, from the print dialog.
 */
public void exportProcesses(String filename, String type, FlowchartPage flowchart, List<Graph> graphs) throws Exception {
    initialize(false);
    options.add(SECTION_NUMBER);
    // step 1: creation of a document-object
    Document document = new Document();
    try {
        // step 2: create PDF or RTF writer
        DocWriter writer;
        if (type.equals(RTF)) {
            writer = RtfWriter2.getInstance(document, new FileOutputStream(filename));
        } else if (type.equals(PDF)) {
            writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        } else {
            StringBuilder sb = printPrologHtml("Processes");
            Graph process;
            for (int i = 0; i < graphs.size(); i++) {
                process = graphs.get(i);
                flowchart.setProcess(process);
                this.printProcessHtml(sb, flowchart.canvas, i + 1, process, filename);
            }
            printEpilogHtml(sb, filename);
            return;
        }
        // step 3: we open the document
        document.open();
        // step 4: we add contents to the document
        document.setPageSize(PageSize.LETTER);
        Graph process;
        Chapter chapter;
        Rectangle pageSize = document.getPageSize();
        for (int i = 0; i < graphs.size(); i++) {
            process = graphs.get(i);
            process.setNodeIdType(nodeIdType);
            flowchart.setProcess(process);
            chapter = printOneProcessPdf(writer, flowchart.canvas, type, i + 1, process, filename, pageSize);
            document.add(chapter);
        }
    } finally {
        // step 5: we close the document
        document.close();
    }
}
Also used : Graph(com.centurylink.mdw.designer.display.Graph) SubGraph(com.centurylink.mdw.designer.display.SubGraph) FileOutputStream(java.io.FileOutputStream) Chapter(com.lowagie.text.Chapter) Rectangle(com.lowagie.text.Rectangle) HTMLDocument(javax.swing.text.html.HTMLDocument) Document(com.lowagie.text.Document) DocWriter(com.lowagie.text.DocWriter)

Example 4 with DocWriter

use of com.lowagie.text.DocWriter in project mdw-designer by CenturyLinkCloud.

the class ExportHelper method printImagePdf.

public void printImagePdf(String filename, DesignerCanvas canvas, Dimension graphsize) {
    try {
        DefaultFontMapper mapper = new DefaultFontMapper();
        FontFactory.registerDirectories();
        mapper.insertDirectory("c:\\winnt\\fonts");
        // we create a template and a Graphics2D object that corresponds
        // with it
        // 1 inch
        int margin = 72;
        float scale = 0.5f;
        Rectangle pageSize;
        pageSize = PageSize.LETTER.rotate();
        Document document = new Document(pageSize);
        DocWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
        document.setPageSize(pageSize);
        int imageW = (int) pageSize.getWidth() - margin;
        int imageH = (int) pageSize.getHeight() - margin;
        boolean edsave = canvas.editable;
        canvas.editable = false;
        Color bgsave = canvas.getBackground();
        canvas.setBackground(Color.white);
        int horizontalPages = (int) (graphsize.width * scale) / imageW + 1;
        int verticalPages = (int) (graphsize.height * scale) / imageH + 1;
        for (int i = 0; i < horizontalPages; i++) {
            for (int j = 0; j < verticalPages; j++) {
                Image img;
                PdfContentByte cb = ((PdfWriter) writer).getDirectContent();
                PdfTemplate tp = cb.createTemplate(imageW, imageH);
                Graphics2D g2 = tp.createGraphics(imageW, imageH, mapper);
                tp.setWidth(imageW);
                tp.setHeight(imageH);
                g2.scale(scale, scale);
                g2.translate(-i * imageW / scale, -j * imageH / scale);
                canvas.paintComponent(g2);
                g2.dispose();
                img = new ImgTemplate(tp);
                document.add(img);
            }
        }
        canvas.setBackground(bgsave);
        canvas.editable = edsave;
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : PdfWriter(com.lowagie.text.pdf.PdfWriter) ImgTemplate(com.lowagie.text.ImgTemplate) Color(java.awt.Color) Rectangle(com.lowagie.text.Rectangle) DefaultFontMapper(com.lowagie.text.pdf.DefaultFontMapper) HTMLDocument(javax.swing.text.html.HTMLDocument) Document(com.lowagie.text.Document) BufferedImage(java.awt.image.BufferedImage) Image(com.lowagie.text.Image) DocWriter(com.lowagie.text.DocWriter) PdfTemplate(com.lowagie.text.pdf.PdfTemplate) BadLocationException(javax.swing.text.BadLocationException) BadElementException(com.lowagie.text.BadElementException) IOException(java.io.IOException) Graphics2D(java.awt.Graphics2D) FileOutputStream(java.io.FileOutputStream) PdfContentByte(com.lowagie.text.pdf.PdfContentByte)

Aggregations

DocWriter (com.lowagie.text.DocWriter)4 Document (com.lowagie.text.Document)4 Rectangle (com.lowagie.text.Rectangle)3 FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 HTMLDocument (javax.swing.text.html.HTMLDocument)3 BadElementException (com.lowagie.text.BadElementException)2 Chapter (com.lowagie.text.Chapter)2 BadLocationException (javax.swing.text.BadLocationException)2 BPMNHelper (com.centurylink.mdw.common.utilities.bpmn.BPMNHelper)1 Graph (com.centurylink.mdw.designer.display.Graph)1 SubGraph (com.centurylink.mdw.designer.display.SubGraph)1 DocxBuilder (com.centurylink.mdw.designer.utils.DocxBuilder)1 DocumentException (com.lowagie.text.DocumentException)1 Image (com.lowagie.text.Image)1 ImgTemplate (com.lowagie.text.ImgTemplate)1 DefaultFontMapper (com.lowagie.text.pdf.DefaultFontMapper)1 PdfContentByte (com.lowagie.text.pdf.PdfContentByte)1 PdfTemplate (com.lowagie.text.pdf.PdfTemplate)1 PdfWriter (com.lowagie.text.pdf.PdfWriter)1