Search in sources :

Example 16 with Rectangle

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

the class FormPrintEngineIText method startPage.

/**
 * Any work to start off printing a page of the document
 * m_currentPage will contain the page being printed, and
 * m_currentTemplatePage will contain the template page number to base this
 * new page on.
 * @throws FormPrintException Thrown if there is any form processing problems
 */
protected void startPage() throws FormPrintException {
    log.debug("startPage: Page=" + getCurrentPage());
    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());
    // Get rotation quadrent 0..3
    int q = (m_templateReader.getPageSizeWithRotation(getCurrentTemplatePage()).getRotation() % 360) / 90;
    float tX = (q == 2 ? r.getTop() : 0) + (q == 3 ? r.getRight() : 0);
    float tY = (q == 1 ? r.getTop() : 0) + (q == 2 ? r.getRight() : 0);
    float sX = 1f, sY = 1f;
    double angle = -r.getRotation() * (Math.PI / 180f);
    double transformA = sX * Math.cos(angle);
    double transformB = sY * Math.sin(angle);
    double transformC = -sX * Math.sin(angle);
    double transformD = sY * Math.cos(angle);
    double transformE = tX;
    double transformF = tY;
    m_generatedDoc.setPageSize(m_templateReader.getPageSizeWithRotation(getCurrentTemplatePage()));
    // m_generatedDoc.setPageSize(m_templateReader.getPageSize(getCurrentTemplatePage()) );
    /**
     * try {
     * m_generatedDoc.newPage();
     * } catch (DocumentException e) {
     * log.error("Error Creating New Page - " + e.getMessage() ,e);
     * throw new EngineProcessingException("Error Creating New Page - " + e.getMessage());
     * }
     */
    m_generatedDoc.newPage();
    PdfImportedPage page = m_writer.getImportedPage(m_templateReader, getCurrentTemplatePage());
    PdfContentByte cb = m_writer.getDirectContent();
    // cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
    cb.addTemplate(page, (float) transformA, (float) transformB, (float) transformC, (float) transformD, (float) transformE, (float) transformF);
    log.debug("Matrix = [A=" + transformA + ", B=" + transformB + ", C=" + transformC + ", D=" + transformD + ", E=" + transformE + ", F=" + transformF + " ]");
}
Also used : PdfImportedPage(com.lowagie.text.pdf.PdfImportedPage) Rectangle(com.lowagie.text.Rectangle) PdfContentByte(com.lowagie.text.pdf.PdfContentByte)

Example 17 with Rectangle

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

use of com.lowagie.text.Rectangle in project boxmaker by rahulbot.

the class Renderer method openDoc.

/**
 * Create the document to write to (needed before any rendering can happen).
 * @param widthMm	the width of the document in millimeters
 * @param heightMm	the height of the document in millimeters
 * @param fileName  the name of the file to save
 * @throws FileNotFoundException
 * @throws DocumentException
 */
private void openDoc(float widthMm, float heightMm, String fileName) throws FileNotFoundException, DocumentException {
    float docWidth = widthMm * DPI * INCH_PER_MM;
    float docHeight = heightMm * DPI * INCH_PER_MM;
    // System.out.println("doc = "+docWidth+" x "+docHeight);
    doc = new Document(new Rectangle(docWidth, docHeight));
    docPdfWriter = PdfWriter.getInstance(doc, new FileOutputStream(filePath));
    String appNameVersion = BoxMakerConstants.APP_NAME + " " + BoxMakerConstants.VERSION;
    doc.addAuthor(appNameVersion);
    doc.open();
    doc.add(new Paragraph("Produced by " + BoxMakerConstants.APP_NAME + " " + BoxMakerConstants.VERSION + "\n" + "  on " + new Date() + "\n" + BoxMakerConstants.WEBSITE_URL));
}
Also used : FileOutputStream(java.io.FileOutputStream) Rectangle(com.lowagie.text.Rectangle) Document(com.lowagie.text.Document) Date(java.util.Date) Paragraph(com.lowagie.text.Paragraph)

Example 19 with Rectangle

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

the class DefaultPdfDataEntryFormService method addCell_WithCheckBox.

private void addCell_WithCheckBox(PdfPTable table, PdfWriter writer, PdfPCell cell, String strfldName) throws IOException, DocumentException {
    float sizeDefault = PdfDataEntryFormUtil.UNITSIZE_DEFAULT;
    RadioCheckField checkbox = new RadioCheckField(writer, new Rectangle(sizeDefault, sizeDefault), "Yes", "On");
    checkbox.setBorderWidth(1);
    checkbox.setBorderColor(Color.BLACK);
    PdfFormField checkboxfield = checkbox.getCheckField();
    checkboxfield.setFieldName(strfldName + "_" + PdfFieldCell.TPYEDEFINE_NAME + PdfFieldCell.TYPE_CHECKBOX);
    setCheckboxAppearance(checkboxfield, writer.getDirectContent(), sizeDefault);
    cell.setCellEvent(new PdfFieldCell(checkboxfield, sizeDefault, sizeDefault, PdfFieldCell.TYPE_CHECKBOX, writer));
    table.addCell(cell);
}
Also used : RadioCheckField(com.lowagie.text.pdf.RadioCheckField) PdfFormField(com.lowagie.text.pdf.PdfFormField) Rectangle(com.lowagie.text.Rectangle)

Example 20 with Rectangle

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

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