Java Code Examples for org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget#setRectangle()
The following examples show how to use
org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget#setRectangle() .
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: AddFormField.java From testarea-pdfbox2 with Apache License 2.0 | 5 votes |
/** * <a href="https://stackoverflow.com/questions/46433388/pdfbox-could-not-find-font-helv"> * PDFbox Could not find font: /Helv * </a> * <br/> * <a href="https://drive.google.com/file/d/0B2--NSDOiujoR3hOZFYteUl2UE0/view?usp=sharing"> * 4.pdf * </a> * <p> * The cause is a combination of the OP and the source PDF not providing * a default appearance for the text field and PDFBox providing defaults * inconsequentially. * </p> * <p> * This is fixed here by setting the default appearance explicitly. * </p> */ @Test public void testAddFieldLikeEugenePodoliako() throws IOException { try ( InputStream originalStream = getClass().getResourceAsStream("4.pdf") ) { PDDocument pdf = Loader.loadPDF(originalStream); PDDocumentCatalog docCatalog = pdf.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); PDPage page = pdf.getPage(0); PDTextField textBox = new PDTextField(acroForm); textBox.setPartialName("SampleField"); acroForm.getFields().add(textBox); PDAnnotationWidget widget = textBox.getWidgets().get(0); PDRectangle rect = new PDRectangle(0, 0, 0, 0); widget.setRectangle(rect); widget.setPage(page); // Unnecessary code from OP // widget.setAppearance(acroForm.getFields().get(0).getWidgets().get(0).getAppearance()); // Fix added to set default appearance accordingly textBox.setDefaultAppearance(acroForm.getFields().get(0).getCOSObject().getString("DA")); widget.setPrinted(false); page.getAnnotations().add(widget); acroForm.refreshAppearances(); acroForm.flatten(); pdf.save(new File(RESULT_FOLDER, "4-add-field.pdf")); pdf.close(); } }
Example 2
Source File: CreateSimpleFormWithEmbeddedFont.java From blog-codes with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws IOException { // Create a new document with an empty page. try (PDDocument doc = new PDDocument()) { PDPage page = new PDPage(); doc.addPage(page); PDAcroForm acroForm = new PDAcroForm(doc); doc.getDocumentCatalog().setAcroForm(acroForm); // Note that the font is fully embedded. If you use a different font, make sure // that // its license allows full embedding. PDFont formFont = PDType0Font.load(doc, CreateSimpleFormWithEmbeddedFont.class .getResourceAsStream("/simhei.ttf"), false); // Add and set the resources and default appearance at the form level final PDResources resources = new PDResources(); acroForm.setDefaultResources(resources); final String fontName = resources.add(formFont).getName(); // Acrobat sets the font size on the form level to be // auto sized as default. This is done by setting the font size to '0' acroForm.setDefaultResources(resources); String defaultAppearanceString = "/" + fontName + " 0 Tf 0 g"; PDTextField textBox = new PDTextField(acroForm); textBox.setPartialName("SampleField"); textBox.setDefaultAppearance(defaultAppearanceString); acroForm.getFields().add(textBox); // Specify the widget annotation associated with the field PDAnnotationWidget widget = textBox.getWidgets().get(0); PDRectangle rect = new PDRectangle(50, 700, 200, 50); widget.setRectangle(rect); widget.setPage(page); page.getAnnotations().add(widget); // set the field value. Note that the last character is a turkish capital I with // a dot, // which is not part of WinAnsiEncoding textBox.setValue("Sample field 陌"); doc.save("/home/lili/data/SimpleFormWithEmbeddedFont.pdf"); } }
Example 3
Source File: AddFormFieldSaveIncremental.java From testarea-pdfbox2 with Apache License 2.0 | 4 votes |
/** * <a href="https://stackoverflow.com/questions/62601879/pdfbox-2-0-create-signature-field-and-save-incremental-with-already-signed-docum"> * PDFBox 2.0 create signature field and save incremental with already signed document * </a> * <br/> * <a href="https://drive.google.com/file/d/1jFfYNtf9YHi7eIXsdyqIeC-18CASyglT/view?usp=sharing"> * test-semnat.pdf * </a> * <p> * The remaining problem after the question update is that the OP replaces * the existing AcroForm definition. That changes too much, more than allowed * and more than he actually wants. What he wants is to retrieve the existing * AcroForm definition and only mark it and its Fields entry for saving. * </p> */ @Test public void testTestSemnat() throws IOException { try ( InputStream resource = getClass().getResourceAsStream("test-semnat.pdf"); ) { PDDocument document = Loader.loadPDF(resource); PDPage page = document.getPage(0); //instead of // // Add a new AcroForm and add that to the document // PDAcroForm acroForm = new PDAcroForm(document); // document.getDocumentCatalog().setAcroForm(acroForm); //use PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm(); acroForm.getCOSObject().setNeedToBeUpdated(true); COSObject fields = acroForm.getCOSObject().getCOSObject(COSName.FIELDS); if (fields != null) fields.setNeedToBeUpdated(true); // acroForm.setSignaturesExist(true); acroForm.setAppendOnly(true); acroForm.getCOSObject().setDirect(true); // Create empty signature field, it will get the name "Signature1" PDSignatureField signatureField = new PDSignatureField(acroForm); PDAnnotationWidget widget = signatureField.getWidgets().get(0); PDRectangle rect = new PDRectangle(50, 250, 200, 50); widget.setRectangle(rect); widget.getCOSObject().setNeedToBeUpdated(true); widget.setPage(page); page.getAnnotations().add(widget); page.getCOSObject().setNeedToBeUpdated(true); acroForm.getFields().add(signatureField); // general updates document.getDocumentCatalog().getCOSObject().setNeedToBeUpdated(true); OutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "test-semnat-Field.pdf")); document.saveIncremental(os); System.out.println("done"); } }
Example 4
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(); } }
Example 5
Source File: NativePdfBoxVisibleSignatureDrawer.java From dss with GNU Lesser General Public License v2.1 | 4 votes |
@Override public void draw() throws IOException { try (PDDocument doc = new PDDocument(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) { PDPage originalPage = document.getPage(parameters.getPage() - 1); SignatureFieldDimensionAndPositionBuilder dimensionAndPositionBuilder = new SignatureFieldDimensionAndPositionBuilder(parameters, originalPage, pdFont); SignatureFieldDimensionAndPosition dimensionAndPosition = dimensionAndPositionBuilder.build(); // create a new page PDPage page = new PDPage(originalPage.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); PDRectangle rectangle = getPdRectangle(dimensionAndPosition, page); widget.setRectangle(rectangle); PDStream stream = new PDStream(doc); PDFormXObject form = new PDFormXObject(stream); PDResources res = new PDResources(); form.setResources(res); form.setFormType(1); form.setBBox(new PDRectangle(rectangle.getWidth(), rectangle.getHeight())); PDAppearanceDictionary appearance = new PDAppearanceDictionary(); appearance.getCOSObject().setDirect(true); PDAppearanceStream appearanceStream = new PDAppearanceStream(form.getCOSObject()); appearance.setNormalAppearance(appearanceStream); widget.setAppearance(appearance); try (PDPageContentStream cs = new PDPageContentStream(doc, appearanceStream)) { rotateSignature(cs, rectangle, dimensionAndPosition); setFieldBackground(cs, parameters.getBackgroundColor()); setText(cs, dimensionAndPosition, parameters); setImage(cs, doc, dimensionAndPosition, parameters.getImage()); } doc.save(baos); try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) { signatureOptions.setVisualSignature(bais); signatureOptions.setPage(parameters.getPage() - 1); } } }