Java Code Examples for org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm#setDefaultResources()

The following examples show how to use org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm#setDefaultResources() . 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: CreateSimpleFormWithEmbeddedFont.java    From blog-codes with Apache License 2.0 4 votes vote down vote up
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 2
Source File: TestEmptySignatureField.java    From testarea-pdfbox2 with Apache License 2.0 4 votes vote down vote up
/**
 * <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();
    }
}