Example usage for com.lowagie.text Rectangle getRotation

List of usage examples for com.lowagie.text Rectangle getRotation

Introduction

In this page you can find the example usage for com.lowagie.text Rectangle getRotation.

Prototype

public int getRotation() 

Source Link

Document

Gets the rotation of the rectangle

Usage

From source file:clases.unirPdf.java

public static void concatPDFs(List<InputStream> streamOfPDFFiles, OutputStream outputStream, boolean paginate) {

    Document document = new Document();
    try {/* w  ww .  ja v a  2s  .co  m*/
        List<InputStream> pdfs = streamOfPDFFiles;
        List<PdfReader> readers = new ArrayList<PdfReader>();
        int totalPages = 0;
        Iterator<InputStream> iteratorPDFs = pdfs.iterator();

        while (iteratorPDFs.hasNext()) {
            InputStream pdf = iteratorPDFs.next();
            PdfReader pdfReader = new PdfReader(pdf);
            readers.add(pdfReader);
            totalPages += pdfReader.getNumberOfPages();
        }

        PdfWriter writer = PdfWriter.getInstance(document, outputStream);

        document.open();
        PdfContentByte cb = writer.getDirectContent();

        PdfImportedPage page;
        int currentPageNumber = 0;
        int pageOfCurrentReaderPDF = 0;
        Iterator<PdfReader> iteratorPDFReader = readers.iterator();

        while (iteratorPDFReader.hasNext()) {
            PdfReader pdfReader = iteratorPDFReader.next();

            while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {

                Rectangle rectangle = pdfReader.getPageSizeWithRotation(1);
                document.setPageSize(rectangle);
                document.newPage();

                pageOfCurrentReaderPDF++;
                currentPageNumber++;
                page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
                switch (rectangle.getRotation()) {
                case 0:
                    cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
                    break;
                case 90:
                    cb.addTemplate(page, 0, -1f, 1f, 0, 0, pdfReader.getPageSizeWithRotation(1).getHeight());
                    break;
                case 180:
                    cb.addTemplate(page, -1f, 0, 0, -1f, 0, 0);
                    break;
                case 270:
                    cb.addTemplate(page, 0, 1.0F, -1.0F, 0, pdfReader.getPageSizeWithRotation(1).getWidth(), 0);
                    break;
                default:
                    break;
                }
                if (paginate) {
                    cb.beginText();
                    cb.getPdfDocument().getPageSize();
                    cb.endText();
                }
            }
            pageOfCurrentReaderPDF = 0;
        }
        outputStream.flush();
        document.close();
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (document.isOpen())
            document.close();
        try {
            if (outputStream != null)
                outputStream.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

From source file:org.jaffa.modules.printing.services.FormPrintEngineIText.java

License:Open Source License

/**
 * Any work to start off printing the document
 * @throws FormPrintException Thrown if there is any form processing problems
 *//*from ww w.ja  v  a  2s . c o m*/
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();

}

From source file:org.jaffa.modules.printing.services.FormPrintEngineIText.java

License:Open Source License

/**
 * 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./*from   w ww .j  av  a  2  s.co m*/
 * @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 + " ]");
}

From source file:org.jpedal.examples.simpleviewer.utils.ItextFunctions.java

License:Open Source License

