use of com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog in project PdfBox-Android by TomRoush.
the class LayerUtility method appendFormAsLayer.
/**
* Places the given form over the existing content of the indicated page (like an overlay).
* The form is enveloped in a marked content section to indicate that it's part of an
* optional content group (OCG), here used as a layer. This optional group is returned and
* can be enabled and disabled through methods on {@link PDOptionalContentProperties}.
* <p>
* You may want to call {@link #wrapInSaveRestore(PDPage) wrapInSaveRestore(PDPage)} before calling this method to make
* sure that the graphics state is reset.
*
* @param targetPage the target page
* @param form the form to place
* @param transform the transformation matrix that controls the placement of your form. You'll
* need this if your page has a crop box different than the media box, or if these have negative
* coordinates, or if you want to scale or adjust your form.
* @param layerName the name for the layer/OCG to produce
* @return the optional content group that was generated for the form usage
* @throws IOException if an I/O error occurs
*/
public PDOptionalContentGroup appendFormAsLayer(PDPage targetPage, PDFormXObject form, AffineTransform transform, String layerName) throws IOException {
PDDocumentCatalog catalog = targetDoc.getDocumentCatalog();
PDOptionalContentProperties ocprops = catalog.getOCProperties();
if (ocprops == null) {
ocprops = new PDOptionalContentProperties();
catalog.setOCProperties(ocprops);
}
if (ocprops.hasGroup(layerName)) {
throw new IllegalArgumentException("Optional group (layer) already exists: " + layerName);
}
PDRectangle cropBox = targetPage.getCropBox();
if ((cropBox.getLowerLeftX() < 0 || cropBox.getLowerLeftY() < 0) && transform.isIdentity()) {
// PDFBOX-4044
Log.w("PdfBox-Android", "Negative cropBox " + cropBox + " and identity transform may make your form invisible");
}
PDOptionalContentGroup layer = new PDOptionalContentGroup(layerName);
ocprops.addGroup(layer);
PDPageContentStream contentStream = new PDPageContentStream(targetDoc, targetPage, AppendMode.APPEND, !DEBUG);
contentStream.beginMarkedContent(COSName.OC, layer);
contentStream.saveGraphicsState();
contentStream.transform(new Matrix(transform));
contentStream.drawForm(form);
contentStream.restoreGraphicsState();
contentStream.endMarkedContent();
contentStream.close();
return layer;
}
use of com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog in project PdfBox-Android by TomRoush.
the class LayerUtility method importOcProperties.
/**
* Imports OCProperties from source document to target document so hidden layers can still be
* hidden after import.
*
* @param sourceDoc The source PDF document that contains the /OCProperties to be copied.
* @throws IOException If an I/O error occurs.
*/
private void importOcProperties(PDDocument srcDoc) throws IOException {
PDDocumentCatalog srcCatalog = srcDoc.getDocumentCatalog();
PDOptionalContentProperties srcOCProperties = srcCatalog.getOCProperties();
if (srcOCProperties == null) {
return;
}
PDDocumentCatalog dstCatalog = targetDoc.getDocumentCatalog();
PDOptionalContentProperties dstOCProperties = dstCatalog.getOCProperties();
if (dstOCProperties == null) {
dstCatalog.setOCProperties(new PDOptionalContentProperties((COSDictionary) cloner.cloneForNewDocument(srcOCProperties)));
} else {
cloner.cloneMerge(srcOCProperties, dstOCProperties);
}
}
use of com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog in project PdfBox-Android by TomRoush.
the class PDAcroFormTest method testAddMissingInformationOnAcroFormAccess.
/*
* Test that we add missing ressouce information to an AcroForm
* when accessing the AcroForm on the PD level
* (PDFBOX-3752)
*/
@Test
public void testAddMissingInformationOnAcroFormAccess() {
try {
byte[] pdfBytes = createAcroFormWithMissingResourceInformation();
PDDocument pdfDocument = PDDocument.load(pdfBytes);
PDDocumentCatalog documentCatalog = pdfDocument.getDocumentCatalog();
// this call shall trigger the generation of missing information
PDAcroForm theAcroForm = documentCatalog.getAcroForm();
// ensure that the missing information has been generated
// DA entry
assertEquals("/Helv 0 Tf 0 g ", theAcroForm.getDefaultAppearance());
assertNotNull(theAcroForm.getDefaultResources());
// DR entry
PDResources acroFormResources = theAcroForm.getDefaultResources();
assertNotNull(acroFormResources.getFont(COSName.getPDFName("Helv")));
assertEquals("Helvetica", acroFormResources.getFont(COSName.getPDFName("Helv")).getName());
assertNotNull(acroFormResources.getFont(COSName.getPDFName("ZaDb")));
assertEquals("ZapfDingbats", acroFormResources.getFont(COSName.getPDFName("ZaDb")).getName());
pdfDocument.close();
} catch (IOException e) {
System.err.println("Couldn't create test document, test skipped");
return;
}
}
use of com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog in project PdfBox-Android by TomRoush.
the class MainActivity method fillForm.
/**
* Fills in a PDF form and saves the result
*/
public void fillForm(View v) {
try {
// Load the document and get the AcroForm
PDDocument document = PDDocument.load(assetManager.open("FormTest.pdf"));
PDDocumentCatalog docCatalog = document.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
// Fill the text field
PDTextField field = (PDTextField) acroForm.getField("TextField");
field.setValue("Filled Text Field");
// Optional: don't allow this field to be edited
field.setReadOnly(true);
PDField checkbox = acroForm.getField("Checkbox");
((PDCheckBox) checkbox).check();
PDField radio = acroForm.getField("Radio");
((PDRadioButton) radio).setValue("Second");
PDField listbox = acroForm.getField("ListBox");
List<Integer> listValues = new ArrayList<>();
listValues.add(1);
listValues.add(2);
((PDListBox) listbox).setSelectedOptionsIndex(listValues);
PDField dropdown = acroForm.getField("Dropdown");
((PDComboBox) dropdown).setValue("Hello");
String path = root.getAbsolutePath() + "/FilledForm.pdf";
tv.setText("Saved filled form to " + path);
document.save(path);
document.close();
} catch (IOException e) {
Log.e("PdfBox-Android-Sample", "Exception thrown while filling form fields", e);
}
}
use of com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog in project PdfBox-Android by TomRoush.
the class TestEmbeddedFiles method testNullEmbeddedFile.
@Test
public void testNullEmbeddedFile() throws IOException {
PDEmbeddedFile embeddedFile = null;
boolean ok = false;
try {
PDDocument doc = PDDocument.load(getClass().getResourceAsStream("/pdfbox/com/tom_roush/pdfbox/pdmodel/common/null_PDComplexFileSpecification.pdf"));
PDDocumentCatalog catalog = doc.getDocumentCatalog();
PDDocumentNameDictionary names = catalog.getNames();
assertEquals("expected two files", 2, names.getEmbeddedFiles().getNames().size());
PDEmbeddedFilesNameTreeNode embeddedFiles = names.getEmbeddedFiles();
PDComplexFileSpecification spec = embeddedFiles.getNames().get("non-existent-file.docx");
if (spec != null) {
embeddedFile = spec.getEmbeddedFile();
ok = true;
}
// now test for actual attachment
spec = embeddedFiles.getNames().get("My first attachment");
assertNotNull("one attachment actually exists", spec);
assertEquals("existing file length", 17660, spec.getEmbeddedFile().getLength());
spec = embeddedFiles.getNames().get("non-existent-file.docx");
} catch (NullPointerException e) {
assertNotNull("null pointer exception", null);
}
assertTrue("Was able to get file without exception", ok);
assertNull("EmbeddedFile was correctly null", embeddedFile);
}
Aggregations