Refine search
if (resources != null && resources.getXObjectNames().iterator().hasNext()) Iterator<COSName> xObjectNames = resources.getXObjectNames().iterator(); PDXObject xObject = resources.getXObject(xObjectNames.next()); if (xObject instanceof PDFormXObject)
public static void main(String[] args) throws IOException try (PDDocument document = new PDDocument()) PDPage page = new PDPage(PDRectangle.A4); document.addPage(page); PDResources resources = new PDResources(); resources.put(COSName.getPDFName("Helv"), font); PDAcroForm acroForm = new PDAcroForm(document); document.getDocumentCatalog().setAcroForm(acroForm); acroForm.setDefaultResources(resources); acroForm.setDefaultAppearance(defaultAppearanceString); widget.setRectangle(rect); widget.setPage(page); page.getAnnotations().add(widget); acroForm.getFields().add(signatureField);
private void initUI(String fontReferenceName, PDResources resources) { PDFont font = null; for (COSName name: resources.getFontNames()) { if (name.getName().equals(fontReferenceName)) { try { font = resources.getFont(name); } catch (IOException e) { e.printStackTrace(); } } } if (font != null) { markup = "<html>" + font.getName() + "</html>"; } }
private LayoutPage getLayoutPage(PDDocument doc) throws IOException { PDPage page = doc.getPage(0); COSBase contents = page.getCOSObject().getDictionaryObject(COSName.CONTENTS); PDResources resources = page.getResources(); if (resources == null) { resources = new PDResources(); } return new LayoutPage(page.getMediaBox(), createCombinedContentStream(contents), resources.getCOSObject()); }
if (getDefaultAppearance().length() == 0) setDefaultAppearance(adobeDefaultAppearanceString); dictionary.setNeedToBeUpdated(true); defaultResources = new PDResources(); setDefaultResources(defaultResources); dictionary.setNeedToBeUpdated(true); COSDictionary fontDict = defaultResources.getCOSObject().getCOSDictionary(COSName.FONT); if (fontDict == null) fontDict = new COSDictionary(); defaultResources.getCOSObject().setItem(COSName.FONT, fontDict); if (!fontDict.containsKey(COSName.HELV)) defaultResources.put(COSName.HELV, PDType1Font.HELVETICA); defaultResources.getCOSObject().setNeedToBeUpdated(true); fontDict.setNeedToBeUpdated(true); if (!fontDict.containsKey(COSName.ZA_DB)) defaultResources.put(COSName.ZA_DB, PDType1Font.ZAPF_DINGBATS); defaultResources.getCOSObject().setNeedToBeUpdated(true); fontDict.setNeedToBeUpdated(true);
private void optimizedMergeDocuments(MemoryUsageSetting memUsageSetting) throws IOException try (PDDocument destination = new PDDocument(memUsageSetting)) sourceDoc = PDDocument.load((File) sourceObject, memUsageSetting); sourceDoc = PDDocument.load((InputStream) sourceObject, memUsageSetting); for (PDPage page : sourceDoc.getPages()) PDPage newPage = new PDPage( (COSDictionary) cloner.cloneForNewDocument(page.getCOSObject())); newPage.setCropBox(page.getCropBox()); newPage.setMediaBox(page.getMediaBox()); newPage.setRotation(page.getRotation()); newPage.setResources(new PDResources( (COSDictionary) cloner.cloneForNewDocument(resources))); newPage.setResources(new PDResources()); destination.addPage(newPage);
public static void main(String[] args) throws IOException try (PDDocument doc = new PDDocument()) PDPage page = new PDPage(); doc.addPage(page); PDAcroForm acroForm = new PDAcroForm(doc); doc.getDocumentCatalog().setAcroForm(acroForm); final PDResources resources = new PDResources(); acroForm.setDefaultResources(resources); final String fontName = resources.add(formFont).getName(); acroForm.setDefaultResources(resources); String defaultAppearanceString = "/" + fontName + " 0 Tf 0 g"; widget.setRectangle(rect); widget.setPage(page); page.getAnnotations().add(widget);
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); PDResources res = new PDResources(); form.setResources(res); form.setFormType(1); float height = bbox.getHeight(); Matrix initialScale = null; switch (srcDoc.getPage(pageNum).getRotation()) appearance.getCOSObject().setDirect(true); PDAppearanceStream appearanceStream = new PDAppearanceStream(form.getCOSObject()); appearance.setNormalAppearance(appearanceStream);
try (PDDocument document = PDDocument.load(new File(args[0]))) if( document.isEncrypted() ) 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); form.setResources(new PDResources()); form.setBBox(rect); form.setFormType(1); PDAppearanceDictionary appearance = new PDAppearanceDictionary(new COSDictionary()); appearance.setNormalAppearance(myDic); rubberStamp.setAppearance(appearance);
public static void main(String[] args) throws IOException try (PDDocument document = PDDocument.load(new File("target/SimpleForm.pdf"))) PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm(); PDField field = acroForm.getField("SampleField"); PDAnnotationWidget widget = field.getWidgets().get(0); COSName fontName = COSName.getPDFName(parts[0].substring(1)); float fontSize = Float.parseFloat(parts[1]); if (resources != null) font = resources.getFont(fontName); font = acroForm.getDefaultResources().getFont(fontName);
public static void main(String[] args) throws IOException try (PDDocument doc = new PDDocument()) PDPage page = new PDPage(); doc.addPage(page); page.setResources(new PDResources()); tilingPattern1.setYStep(10); COSName patternName1 = page.getResources().add(tilingPattern1); try (PDPatternContentStream cs1 = new PDPatternContentStream(tilingPattern1)) tilingPattern2.setYStep(10); COSName patternName2 = page.getResources().add(tilingPattern2); try (PDPatternContentStream cs2 = new PDPatternContentStream(tilingPattern2)) pcs.fill(); doc.save("patterns.pdf");
PDDocument doc = PDDocument.load(new File(...)); PDPage page = doc.getPage(0); // 0 based PDResources resources = page.getResources(); for (COSName fontName : resources.getFontNames()) { System.out.println(fontName.getName()); } doc.close();
PDDocument doc = PDDocument.load("C:/mydoc3.pdf"); for (int i = 0; i < doc.getNumberOfPages(); ++i) { PDPage page = doc.getPage(i); PDResources res = page.getResources(); for (COSName fontName : res.getFontNames()) { PDFont font = res.getFont(fontName); // do stuff with the font } }
private void assignAcroFormDefaultResource(PDAcroForm acroForm, COSDictionary newDict) { // read and set/update AcroForm default resource dictionary /DR if available COSBase newBase = newDict.getDictionaryObject(COSName.DR); if (newBase instanceof COSDictionary) { COSDictionary newDR = (COSDictionary) newBase; PDResources defaultResources = acroForm.getDefaultResources(); if (defaultResources == null) { acroForm.getCOSObject().setItem(COSName.DR, newDR); newDR.setDirect(true); newDR.setNeedToBeUpdated(true); } else { COSDictionary oldDR = defaultResources.getCOSObject(); COSBase newXObjectBase = newDR.getItem(COSName.XOBJECT); COSBase oldXObjectBase = oldDR.getItem(COSName.XOBJECT); if (newXObjectBase instanceof COSDictionary && oldXObjectBase instanceof COSDictionary) { ((COSDictionary) oldXObjectBase).addAll((COSDictionary) newXObjectBase); oldDR.setNeedToBeUpdated(true); } } } }
for (COSName key : resources.getFontNames()) PDFont font = resources.getFont(key); for (COSName name : resources.getXObjectNames()) PDXObject xobject = resources.getXObject(name); if (xobject instanceof PDFormXObject)
@Override public void createBackgroundLayerForm(PDResources innerFormResource, PDRectangle bbox) throws IOException { // create blank n0 background layer form PDFormXObject n0Form = new PDFormXObject(pdfStructure.getTemplate().getDocument().createCOSStream()); n0Form.setBBox(bbox); n0Form.setResources(new PDResources()); n0Form.setFormType(1); innerFormResource.put(COSName.getPDFName("n0"), n0Form); LOG.info("Created background layer form"); }
public static void testPDFBoxExtractImages() throws Exception { PDDocument document = PDDocument.load(new File("D:/Temp/Test.pdf")); PDPageTree list = document.getPages(); for (PDPage page : list) { PDResources pdResources = page.getResources(); for (COSName c : pdResources.getXObjectNames()) { PDXObject o = pdResources.getXObject(c); if (o instanceof org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject) { File file = new File("D:/Temp/" + System.nanoTime() + ".png"); ImageIO.write(((org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject)o).getImage(), "png", file); } } } }
private Set<String> parseFontFromResources(PDResources resources) { if (resources == null || resources.getFontNames() == null) { return null; } Set<String> fontIDs = new HashSet<>(); for (COSName name : resources.getFontNames()) { COSDictionary dict = (COSDictionary) resources.getCOSObject().getDictionaryObject(COSName.FONT); COSBase base = dict.getItem(name); String id = getId(base, FeatureObjectType.FONT); fontIDs.add(id); if (checkIDBeforeProcess(id)) { try { PDFont font = resources.getFont(name); parseFont(font, id); } catch (IOException e) { LOGGER.info(e); fontCreationProblem(id, e.getMessage()); } } } return fontIDs; }
private static void processResources(PDResources resources) throws IOException { for (COSName name : resources.getXObjectNames()) { PDXObject xobject = resources.getXObject(name); if (xobject instanceof PDFormXObject) { PDFormXObject formXObject = (PDFormXObject) xobject; writeTokensToStream(formXObject.getContentStream(), createTokensWithoutText(formXObject)); processResources(formXObject.getResources()); } } for (COSName name : resources.getPatternNames()) { PDAbstractPattern pattern = resources.getPattern(name); if (pattern instanceof PDTilingPattern) { PDTilingPattern tilingPattern = (PDTilingPattern) pattern; writeTokensToStream(tilingPattern.getContentStream(), createTokensWithoutText(tilingPattern)); processResources(tilingPattern.getResources()); } } }
private Set<String> parsePatternFromResource(PDResources resources) { if (resources == null || resources.getPatternNames() == null) { return null; } Set<String> patternIDs = new HashSet<>(); for (COSName name : resources.getPatternNames()) { COSDictionary dict = (COSDictionary) resources.getCOSObject().getDictionaryObject(COSName.PATTERN); COSBase base = dict.getItem(name); String id = getId(base, FeatureObjectType.PATTERN); patternIDs.add(id); if (checkIDBeforeProcess(id)) { try { PDAbstractPattern pattern = resources.getPattern(name); parsePattern(pattern, id); } catch (IOException e) { LOGGER.info(e); patternCreationProblem(id, e.getMessage()); } } } return patternIDs; }