try doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); PDPageContentStream contentStream = new PDPageContentStream(doc, page); float leading = 1.5f * fontSize; PDRectangle mediabox = page.getMediaBox(); float margin = 72; float width = mediabox.getWidth() - 2*margin; float startX = mediabox.getLowerLeftX() + margin; float startY = mediabox.getUpperRightY() - margin; contentStream.beginText(); contentStream.setFont(pdfFont, fontSize); contentStream.newLineAtOffset(startX, startY); for (String line: lines) contentStream.close(); doc.save(new File(RESULT_FOLDER, "break-long-string.pdf")); doc.close();
@Override PDDocument open() throws IOException { return PDDocument.load(file, password); } };
//Using PDFBox library available from http://pdfbox.apache.org/ //Writes pdf document of specific pages as a new pdf file //Reads in pdf document PDDocument pdDoc = PDDocument.load(file); //Creates a new pdf document PDDocument document = null; //Adds specific page "i" where "i" is the page number and then saves the new pdf document try { document = new PDDocument(); document.addPage((PDPage) pdDoc.getDocumentCatalog().getAllPages().get(i)); document.save("file path"+"new document title"+".pdf"); document.close(); }catch(Exception e){}
try doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); PDPageContentStream contentStream = new PDPageContentStream(doc, page); float leading = 1.5f * fontSize; PDRectangle mediabox = page.findMediaBox(); float margin = 72; float width = mediabox.getWidth() - 2*margin; float startX = mediabox.getLowerLeftX() + margin; float startY = mediabox.getUpperRightY() - margin; contentStream.beginText(); contentStream.setFont(pdfFont, fontSize); contentStream.moveTextPositionByAmount(startX, startY); for (String line: lines) contentStream.close(); doc.save("break-long-string.pdf"); doc.close();
if (source.getDocument().isClosed()) if (destination.getDocument().isClosed()) PDDocumentCatalog destCatalog = destination.getDocumentCatalog(); PDDocumentCatalog srcCatalog = source.getDocumentCatalog(); if (isDynamicXfa(srcCatalog.getAcroForm())) PDDocumentInformation destInfo = destination.getDocumentInformation(); PDDocumentInformation srcInfo = source.getDocumentInformation(); mergeInto(srcInfo.getCOSObject(), destInfo.getCOSObject(), Collections.<COSName>emptySet()); float destVersion = destination.getVersion(); float srcVersion = source.getVersion(); destination.setVersion(srcVersion); if (destCatalog.getOpenAction() == null) try int destPageCount = destination.getNumberOfPages(); COSArray destNums; if (destLabels == null) destination.addPage(newPage);
private InputStream createVisualSignatureTemplate(PDDocument srcDoc, int pageNum, PDRectangle rect) throws IOException try (PDDocument doc = new PDDocument()) PDPage page = new PDPage(srcDoc.getPage(pageNum).getMediaBox()); doc.addPage(page); PDAcroForm acroForm = new PDAcroForm(doc); doc.getDocumentCatalog().setAcroForm(acroForm); PDSignatureField signatureField = new PDSignatureField(acroForm); PDAnnotationWidget widget = signatureField.getWidgets().get(0); List<PDField> acroFormFields = acroForm.getFields(); acroForm.setSignaturesExist(true); acroForm.setAppendOnly(true); acroForm.getCOSObject().setDirect(true); acroFormFields.add(signatureField); form.setResources(res); form.setFormType(1); PDRectangle bbox = new PDRectangle(rect.getWidth(), rect.getHeight()); float height = bbox.getHeight(); Matrix initialScale = null; switch (srcDoc.getPage(pageNum).getRotation()) try (PDPageContentStream cs = new PDPageContentStream(doc, appearanceStream)) cs.transform(initialScale); doc.save(baos); return new ByteArrayInputStream(baos.toByteArray());
public static void main(String[] args) throws IOException try (PDDocument document = new PDDocument()) PDPage page = new PDPage(PDRectangle.A4); document.addPage(page); PDAcroForm acroForm = new PDAcroForm(document); document.getDocumentCatalog().setAcroForm(acroForm); acroForm.setDefaultResources(resources); acroForm.setDefaultAppearance(defaultAppearanceString); PDRectangle rect = new PDRectangle(50, 650, 200, 50); widget.setRectangle(rect); widget.setPage(page); page.getAnnotations().add(widget); acroForm.getFields().add(signatureField); document.save(args[0]);
String message = args[1]; try (PDDocument doc = new PDDocument()) PDPage page = new PDPage(); doc.addPage(page); try (PDPageContentStream contents = new PDPageContentStream(doc, page)) contents.beginText(); contents.setFont(font, 12); contents.newLineAtOffset(100, 700); contents.showText(message); doc.save(filename);
public static void main(String[] args) throws IOException try (PDDocument document = new PDDocument()) PDPage page = new PDPage(PDRectangle.A4); document.addPage(page); PDAcroForm acroForm = new PDAcroForm(document); document.getDocumentCatalog().setAcroForm(acroForm); List<String> options = Arrays.asList("a", "b", "c"); PDRadioButton radioButton = new PDRadioButton(acroForm); radioButton.setExportValues(options); PDAppearanceCharacteristicsDictionary appearanceCharacteristics = new PDAppearanceCharacteristicsDictionary(new COSDictionary()); appearanceCharacteristics.setBorderColour(new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE)); appearanceCharacteristics.setBackground(new PDColor(new float[]{0, 1, 0.3f}, PDDeviceRGB.INSTANCE)); widget.setRectangle(new PDRectangle(30, PDRectangle.A4.getHeight() - 40 - i * 35, 30, 30)); widget.setAppearanceCharacteristics(appearanceCharacteristics); PDBorderStyleDictionary borderStyleDictionary = new PDBorderStyleDictionary(); widget.setPage(page); acroForm.getFields().add(radioButton); try (PDPageContentStream contents = new PDPageContentStream(document, page)) document.save("RadioButtonsSample.pdf");
private BufferedImage renderType3Glyph(PDType3Font font, int index) throws IOException PDDocument doc = new PDDocument(); int scale = 1; if (fontBBox.getWidth() < 72 || fontBBox.getHeight() < 72) scale = (int) (72 / Math.min(fontBBox.getWidth(), fontBBox.getHeight())); PDPage page = new PDPage(new PDRectangle(fontBBox.getWidth() * scale, fontBBox.getHeight() * scale)); page.setResources(resources); try try (PDPageContentStream cs = new PDPageContentStream(doc, page)) cs.transform(Matrix.getTranslateInstance(-fontBBox.getLowerLeftX(), -fontBBox.getLowerLeftY())); try cs.transform(new Matrix(at)); cs.endText(); doc.addPage(page); return new PDFRenderer(doc).renderImage(0); doc.close();
try document = new PDDocument(); PDPage page = new PDPage(); document.addPage(page); PDShadingType4 gouraudShading = new PDShadingType4(document.getDocument().createCOSStream()); gouraudShading.setShadingType(PDShading.SHADING_TYPE4); try (PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false)) contentStream.shadingFill(axialShading); contentStream.shadingFill(radialShading); contentStream.shadingFill(gouraudShading); document.save(file); document.close(); document = PDDocument.load(new File(file)); BufferedImage bim = new PDFRenderer(document).renderImageWithDPI(0, 100); ImageIO.write(bim, "png", new File(file + ".png")); document.close(); document.close();
String destPath = args[1]; try (PDDocument sourceDoc = PDDocument.load(new File(sourcePath))) try (PDDocument doc = new PDDocument()) PDPage page = new PDPage(); doc.addPage(page); try (PDPageContentStream contents = new PDPageContentStream(doc, page)) contents.beginText(); contents.setFont(PDType1Font.HELVETICA_BOLD, 12); contents.newLineAtOffset(2, PDRectangle.LETTER.getHeight() - 12); contents.showText("Sample text"); contents.endText(); contents.restoreGraphicsState(); doc.save(destPath);
try (PDDocument doc = new PDDocument()) PDPage page = new PDPage(PDRectangle.A4); page.setRotation(90); doc.addPage(page); PDRectangle pageSize = page.getMediaBox(); float pageWidth = pageSize.getWidth(); float fontSize = 12; float stringWidth = font.getStringWidth( message )*fontSize/1000f; float startY = 100; try (PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false)) contentStream.transform(new Matrix(0, 1, -1, 0, pageWidth, 0)); contentStream.setFont( font, fontSize ); contentStream.beginText(); contentStream.newLineAtOffset(startX, startY); doc.save( outfile );
try (PDDocument doc = new PDDocument()) true); PDRectangle rectangle = getPageSize(); float workablePageWidth = rectangle.getWidth() - 2 * MARGIN; float workablePageHeight = rectangle.getHeight() - 2 * MARGIN; PDPage page = new PDPage(getPageSize()); doc.addPage(page); try (PDPageContentStream contents = new PDPageContentStream(doc, page)) contents.beginText(); contents.setFont(font, FONT_SIZE); contents.newLineAtOffset(rectangle.getLowerLeftX() + MARGIN, rectangle.getUpperRightY() - MARGIN); doc.save(filename);
public static void main(String[] args) throws IOException try (PDDocument document = new PDDocument()) PDPage page = new PDPage(); document.addPage(page); PDAcroForm acroForm = new PDAcroForm(document); document.getDocumentCatalog().setAcroForm(acroForm); float y = page.getMediaBox().getHeight() - 50; PDRectangle rect = new PDRectangle(x, y, 20, 20); widget.setPrinted(true); PDAppearanceCharacteristicsDictionary appearanceCharacteristics = new PDAppearanceCharacteristicsDictionary(new COSDictionary()); appearanceCharacteristics.setBorderColour(new PDColor(new float[]{1, 0, 0}, PDDeviceRGB.INSTANCE)); appearanceCharacteristics.setBackground(new PDColor(new float[]{1, 1, 0}, PDDeviceRGB.INSTANCE)); normalAppearanceDict.setItem(COSName.Off, createRadioButtonAppearanceStream(document, widget, false)); normalAppearanceDict.setItem(COSName.YES, createRadioButtonAppearanceStream(document, widget, true)); page.getAnnotations().add(checkbox.getWidgets().get(0)); acroForm.getFields().add(checkbox); document.save("CheckBoxSample.pdf");
try (PDDocument document = PDDocument.load(new File(args[0]))) if( document.isEncrypted() ) for (int i = 0; i < document.getNumberOfPages(); i++) PDPage page = document.getPage(i); List<PDAnnotation> annotations = page.getAnnotations(); PDAnnotationRubberStamp rubberStamp = new PDAnnotationRubberStamp(); rubberStamp.setName(PDAnnotationRubberStamp.NAME_TOP_SECRET); rubberStamp.setRectangle(new PDRectangle(200,100)); rubberStamp.setContents("A top secret note"); int imgHeight = 25; PDRectangle rect = new PDRectangle(); rect.setLowerLeftX(lowerLeftX); rect.setLowerLeftY(lowerLeftY); rect.setUpperRightX(lowerLeftX + formWidth); PDAppearanceDictionary appearance = new PDAppearanceDictionary(new COSDictionary()); appearance.setNormalAppearance(myDic); rubberStamp.setAppearance(appearance); document.save( args[1] );
int pageCount = getNumberOfPages(); if (pageCount == 0) PDPage page = getPage(startIndex); PDDocumentCatalog catalog = getDocumentCatalog(); PDAcroForm acroForm = catalog.getAcroForm(); catalog.getCOSObject().setNeedToBeUpdated(true); acroForm.getCOSObject().setNeedToBeUpdated(true); if (!(acroForm.getCOSObject().getDictionaryObject(COSName.FIELDS) instanceof COSArray)) acroForm.getCOSObject().setItem(COSName.FIELDS, new COSArray()); COSArray fieldArray = (COSArray) acroForm.getCOSObject().getDictionaryObject(COSName.FIELDS); fieldArray.setNeedToBeUpdated(true); signatureField = findSignatureField(acroForm.getFieldIterator(), sigObject); boolean checkFields = checkSignatureField(acroForm.getFieldIterator(), signatureField); if (checkFields) prepareNonVisibleSignature(signatureField); return; prepareVisibleSignature(signatureField, acroForm, visualSignature); if (checkSignatureAnnotation(annotations, widget))
PDDocument extractedDocument = new PDDocument(); extractedDocument.setDocumentInformation(sourceDocument.getDocumentInformation()); extractedDocument.getDocumentCatalog().setViewerPreferences( sourceDocument.getDocumentCatalog().getViewerPreferences()); PDPage page = sourceDocument.getPage(i - 1); PDPage imported = extractedDocument.importPage(page); imported.setCropBox(page.getCropBox()); imported.setMediaBox(page.getMediaBox()); imported.setResources(page.getResources()); imported.setRotation(page.getRotation());
try (PDDocument doc = new PDDocument()) PDPage page = new PDPage(); doc.addPage(page); try (PDPageContentStream contents = new PDPageContentStream(doc, page)) contents.setNonStrokingColor(Color.CYAN); contents.addRect(0, 0, page.getMediaBox().getWidth(), page.getMediaBox().getHeight()); contents.fill(); contents.fill(); doc.save(filename);
public static void main(String[] args) throws IOException { String formTemplate = "src/main/resources/org/apache/pdfbox/examples/interactive/form/FillFormField.pdf"; try (PDDocument pdfDocument = PDDocument.load(new File(formTemplate))) { // get the document catalog PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm(); // as there might not be an AcroForm entry a null check is necessary if (acroForm != null) { // Retrieve an individual field and set its value. PDTextField field = (PDTextField) acroForm.getField( "sampleField" ); field.setValue("Text Entry"); // If a field is nested within the form tree a fully qualified name // might be provided to access the field. field = (PDTextField) acroForm.getField( "fieldsContainer.nestedSampleField" ); field.setValue("Text Entry"); } // Save and close the filled out form. pdfDocument.save("target/FillFormField.pdf"); } }