Search in sources :

Example 31 with DocumentException

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

the class DownloadDiscrepancyNote method serializeListToPDF.

public void serializeListToPDF(String content, OutputStream stream) {
    ServletOutputStream servletStream = (ServletOutputStream) stream;
    Document pdfDoc = new Document();
    try {
        PdfWriter.getInstance(pdfDoc, servletStream);
        pdfDoc.open();
        pdfDoc.add(new Paragraph(content));
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    pdfDoc.close();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) DocumentException(com.lowagie.text.DocumentException) Document(com.lowagie.text.Document) Paragraph(com.lowagie.text.Paragraph)

Example 32 with DocumentException

use of com.lowagie.text.DocumentException in project perun by CESNET.

the class PdfSerializer method write.

@Override
public void write(Object object) throws IOException {
    if (object instanceof String) {
        String htmlText = (String) object;
        ITextRenderer renderer = new ITextRenderer();
        if (BeansUtils.getCoreConfig() != null && BeansUtils.getCoreConfig().getPdfFontPath() != null) {
            try {
                renderer.getFontResolver().addFont(new File(BeansUtils.getCoreConfig().getPdfFontPath()).getAbsolutePath(), "CP1250", true);
            } catch (Exception e) {
                log.error("Failed to add font for PDF: {}", e);
            }
        }
        renderer.setDocumentFromString(htmlText);
        renderer.layout();
        try {
            renderer.createPDF(outputStream);
        } catch (DocumentException e) {
            throw new IOException(e);
        }
    }
}
Also used : ITextRenderer(org.xhtmlrenderer.pdf.ITextRenderer) DocumentException(com.lowagie.text.DocumentException) IOException(java.io.IOException) File(java.io.File) RpcException(cz.metacentrum.perun.core.api.exceptions.RpcException) IOException(java.io.IOException) DocumentException(com.lowagie.text.DocumentException) PerunException(cz.metacentrum.perun.core.api.exceptions.PerunException) PerunRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.PerunRuntimeException)

Example 33 with DocumentException

use of com.lowagie.text.DocumentException in project dhis2-core by dhis2.

the class PdfFieldCell method cellLayout.

