Search in sources :

Example 26 with Document

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

the class DroolsDocsBuilder method writePDF.

public void writePDF(OutputStream out) {
    // TODO: Use i18n!
    Document document = new Document();
    try {
        PdfWriter.getInstance(document, out);
        HeaderFooter footer = DroolsDocsComponentFactory.createFooter(packageData.getName());
        document.setFooter(footer);
        document.addTitle(packageData.getName().toUpperCase());
        document.open();
        // First page, documentation info.
        DroolsDocsComponentFactory.createFirstPage(document, currentDate, packageData);
        document.newPage();
        // List index of the rules
        document.add(new Phrase("Table of Contents"));
        document.add(DroolsDocsComponentFactory.createContents(packageData.getRules()));
        document.newPage();
        for (DrlRuleParser ruleData : packageData.getRules()) {
            DroolsDocsComponentFactory.newRulePage(document, packageData.getName(), ruleData);
        }
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    }
    document.close();
}
Also used : DocumentException(com.lowagie.text.DocumentException) HeaderFooter(com.lowagie.text.HeaderFooter) Phrase(com.lowagie.text.Phrase) Document(com.lowagie.text.Document) DrlRuleParser(org.drools.verifier.misc.DrlRuleParser)

Example 27 with Document

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

Example 28 with Document

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

the class PdfHelper method scalePdfPages.

/**
 * Scale the pages of the input pdfOutput document to the given pageSize.
 * @param pdfOutput The PDF document to rescale, in the form of a ByteArrayOutputStream.
 * @param pageSize The new page size to which to scale to PDF document, e.g. "A4".
 * @param noEnlarge If true, center pages instead of enlarging them.
 *        Use noEnlarge if the new page size is larger than the old one
 *        and the pages should be centered instead of enlarged.
 * @param preserveAspectRatio If true, the aspect ratio will be preserved.
 * @return The PDF document with its pages scaled to the input pageSize.
 */
