Example usage for com.lowagie.text PageSize A4

List of usage examples for com.lowagie.text PageSize A4

Introduction

In this page you can find the example usage for com.lowagie.text PageSize A4.

Prototype

Rectangle A4

To view the source code for com.lowagie.text PageSize A4.

Click Source Link

Document

This is the a4 format

Usage

From source file:ispyb.common.util.PDFFormFiller.java

License:Open Source License

/**
 * @param pageOrientation/*from  w  w  w  . j a va  2s.c om*/
 *            The pageOrientation to set.
 */
public void setPageOrientation(int pageOrientation) {
    this.pageOrientation = ((pageOrientation <= 0) ? PAGE_PORTRAIT : PAGE_LANDSCAPE);
    if (this.writer != null) {
        if (this.pageOrientation == PAGE_PORTRAIT)
            this.writer.setPageSize(PageSize.A4);
        else
            this.writer.setPageSize(PageSize.A4.rotate());
    }
}

From source file:it.eng.spagobi.engines.chart.Utilities.ExportCharts.java

License:Mozilla Public License

public static void transformSVGIntoPDF(InputStream inputStream, OutputStream outputStream)
        throws IOException, DocumentException {
    FileOutputStream imageFileOutputStream = null;
    File imageFile = null;// w w  w. jav  a  2s .co m
    try {
        imageFile = File.createTempFile("chart", ".jpg");
        imageFileOutputStream = new FileOutputStream(imageFile);
        transformSVGIntoPNG(inputStream, imageFileOutputStream);

        Document pdfDocument = new Document(PageSize.A4.rotate());
        PdfWriter docWriter = PdfWriter.getInstance(pdfDocument, outputStream);
        pdfDocument.open();
        Image jpg = Image.getInstance(imageFile.getPath());
        fitImage(jpg);

        pdfDocument.add(jpg);
        pdfDocument.close();
        docWriter.close();
    } finally {
        if (imageFileOutputStream != null) {
            try {
                imageFileOutputStream.close();
            } catch (IOException e) {
                logger.error(e);
            }
        }
        if (imageFile.exists()) {
            imageFile.delete();
        }
    }
}

From source file:it.eng.spagobi.engines.chart.Utilities.ExportCharts.java

License:Mozilla Public License

/**
 * Set the dimension of the image to fit the A4 page size
 * The layout of the page should be horizontal 
 * @param jpg the image to fit/*from  w  ww .  ja v  a  2 s.  c o m*/
 */
public static void fitImage(Image jpg) {
    if (jpg.getWidth() > PageSize.A4.getHeight()) {
        float imgScaledWidth = PageSize.A4.getHeight() - 100;
        float imgScaledHeight = (imgScaledWidth / jpg.getWidth()) * jpg.getHeight();
        jpg.scaleAbsolute(imgScaledWidth, imgScaledHeight);
    }
    if (jpg.getHeight() > PageSize.A4.getWidth()) {
        float imgScaledHeight = PageSize.A4.getWidth() - 100;
        float imgScaledWidth = (imgScaledHeight / jpg.getHeight()) * jpg.getWidth();
        jpg.scaleAbsolute(imgScaledWidth, imgScaledHeight);
    }
}

From source file:it.eng.spagobi.engines.documentcomposition.exporterUtils.PdfCreator.java

License:Mozilla Public License

