Java Code Examples for org.apache.pdfbox.pdmodel.PDPage#getRotation()
The following examples show how to use
org.apache.pdfbox.pdmodel.PDPage#getRotation() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PDFCreator.java From Knowage-Server with GNU Affero General Public License v3.0 | 7 votes |
private static void writeFrontpageDetails(PDDocument doc, PDFont font, float fontSize, FrontpageDetails details) throws IOException { String name = "Name: " + details.getName(); String description = "Description: " + details.getDescription(); String date = "Date: " + DEFAULT_DATE_FORMATTER.format(details.getDate()); PDPage page = doc.getPage(0); PDRectangle pageSize = page.getMediaBox(); float stringWidth = font.getStringWidth(StringUtilities.findLongest(name, description, date)) * fontSize / 1000f; float stringHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() * fontSize / 1000f; int rotation = page.getRotation(); boolean rotate = rotation == 90 || rotation == 270; float pageWidth = rotate ? pageSize.getHeight() : pageSize.getWidth(); float pageHeight = rotate ? pageSize.getWidth() : pageSize.getHeight(); float startX = rotate ? pageHeight / 3f : (pageWidth - stringWidth - stringHeight) / 3f; float startY = rotate ? (pageWidth - stringWidth) / 1f : pageWidth / 0.9f; // append the content to the existing stream try (PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true)) { // draw rectangle writeText(contentStream, new Color(4, 44, 86), font, fontSize, rotate, startX, startY, name, description, date); } }
Example 2
Source File: PDFRenderer.java From gcs with Mozilla Public License 2.0 | 6 votes |
private void transform(Graphics2D graphics, PDPage page, float scaleX, float scaleY) { graphics.scale(scaleX, scaleY); // TODO should we be passing the scale to PageDrawer rather than messing with Graphics? int rotationAngle = page.getRotation(); PDRectangle cropBox = page.getCropBox(); if (rotationAngle != 0) { float translateX = 0; float translateY = 0; switch (rotationAngle) { case 90: translateX = cropBox.getHeight(); break; case 270: translateY = cropBox.getWidth(); break; case 180: translateX = cropBox.getWidth(); translateY = cropBox.getHeight(); break; default: break; } graphics.translate(translateX, translateY); graphics.rotate(Math.toRadians(rotationAngle)); } }
Example 3
Source File: LegacyPDFStreamEngine.java From gcs with Mozilla Public License 2.0 | 6 votes |
/** * This will initialize and process the contents of the stream. * * @param page the page to process * @throws java.io.IOException if there is an error accessing the stream. */ @Override public void processPage(PDPage page) throws IOException { this.pageRotation = page.getRotation(); this.pageSize = page.getCropBox(); if (pageSize.getLowerLeftX() == 0 && pageSize.getLowerLeftY() == 0) { translateMatrix = null; } else { // translation matrix for cropbox translateMatrix = Matrix.getTranslateInstance(-pageSize.getLowerLeftX(), -pageSize.getLowerLeftY()); } super.processPage(page); }
Example 4
Source File: GenericSegment.java From pdfxtk with Apache License 2.0 | 6 votes |
public void rotate(PDPage page) { GenericSegment mediaBox = new GenericSegment(page.getMediaBox()); if (page.getRotation() != null) { rotate(mediaBox.getX1(), mediaBox.getY1(), page.getRotation()); if (page.getRotation() == 90 || page.getRotation() == -270) { x1 = x1 + mediaBox.getHeight(); x2 = x2 + mediaBox.getHeight(); } else if (page.getRotation() == 270 || page.getRotation() == -90) { y1 = y1 + mediaBox.getWidth(); y2 = y2 + mediaBox.getWidth(); } } }
Example 5
Source File: PDFCreator.java From Knowage-Server with GNU Affero General Public License v3.0 | 5 votes |
private static void writePageNumbering(PDDocument doc, PDFont font, float fontSize, PageNumbering pageNumbering) throws IOException { int totalPages = doc.getNumberOfPages(); int numberOfPages = pageNumbering.isLastIncluded() ? doc.getNumberOfPages() : doc.getNumberOfPages() - 1; for (int pageIndex = pageNumbering.isFirstIncluded() ? 0 : 1; pageIndex < numberOfPages; pageIndex++) { String footer = "Page " + (pageIndex + 1) + " of " + totalPages; PDPage page = doc.getPage(pageIndex); PDRectangle pageSize = page.getMediaBox(); float stringWidth = font.getStringWidth(footer) * fontSize / 1000f; float stringHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() * fontSize / 1000f; int rotation = page.getRotation(); boolean rotate = rotation == 90 || rotation == 270; float pageWidth = rotate ? pageSize.getHeight() : pageSize.getWidth(); float pageHeight = rotate ? pageSize.getWidth() : pageSize.getHeight(); float startX = rotate ? pageHeight / 2f : (pageWidth - stringWidth - stringHeight) / 2f; float startY = rotate ? (pageWidth - stringWidth) : stringHeight; // append the content to the existing stream try (PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true)) { // draw rectangle contentStream.setNonStrokingColor(255, 255, 255); // gray background // Draw a white filled rectangle drawRect(contentStream, Color.WHITE, new java.awt.Rectangle((int) startX, (int) startY - 3, (int) stringWidth + 2, (int) stringHeight), true); writeText(contentStream, new Color(4, 44, 86), font, fontSize, rotate, startX, startY, footer); } } }
Example 6
Source File: PDFPrintable.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * This will find the CropBox with rotation applied, for this page by looking up the hierarchy * until it finds them. * * @return The CropBox at this level in the hierarchy. */ static PDRectangle getRotatedCropBox(PDPage page) { PDRectangle cropBox = page.getCropBox(); int rotationAngle = page.getRotation(); if (rotationAngle == 90 || rotationAngle == 270) { return new PDRectangle(cropBox.getLowerLeftY(), cropBox.getLowerLeftX(), cropBox.getHeight(), cropBox.getWidth()); } else { return cropBox; } }
Example 7
Source File: PDFPrintable.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * This will find the MediaBox with rotation applied, for this page by looking up the hierarchy * until it finds them. * * @return The MediaBox at this level in the hierarchy. */ static PDRectangle getRotatedMediaBox(PDPage page) { PDRectangle mediaBox = page.getMediaBox(); int rotationAngle = page.getRotation(); if (rotationAngle == 90 || rotationAngle == 270) { return new PDRectangle(mediaBox.getLowerLeftY(), mediaBox.getLowerLeftX(), mediaBox.getHeight(), mediaBox.getWidth()); } else { return mediaBox; } }
Example 8
Source File: PDVisibleSignDesigner.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * Each page of document can be different sizes. This method calculates the page size based on * the page media box. * * @param document * @param page The 1-based page number for which the page size should be calculated. * @throws IllegalArgumentException if the page argument is lower than 0. */ private void calculatePageSize(PDDocument document, int page) { if (page < 1) { throw new IllegalArgumentException("First page of pdf is 1, not " + page); } PDPage firstPage = document.getPage(page - 1); PDRectangle mediaBox = firstPage.getMediaBox(); pageHeight(mediaBox.getHeight()); pageWidth = mediaBox.getWidth(); imageSizeInPercents = 100; rotation = firstPage.getRotation() % 360; }
Example 9
Source File: PdfComparator.java From pdfcompare with Apache License 2.0 | 5 votes |
public static ImageWithDimension renderPageAsImage(final PDDocument document, final PDFRenderer expectedPdfRenderer, final int pageIndex, Environment environment) throws IOException { final BufferedImage bufferedImage = expectedPdfRenderer.renderImageWithDPI(pageIndex, environment.getDPI()); final PDPage page = document.getPage(pageIndex); final PDRectangle mediaBox = page.getMediaBox(); if (page.getRotation() == 90 || page.getRotation() == 270) return new ImageWithDimension(bufferedImage, mediaBox.getHeight(), mediaBox.getWidth()); else return new ImageWithDimension(bufferedImage, mediaBox.getWidth(), mediaBox.getHeight()); }
Example 10
Source File: ObjectExtractor.java From tabula-java with MIT License | 5 votes |
protected Page extractPage(Integer pageNumber) throws IOException { if (pageNumber > this.pdfDocument.getNumberOfPages() || pageNumber < 1) { throw new java.lang.IndexOutOfBoundsException( "Page number does not exist"); } PDPage p = this.pdfDocument.getPage(pageNumber - 1); ObjectExtractorStreamEngine se = new ObjectExtractorStreamEngine(p); se.processPage(p); TextStripper pdfTextStripper = new TextStripper(this.pdfDocument, pageNumber); pdfTextStripper.process(); Utils.sort(pdfTextStripper.textElements, Rectangle.ILL_DEFINED_ORDER); float w, h; int pageRotation = p.getRotation(); if (Math.abs(pageRotation) == 90 || Math.abs(pageRotation) == 270) { w = p.getCropBox().getHeight(); h = p.getCropBox().getWidth(); } else { w = p.getCropBox().getWidth(); h = p.getCropBox().getHeight(); } return new Page(0, 0, w, h, pageRotation, pageNumber, p, pdfTextStripper.textElements, se.rulings, pdfTextStripper.minCharWidth, pdfTextStripper.minCharHeight, pdfTextStripper.spatialIndex); }
Example 11
Source File: PDFExtractor.java From inception with Apache License 2.0 | 4 votes |
public PDFExtractor(PDPage page, int pageIndex, Writer output) throws IOException { super(page); this.pageIndex = pageIndex; this.output = output; this.writeGlyphCoords = true; String path = "org/apache/pdfbox/resources/glyphlist/additional.txt"; InputStream input = GlyphList.class.getClassLoader().getResourceAsStream(path); this.glyphList = new GlyphList(GlyphList.getAdobeGlyphList(), input); this.pageRotation = page.getRotation(); this.pageSize = page.getCropBox(); if (this.pageSize.getLowerLeftX() == 0.0F && this.pageSize.getLowerLeftY() == 0.0F) { this.translateMatrix = null; } else { this.translateMatrix = Matrix.getTranslateInstance(-this.pageSize.getLowerLeftX(), -this.pageSize.getLowerLeftY()); } // taken from DrawPrintTextLocations for setting flipAT, rotateAT and transAT PDRectangle cropBox = page.getCropBox(); // flip y-axis flipAT = new AffineTransform(); flipAT.translate(0, page.getBBox().getHeight()); flipAT.scale(1, -1); // page may be rotated rotateAT = new AffineTransform(); int rotation = page.getRotation(); if (rotation != 0) { PDRectangle mediaBox = page.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)); } // cropbox transAT = AffineTransform .getTranslateInstance(-cropBox.getLowerLeftX(), cropBox.getLowerLeftY()); }
Example 12
Source File: PdfRenderer.java From gcs with Mozilla Public License 2.0 | 4 votes |
@Override protected void writeString(String text, List<TextPosition> textPositions) throws IOException { text = text.toLowerCase(); int index = text.indexOf(mTextToHighlight); if (index != -1) { PDPage currentPage = getCurrentPage(); PDRectangle pageBoundingBox = currentPage.getBBox(); AffineTransform flip = new AffineTransform(); flip.translate(0, pageBoundingBox.getHeight()); flip.scale(1, -1); PDRectangle mediaBox = currentPage.getMediaBox(); float mediaHeight = mediaBox.getHeight(); float mediaWidth = mediaBox.getWidth(); int size = textPositions.size(); while (index != -1) { int last = index + mTextToHighlight.length() - 1; for (int i = index; i <= last; i++) { TextPosition pos = textPositions.get(i); PDFont font = pos.getFont(); BoundingBox bbox = font.getBoundingBox(); Rectangle2D.Float rect = new Rectangle2D.Float(0, bbox.getLowerLeftY(), font.getWidth(pos.getCharacterCodes()[0]), bbox.getHeight()); AffineTransform at = pos.getTextMatrix().createAffineTransform(); if (font instanceof PDType3Font) { at.concatenate(font.getFontMatrix().createAffineTransform()); } else { at.scale(1 / 1000.0f, 1 / 1000.0f); } Shape shape = flip.createTransformedShape(at.createTransformedShape(rect)); AffineTransform transform = mGC.getTransform(); int rotation = currentPage.getRotation(); if (rotation != 0) { switch (rotation) { case 90: mGC.translate(mediaHeight, 0); break; case 270: mGC.translate(0, mediaWidth); break; case 180: mGC.translate(mediaWidth, mediaHeight); break; default: break; } mGC.rotate(Math.toRadians(rotation)); } mGC.fill(shape); if (rotation != 0) { mGC.setTransform(transform); } } index = last < size - 1 ? text.indexOf(mTextToHighlight, last + 1) : -1; } } }
Example 13
Source File: LayerUtility.java From gcs with Mozilla Public License 2.0 | 4 votes |
/** * Imports a page from some PDF file as a Form XObject so it can be placed on another page * in the target document. * <p> * You may want to call {@link #wrapInSaveRestore(PDPage) wrapInSaveRestore(PDPage)} before invoking the Form XObject to * make sure that the graphics state is reset. * * @param sourceDoc the source PDF document that contains the page to be copied * @param page the page in the source PDF document to be copied * @return a Form XObject containing the original page's content * @throws IOException if an I/O error occurs */ public PDFormXObject importPageAsForm(PDDocument sourceDoc, PDPage page) throws IOException { importOcProperties(sourceDoc); PDStream newStream = new PDStream(targetDoc, page.getContents(), COSName.FLATE_DECODE); PDFormXObject form = new PDFormXObject(newStream); //Copy resources PDResources pageRes = page.getResources(); PDResources formRes = new PDResources(); cloner.cloneMerge(pageRes, formRes); form.setResources(formRes); //Transfer some values from page to form transferDict(page.getCOSObject(), form.getCOSObject(), PAGE_TO_FORM_FILTER, true); Matrix matrix = form.getMatrix(); AffineTransform at = matrix.createAffineTransform(); PDRectangle mediaBox = page.getMediaBox(); PDRectangle cropBox = page.getCropBox(); PDRectangle viewBox = (cropBox != null ? cropBox : mediaBox); //Handle the /Rotation entry on the page dict int rotation = page.getRotation(); //Transform to FOP's user space //at.scale(1 / viewBox.getWidth(), 1 / viewBox.getHeight()); at.translate(mediaBox.getLowerLeftX() - viewBox.getLowerLeftX(), mediaBox.getLowerLeftY() - viewBox.getLowerLeftY()); switch (rotation) { case 90: at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth()); at.translate(0, viewBox.getWidth()); at.rotate(-Math.PI / 2.0); break; case 180: at.translate(viewBox.getWidth(), viewBox.getHeight()); at.rotate(-Math.PI); break; case 270: at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth()); at.translate(viewBox.getHeight(), 0); at.rotate(-Math.PI * 1.5); break; default: //no additional transformations necessary } //Compensate for Crop Boxes not starting at 0,0 at.translate(-viewBox.getLowerLeftX(), -viewBox.getLowerLeftY()); if (!at.isIdentity()) { form.setMatrix(at); } BoundingBox bbox = new BoundingBox(); bbox.setLowerLeftX(viewBox.getLowerLeftX()); bbox.setLowerLeftY(viewBox.getLowerLeftY()); bbox.setUpperRightX(viewBox.getUpperRightX()); bbox.setUpperRightY(viewBox.getUpperRightY()); form.setBBox(new PDRectangle(bbox)); return form; }
Example 14
Source File: CompatibilityHelper.java From pdfbox-layout with MIT License | 4 votes |
public static int getPageRotation(final PDPage page) { return page.getRotation(); }
Example 15
Source File: CompatibilityHelper.java From pdfbox-layout with MIT License | 4 votes |
public static int getPageRotation(final PDPage page) { return page.getRotation() == null ? 0 : page.getRotation(); }