public static byte[] scalePdfPages(byte[] pdfOutput, String pageSize, boolean noEnlarge, boolean preserveAspectRatio) throws FormPrintException {
    if (pageSize == null || pdfOutput == null) {
        return pdfOutput;
    }
    // Get the dimensions of the given pageSize in PostScript points.
    // A PostScript point is a 72th of an inch.
    float dimX;
    float dimY;
    Rectangle rectangle;
    try {
        rectangle = PageSize.getRectangle(pageSize);
    } catch (Exception ex) {
        FormPrintException e = new PdfProcessingException("scalePdfPages  - Invalid page size = " + pageSize + "  ");
        log.error(" scalePdfPages  - Invalid page size: " + pageSize + ".  " + ex.getMessage() + ". ");
        throw e;
    }
    if (rectangle != null) {
        dimX = rectangle.getWidth();
        dimY = rectangle.getHeight();
    } else {
        FormPrintException e = new PdfProcessingException("scalePdfPages  - Invalid page size: " + pageSize);
        log.error(" scalePdfPages  - Invalid page size: " + pageSize);
        throw e;
    }
    // Create portrait and landscape rectangles for the given page size.
    Rectangle portraitPageSize;
    Rectangle landscapePageSize;
    if (dimY > dimX) {
        portraitPageSize = new Rectangle(dimX, dimY);
        landscapePageSize = new Rectangle(dimY, dimX);
    } else {
        portraitPageSize = new Rectangle(dimY, dimX);
        landscapePageSize = new Rectangle(dimX, dimY);
    }
    // Remove the document rotation before resizing the document.
    byte[] output = removeRotation(pdfOutput);
    PdfReader currentReader = null;
    try {
        currentReader = new PdfReader(output);
    } catch (IOException ex) {
        FormPrintException e = new PdfProcessingException("scalePdfPages  - Failed to create a PDF Reader");
        log.error(" scalePdfPages  - Failed to create a PDF Reader ");
        throw e;
    }
    OutputStream baos = new ByteArrayOutputStream();
    Rectangle newSize = new Rectangle(dimX, dimY);
    Document document = new Document(newSize, 0, 0, 0, 0);
    PdfWriter writer = null;
    try {
        writer = PdfWriter.getInstance(document, baos);
    } catch (DocumentException ex) {
        FormPrintException e = new PdfProcessingException("scalePdfPages  - Failed to create a PDF Writer");
        log.error(" scalePdfPages  - Failed to create a PDF Writer ");
        throw e;
    }
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    PdfImportedPage page;
    float offsetX, offsetY;
    for (int i = 1; i <= currentReader.getNumberOfPages(); i++) {
        Rectangle currentSize = currentReader.getPageSizeWithRotation(i);
        if (currentReader.getPageRotation(i) != 0) {
            FormPrintException e = new PdfProcessingException("Page Rotation, " + currentReader.getPageRotation(i) + ", must be removed to re-scale the form.");
            log.error(" Page Rotation, " + currentReader.getPageRotation(i) + ", must be removed to re-scale the form. ");
            throw e;
        }
        // Reset the page size for each page because there may be a mix of sizes in the document.
        float currentWidth = currentSize.getWidth();
        float currentHeight = currentSize.getHeight();
        if (currentWidth > currentHeight) {
            newSize = landscapePageSize;
        } else {
            newSize = portraitPageSize;
        }
        document.setPageSize(newSize);
        document.newPage();
        float factorX = newSize.getWidth() / currentSize.getWidth();
        float factorY = newSize.getHeight() / currentSize.getHeight();
        // and the pages should be centered instead of enlarged.
        if (noEnlarge) {
            if (factorX > 1) {
                factorX = 1;
            }
            if (factorY > 1) {
                factorY = 1;
            }
        }
        if (preserveAspectRatio) {
            factorX = Math.min(factorX, factorY);
            factorY = factorX;
        }
        offsetX = (newSize.getWidth() - (currentSize.getWidth() * factorX)) / 2f;
        offsetY = (newSize.getHeight() - (currentSize.getHeight() * factorY)) / 2f;
        page = writer.getImportedPage(currentReader, i);
        cb.addTemplate(page, factorX, 0, 0, factorY, offsetX, offsetY);
    }
    document.close();
    return ((ByteArrayOutputStream) baos).toByteArray();
}
Also used : PdfProcessingException(org.jaffa.modules.printing.services.exceptions.PdfProcessingException) PdfWriter(com.lowagie.text.pdf.PdfWriter) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Rectangle(com.lowagie.text.Rectangle) PdfReader(com.lowagie.text.pdf.PdfReader) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.lowagie.text.Document) IOException(java.io.IOException) DocumentException(com.lowagie.text.DocumentException) PdfProcessingException(org.jaffa.modules.printing.services.exceptions.PdfProcessingException) FormPrintException(org.jaffa.modules.printing.services.exceptions.FormPrintException) PdfImportedPage(com.lowagie.text.pdf.PdfImportedPage) DocumentException(com.lowagie.text.DocumentException) FormPrintException(org.jaffa.modules.printing.services.exceptions.FormPrintException) PdfContentByte(com.lowagie.text.pdf.PdfContentByte)

Example 29 with Document

use of com.lowagie.text.Document in project OpenClinica by OpenClinica.

the class DownloadDiscrepancyNote method serializeThreadsToPDF.