public FileOutputStream createPdfFile(FileOutputStream fileOutputStream,
        Map<String, DocumentContainer> documentsMap, boolean defaultStyle)
        throws MalformedURLException, IOException, DocumentException {

    logger.debug("IN");

    Document document = new Document(PageSize.A4.rotate());
    Rectangle rect = document.getPageSize();
    docWidth = rect.getWidth();//from w  ww  .j a  v a  2 s.co m
    docHeight = rect.getHeight();

    logger.debug("document size width: " + docWidth + " height: " + docHeight);

    //PdfWriter writer=PdfWriter.getInstance(document,new FileOutputStream("C:/comp/SpagoBIProva.pdf"));
    PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream);
    document.open();

    int documentsNumber = documentsMap.keySet().size();
    int columnnsNumber = 2;

    if (defaultStyle == true) {
        logger.debug("use default style");
        int cellsCounter = 0;

        PdfPTable table = new PdfPTable(columnnsNumber);
        table.setWidthPercentage(100);

        for (Iterator iterator = documentsMap.keySet().iterator(); iterator.hasNext();) {
            String label = (String) iterator.next();
            DocumentContainer docContainer = documentsMap.get(label);
            byte[] content = docContainer.getContent();
            if (content != null) {
                Image img = null;
                try {
                    img = Image.getInstance(content);
                    table.addCell(img);
                } catch (Exception e) {
                    logger.debug("Trying to evaluate response as a PDF file... ");
                    table.addCell("");
                    //                  try {
                    //                     PdfReader reader = new PdfReader(content);
                    //                     PdfImportedPage page = writer.getImportedPage(reader, 1);
                    //                     writer.addPage(page);
                    //                     table.addCell("");
                    //                  } catch (Exception x) {
                    //                     logger.error("Error in inserting image for document " + label, e);
                    //                     logger.error("Error in inserting pdf file for document " + label, x);
                    //                     table.addCell("");
                    //                  }
                }
            }
            cellsCounter++;
        }

        // if cell counter is not pair make it pair
        if (cellsCounter % 2 != 0) {
            table.addCell("");
        }
        document.add(table);

    } else { // ************* NO DEFAULT STYLE *****************
        logger.debug("No default style");

        // I want to calculate total height of scaled heights!!
        //int totalScaledHeight=calculateTotaleScaledHeights(documentsMap, defaultStyle);

        // run on all documents
        for (Iterator iterator = documentsMap.keySet().iterator(); iterator.hasNext();) {
            String label = (String) iterator.next();
            logger.debug("document with label " + label);

            DocumentContainer docContainer = documentsMap.get(label);
            MetadataStyle style = docContainer.getStyle();

            // one table for each image, set at absolute position
            PdfPTable table = new PdfPTable(1);

            // width and height specified for the container by style attribute
            int widthStyle = style.getWidth();
            int heightStyle = style.getHeight();
            logger.debug("style for document width: " + widthStyle + " height: " + heightStyle);

            // width and height for the table scaled to the document size
            int tableWidth = calculatePxSize(docWidth, widthStyle, videoWidth);
            int tableHeight = calculatePxSize(docHeight, heightStyle, videoHeight);

            logger.debug("table for document width: " + tableWidth + " height: " + tableHeight);

            // x and y position as specified for the container by the style attribute
            int yStyle = style.getY();
            int xStyle = style.getX();
            // width and height scaled to the document size
            int xPos = (calculatePxPos(docWidth, xStyle, videoWidth));
            int yPos = (int) docHeight - (calculatePxPos(docHeight, yStyle, videoHeight));
            logger.debug("Table position at x: " + xPos + " y: " + yPos);

            // get the image
            byte[] content = docContainer.getContent();
            if (content != null) {
                Image img = null;
                try {
                    img = Image.getInstance(content);
                } catch (Exception e) {
                    logger.debug("Trying to evaluate response as a PDF file... ");
                    try {
                        PdfReader reader = new PdfReader(content);
                        PdfContentByte cb = writer.getDirectContent();
                        PdfImportedPage page = writer.getImportedPage(reader, 1);
                        float[] tm = getTransformationMatrix(page, xPos, yPos, tableWidth, tableHeight);
                        cb.addTemplate(page, tm[0], tm[1], tm[2], tm[3], tm[4], tm[5]);
                    } catch (Exception x) {
                        logger.error("Error in inserting image for document " + label, e);
                        logger.error("Error in inserting pdf file for document " + label, x);
                    }
                    continue;
                }

                //if it is a REPORT and has more than one page, too large, you have to resize the image, but how to understand it?
                // if image size is more than double of the container size cut the first part,otherwise scale it
                if (docContainer.getDocumentType().equals("REPORT")) {
                    boolean cutImageWIdth = isToCutWidth(img, tableWidth);
                    boolean cutImageHeight = isToCutHeight(img, tableWidth);

                    if (cutImageWIdth == true || cutImageHeight == true) {
                        logger.debug(
                                "Report will be cut to width " + tableWidth + " and height " + tableHeight);
                        try {
                            img = cutImage(content, cutImageHeight, cutImageWIdth, tableHeight, tableWidth,
                                    (int) img.getWidth(), (int) img.getHeight());
                        } catch (Exception e) {
                            logger.error(
                                    "Error in image cut, cutt will be ignored and image will be drawn anyway ",
                                    e);
                        }
                    }
                }

                // this is percentage to resize
                // The image must be size within the cell               
                int percToResize = percentageToResize((int) img.getWidth(), (int) img.getHeight(), tableWidth,
                        tableHeight);
                logger.debug("image will be scaled of percentage " + percToResize);
                img.scalePercent(percToResize);

                PdfPCell cell = new PdfPCell(img);
                cell.setNoWrap(true);
                cell.setFixedHeight(tableHeight);
                cell.setBorderWidth(0);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_CENTER);
                table.addCell(cell);

                //table.setWidthPercentage(tableWidthPerc);
                table.setTotalWidth(tableWidth);
                table.setLockedWidth(true);
            } else {
                // TODO: setALT!
            }
            logger.debug("Add table");
            table.writeSelectedRows(0, -1, xPos, yPos, writer.getDirectContent());
            logger.debug("Document added");
        }

    }
    document.close();
    logger.debug("OUT");
    return fileOutputStream;
}

