use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class PDDocument method prepareNonVisibleSignature.
private void prepareNonVisibleSignature(PDSignatureField signatureField) {
// "Signature fields that are not intended to be visible shall
// have an annotation rectangle that has zero height and width."
// Set rectangle for non-visual signature to rectangle array [ 0 0 0 0 ]
signatureField.getWidgets().get(0).setRectangle(new PDRectangle());
// The visual appearance must also exist for an invisible signature but may be empty.
PDAppearanceDictionary appearanceDictionary = new PDAppearanceDictionary();
PDAppearanceStream appearanceStream = new PDAppearanceStream(this);
appearanceStream.setBBox(new PDRectangle());
appearanceDictionary.setNormalAppearance(appearanceStream);
signatureField.getWidgets().get(0).setAppearance(appearanceDictionary);
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class PDDocument method assignSignatureRectangle.
private void assignSignatureRectangle(PDSignatureField signatureField, COSDictionary annotDict) {
// Read and set the rectangle for visual signature
COSArray rectArray = (COSArray) annotDict.getDictionaryObject(COSName.RECT);
PDRectangle rect = new PDRectangle(rectArray);
PDRectangle existingRectangle = signatureField.getWidgets().get(0).getRectangle();
// in case of an existing field keep the original rect
if (existingRectangle == null || existingRectangle.getCOSArray().size() != 4) {
signatureField.getWidgets().get(0).setRectangle(rect);
}
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class PDFormXObject method getBBox.
/**
* An array of four numbers in the form coordinate system (see below),
* giving the coordinates of the left, bottom, right, and top edges, respectively,
* of the form XObject's bounding box.
* These boundaries are used to clip the form XObject and to determine its size for caching.
* @return The BBox of the form.
*/
@Override
public PDRectangle getBBox() {
PDRectangle retval = null;
COSArray array = (COSArray) getCOSObject().getDictionaryObject(COSName.BBOX);
if (array != null) {
retval = new PDRectangle(array);
}
return retval;
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class PDAcroFormTest method testBadDA.
/**
* PDFBOX-4235: a bad /DA string should not result in an NPE.
*
* @throws IOException
*/
@Test
public void testBadDA() throws IOException {
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDAcroForm acroForm = new PDAcroForm(document);
doc.getDocumentCatalog().setAcroForm(acroForm);
acroForm.setDefaultResources(new PDResources());
PDTextField textBox = new PDTextField(acroForm);
textBox.setPartialName("SampleField");
// https://stackoverflow.com/questions/50609478/
// "tf" is a typo, should have been "Tf" and this results that no font is chosen
textBox.setDefaultAppearance("/Helv 0 tf 0 g");
acroForm.getFields().add(textBox);
PDAnnotationWidget widget = textBox.getWidgets().get(0);
PDRectangle rect = new PDRectangle(50, 750, 200, 20);
widget.setRectangle(rect);
widget.setPage(page);
page.getAnnotations().add(widget);
try {
textBox.setValue("huhu");
} catch (IllegalArgumentException ex) {
return;
} finally {
doc.close();
}
fail("IllegalArgumentException should have been thrown");
}
use of com.tom_roush.pdfbox.pdmodel.common.PDRectangle in project PdfBox-Android by TomRoush.
the class LayerUtility method importPageAsForm.
/**
* Imports a page from some PDF file as a Form XObject so it can be placed on another page
* in the target document.
* <p>
* You may want to call {@link #wrapInSaveRestore(PDPage) wrapInSaveRestore(PDPage)} before invoking the Form XObject to
* make sure that the graphics state is reset.
*
* @param sourceDoc the source PDF document that contains the page to be copied
* @param page the page in the source PDF document to be copied
* @return a Form XObject containing the original page's content
* @throws IOException if an I/O error occurs
*/
public PDFormXObject importPageAsForm(PDDocument sourceDoc, PDPage page) throws IOException {
importOcProperties(sourceDoc);
PDStream newStream = new PDStream(targetDoc, page.getContents(), COSName.FLATE_DECODE);
PDFormXObject form = new PDFormXObject(newStream);
// Copy resources
PDResources pageRes = page.getResources();
PDResources formRes = new PDResources();
cloner.cloneMerge(pageRes, formRes);
form.setResources(formRes);
// Transfer some values from page to form
transferDict(page.getCOSObject(), form.getCOSObject(), PAGE_TO_FORM_FILTER, true);
Matrix matrix = form.getMatrix();
AffineTransform at = matrix.createAffineTransform();
PDRectangle mediaBox = page.getMediaBox();
PDRectangle cropBox = page.getCropBox();
PDRectangle viewBox = (cropBox != null ? cropBox : mediaBox);
// Handle the /Rotation entry on the page dict
int rotation = page.getRotation();
// Transform to FOP's user space
// at.scale(1 / viewBox.getWidth(), 1 / viewBox.getHeight());
at.translate(mediaBox.getLowerLeftX() - viewBox.getLowerLeftX(), mediaBox.getLowerLeftY() - viewBox.getLowerLeftY());
switch(rotation) {
case 90:
at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
at.translate(0, viewBox.getWidth());
at.rotate(-Math.PI / 2.0);
break;
case 180:
at.translate(viewBox.getWidth(), viewBox.getHeight());
at.rotate(-Math.PI);
break;
case 270:
at.scale(viewBox.getWidth() / viewBox.getHeight(), viewBox.getHeight() / viewBox.getWidth());
at.translate(viewBox.getHeight(), 0);
at.rotate(-Math.PI * 1.5);
break;
default:
}
// Compensate for Crop Boxes not starting at 0,0
at.translate(-viewBox.getLowerLeftX(), -viewBox.getLowerLeftY());
if (!at.isIdentity()) {
form.setMatrix(at);
}
BoundingBox bbox = new BoundingBox();
bbox.setLowerLeftX(viewBox.getLowerLeftX());
bbox.setLowerLeftY(viewBox.getLowerLeftY());
bbox.setUpperRightX(viewBox.getUpperRightX());
bbox.setUpperRightY(viewBox.getUpperRightY());
form.setBBox(new PDRectangle(bbox));
return form;
}
Aggregations