Search in sources :

Example 11 with Rectangle

use of com.lowagie.text.Rectangle 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)

Example 12 with Rectangle

use of com.lowagie.text.Rectangle in project cytoscape-impl by cytoscape.

the class PDFWriter method run.

@Override
public void run(TaskMonitor taskMonitor) throws Exception {
    // TODO should be accomplished with renderer properties
    // view.setPrintingTextAsShape(!exportTextAsFont);
    taskMonitor.setProgress(0.0);
    taskMonitor.setStatusMessage("Creating PDF image...");
    logger.debug("PDF Rendering start");
    final Rectangle pageSize = PageSize.LETTER;
    final Document document = new Document(pageSize);
    logger.debug("Document created: " + document);
    final PdfWriter writer = PdfWriter.getInstance(document, stream);
    document.open();
    taskMonitor.setProgress(0.1);
    final PdfContentByte canvas = writer.getDirectContent();
    logger.debug("CB0 created: " + canvas.getClass());
    final float pageWidth = pageSize.getWidth();
    final float pageHeight = pageSize.getHeight();
    logger.debug("Page W: " + pageWidth + " Page H: " + pageHeight);
    final DefaultFontMapper fontMapper = new DefaultFontMapper();
    logger.debug("FontMapper created = " + fontMapper);
    Graphics2D g = null;
    logger.debug("!!!!! Enter block 2");
    engine.getProperties().setProperty("exportTextAsShape", new Boolean(!exportTextAsFont).toString());
    taskMonitor.setProgress(0.2);
    if (exportTextAsFont) {
        g = canvas.createGraphics(pageWidth, pageHeight, new DefaultFontMapper());
    } else {
        g = canvas.createGraphicsShapes(pageWidth, pageHeight);
    }
    taskMonitor.setProgress(0.4);
    logger.debug("##### G2D created: " + g);
    double imageScale = Math.min(pageSize.getWidth() / width, pageSize.getHeight() / height);
    g.scale(imageScale, imageScale);
    logger.debug("##### Start Rendering Phase 2: " + engine.toString());
    engine.printCanvas(g);
    logger.debug("##### Canvas Rendering Done: ");
    taskMonitor.setProgress(0.8);
    g.dispose();
    document.close();
    writer.close();
    stream.close();
    logger.debug("PDF rendering finished.");
    taskMonitor.setProgress(1.0);
}
Also used : PdfWriter(com.lowagie.text.pdf.PdfWriter) Rectangle(com.lowagie.text.Rectangle) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) DefaultFontMapper(com.lowagie.text.pdf.DefaultFontMapper) Document(com.lowagie.text.Document) Graphics2D(java.awt.Graphics2D)

Example 13 with Rectangle

use of com.lowagie.text.Rectangle in project vcell by virtualcell.

the class ITextWriter method createDocument.

protected void createDocument(PageFormat pageFormat) {
    Rectangle pageSize = new Rectangle((float) pageFormat.getWidth(), (float) pageFormat.getHeight());
    double marginL = pageFormat.getImageableX();
    double marginT = pageFormat.getImageableY();
    double marginR = pageFormat.getWidth() - pageFormat.getImageableWidth() - marginL;
    double marginB = pageFormat.getHeight() - pageFormat.getImageableHeight() - marginT;
    // System.out.println(pageFormat.getWidth() +  " " + pageFormat.getHeight() + " " + marginL + " " + marginT);
    ITextWriter.DEF_IMAGE_WIDTH = (int) pageFormat.getImageableWidth();
    // ITextWriter.DEF_IMAGE_HEIGHT = (int)pageFormat.getImageableHeight();
    // can also use some of the built-in PageSize objects, like PageSize.A4, PageSize.LETTER
    this.document = new Document(pageSize, (float) marginL, (float) marginR, (float) marginT, (float) marginB);
}
Also used : Rectangle(com.lowagie.text.Rectangle) Document(com.lowagie.text.Document)

Example 14 with Rectangle

use of com.lowagie.text.Rectangle in project drools by kiegroup.

the class EndPage method onEndPage.