@Override
public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvases) {
    try {
        PdfContentByte canvasText = canvases[PdfPTable.TEXTCANVAS];
        if (type == TYPE_RADIOBUTTON) {
            if (parent != null) {
                float leftLoc = rect.getLeft();
                float rightLoc = rect.getLeft() + RADIOBUTTON_WIDTH;
                String text;
                String value;
                for (int i = 0; i < texts.length; i++) {
                    text = texts[i];
                    value = values[i];
                    Rectangle radioRec = new Rectangle(leftLoc, rect.getTop() - height, rightLoc, rect.getTop());
                    RadioCheckField rf = new RadioCheckField(writer, radioRec, "RDBtn_" + text, value);
                    if (value != null && value.equals(checkValue)) {
                        rf.setChecked(true);
                    }
                    rf.setBorderColor(GrayColor.GRAYBLACK);
                    rf.setBackgroundColor(GrayColor.GRAYWHITE);
                    rf.setCheckType(RadioCheckField.TYPE_CIRCLE);
                    parent.addKid(rf.getRadioField());
                    leftLoc = rightLoc;
                    rightLoc += width;
                    ColumnText.showTextAligned(canvasText, Element.ALIGN_LEFT, new Phrase(text), leftLoc + RADIOBUTTON_TEXTOFFSET, height, 0);
                    leftLoc = rightLoc;
                    rightLoc += RADIOBUTTON_WIDTH;
                }
                writer.addAnnotation(parent);
            }
        } else if (type == TYPE_BUTTON) {
            // Add the push button
            PushbuttonField button = new PushbuttonField(writer, rect, name);
            button.setBackgroundColor(new GrayColor(0.75f));
            button.setBorderColor(GrayColor.GRAYBLACK);
            button.setBorderWidth(1);
            button.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
            button.setTextColor(GrayColor.GRAYBLACK);
            button.setFontSize(PdfDataEntryFormUtil.UNITSIZE_DEFAULT);
            button.setText(text);
            button.setLayout(PushbuttonField.LAYOUT_ICON_LEFT_LABEL_RIGHT);
            button.setScaleIcon(PushbuttonField.SCALE_ICON_ALWAYS);
            button.setProportionalIcon(true);
            button.setIconHorizontalAdjustment(0);
            formField = button.getField();
            formField.setAction(PdfAction.javaScript(jsAction, writer));
        } else if (type == TYPE_CHECKBOX) {
            float extraCheckBoxOffset_Left = 2.0f;
            float extraCheckBoxOffset_Top = 1.5f;
            formField.setWidget(new Rectangle(rect.getLeft() + OFFSET_LEFT + extraCheckBoxOffset_Left, rect.getTop() - height - OFFSET_TOP - extraCheckBoxOffset_Top, rect.getLeft() + width + OFFSET_LEFT + extraCheckBoxOffset_Left, rect.getTop() - OFFSET_TOP - extraCheckBoxOffset_Top), PdfAnnotation.HIGHLIGHT_NONE);
        } else {
            if (type == TYPE_TEXT_ORGUNIT) {
                formField.setAdditionalActions(PdfName.BL, PdfAction.javaScript("if(event.value == '') app.alert('Please enter org unit identifier');", writer));
            }
            // TYPE_TEXT_NUMBER and TYPE_CHECKBOX cases included as well
            // here
            formField.setWidget(new Rectangle(rect.getLeft() + OFFSET_LEFT, rect.getTop() - height - OFFSET_TOP, rect.getLeft() + width + OFFSET_LEFT, rect.getTop() - OFFSET_TOP), PdfAnnotation.HIGHLIGHT_NONE);
        }
        writer.addAnnotation(formField);
    } catch (DocumentException ex) {
        throw new RuntimeException(ex.getMessage());
    } catch (IOException ex) {
        throw new RuntimeException(ex.getMessage());
    }
}
Also used : RadioCheckField(com.lowagie.text.pdf.RadioCheckField) DocumentException(com.lowagie.text.DocumentException) Rectangle(com.lowagie.text.Rectangle) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) Phrase(com.lowagie.text.Phrase) GrayColor(com.lowagie.text.pdf.GrayColor) IOException(java.io.IOException) PushbuttonField(com.lowagie.text.pdf.PushbuttonField)

Example 34 with DocumentException

use of com.lowagie.text.DocumentException in project dhis2-core by dhis2.

the class PDFUtils method openDocument.

/**
 * Creates a document.
 *
 * @param outputStream The output stream to write the document content.
 * @param pageSize the page size.
 * @return A Document.
 */
public static Document openDocument(OutputStream outputStream, Rectangle pageSize) {
    try {
        Document document = new Document(pageSize);
        PdfWriter.getInstance(document, outputStream);
        document.open();
        return document;
    } catch (DocumentException ex) {
        throw new RuntimeException("Failed to open PDF document", ex);
    }
}
Also used : DocumentException(com.lowagie.text.DocumentException) Document(com.lowagie.text.Document)

Aggregations

DocumentException (com.lowagie.text.DocumentException)34 Document (com.lowagie.text.Document)19 IOException (java.io.IOException)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 Paragraph (com.lowagie.text.Paragraph)12 Color (java.awt.Color)9 PdfContentByte (com.lowagie.text.pdf.PdfContentByte)7 PdfReader (com.lowagie.text.pdf.PdfReader)7 ByteBuffer (java.nio.ByteBuffer)6 Delegator (org.apache.ofbiz.entity.Delegator)6 Chunk (com.lowagie.text.Chunk)5 Phrase (com.lowagie.text.Phrase)5 Section (com.lowagie.text.Section)5 PdfObject (com.lowagie.text.pdf.PdfObject)5 PdfWriter (com.lowagie.text.pdf.PdfWriter)5 GeneralException (org.apache.ofbiz.base.util.GeneralException)5 ExpressionException (cbit.vcell.parser.ExpressionException)4 AcroFields (com.lowagie.text.pdf.AcroFields)4 PdfStamper (com.lowagie.text.pdf.PdfStamper)4 File (java.io.File)4