Example usage for org.apache.pdfbox.pdmodel PDPage getRotation

List of usage examples for org.apache.pdfbox.pdmodel PDPage getRotation

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDPage getRotation.

Prototype

public int getRotation() 

Source Link

Document

Returns the rotation angle in degrees by which the page should be rotated clockwise when displayed or printed.

Usage

From source file:com.vns.pdf.impl.PdfDocument.java

License:Apache License

private List<Annotation> parseAnnotation(PDPage pdPage) throws IOException {
    List<Annotation> annotations = new ArrayList<>();
    for (PDAnnotation annt : pdPage.getAnnotations()) {
        if (annt instanceof PDAnnotationLink) {
            PDAnnotationLink link = (PDAnnotationLink) annt;
            PDRectangle rect = link.getRectangle();
            float x = rect.getLowerLeftX();
            float y = rect.getUpperRightY();
            float width = rect.getWidth();
            float height = rect.getHeight();
            int rotation = pdPage.getRotation();
            if (rotation == 0) {
                PDRectangle pageSize = pdPage.getMediaBox();
                y = pageSize.getHeight() - y;
            } else if (rotation == 90) {
                //do nothing
            }/* w ww.  j a  va 2s. c o  m*/

            ActionData actionData = parsePDAction(link.getAction());
            if (actionData == null) {
                actionData = parsePDDestination(link.getDestination());
            }
            if (actionData != null) {
                Annotation a = new Annotation(x, y, width, height, actionData.destX, actionData.destY,
                        actionData.destPage, actionData.destZoom);
                annotations.add(a);
            }
        }
    }
    return annotations;
}

From source file:com.yiyihealth.tools.test.DrawPrintTextLocations.java

License:Apache License

private void stripPage(int page) throws IOException {
    PDFRenderer pdfRenderer = new PDFRenderer(document);
    image = pdfRenderer.renderImage(page, SCALE);

    PDPage pdPage = document.getPage(page);
    PDRectangle cropBox = pdPage.getCropBox();

    // flip y-axis
    flipAT = new AffineTransform();
    flipAT.translate(0, pdPage.getBBox().getHeight());
    flipAT.scale(1, -1);/*from  w  ww .j av a  2  s .  com*/

    // page may be rotated
    rotateAT = new AffineTransform();
    int rotation = pdPage.getRotation();
    if (rotation != 0) {
        PDRectangle mediaBox = pdPage.getMediaBox();
        switch (rotation) {
        case 90:
            rotateAT.translate(mediaBox.getHeight(), 0);
            break;
        case 270:
            rotateAT.translate(0, mediaBox.getWidth());
            break;
        case 180:
            rotateAT.translate(mediaBox.getWidth(), mediaBox.getHeight());
            break;
        default:
            break;
        }
        rotateAT.rotate(Math.toRadians(rotation));
    }

    g2d = image.createGraphics();
    g2d.setStroke(new BasicStroke(0.1f));
    g2d.scale(SCALE, SCALE);

    setStartPage(page + 1);
    setEndPage(page + 1);

    Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
    writeText(document, dummy);

    // beads in green
    g2d.setStroke(new BasicStroke(0.4f));
    List<PDThreadBead> pageArticles = pdPage.getThreadBeads();
    for (PDThreadBead bead : pageArticles) {
        PDRectangle r = bead.getRectangle();
        GeneralPath p = r
                .transform(Matrix.getTranslateInstance(-cropBox.getLowerLeftX(), cropBox.getLowerLeftY()));

        Shape s = flipAT.createTransformedShape(p);
        s = rotateAT.createTransformedShape(s);
        g2d.setColor(Color.green);
        g2d.draw(s);
    }

    g2d.dispose();

    String imageFilename = filename;
    int pt = imageFilename.lastIndexOf('.');
    imageFilename = imageFilename.substring(0, pt) + "-marked-" + (page + 1) + ".png";
    ImageIO.write(image, "png", new File(imageFilename));
}

From source file:org.apache.fop.render.pdf.pdfbox.PDFUtil.java

License:Apache License

