use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary in project pdfbox by apache.
the class PDAbstractAppearanceHandler method getRolloverAppearance.
/**
* Get the annotations rollover appearance.
*
* <p>
* This will get the annotations rollover appearance. If this is not
* existent an empty appearance entry will be created.
*
* @return the appearance entry representing the rollover appearance.
*/
PDAppearanceEntry getRolloverAppearance() {
PDAppearanceDictionary appearanceDictionary = getAppearance();
PDAppearanceEntry rolloverAppearanceEntry = appearanceDictionary.getRolloverAppearance();
if (rolloverAppearanceEntry.isSubDictionary()) {
// TODO replace with "document.getDocument().createCOSStream()"
rolloverAppearanceEntry = new PDAppearanceEntry(new COSStream());
appearanceDictionary.setRolloverAppearance(rolloverAppearanceEntry);
}
return rolloverAppearanceEntry;
}
use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary in project pdfbox by apache.
the class PDAbstractAppearanceHandler method getNormalAppearance.
/**
* Get the annotations normal appearance.
*
* <p>
* This will get the annotations normal appearance. If this is not existent
* an empty appearance entry will be created.
*
* @return the appearance entry representing the normal appearance.
*/
private PDAppearanceEntry getNormalAppearance() {
PDAppearanceDictionary appearanceDictionary = getAppearance();
PDAppearanceEntry normalAappearanceEntry = appearanceDictionary.getNormalAppearance();
if (normalAappearanceEntry.isSubDictionary()) {
// TODO replace with "document.getDocument().createCOSStream()"
normalAappearanceEntry = new PDAppearanceEntry(new COSStream());
appearanceDictionary.setNormalAppearance(normalAappearanceEntry);
}
return normalAappearanceEntry;
}
use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary in project pdfbox by apache.
the class PDVisibleSigBuilder method createAppearanceDictionary.
@Override
public void createAppearanceDictionary(PDFormXObject holderForml, PDSignatureField signatureField) throws IOException {
PDAppearanceDictionary appearance = new PDAppearanceDictionary();
appearance.getCOSObject().setDirect(true);
PDAppearanceStream appearanceStream = new PDAppearanceStream(holderForml.getCOSObject());
appearance.setNormalAppearance(appearanceStream);
signatureField.getWidgets().get(0).setAppearance(appearance);
pdfStructure.setAppearanceDictionary(appearance);
LOG.info("PDF appearance dictionary has been created");
}
use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary in project pdfbox by apache.
the class CreateVisibleSignature2 method createVisualSignatureTemplate.
// create a template PDF document with empty signature and return it as a stream.
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);
widget.setRectangle(rect);
// from PDVisualSigBuilder.createHolderForm()
PDStream stream = new PDStream(doc);
PDFormXObject form = new PDFormXObject(stream);
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()) {
case 90:
form.setMatrix(AffineTransform.getQuadrantRotateInstance(1));
initialScale = Matrix.getScaleInstance(bbox.getWidth() / bbox.getHeight(), bbox.getHeight() / bbox.getWidth());
height = bbox.getWidth();
break;
case 180:
form.setMatrix(AffineTransform.getQuadrantRotateInstance(2));
break;
case 270:
form.setMatrix(AffineTransform.getQuadrantRotateInstance(3));
initialScale = Matrix.getScaleInstance(bbox.getWidth() / bbox.getHeight(), bbox.getHeight() / bbox.getWidth());
height = bbox.getWidth();
break;
case 0:
default:
break;
}
form.setBBox(bbox);
PDFont font = PDType1Font.HELVETICA_BOLD;
// from PDVisualSigBuilder.createAppearanceDictionary()
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)) {
// why does scale have no effect when done in the form matrix???
if (initialScale != null) {
cs.transform(initialScale);
}
// show background (just for debugging, to see the rect size + position)
cs.setNonStrokingColor(Color.yellow);
cs.addRect(-5000, -5000, 10000, 10000);
cs.fill();
// show background image
// save and restore graphics if the image is too large and needs to be scaled
cs.saveGraphicsState();
cs.transform(Matrix.getScaleInstance(0.25f, 0.25f));
PDImageXObject img = PDImageXObject.createFromFileByExtension(imageFile, doc);
cs.drawImage(img, 0, 0);
cs.restoreGraphicsState();
// show text
float fontSize = 10;
float leading = fontSize * 1.5f;
cs.beginText();
cs.setFont(font, fontSize);
cs.setNonStrokingColor(Color.black);
cs.newLineAtOffset(fontSize, height - leading);
cs.setLeading(leading);
cs.showText("(Signature very wide line 1)");
cs.newLine();
cs.showText("(Signature very wide line 2)");
cs.newLine();
cs.showText("(Signature very wide line 3)");
cs.endText();
}
// no need to set annotations and /P entry
ByteArrayOutputStream baos = new ByteArrayOutputStream();
doc.save(baos);
return new ByteArrayInputStream(baos.toByteArray());
}
}
use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary in project pdfbox by apache.
the class RubberStampWithImage method doIt.
/**
* Add a rubber stamp with an jpg image to every page of the given document.
* @param args the command line arguments
* @throws IOException an exception is thrown if something went wrong
*/
public void doIt(String[] args) throws IOException {
if (args.length != 3) {
usage();
} else {
try (PDDocument document = PDDocument.load(new File(args[0]))) {
if (document.isEncrypted()) {
throw new IOException("Encrypted documents are not supported for this example");
}
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");
// create a PDXObjectImage with the given image file
// if you already have the image in a BufferedImage,
// call LosslessFactory.createFromImage() instead
PDImageXObject ximage = PDImageXObject.createFromFile(args[2], document);
// define and set the target rectangle
int lowerLeftX = 250;
int lowerLeftY = 550;
int formWidth = 150;
int formHeight = 25;
int imgWidth = 50;
int imgHeight = 25;
PDRectangle rect = new PDRectangle();
rect.setLowerLeftX(lowerLeftX);
rect.setLowerLeftY(lowerLeftY);
rect.setUpperRightX(lowerLeftX + formWidth);
rect.setUpperRightY(lowerLeftY + formHeight);
// Create a PDFormXObject
PDFormXObject form = new PDFormXObject(document);
form.setResources(new PDResources());
form.setBBox(rect);
form.setFormType(1);
// adjust the image to the target rectangle and add it to the stream
try (OutputStream os = form.getStream().createOutputStream()) {
drawXObject(ximage, form.getResources(), os, lowerLeftX, lowerLeftY, imgWidth, imgHeight);
}
PDAppearanceStream myDic = new PDAppearanceStream(form.getCOSObject());
PDAppearanceDictionary appearance = new PDAppearanceDictionary(new COSDictionary());
appearance.setNormalAppearance(myDic);
rubberStamp.setAppearance(appearance);
rubberStamp.setRectangle(rect);
// add the new RubberStamp to the document
annotations.add(rubberStamp);
}
document.save(args[1]);
}
}
}
Aggregations