public void nup(int pageCount, PdfPageData currentPageData, ExtractPDFPagesNup extractPage) {

    try {/* ww  w  . ja v a  2s  .co  m*/

        int[] pgsToEdit = extractPage.getPages();

        if (pgsToEdit == null)
            return;

        //get user choice
        final String output_dir = extractPage.getRootDir() + separator + fileName + separator + "PDFs"
                + separator;

        File testDirExists = new File(output_dir);
        if (!testDirExists.exists())
            testDirExists.mkdirs();

        List pagesToEdit = new ArrayList();
        for (int i = 0; i < pgsToEdit.length; i++)
            pagesToEdit.add(new Integer(pgsToEdit[i]));

        PdfReader reader = new PdfReader(selectedFile);

        File fileToSave = new File(output_dir + "export_" + fileName + ".pdf");

        if (fileToSave.exists()) {
            int n = currentGUI.showOverwriteDialog(fileToSave.getAbsolutePath(), false);

            if (n == 0) {
                // clicked yes so just carry on
            } else {
                // clicked no, so exit
                return;
            }
        }

        int rows = extractPage.getLayoutRows();
        int coloumns = extractPage.getLayoutColumns();

        int paperWidth = extractPage.getPaperWidth();
        int paperHeight = extractPage.getPaperHeight();

        Rectangle pageSize = new Rectangle(paperWidth, paperHeight);

        String orientation = extractPage.getPaperOrientation();

        Rectangle newSize = null;
        if (orientation.equals(Messages.getMessage("PdfViewerNUPOption.Auto"))) {
            if (coloumns > rows)
                newSize = new Rectangle(pageSize.height(), pageSize.width());
            else
                newSize = new Rectangle(pageSize.width(), pageSize.height());
        } else if (orientation.equals("Portrait")) {
            newSize = new Rectangle(pageSize.width(), pageSize.height());
        } else if (orientation.equals("Landscape")) {
            newSize = new Rectangle(pageSize.height(), pageSize.width());
        }

        String scale = extractPage.getScale();

        float leftRightMargin = extractPage.getLeftRightMargin();
        float topBottomMargin = extractPage.getTopBottomMargin();
        float horizontalSpacing = extractPage.getHorizontalSpacing();
        float verticalSpacing = extractPage.getVerticalSpacing();

        Rectangle unitSize = null;
        if (scale.equals("Auto")) {
            float totalHorizontalSpacing = (coloumns - 1) * horizontalSpacing;

            int totalWidth = (int) (newSize.width() - leftRightMargin * 2 - totalHorizontalSpacing);
            int unitWidth = totalWidth / coloumns;

            float totalVerticalSpacing = (rows - 1) * verticalSpacing;

            int totalHeight = (int) (newSize.height() - topBottomMargin * 2 - totalVerticalSpacing);
            int unitHeight = totalHeight / rows;

            unitSize = new Rectangle(unitWidth, unitHeight);

        } else if (scale.equals("Use Original Size")) {
            unitSize = null;
        } else if (scale.equals("Specified")) {
            unitSize = new Rectangle(extractPage.getScaleWidth(), extractPage.getScaleHeight());
        }

        int order = extractPage.getPageOrdering();

        int pagesPerPage = rows * coloumns;

        int repeats = 1;
        if (extractPage.getRepeat() == REPEAT_AUTO)
            repeats = coloumns * rows;
        else if (extractPage.getRepeat() == REPEAT_SPECIFIED)
            repeats = extractPage.getCopies();

        Document document = new Document(newSize, 0, 0, 0, 0);

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileToSave));

        document.open();

        PdfContentByte cb = writer.getDirectContent();
        PdfImportedPage importedPage;
        float offsetX = 0, offsetY = 0, factor;
        int actualPage = 0, page = 0;
        Rectangle currentSize;

        boolean isProportional = extractPage.isScaleProportional();

        for (int i = 1; i <= pageCount; i++) {
            if (pagesToEdit.contains(new Integer(i))) {
                for (int j = 0; j < repeats; j++) {

                    int currentUnit = page % pagesPerPage;

                    if (currentUnit == 0) {
                        document.newPage();
                        actualPage++;
                    }

                    currentSize = reader.getPageSizeWithRotation(i);
                    if (unitSize == null)
                        unitSize = currentSize;

                    int currentColoumn = 0, currentRow = 0;
                    if (order == ORDER_DOWN) {
                        currentColoumn = currentUnit / rows;
                        currentRow = currentUnit % rows;

                        offsetX = unitSize.width() * currentColoumn;
                        offsetY = newSize.height() - (unitSize.height() * (currentRow + 1));

                    } else if (order == ORDER_ACCROS) {
                        currentColoumn = currentUnit % coloumns;
                        currentRow = currentUnit / coloumns;

                        offsetX = unitSize.width() * currentColoumn;
                        offsetY = newSize.height() - (unitSize.height() * ((currentUnit / coloumns) + 1));

                    }

                    factor = Math.min(unitSize.width() / currentSize.width(),
                            unitSize.height() / currentSize.height());

                    float widthFactor = factor, heightFactor = factor;
                    if (!isProportional) {
                        widthFactor = unitSize.width() / currentSize.width();
                        heightFactor = unitSize.height() / currentSize.height();
                    } else {
                        offsetX += ((unitSize.width() - (currentSize.width() * factor)) / 2f);
                        offsetY += ((unitSize.height() - (currentSize.height() * factor)) / 2f);
                    }

                    offsetX += (horizontalSpacing * currentColoumn) + leftRightMargin;
                    offsetY -= ((verticalSpacing * currentRow) + topBottomMargin);

                    importedPage = writer.getImportedPage(reader, i);

                    double rotation = currentSize.getRotation() * Math.PI / 180;

                    /**
                     * see 
                     * http://itextdocs.lowagie.com/tutorial/directcontent/coordinates/index.html 
                     * for information about transformation matrices, and the coordinate system
                     */

                    int mediaBoxX = -currentPageData.getMediaBoxX(i);
                    int mediaBoxY = -currentPageData.getMediaBoxY(i);

                    float a, b, c, d, e, f;
                    switch (currentSize.getRotation()) {
                    case 0:
                        a = widthFactor;
                        b = 0;
                        c = 0;
                        d = heightFactor;
                        e = offsetX + (mediaBoxX * widthFactor);
                        f = offsetY + (mediaBoxY * heightFactor);

                        cb.addTemplate(importedPage, a, b, c, d, e, f);

                        break;
                    case 90:
                        a = 0;
                        b = (float) (Math.sin(rotation) * -heightFactor);
                        c = (float) (Math.sin(rotation) * widthFactor);
                        d = 0;
                        e = offsetX + (mediaBoxY * widthFactor);
                        f = ((currentSize.height() * heightFactor) + offsetY) - (mediaBoxX * heightFactor);

                        cb.addTemplate(importedPage, a, b, c, d, e, f);

                        break;
                    case 180:
                        a = (float) (Math.cos(rotation) * widthFactor);
                        b = 0;
                        c = 0;
                        d = (float) (Math.cos(rotation) * heightFactor);
                        e = (offsetX + (currentSize.width() * widthFactor)) - (mediaBoxX * widthFactor);
                        f = ((currentSize.height() * heightFactor) + offsetY) - (mediaBoxY * heightFactor);

                        cb.addTemplate(importedPage, a, b, c, d, e, f);

                        break;
                    case 270:
                        a = 0;
                        b = (float) (Math.sin(rotation) * -heightFactor);
                        c = (float) (Math.sin(rotation) * widthFactor);
                        d = 0;
                        e = (offsetX + (currentSize.width() * widthFactor)) - (mediaBoxY * widthFactor);
                        f = offsetY + (mediaBoxX * heightFactor);

                        cb.addTemplate(importedPage, a, b, c, d, e, f);

                        break;
                    }

                    page++;
                }
            }
        }

        document.close();

        currentGUI.showMessageDialog(
                Messages.getMessage("PdfViewerMessage.PagesSavedAsPdfTo") + " " + output_dir);

    } catch (Exception e) {

        e.printStackTrace();

    }
}