/**
 * Determines the rotation of a given page and normalizes the returned value to the values
 * 0, 90, 180 and 270. If not a multiple of 90 is encountered, 0 is returned.
 * @param page the page/*from  www  .  ja va  2 s. co  m*/
 * @return the page rotation (0, 90, 180 or 270)
 */
public static int getNormalizedRotation(PDPage page) {
    //Handle the /Rotation entry on the page dict
    int rotation = page.getRotation();
    rotation %= 360;
    if (rotation < 0) {
        rotation += 360;
    }
    switch (rotation) {
    case 90:
    case 180:
    case 270:
        return rotation;
    default:
        return 0;
    }
}

From source file:org.oscarehr.document.web.SplitDocumentAction.java

License:Open Source License

public ActionForward rotate180(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    Document doc = documentDAO.getDocument(request.getParameter("document"));

    //      String docdownload = oscar.OscarProperties.getInstance().getProperty("DOCUMENT_DIR");
    String docdownload = EDocUtil.getDocumentPath(doc.getDocfilename());

    if (doc.getContenttype().equals("application/pdf")) {
        FileInputStream input = null;
        PDDocument pdf = null;//from   w w w.ja  va2s. co m
        try {
            //      FileInputStream input = new FileInputStream(docdownload + doc.getDocfilename());
            input = new FileInputStream(docdownload);
            PDFParser parser = new PDFParser(input);
            parser.parse();
            pdf = parser.getPDDocument();
            int x = 1;
            for (Object p : pdf.getDocumentCatalog().getAllPages()) {
                PDPage pg = (PDPage) p;
                Integer r = (pg.getRotation() != null ? pg.getRotation() : 0);
                pg.setRotation((r + 180) % 360);

                ManageDocumentAction.deleteCacheVersion(doc, x);
                x++;
            }

            //      pdf.save(docdownload + doc.getDocfilename());
            pdf.save(docdownload);

        } finally {
            if (pdf != null)
                pdf.close();
            input.close();
        }

    } else if (doc.getContenttype().equals("image/jpg") || doc.getContenttype().equals("image/png")
            || doc.getContenttype().equals("image/gif")) {
        String documentDir = EDocUtil.getDocumentDir(doc.getDocfilename());
        File file = new File(documentDir + doc.getDocfilename());
        BufferedImage image = ImageIO.read(file);
        if (image == null)
            return null;
        BufferedImage rotatedImage = new BufferedImage(image.getHeight(), image.getWidth(),
                BufferedImage.TYPE_INT_ARGB);

        String suffix = null;
        String contentType = doc.getContenttype();
        if (contentType.equalsIgnoreCase("image/jpg") || contentType.equalsIgnoreCase("image/jpeg")) {
            suffix = "jpg";
        } else if (contentType.equalsIgnoreCase("image/png")) {
            suffix = "png";
        } else if (contentType.equalsIgnoreCase("image/gif")) {
            suffix = "gif";
        }
        AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
        tx = AffineTransform.getScaleInstance(-1, -1);
        tx.translate(-image.getWidth(null), -image.getHeight(null));
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
        image = op.filter(image, null);
        ImageIO.write(image, suffix, file);
    } else {
        //umknown type - does nothing
    }

    return null;
}

From source file:org.oscarehr.document.web.SplitDocumentAction.java

License:Open Source License

public ActionForward rotate90(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    Document doc = documentDAO.getDocument(request.getParameter("document"));

    FileInputStream input = new FileInputStream(EDocUtil.getDocumentPath(doc.getDocfilename()));
    PDFParser parser = new PDFParser(input);
    parser.parse();//  www. jav a  2s  .c  o m
    PDDocument pdf = parser.getPDDocument();
    int x = 1;
    for (Object p : pdf.getDocumentCatalog().getAllPages()) {
        PDPage pg = (PDPage) p;
        Integer r = (pg.getRotation() != null ? pg.getRotation() : 0);
        pg.setRotation((r + 90) % 360);

        ManageDocumentAction.deleteCacheVersion(doc, x);
        x++;
    }

    pdf.save(EDocUtil.getDocumentPath(doc.getDocfilename()));
    pdf.close();

    input.close();

    return null;

}

