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"));
PDPage importedPage = new PDPage(new COSDictionary(page.getCOSObject()), resourceCache); PDStream dest = new PDStream(this, page.getContents(), COSName.FLATE_DECODE); importedPage.setContents(dest); addPage(importedPage); importedPage.setCropBox(page.getCropBox()); importedPage.setMediaBox(page.getMediaBox()); importedPage.setRotation(page.getRotation()); if (page.getResources() != null && !page.getCOSObject().containsKey(COSName.RESOURCES))
/** * This will return a list of the annotations for this page. * * @return List of the PDAnnotation objects, never null. The returned list is backed by the * annotations COSArray, so any adding or deleting in this list will change the document too. * * @throws IOException If there is an error while creating the annotation list. */ public List<PDAnnotation> getAnnotations() throws IOException { return getAnnotations(new AnnotationFilter() { @Override public boolean accept(PDAnnotation annotation) { return true; } }); }
String title = "This is my wonderful title!"; // Or whatever title you want. int marginTop = 30; // Or whatever margin you want. PDDocument document = new PDDocument(); PDPage page = new PDPage() PDPageStreamContent stream = new PDPageContentStream(document, page); PDFont font = PDType1Font.HELVETICA_BOLD; // Or whatever font you want. int fontSize = 16; // Or whatever font size you want. float titleWidth = font.getStringWidth(title) / 1000 * fontSize; float titleHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize; stream.beginText(); stream.setFont(font, fontSize); stream.moveTextPositionByAmount((page.getMediaBox().getWidth() - titleWidth) / 2, page.getMediaBox().getHeight - marginTop - titleheight); stream.drawString(title); stream.endText(); stream.close();
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");
if (source.getDocument().isClosed()) if (destination.getDocument().isClosed()) page.getCOSObject().removeItem(COSName.STRUCT_PARENTS); for (PDAnnotation ann : page.getAnnotations()) PDPage newPage = new PDPage((COSDictionary) cloner.cloneForNewDocument(page.getCOSObject())); if (!mergeStructTree) newPage.getCOSObject().removeItem(COSName.STRUCT_PARENTS); for (PDAnnotation ann : newPage.getAnnotations()) newPage.setCropBox(page.getCropBox()); newPage.setMediaBox(page.getMediaBox()); newPage.setRotation(page.getRotation()); PDResources resources = page.getResources(); if (resources != null) newPage.setResources(new PDResources((COSDictionary) cloner.cloneForNewDocument(resources))); newPage.setResources(new PDResources()); objMapping.put(page.getCOSObject(), newPage.getCOSObject()); List<PDAnnotation> oldAnnots = page.getAnnotations(); List<PDAnnotation> newAnnots = newPage.getAnnotations(); for (int i = 0; i < oldAnnots.size(); i++)
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); PDResources res = new PDResources(); 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); cs.setNonStrokingColor(Color.yellow); cs.addRect(-5000, -5000, 10000, 10000); cs.fill();
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); PDResources resources = new PDResources(); resources.put(COSName.getPDFName("Helv"), font); document.getDocumentCatalog().setAcroForm(acroForm); PDRectangle rect = new PDRectangle(50, 650, 200, 50); widget.setRectangle(rect); widget.setPage(page); page.getAnnotations().add(widget);
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();
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 document = new PDDocument(); PDPage page = new PDPage(); document.addPage(page); COSDictionary fdict = new COSDictionary(); fdict.setInt(COSName.FUNCTION_TYPE, 2); COSArray domain = new COSArray(); domain.add(COSInteger.get(0)); domain.add(COSInteger.get(1)); COSArray c0 = new COSArray(); c0.add(COSFloat.get("1")); c1.add(COSFloat.get("1")); c1.add(COSFloat.get("0.5")); fdict.setItem(COSName.DOMAIN, domain); fdict.setItem(COSName.C0, c0); fdict.setItem(COSName.C1, c1); 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);
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); widget.setAppearanceState("Off"); // don't forget this, or button will be invisible widgets.add(widget); page.getAnnotations().add(widget); try (PDPageContentStream contents = new PDPageContentStream(document, page))
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();
private void optimizedMergeDocuments(MemoryUsageSetting memUsageSetting) throws IOException try (PDDocument destination = new PDDocument(memUsageSetting)) sourceDoc = PDDocument.load((File) sourceObject, memUsageSetting); sourceDoc = PDDocument.load((InputStream) sourceObject, memUsageSetting); for (PDPage page : sourceDoc.getPages()) PDPage newPage = new PDPage( (COSDictionary) cloner.cloneForNewDocument(page.getCOSObject())); newPage.setCropBox(page.getCropBox()); newPage.setMediaBox(page.getMediaBox()); newPage.setRotation(page.getRotation()); PDResources resources = page.getResources(); if (resources != null) newPage.setResources(new PDResources( (COSDictionary) cloner.cloneForNewDocument(resources))); newPage.setResources(new PDResources()); destination.addPage(newPage);
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);
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); form.setResources(new PDResources()); form.setBBox(rect); form.setFormType(1); PDAppearanceDictionary appearance = new PDAppearanceDictionary(new COSDictionary()); appearance.setNormalAppearance(myDic); rubberStamp.setAppearance(appearance);
public static void main(String[] args) throws IOException try (PDDocument document = new PDDocument()) PDPage page = new PDPage(); document.addPage(page); 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);
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);