public void serializeThreadsToPDF(List<DiscrepancyNoteThread> listOfThreads, OutputStream stream, String studyIdentifier) {
    ServletOutputStream servletStream = (ServletOutputStream) stream;
    Document pdfDoc = new Document();
    try {
        PdfWriter.getInstance(pdfDoc, servletStream);
        pdfDoc.open();
        // Create header for the study identifier or name
        if (studyIdentifier != null) {
            HeaderFooter header = new HeaderFooter(new Phrase("Study Identifier: " + studyIdentifier + " pg."), true);
            header.setAlignment(Element.ALIGN_CENTER);
            Paragraph para = new Paragraph("Study Identifier: " + studyIdentifier, new Font(Font.HELVETICA, 18, Font.BOLD, new Color(0, 0, 0)));
            para.setAlignment(Element.ALIGN_CENTER);
            pdfDoc.setHeader(header);
            pdfDoc.add(para);
        }
        for (DiscrepancyNoteThread discNoteThread : listOfThreads) {
            pdfDoc.add(this.createTableThreadHeader(discNoteThread));
            // Just the parent of the thread?  discNoteThread.getLinkedNoteList()
            for (DiscrepancyNoteBean discNoteBean : discNoteThread.getLinkedNoteList()) {
                // DiscrepancyNoteBean discNoteBean = discNoteThread.getLinkedNoteList().getFirst();
                if (discNoteBean.getParentDnId() > 0) {
                    pdfDoc.add(this.createTableFromBean(discNoteBean));
                    pdfDoc.add(new Paragraph("\n"));
                }
            }
        }
    // pdfDoc.add(new Paragraph(content));
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    pdfDoc.close();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) DiscrepancyNoteBean(org.akaza.openclinica.bean.managestudy.DiscrepancyNoteBean) Color(java.awt.Color) DocumentException(com.lowagie.text.DocumentException) HeaderFooter(com.lowagie.text.HeaderFooter) Phrase(com.lowagie.text.Phrase) Document(com.lowagie.text.Document) Font(com.lowagie.text.Font) Paragraph(com.lowagie.text.Paragraph) DiscrepancyNoteThread(org.akaza.openclinica.service.DiscrepancyNoteThread)

Example 30 with Document

use of com.lowagie.text.Document in project OpenClinica by OpenClinica.

the class DownloadDiscrepancyNote method serializeListToPDF.

public void serializeListToPDF(List<DiscrepancyNoteBean> listOfBeans, OutputStream stream, String studyIdentifier) {
    ServletOutputStream servletStream = (ServletOutputStream) stream;
    Document pdfDoc = new Document();
    try {
        PdfWriter.getInstance(pdfDoc, servletStream);
        pdfDoc.open();
        // Create header for the study identifier or name
        if (studyIdentifier != null) {
            HeaderFooter header = new HeaderFooter(new Phrase("Study Identifier: " + studyIdentifier + " pg."), true);
            header.setAlignment(Element.ALIGN_CENTER);
            Paragraph para = new Paragraph("Study Identifier: " + studyIdentifier, new Font(Font.HELVETICA, 18, Font.BOLD, new Color(0, 0, 0)));
            para.setAlignment(Element.ALIGN_CENTER);
            pdfDoc.setHeader(header);
            pdfDoc.add(para);
        }
        for (DiscrepancyNoteBean discNoteBean : listOfBeans) {
            pdfDoc.add(this.createTableFromBean(discNoteBean));
            pdfDoc.add(new Paragraph("\n"));
        }
    // pdfDoc.add(new Paragraph(content));
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    pdfDoc.close();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) DiscrepancyNoteBean(org.akaza.openclinica.bean.managestudy.DiscrepancyNoteBean) Color(java.awt.Color) DocumentException(com.lowagie.text.DocumentException) HeaderFooter(com.lowagie.text.HeaderFooter) Phrase(com.lowagie.text.Phrase) Document(com.lowagie.text.Document) Font(com.lowagie.text.Font) Paragraph(com.lowagie.text.Paragraph)

Aggregations

Document (com.lowagie.text.Document)36 DocumentException (com.lowagie.text.DocumentException)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 PdfWriter (com.lowagie.text.pdf.PdfWriter)13 Paragraph (com.lowagie.text.Paragraph)12 IOException (java.io.IOException)12 Rectangle (com.lowagie.text.Rectangle)10 PdfContentByte (com.lowagie.text.pdf.PdfContentByte)9 FileOutputStream (java.io.FileOutputStream)9 Color (java.awt.Color)8 DefaultFontMapper (com.lowagie.text.pdf.DefaultFontMapper)6 PdfImportedPage (com.lowagie.text.pdf.PdfImportedPage)5 File (java.io.File)5 DocWriter (com.lowagie.text.DocWriter)4 Phrase (com.lowagie.text.Phrase)4 PdfPCell (com.lowagie.text.pdf.PdfPCell)4 PdfPTable (com.lowagie.text.pdf.PdfPTable)4 PdfReader (com.lowagie.text.pdf.PdfReader)4 Graphics2D (java.awt.Graphics2D)4 OutputStream (java.io.OutputStream)4