From source file:org.pdfsam.pdfbox.component.PdfRotator.java

License:Open Source License

/**
 * apply the rotation to the given page if necessary
 * //from  www. j  a  v a  2  s .  c  o  m
 * @param pageNmber
 */
private void apply(int pageNmber) {
    PDPage page = document.getPage(pageNmber - 1);
    page.setRotation(rotation.addRotation(getRotation(page.getRotation())).getDegrees());
}

From source file:org.pdfsam.pdfbox.component.PdfRotatorTest.java

License:Open Source License

@Test
public void singlePage() {
    PDDocument document = mock(PDDocument.class);
    PDPage page = mock(PDPage.class);
    when(page.getRotation()).thenReturn(180);
    when(document.getPage(2)).thenReturn(page);
    applyRotation(Rotation.DEGREES_270, Collections.singleton(3)).to(document);
    verify(page).setRotation(90);/*w  ww .  j  a  v  a  2  s.c o m*/
}

From source file:org.pdfsam.pdfbox.component.PdfRotatorTest.java

License:Open Source License

@Test
public void multiplePages() {
    PDDocument document = mock(PDDocument.class);
    PDPage page1 = mock(PDPage.class);
    when(page1.getRotation()).thenReturn(180);
    when(document.getPage(0)).thenReturn(page1);
    PDPage page2 = mock(PDPage.class);
    when(page2.getRotation()).thenReturn(90);
    when(document.getPage(1)).thenReturn(page2);
    when(document.getNumberOfPages()).thenReturn(2);
    applyRotation(Rotation.DEGREES_270, new HashSet<>(Arrays.asList(1, 2))).to(document);
    verify(page1).setRotation(90);/*  ww  w. ja va  2  s  . c  o  m*/
    verify(page2).setRotation(0);
}

From source file:org.xwiki.test.misc.PDFTest.java

License:Open Source License

/**
 * Code adapted from http://www.docjar.com/html/api/org/apache/pdfbox/examples/pdmodel/PrintURLs.java.html
 *//*from   w  ww.  j av  a  2 s . c  o  m*/
private Map<String, PDAction> extractLinks(PDPage page) throws Exception {
    Map<String, PDAction> links = new HashMap<String, PDAction>();
    PDFTextStripperByArea stripper = new PDFTextStripperByArea();
    List<PDAnnotation> annotations = page.getAnnotations();
    // First setup the text extraction regions.
    for (int j = 0; j < annotations.size(); j++) {
        PDAnnotation annotation = annotations.get(j);
        if (annotation instanceof PDAnnotationLink) {
            PDAnnotationLink link = (PDAnnotationLink) annotation;
            PDRectangle rect = link.getRectangle();
            // Need to reposition link rectangle to match text space.
            float x = rect.getLowerLeftX();
            float y = rect.getUpperRightY();
            float width = rect.getWidth();
            float height = rect.getHeight();
            int rotation = page.getRotation();
            if (rotation == 0) {
                PDRectangle pageSize = page.getMediaBox();
                y = pageSize.getHeight() - y;
            } else if (rotation == 90) {
                // Do nothing.
            }

            Rectangle2D.Float awtRect = new Rectangle2D.Float(x, y, width, height);
            stripper.addRegion(String.valueOf(j), awtRect);
        }
    }

    stripper.extractRegions(page);

    for (int j = 0; j < annotations.size(); j++) {
        PDAnnotation annotation = annotations.get(j);
        if (annotation instanceof PDAnnotationLink) {
            PDAnnotationLink link = (PDAnnotationLink) annotation;
            String label = stripper.getTextForRegion(String.valueOf(j)).trim();
            links.put(label, link.getAction());
        }
    }

    return links;
}

From source file:src.controller.PageController.java

/**
 * Tourne la page spcifie//from   ww  w  .  j  a  va 2s  .  c om
 * @param document
 * @param id
 * @param degree 
 */
public void rotatePage(PDDocument document, int id, int degree) {
    PDPage page = document.getPage(id);
    page.setRotation(page.getRotation() + degree);
}