Java Code Examples for org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream#setResources()
The following examples show how to use
org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream#setResources() .
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: AppearanceGeneratorHelper.java From gcs with Mozilla Public License 2.0 | 6 votes |
private PDAppearanceStream prepareNormalAppearanceStream(PDAnnotationWidget widget) { PDAppearanceStream appearanceStream = new PDAppearanceStream(field.getAcroForm().getDocument()); // Calculate the entries for the bounding box and the transformation matrix // settings for the appearance stream int rotation = resolveRotation(widget); PDRectangle rect = widget.getRectangle(); Matrix matrix = Matrix.getRotateInstance(Math.toRadians(rotation), 0, 0); Point2D.Float point2D = matrix.transformPoint(rect.getWidth(), rect.getHeight()); PDRectangle bbox = new PDRectangle(Math.abs((float) point2D.getX()), Math.abs((float) point2D.getY())); appearanceStream.setBBox(bbox); AffineTransform at = calculateMatrix(bbox, rotation); if (!at.isIdentity()) { appearanceStream.setMatrix(at); } appearanceStream.setFormType(1); appearanceStream.setResources(new PDResources()); return appearanceStream; }
Example 2
Source File: TestEmptySignatureField.java From testarea-pdfbox2 with Apache License 2.0 | 4 votes |
/** * <a href="http://stackoverflow.com/questions/37601092/pdfbox-identify-specific-pages-and-functionalities-recommendations"> * PDFBox identify specific pages and functionalities recommendations * </a> * * <p> * This test shows how to add an empty signature field with a custom appearance * to an existing PDF. * </p> */ @Test public void testAddEmptySignatureField() throws IOException { try ( InputStream sourceStream = getClass().getResourceAsStream("test.pdf"); OutputStream output = new FileOutputStream(new File(RESULT_FOLDER, "test-with-empty-sig-field.pdf"))) { PDFont font = PDType1Font.HELVETICA; PDResources resources = new PDResources(); resources.put(COSName.getPDFName("Helv"), font); PDDocument document = Loader.loadPDF(sourceStream); PDAcroForm acroForm = new PDAcroForm(document); acroForm.setDefaultResources(resources); document.getDocumentCatalog().setAcroForm(acroForm); PDRectangle rect = new PDRectangle(50, 750, 200, 50); PDAppearanceDictionary appearanceDictionary = new PDAppearanceDictionary(); PDAppearanceStream appearanceStream = new PDAppearanceStream(document); appearanceStream.setBBox(rect.createRetranslatedRectangle()); appearanceStream.setResources(resources); appearanceDictionary.setNormalAppearance(appearanceStream); PDPageContentStream contentStream = new PDPageContentStream(document, appearanceStream); contentStream.setStrokingColor(Color.BLACK); contentStream.setNonStrokingColor(Color.LIGHT_GRAY); contentStream.setLineWidth(2); contentStream.addRect(0, 0, rect.getWidth(), rect.getHeight()); contentStream.fill(); contentStream.moveTo(1 * rect.getHeight() / 4, 1 * rect.getHeight() / 4); contentStream.lineTo(2 * rect.getHeight() / 4, 3 * rect.getHeight() / 4); contentStream.moveTo(1 * rect.getHeight() / 4, 3 * rect.getHeight() / 4); contentStream.lineTo(2 * rect.getHeight() / 4, 1 * rect.getHeight() / 4); contentStream.moveTo(3 * rect.getHeight() / 4, 1 * rect.getHeight() / 4); contentStream.lineTo(rect.getWidth() - rect.getHeight() / 4, 1 * rect.getHeight() / 4); contentStream.stroke(); contentStream.setNonStrokingColor(Color.DARK_GRAY); contentStream.beginText(); contentStream.setFont(font, rect.getHeight() / 5); contentStream.newLineAtOffset(3 * rect.getHeight() / 4, -font.getBoundingBox().getLowerLeftY() * rect.getHeight() / 5000); contentStream.showText("Customer"); contentStream.endText(); contentStream.close(); PDSignatureField signatureField = new PDSignatureField(acroForm); signatureField.setPartialName("SignatureField"); PDPage page = document.getPage(0); PDAnnotationWidget widget = signatureField.getWidgets().get(0); widget.setAppearance(appearanceDictionary); widget.setRectangle(rect); widget.setPage(page); page.getAnnotations().add(widget); acroForm.getFields().add(signatureField); document.save(output); document.close(); } }