From source file:it.eng.spagobi.engines.worksheet.exporter.WorkSheetPDFExporter.java

License:Mozilla Public License

public void open(OutputStream outputStream) throws DocumentException {
    pdfDocument = new Document(PageSize.A4.rotate());
    docWriter = PdfWriter.getInstance(pdfDocument, outputStream);
    docWriter.setPageEvent(new MyHeaderFooter());
    pdfDocument.open();/*from www  .  j a v a  2 s. co  m*/
}

From source file:it.eng.spagobi.engines.worksheet.exporter.WorkSheetPDFExporter.java

License:Mozilla Public License

private float[] getHeaderTitlePosition(Image image, String imagePosition) {
    float llx, lly; // lower-left corner x and y position
    float urx, ury; // upper-right corner x and y position
    float imageHeight = image != null ? image.getHeight() : 0;
    float imageWidth = image != null ? image.getWidth() : 0;
    if (LEFT.equals(imagePosition)) {
        llx = MARGIN_LEFT + imageWidth + 30;
        lly = PageSize.A4.getWidth() - (MARGIN_TOP + Math.max(imageHeight, IMAGE_MAX_HEIGHT));
        urx = PageSize.A4.getHeight() - MARGIN_RIGHT;
        ury = PageSize.A4.getWidth() - MARGIN_TOP;
    } else if (RIGHT.equals(imagePosition)) {
        llx = MARGIN_LEFT;//from  w w w.ja v a 2 s .  com
        lly = PageSize.A4.getWidth() - (MARGIN_TOP + Math.max(imageHeight, IMAGE_MAX_HEIGHT));
        urx = PageSize.A4.getHeight() - (MARGIN_RIGHT + imageWidth + 30);
        ury = PageSize.A4.getWidth() - MARGIN_TOP;
    } else { // CENTER case
        llx = MARGIN_LEFT;
        lly = PageSize.A4.getWidth() - (MARGIN_TOP + imageHeight + TITLE_MAX_HEIGHT);
        urx = PageSize.A4.getHeight() - MARGIN_RIGHT;
        ury = PageSize.A4.getWidth() - (MARGIN_TOP + imageHeight);
    }
    float[] toReturn = new float[] { llx, lly, urx, ury };
    return toReturn;
}

From source file:it.eng.spagobi.engines.worksheet.exporter.WorkSheetPDFExporter.java

License:Mozilla Public License

