PdfReader reader = new PdfReader("src/main/resources/pages.pdf"); show(reader.getPageSize(1)); show(reader.getPageSize(3)); show(reader.getPageSizeWithRotation(3)); show(reader.getPageSize(4)); show(reader.getPageSizeWithRotation(4));
/** Gets the page size without taking rotation into account. This * is the value of the /MediaBox key. * @param index the page number. The first page is 1 * @return the page size */ public Rectangle getPageSize(int index) { return getPageSize(pageRefs.getPageNRelease(index)); }
/** Gets the page size without taking rotation into account. This * is the value of the /MediaBox key. * @param index the page number. The first page is 1 * @return the page size */ public Rectangle getPageSize(int index) { return getPageSize(pageRefs.getPageNRelease(index)); }
/** * Gets the page size without taking rotation into account. This is the value * of the /MediaBox key. * * @param index * the page number. The first page is 1 * @return the page size */ public Rectangle getPageSize(int index) { return getPageSize(pageRefs.getPageNRelease(index)); }
public void addBlankPage(PdfReader reader) { pdfCopy.addPage(reader.getPageSize(1), reader.getPageRotation(1)); numberOfCopiedPages++; }
/** * Gets the rotated page from a page dictionary. * @param page the page dictionary * @return the rotated page */ public Rectangle getPageSizeWithRotation(PdfDictionary page) { Rectangle rect = getPageSize(page); int rotation = getPageRotation(page); while (rotation > 0) { rect = rect.rotate(); rotation -= 90; } return rect; }
/** * Gets the rotated page from a page dictionary. * @param page the page dictionary * @return the rotated page */ public Rectangle getPageSizeWithRotation(PdfDictionary page) { Rectangle rect = getPageSize(page); int rotation = getPageRotation(page); while (rotation > 0) { rect = rect.rotate(); rotation -= 90; } return rect; }
/** * Gets the rotated page from a page dictionary. * * @param page * the page dictionary * @return the rotated page */ public Rectangle getPageSizeWithRotation(PdfDictionary page) { Rectangle rect = getPageSize(page); int rotation = getPageRotation(page); while (rotation > 0) { rect = rect.rotate(); rotation -= 90; } return rect; }
public void manipulatePdf(String src, String dest) throws IOException, DocumentException { PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); Phrase header = new Phrase("Copy", new Font(FontFamily.HELVETICA, 14)); for (int i = 1; i <= reader.getNumberOfPages(); i++) { float x = reader.getPageSize(i).getWidth() / 2; float y = reader.getPageSize(i).getTop(20); ColumnText.showTextAligned( stamper.getOverContent(i), Element.ALIGN_CENTER, header, x, y, 0); } stamper.close(); reader.close(); }
public void manipulatePdf(String src, String dest) throws IOException, DocumentException { PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); int n = reader.getNumberOfPages(); float percentage = 0.8f; for (int p = 1; p <= n; p++) { float offsetX = (reader.getPageSize(p).getWidth() * (1 - percentage)) / 2; float offsetY = (reader.getPageSize(p).getHeight() * (1 - percentage)) / 2; stamper.getUnderContent(p).setLiteral( String.format("\nq %s 0 0 %s %s %s cm\nq\n", percentage, percentage, offsetX, offsetY)); stamper.getOverContent(p).setLiteral("\nQ\nQ\n"); } stamper.close(); reader.close(); }
PdfReader reader = new PdfReader(pdf); //yout.toByteArray()); Rectangle psize = reader.getPageSize(1); doc = new Document(psize, 0, 0, 0, 0); PdfWriter writer = PdfWriter.getInstance(doc, output); doc.open(); PdfContentByte cb = writer.getDirectContent(); for (int i = 0; i < reader.getNumberOfPages(); i++) { writer.setCropBoxSize(psize); PdfImportedPage page = writer.getImportedPage(reader, i + 1); cb.addTemplate(page, 0, 0); }
PdfImportedPage(PdfReaderInstance readerInstance, PdfWriter writer, int pageNumber) { this.readerInstance = readerInstance; this.pageNumber = pageNumber; this.writer = writer; bBox = readerInstance.getReader().getPageSize(pageNumber); setMatrix(1, 0, 0, 1, -bBox.getLeft(), -bBox.getBottom()); type = TYPE_IMPORTED; }
PdfImportedPage(PdfReaderInstance readerInstance, PdfWriter writer, int pageNumber) { this.readerInstance = readerInstance; this.pageNumber = pageNumber; this.writer = writer; bBox = readerInstance.getReader().getPageSize(pageNumber); setMatrix(1, 0, 0, 1, -bBox.getLeft(), -bBox.getBottom()); type = TYPE_IMPORTED; }
PdfImportedPage(PdfReaderInstance readerInstance, PdfWriter writer, int pageNumber) { this.readerInstance = readerInstance; this.pageNumber = pageNumber; this.writer = writer; bBox = readerInstance.getReader().getPageSize(pageNumber); setMatrix(1, 0, 0, 1, -bBox.getLeft(), -bBox.getBottom()); type = TYPE_IMPORTED; }
public static void main(String[] args) throws IOException { String pdfFile = args[0]; PdfReader reader = new PdfReader(pdfFile); AcroFields fields = reader.getAcroFields(); for(String signame : fields.getBlankSignatureNames()) { List<AcroFields.FieldPosition> positions = fields.getFieldPositions(signame); Rectangle rect = positions.get(0).position; // In points: float left = rect.getLeft(); float bTop = rect.getTop(); float width = rect.getWidth(); float height = rect.getHeight(); int page = positions.get(0).page; Rectangle pageSize = reader.getPageSize(page); float pageHeight = pageSize.getTop(); float top = pageHeight - bTop; System.out.print(signame + "::" + page + "::" + left + "::" + top + "::" + width + "::" + height + "\n"); } }
/** Gets the crop box without taking rotation into account. This * is the value of the /CropBox key. The crop box is the part * of the document to be displayed or printed. It usually is the same * as the media box but may be smaller. If the page doesn't have a crop * box the page size will be returned. * @param index the page number. The first page is 1 * @return the crop box */ public Rectangle getCropBox(int index) { PdfDictionary page = pageRefs.getPageNRelease(index); PdfArray cropBox = (PdfArray)getPdfObjectRelease(page.get(PdfName.CROPBOX)); if (cropBox == null) return getPageSize(page); return getNormalizedRectangle(cropBox); }
/** Gets the crop box without taking rotation into account. This * is the value of the /CropBox key. The crop box is the part * of the document to be displayed or printed. It usually is the same * as the media box but may be smaller. If the page doesn't have a crop * box the page size will be returned. * @param index the page number. The first page is 1 * @return the crop box */ public Rectangle getCropBox(int index) { PdfDictionary page = pageRefs.getPageNRelease(index); PdfArray cropBox = (PdfArray)getPdfObjectRelease(page.get(PdfName.CROPBOX)); if (cropBox == null) return getPageSize(page); return getNormalizedRectangle(cropBox); }
PdfReader reader = new PdfReader(resourceStream); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("target/test-outputs/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();
/** * Gets the crop box without taking rotation into account. This is the value * of the /CropBox key. The crop box is the part of the document to be * displayed or printed. It usually is the same as the media box but may be * smaller. If the page doesn't have a crop box the page size will be * returned. * * @param index * the page number. The first page is 1 * @return the crop box */ public Rectangle getCropBox(int index) { PdfDictionary page = pageRefs.getPageNRelease(index); PdfArray cropBox = (PdfArray) getPdfObjectRelease(page.get(PdfName.CROPBOX)); if (cropBox == null) return getPageSize(page); return getNormalizedRectangle(cropBox); }
PdfReader reader = new PdfReader(SOURCE); 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(RESULT)); stamper.close(); reader.close();