Java Code Examples for com.itextpdf.text.pdf.PdfReader#getPageSize()
The following examples show how to use
com.itextpdf.text.pdf.PdfReader#getPageSize() .
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: Abstract2DPdfPageSplittingTool.java From testarea-itext5 with GNU Affero General Public License v3.0 | 7 votes |
void split(PdfReader reader, int page) throws IOException { PdfImportedPage importedPage = writer.getImportedPage(reader, page); Rectangle pageSizeToImport = reader.getPageSize(page); Iterable<Rectangle> rectangles = determineSplitRectangles(reader, page); for (Rectangle rectangle : rectangles) { newPage(rectangle); PdfContentByte directContent = writer.getDirectContent(); directContent.saveState(); directContent.rectangle(rectangle.getLeft(), rectangle.getBottom(), rectangle.getWidth(), rectangle.getHeight()); directContent.clip(); directContent.newPath(); writer.getDirectContent().addTemplate(importedPage, -pageSizeToImport.getLeft(), -pageSizeToImport.getBottom()); directContent.restoreState(); } }
Example 2
Source File: AbstractPdfPageSplittingTool.java From testarea-itext5 with GNU Affero General Public License v3.0 | 6 votes |
void split(PdfReader reader, int page) throws IOException { PdfImportedPage importedPage = writer.getImportedPage(reader, page); PdfContentByte directContent = writer.getDirectContent(); yPosition = pageSize.getTop(); Rectangle pageSizeToImport = reader.getPageSize(page); float[] borderPositions = determineSplitPositions(reader, page); if (borderPositions == null || borderPositions.length < 2) return; for (int borderIndex = 0; borderIndex + 1 < borderPositions.length; borderIndex++) { float height = borderPositions[borderIndex] - borderPositions[borderIndex + 1]; if (height <= 0) continue; directContent.saveState(); directContent.rectangle(0, yPosition - height, pageSizeToImport.getWidth(), height); directContent.clip(); directContent.newPath(); writer.getDirectContent().addTemplate(importedPage, 0, yPosition - (borderPositions[borderIndex] - pageSizeToImport.getBottom())); directContent.restoreState(); newPage(); } }
Example 3
Source File: SimpleRedactionTest.java From testarea-itext5 with GNU Affero General Public License v3.0 | 6 votes |
static byte[] createRotatedIndirectTextPdf() throws DocumentException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, baos); document.open(); PdfReader reader = new PdfReader(createSimpleTextPdf()); PdfImportedPage template = writer.getImportedPage(reader, 1); Rectangle pageSize = reader.getPageSize(1); writer.getDirectContent().addTemplate(template, .7f, .7f, -.7f, .7f, 400, -200); document.newPage(); writer.getDirectContent().addTemplate(template, pageSize.getLeft(), pageSize.getBottom()); document.close(); return baos.toByteArray(); }
Example 4
Source File: SimpleRedactionTest.java From testarea-itext5 with GNU Affero General Public License v3.0 | 6 votes |
static byte[] createMultiUseIndirectTextPdf() throws DocumentException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, baos); document.open(); PdfReader reader = new PdfReader(createSimpleTextPdf()); PdfImportedPage template = writer.getImportedPage(reader, 1); Rectangle pageSize = reader.getPageSize(1); writer.getDirectContent().addTemplate(template, 0, .7f, -.7f, 0, pageSize.getRight(), (pageSize.getTop() + pageSize.getBottom()) / 2); writer.getDirectContent().addTemplate(template, 0, .7f, -.7f, 0, pageSize.getRight(), pageSize.getBottom()); document.newPage(); writer.getDirectContent().addTemplate(template, pageSize.getLeft(), pageSize.getBottom()); document.close(); return baos.toByteArray(); }
Example 5
Source File: RedactText.java From testarea-itext5 with GNU Affero General Public License v3.0 | 6 votes |
/** * <a href="http://stackoverflow.com/questions/38278816/remove-header-of-a-pdf-using-itext-pdfcleanupprocessor-does-not-work"> * Remove header of a pdf using iText PdfCleanUpProcessor does not work * </a> * <br/> * <a href="https://www.dropbox.com/s/4u8vupjqc4st3ib/love.pdf?dl=0"> * love.pdf * </a> * <p> * Cannot reproduce, I get a <code>org.apache.commons.imaging.ImageReadException: Invalid marker found in entropy data</code>. * </p> */ @Test public void testRedactLikeShiranSEkanayake() throws IOException, DocumentException { try ( InputStream resource = getClass().getResourceAsStream("love.pdf"); OutputStream result = new FileOutputStream(new File(OUTPUTDIR, "love-redacted.pdf")) ) { PdfReader reader = new PdfReader(resource); PdfStamper stamper = new PdfStamper(reader, result); List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<PdfCleanUpLocation>(); for(int i=1; i<=reader.getNumberOfPages(); i++) { //System.out.println(i); Rectangle mediabox = reader.getPageSize(i); cleanUpLocations.add(new PdfCleanUpLocation(i, new Rectangle(0,800,1000,1000))); } PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(cleanUpLocations, stamper); cleaner.cleanUp(); stamper.close(); reader.close(); } }
Example 6
Source File: EnlargePagePart.java From testarea-itext5 with GNU Affero General Public License v3.0 | 6 votes |
/** * <a href="http://stackoverflow.com/questions/35374110/how-do-i-use-itext-to-have-a-landscaped-pdf-on-half-of-a-a4-back-to-portrait-and"> * How do i use iText to have a landscaped PDF on half of a A4 back to portrait and full size on A4 * </a> * <p> * This sample shows how to rotate and enlarge the upper half of an A4 page to fit into a new A4 page. * </p> */ @Test public void testRotateAndZoomUpperHalfPage() throws IOException, DocumentException { try ( InputStream resource = getClass().getResourceAsStream("/mkl/testarea/itext5/extract/test.pdf"); OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "test-upperHalf.pdf")) ) { PdfReader reader = new PdfReader(resource); Document document = new Document(PageSize.A4); PdfWriter writer = PdfWriter.getInstance(document, result); document.open(); double sqrt2 = Math.sqrt(2); Rectangle pageSize = reader.getPageSize(1); PdfImportedPage importedPage = writer.getImportedPage(reader, 1); writer.getDirectContent().addTemplate(importedPage, 0, sqrt2, -sqrt2, 0, pageSize.getTop() * sqrt2, -pageSize.getLeft() * sqrt2); document.close(); } }
Example 7
Source File: SwitchPageCanvas.java From testarea-itext5 with GNU Affero General Public License v3.0 | 6 votes |
/** * <a href="http://stackoverflow.com/questions/34394199/i-cant-rotate-my-page-from-existing-pdf"> * I can't rotate my page from existing PDF * </a> * <p> * Switching between portrait and landscape like this obviously will cut off some parts of the page. * </p> */ @Test public void testSwitchOrientation() throws DocumentException, IOException { try (InputStream resourceStream = getClass().getResourceAsStream("/mkl/testarea/itext5/extract/n2013.00849449.pdf")) { PdfReader reader = new PdfReader(resourceStream); int n = reader.getNumberOfPages(); PdfDictionary pageDict; for (int i = 1; i <= n; i++) { Rectangle rect = reader.getPageSize(i); Rectangle crop = reader.getCropBox(i); pageDict = reader.getPageN(i); pageDict.put(PdfName.MEDIABOX, new PdfArray(new float[] {rect.getBottom(), rect.getLeft(), rect.getTop(), rect.getRight()})); pageDict.put(PdfName.CROPBOX, new PdfArray(new float[] {crop.getBottom(), crop.getLeft(), crop.getTop(), crop.getRight()})); } PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "n2013.00849449-switch.pdf"))); stamper.close(); reader.close(); } }
Example 8
Source File: TestTrimPdfPage.java From testarea-itext5 with GNU Affero General Public License v3.0 | 6 votes |
@Test public void testWithWriter() throws DocumentException, IOException { InputStream resourceStream = getClass().getResourceAsStream("test.pdf"); try { PdfReader reader = new PdfReader(resourceStream); Rectangle pageSize = reader.getPageSize(1); Rectangle rect = getOutputPageSize(pageSize, reader, 1); Document document = new Document(rect, 0, 0, 0, 0); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "test-trimmed-writer.pdf"))); document.open(); PdfImportedPage page; // Go through all pages int n = reader.getNumberOfPages(); for (int i = 1; i <= n; i++) { document.newPage(); page = writer.getImportedPage(reader, i); System.out.println("BBox: "+ page.getBoundingBox().toString()); Image instance = Image.getInstance(page); document.add(instance); Rectangle outputPageSize = document.getPageSize(); System.out.println(outputPageSize.toString()); } document.close(); } finally { if (resourceStream != null) resourceStream.close(); } }
Example 9
Source File: HideContent.java From testarea-itext5 with GNU Affero General Public License v3.0 | 6 votes |
/** * <a href="http://stackoverflow.com/questions/43870545/filling-a-pdf-with-itextsharp-and-then-hiding-the-base-layer"> * Filling a PDF with iTextsharp and then hiding the base layer * </a> * <p> * This test shows how to cover all content using a white rectangle. * </p> */ @Test public void testHideContenUnderRectangle() throws IOException, DocumentException { try ( InputStream resource = getClass().getResourceAsStream("document.pdf"); OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "document-hiddenContent.pdf"))) { PdfReader pdfReader = new PdfReader(resource); PdfStamper pdfStamper = new PdfStamper(pdfReader, result); for (int page = 1; page <= pdfReader.getNumberOfPages(); page++) { Rectangle pageSize = pdfReader.getPageSize(page); PdfContentByte canvas = pdfStamper.getOverContent(page); canvas.setColorFill(BaseColor.WHITE); canvas.rectangle(pageSize.getLeft(), pageSize.getBottom(), pageSize.getWidth(), pageSize.getHeight()); canvas.fill(); } pdfStamper.close(); } }
Example 10
Source File: SplitPages.java From testarea-itext5 with GNU Affero General Public License v3.0 | 6 votes |
/** * <a href="https://stackoverflow.com/questions/46466747/how-to-split-a-pdf-page-in-java"> * How to split a PDF page in java? * </a> * <p> * This test shows how to split the pages of a document into tiles of A6 * size using the {@link Abstract2DPdfPageSplittingTool}. * </p> */ @Test public void testSplitDocumentA6() throws IOException, DocumentException { try (InputStream resource = getClass().getResourceAsStream("document.pdf"); OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "document-A6.pdf"))) { Abstract2DPdfPageSplittingTool tool = new Abstract2DPdfPageSplittingTool() { @Override protected Iterable<Rectangle> determineSplitRectangles(PdfReader reader, int page) { Rectangle targetSize = PageSize.A6; List<Rectangle> rectangles = new ArrayList<>(); Rectangle pageSize = reader.getPageSize(page); for (float y = pageSize.getTop(); y > pageSize.getBottom() + 5; y-=targetSize.getHeight()) { for (float x = pageSize.getLeft(); x < pageSize.getRight() - 5; x+=targetSize.getWidth()) { rectangles.add(new Rectangle(x, y - targetSize.getHeight(), x + targetSize.getWidth(), y)); } } return rectangles; } }; tool.split(result, new PdfReader(resource)); } }
Example 11
Source File: PdfDenseMergeTool.java From testarea-itext5 with GNU Affero General Public License v3.0 | 6 votes |
void merge(PdfReader reader, PdfReaderContentParser parser, int page) throws IOException { TextMarginFinder finder = parser.processContent(page, new TextMarginFinder()); Rectangle pageSizeToImport = reader.getPageSize(page); float heightToImport = finder.getHeight(); float maxHeight = pageSize.getHeight() - topMargin - bottomMargin; if (heightToImport > maxHeight) { throw new IllegalArgumentException(String.format("Page %s content too large; height: %s, limit: %s.", page, heightToImport, maxHeight)); } if (heightToImport > yPosition - pageSize.getBottom(bottomMargin)) { newPage(); } else if (!writer.isPageEmpty()) { heightToImport += gap; } yPosition -= heightToImport; PdfImportedPage importedPage = writer.getImportedPage(reader, page); writer.getDirectContent().addTemplate(importedPage, 0, yPosition - (finder.getLly() - pageSizeToImport.getBottom())); }
Example 12
Source File: TestTrimPdfPage.java From testarea-itext5 with GNU Affero General Public License v3.0 | 5 votes |
@Test public void testWithStamper() throws DocumentException, IOException { InputStream resourceStream = getClass().getResourceAsStream("test.pdf"); try { PdfReader reader = new PdfReader(resourceStream); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "test-trimmed-stamper.pdf"))); // Go through all pages int n = reader.getNumberOfPages(); for (int i = 1; i <= n; i++) { Rectangle pageSize = reader.getPageSize(i); Rectangle rect = getOutputPageSize(pageSize, reader, i); PdfDictionary page = reader.getPageN(i); page.put(PdfName.CROPBOX, new PdfArray(new float[]{rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop()})); stamper.markUsed(page); } stamper.close(); } finally { if (resourceStream != null) resourceStream.close(); } }
Example 13
Source File: TestTrimPdfPage.java From testarea-itext5 with GNU Affero General Public License v3.0 | 5 votes |
@Test public void testWithStamperTopBottom() throws DocumentException, IOException { InputStream resourceStream = getClass().getResourceAsStream("test.pdf"); try { PdfReader reader = new PdfReader(resourceStream); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "test-trimmed-stamper-top-bottom.pdf"))); // Go through all pages int n = reader.getNumberOfPages(); for (int i = 1; i <= n; i++) { Rectangle pageSize = reader.getPageSize(i); Rectangle rect = getOutputPageSize2(pageSize, reader, i); PdfDictionary page = reader.getPageN(i); page.put(PdfName.CROPBOX, new PdfArray(new float[]{rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop()})); stamper.markUsed(page); } stamper.close(); } finally { if (resourceStream != null) resourceStream.close(); } }
Example 14
Source File: TestTrimPdfPage.java From testarea-itext5 with GNU Affero General Public License v3.0 | 5 votes |
@Test public void testWithStamperCentered() throws DocumentException, IOException { InputStream resourceStream = getClass().getResourceAsStream("test.pdf"); try { PdfReader reader = new PdfReader(resourceStream); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "test-trimmed-stamper-centered.pdf"))); // Go through all pages int n = reader.getNumberOfPages(); for (int i = 1; i <= n; i++) { Rectangle pageSize = reader.getPageSize(i); Rectangle rect = getOutputPageSize3(pageSize, reader, i); PdfDictionary page = reader.getPageN(i); page.put(PdfName.CROPBOX, new PdfArray(new float[]{rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop()})); stamper.markUsed(page); } stamper.close(); } finally { if (resourceStream != null) resourceStream.close(); } }
Example 15
Source File: TestTrimPdfPage.java From testarea-itext5 with GNU Affero General Public License v3.0 | 5 votes |
@Test public void testWithStamperExtFinder() throws DocumentException, IOException { InputStream resourceStream = getClass().getResourceAsStream("test.pdf"); try { PdfReader reader = new PdfReader(resourceStream); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "test-trimmed-stamper-ext.pdf"))); // Go through all pages int n = reader.getNumberOfPages(); for (int i = 1; i <= n; i++) { Rectangle pageSize = reader.getPageSize(i); Rectangle rect = getOutputPageSize4(pageSize, reader, i); PdfDictionary page = reader.getPageN(i); page.put(PdfName.CROPBOX, new PdfArray(new float[]{rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop()})); stamper.markUsed(page); } stamper.close(); } finally { if (resourceStream != null) resourceStream.close(); } }
Example 16
Source File: ExtractSuperAndSubInLine.java From testarea-itext5 with GNU Affero General Public License v3.0 | 4 votes |
void markLineBoundaries(String resource, int startPage, int endPage) throws IOException, DocumentException { String name = new File(resource).getName(); String target = String.format("%s-lines-%s-%s.pdf", name, startPage, endPage); InputStream resourceStream = getClass().getResourceAsStream(resource); try { PdfReader reader = new PdfReader(resourceStream); PdfReaderContentParser parser = new PdfReaderContentParser(reader); System.out.printf("\nLine boundaries in %s\n", name); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, target))); for (int page = startPage; page < endPage; page++) { System.out.printf("\n Page %s\n ", page); TextLineFinder finder = new TextLineFinder(); parser.processContent(page, finder); PdfContentByte over = stamper.getOverContent(page); Rectangle mediaBox = reader.getPageSize(page); for (float flip: finder.verticalFlips) { System.out.printf(" %s", flip); over.moveTo(mediaBox.getLeft(), flip); over.lineTo(mediaBox.getRight(), flip); } System.out.println(); over.stroke(); } stamper.close(); } finally { if (resourceStream != null) resourceStream.close(); } }
Example 17
Source File: ShrinkRotated.java From testarea-itext5 with GNU Affero General Public License v3.0 | 4 votes |
/** * <a href="https://stackoverflow.com/questions/48601115/pdf-shrink-causing-change-in-orientation"> * PDF Shrink causing change in Orientation * </a> * <p> * Sample.PDF - retrieved via e-mail * </p> * <p> * This issue is just another result of iText adding rotations to the * transformation matrices at the start of undercontent and overcontent * of rotated pages: Due to the nature of the literal the OP adds to * the undercontent, this rotation bleeds through and also affects the * original content. * </p> */ @Test public void testShrinkSample() throws IOException, DocumentException { float xPercentage = 0.3F; float yPercentage = 0.6F; try ( InputStream resource = getClass().getResourceAsStream("Sample.PDF"); OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "Sample-shrunken.PDF"))) { PdfReader reader = new PdfReader(resource); PdfStamper stamper = new PdfStamper(reader, result); //vvv stamper.setRotateContents(false); //^^^ for (int p = 1; p <= 1; p++) { float offsetX = (reader.getPageSize(p).getWidth() * (1 - xPercentage)) / 2; float offsetY = (reader.getPageSize(p).getHeight() * (1 - yPercentage)) / 2; PdfDictionary page; PdfArray crop; PdfArray media; page = reader.getPageN(p); System.out.println("reader.getPateRoatation-->"+reader.getPageRotation(p)); media = page.getAsArray(PdfName.CROPBOX); if (media == null) { media = page.getAsArray(PdfName.MEDIABOX); } crop = new PdfArray(); crop.add(new PdfNumber(0)); crop.add(new PdfNumber(0)); crop.add(new PdfNumber(media.getAsNumber(2).floatValue())); crop.add(new PdfNumber(media.getAsNumber(3).floatValue())); page.put(PdfName.MEDIABOX, crop); page.put(PdfName.CROPBOX, crop); Rectangle mediabox = reader.getPageSize(p); stamper.getUnderContent(p).setLiteral( String.format("\nq %s %s %s %s %s %s cm\nq\n", xPercentage, mediabox.getLeft(),mediabox.getBottom(), yPercentage, offsetX, offsetY)); stamper.getOverContent(p).setLiteral("\nQ\nQ\n"); } stamper.close(); reader.close(); } }
Example 18
Source File: PdfVeryDenseMergeTool.java From testarea-itext5 with GNU Affero General Public License v3.0 | 4 votes |
void merge(PdfReader reader, PdfReaderContentParser parser, int page) throws IOException { PdfImportedPage importedPage = writer.getImportedPage(reader, page); PdfContentByte directContent = writer.getDirectContent(); PageVerticalAnalyzer finder = parser.processContent(page, new PageVerticalAnalyzer()); if (finder.verticalFlips.size() < 2) return; Rectangle pageSizeToImport = reader.getPageSize(page); int startFlip = finder.verticalFlips.size() - 1; boolean first = true; while (startFlip > 0) { if (!first) newPage(); float freeSpace = yPosition - pageSize.getBottom(bottomMargin); int endFlip = startFlip + 1; while ((endFlip > 1) && (finder.verticalFlips.get(startFlip) - finder.verticalFlips.get(endFlip - 2) < freeSpace)) endFlip -=2; if (endFlip < startFlip) { float height = finder.verticalFlips.get(startFlip) - finder.verticalFlips.get(endFlip); directContent.saveState(); directContent.rectangle(0, yPosition - height, pageSizeToImport.getWidth(), height); directContent.clip(); directContent.newPath(); writer.getDirectContent().addTemplate(importedPage, 0, yPosition - (finder.verticalFlips.get(startFlip) - pageSizeToImport.getBottom())); directContent.restoreState(); yPosition -= height + gap; startFlip = endFlip - 1; } else if (!first) throw new IllegalArgumentException(String.format("Page %s content sections too large.", page)); first = false; } }