private void setHeaderImagePosition(Image image, String imagePosition) {
    float top = PageSize.A4.getWidth() - (MARGIN_TOP + image.getHeight()); // remember that the page is A4 rotated
    float left = MARGIN_LEFT;
    if (LEFT.equals(imagePosition)) {
        left = MARGIN_LEFT;//from w  w  w .  j  a  v  a 2 s . c o m
    } else if (CENTER.equals(imagePosition)) {
        left = PageSize.A4.getHeight() / 2 - image.getWidth() / 2;
    } else if (RIGHT.equals(imagePosition)) {
        left = PageSize.A4.getHeight() - (MARGIN_RIGHT + image.getWidth()); // remember that the page is A4 rotated
    }
    image.setAbsolutePosition(left, top);
}

From source file:it.eng.spagobi.engines.worksheet.exporter.WorkSheetPDFExporter.java

License:Mozilla Public License

private void setFooterImagePosition(Image image, String imagePosition) {
    float top = MARGIN_BOTTOM;
    float left = MARGIN_LEFT;
    if (LEFT.equals(imagePosition)) {
        left = MARGIN_LEFT;/*from  w w w.ja v  a 2s .  c  om*/
    } else if (CENTER.equals(imagePosition)) {
        left = PageSize.A4.getHeight() / 2 - image.getWidth() / 2;
    } else if (RIGHT.equals(imagePosition)) {
        left = PageSize.A4.getHeight() - (MARGIN_RIGHT + image.getWidth()); // remember that the page is A4 rotated
    }
    image.setAbsolutePosition(left, top);
}

From source file:it.eng.spagobi.engines.worksheet.exporter.WorkSheetPDFExporter.java

License:Mozilla Public License

private float[] getFooterTitlePosition(Image image, String imagePosition) {
    float llx, lly; // lower-left corner x and y position
    float urx, ury; // upper-right corner x and y position
    float imageHeight = image != null ? image.getHeight() : 0;
    float imageWidth = image != null ? image.getWidth() : 0;
    if (LEFT.equals(imagePosition)) {
        llx = MARGIN_LEFT + imageWidth + 30;
        lly = MARGIN_BOTTOM;// www.j  a va  2s.  c o m
        urx = PageSize.A4.getHeight() - MARGIN_RIGHT;
        ury = MARGIN_BOTTOM + Math.max(imageHeight, IMAGE_MAX_HEIGHT);
    } else if (RIGHT.equals(imagePosition)) {
        llx = MARGIN_LEFT;
        lly = MARGIN_BOTTOM;
        urx = PageSize.A4.getHeight() - (MARGIN_RIGHT + imageWidth + 30);
        ury = MARGIN_BOTTOM + Math.max(imageHeight, IMAGE_MAX_HEIGHT);
    } else { // CENTER case
        llx = MARGIN_LEFT;
        lly = MARGIN_BOTTOM + imageHeight;
        urx = PageSize.A4.getHeight() - MARGIN_RIGHT;
        ury = MARGIN_BOTTOM + imageHeight + TITLE_MAX_HEIGHT;
    }
    float[] toReturn = new float[] { llx, lly, urx, ury };
    return toReturn;
}

From source file:it.eng.spagobi.engines.worksheet.exporter.WorkSheetPDFExporter.java

License:Mozilla Public License

private void addChart(File imageFile, JSONObject content, float[] margins)
        throws MalformedURLException, IOException, DocumentException {
    Image jpg = Image.getInstance(imageFile.getPath());

    float topMargin = margins[0];
    float bottomMargin = margins[1];

    float chartMaxHeight = PageSize.A4.getWidth() - (topMargin + bottomMargin); // remember that the page is A4 rotated
    float chartMaxWidth = PageSize.A4.getHeight() - (MARGIN_LEFT + MARGIN_RIGHT); // remember that the page is A4 rotated

    float[] newDimensions = fitImage(jpg, chartMaxWidth, chartMaxHeight);

    float positionX = (PageSize.A4.getHeight() - newDimensions[0]) / 2;
    float positionY = bottomMargin + (chartMaxHeight - newDimensions[1]) / 2; // center the image into the available height
    jpg.setAbsolutePosition(positionX, positionY);
    pdfDocument.add(jpg);//w ww . j a  v  a  2 s  . c om
}