public void onEndPage(PdfWriter writer, Document document) {
    try {
        // TODO this image never existed
        Image image = Image.getInstance(DroolsDocsBuilder.class.getResource("guvnor-webapp.png"));
        image.setAlignment(Image.RIGHT);
        image.scaleAbsolute(100, 30);
        Rectangle page = document.getPageSize();
        PdfPTable head = new PdfPTable(2);
        PdfPCell cell1 = new PdfPCell(image);
        cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell1.setBorder(0);
        head.addCell(cell1);
        PdfPCell cell2 = new PdfPCell(new Phrase(currentDate, DroolsDocsComponentFactory.HEADER_FOOTER_TEXT));
        cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell2.setBorder(0);
        head.addCell(cell2);
        head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        head.writeSelectedRows(0, -1, document.leftMargin(), page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent());
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
Also used : ExceptionConverter(com.lowagie.text.ExceptionConverter) PdfPCell(com.lowagie.text.pdf.PdfPCell) PdfPTable(com.lowagie.text.pdf.PdfPTable) Rectangle(com.lowagie.text.Rectangle) Phrase(com.lowagie.text.Phrase) Image(com.lowagie.text.Image) BadElementException(com.lowagie.text.BadElementException) DocumentException(com.lowagie.text.DocumentException)

Example 15 with Rectangle

use of com.lowagie.text.Rectangle in project jaffa-framework by jaffa-projects.

the class FormPrintEngineIText method startDocument.

/**
 * Any work to start off printing the document
 * @throws FormPrintException Thrown if there is any form processing problems
 */
protected void startDocument() throws FormPrintException {
    log.debug("startDocument:");
    Rectangle r = m_templateReader.getPageSize(getCurrentTemplatePage());
    log.debug("Page Size      : t=" + r.getTop() + ",l=" + r.getLeft() + ",b=" + r.getBottom() + ",r=" + r.getRight() + ", rot=" + r.getRotation());
    r = m_templateReader.getPageSizeWithRotation(getCurrentTemplatePage());
    log.debug("Page Size w/Rot: t=" + r.getTop() + ",l=" + r.getLeft() + ",b=" + r.getBottom() + ",r=" + r.getRight() + ", rot=" + r.getRotation());
    m_generatedDoc = new Document(m_templateReader.getPageSizeWithRotation(getCurrentTemplatePage()));
    // m_generatedDoc = new Document(m_templateReader.getPageSize(getCurrentTemplatePage()));
    m_output = new ByteArrayOutputStream();
    try {
        m_writer = PdfWriter.getInstance(m_generatedDoc, m_output);
    } catch (DocumentException e) {
        log.error("Error Creating Writer - " + e.getMessage(), e);
        throw new EngineProcessingException("Error Creating Writer - " + e.getMessage());
    }
    if (getDocumentProperties() != null) {
        Properties dp = (Properties) getDocumentProperties().clone();
        if (dp.getProperty(DOCUMENT_PROPERTY_TITLE) != null) {
            m_generatedDoc.addTitle(dp.getProperty(DOCUMENT_PROPERTY_TITLE));
            dp.remove(DOCUMENT_PROPERTY_TITLE);
        }
        if (dp.getProperty(DOCUMENT_PROPERTY_SUBJECT) != null) {
            m_generatedDoc.addSubject(dp.getProperty(DOCUMENT_PROPERTY_SUBJECT));
            dp.remove(DOCUMENT_PROPERTY_SUBJECT);
        }
        if (dp.getProperty(DOCUMENT_PROPERTY_KEYWORDS) != null) {
            m_generatedDoc.addKeywords(dp.getProperty(DOCUMENT_PROPERTY_KEYWORDS));
            dp.remove(DOCUMENT_PROPERTY_KEYWORDS);
        }
        if (dp.getProperty(DOCUMENT_PROPERTY_CREATOR) != null) {
            m_generatedDoc.addCreator(dp.getProperty(DOCUMENT_PROPERTY_CREATOR, "Jaffa Print Engine"));
            dp.remove(DOCUMENT_PROPERTY_CREATOR);
        }
        if (dp.getProperty(DOCUMENT_PROPERTY_AUTHOR) != null) {
            m_generatedDoc.addAuthor(dp.getProperty(DOCUMENT_PROPERTY_AUTHOR));
            dp.remove(DOCUMENT_PROPERTY_AUTHOR);
        }
        // loop through other properties and set them as header parameters
        for (Enumeration en = dp.elements(); en.hasMoreElements(); ) {
            Map.Entry e = (Map.Entry) en.nextElement();
            if (e.getKey() != null && e.getValue() != null)
                m_generatedDoc.addHeader(e.getKey().toString(), e.getValue().toString());
        }
    }
    m_generatedDoc.addCreationDate();
    m_generatedDoc.open();
}
Also used : Enumeration(java.util.Enumeration) DocumentException(com.lowagie.text.DocumentException) Rectangle(com.lowagie.text.Rectangle) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.lowagie.text.Document) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map) EngineProcessingException(org.jaffa.modules.printing.services.exceptions.EngineProcessingException)

Aggregations

Rectangle (com.lowagie.text.Rectangle)20 Document (com.lowagie.text.Document)10 PdfContentByte (com.lowagie.text.pdf.PdfContentByte)8 PdfWriter (com.lowagie.text.pdf.PdfWriter)6 IOException (java.io.IOException)6 DocumentException (com.lowagie.text.DocumentException)5 PdfPTable (com.lowagie.text.pdf.PdfPTable)5 FileOutputStream (java.io.FileOutputStream)5 DefaultFontMapper (com.lowagie.text.pdf.DefaultFontMapper)4 PdfPCell (com.lowagie.text.pdf.PdfPCell)4 Graphics2D (java.awt.Graphics2D)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 OutputStream (java.io.OutputStream)4 BadElementException (com.lowagie.text.BadElementException)3 DocWriter (com.lowagie.text.DocWriter)3 PdfImportedPage (com.lowagie.text.pdf.PdfImportedPage)3 HTMLDocument (javax.swing.text.html.HTMLDocument)3 Chapter (com.lowagie.text.Chapter)2 ExceptionConverter (com.lowagie.text.ExceptionConverter)2 Image (com.lowagie.text.Image)2