List of usage examples for org.apache.pdfbox.cos COSDictionary getItem
public COSBase getItem(String key)
From source file:org.apache.fop.render.pdf.pdfbox.StructureTreeMerger.java
License:Apache License
private void createKidFromCOSDictionary(COSDictionary mcrDict, PDFStructElem parent, COSDictionary baseDict) throws IOException { Collection<COSName> exclude = Arrays.asList(COSName.PG); PDFReference referenceObj;//from w w w . ja v a2 s.c o m if (isElementFromSourcePage(mcrDict, baseDict)) { PDFDictionary contentItem = (PDFDictionary) adapter.cloneForNewDocument(mcrDict, mcrDict, exclude); if (mcrDict.keySet().contains(COSName.TYPE)) { String type = ((COSName) mcrDict.getDictionaryObject(COSName.TYPE)).getName(); if (type.equals("OBJR")) { COSObject obj = (COSObject) mcrDict.getItem(COSName.OBJ); if (adapter.getCachedClone(obj) == null) { referenceObj = null; } else { referenceObj = ((PDFObject) adapter.getCachedClone(obj)).makeReference(); } contentItem.put(COSName.OBJ.getName(), referenceObj); updateStructParentAndAddToPageParentTree(referenceObj, parent); } else if (type.equals("MCR")) { updateMCIDEntry(contentItem); markedContentMap.put( (((PDFNumber) contentItem.get(COSName.MCID.getName())).getNumber()).intValue(), parent); } } if (mcrDict.keySet().contains(COSName.PG)) { contentItem.put(COSName.PG.getName(), targetPage.makeReference()); } else { parent.put(COSName.PG.getName(), targetPage.makeReference()); } parent.addKid(contentItem); } else { parent.addKid(null); } }
From source file:org.apache.fop.render.pdf.pdfbox.StructureTreeMerger.java
License:Apache License
private void findLeafNodesInPageFromStructElemObjects(COSBase entry) throws IOException { if (entry instanceof COSObject) { COSObject entryObj = (COSObject) entry; COSDictionary structElemDictionary = (COSDictionary) entryObj.getObject(); COSBase kid = structElemDictionary.getItem(COSName.K); findLeafKids(kid, entryObj);/*from w w w . j a v a 2 s . c om*/ } }
From source file:org.apache.fop.render.pdf.pdfbox.StructureTreeMerger.java
License:Apache License
private void findLeafKids(COSBase kid, COSObject parent) throws IOException { if (kid instanceof COSArray) { COSArray arrayKid = (COSArray) kid; for (COSBase arrayEntry : arrayKid) { findLeafKids(arrayEntry, parent); }/*www . j a va 2 s . c om*/ } else if (kid instanceof COSObject) { COSObject kidObject = (COSObject) kid; COSBase base = kidObject.getObject(); COSDictionary temp = (COSDictionary) base; if (temp.getDictionaryObject(COSName.S) != null && temp.getItem(COSName.K) != null) { COSBase tempKids = temp.getItem(COSName.K); findLeafKids(tempKids, kidObject); } else { findLeafKids(temp, parent); } } else if (kid instanceof COSDictionary) { COSDictionary kidDictionary = (COSDictionary) kid; COSDictionary parentDict = (COSDictionary) parent.getObject(); if (isElementFromSourcePage(kidDictionary, parentDict)) { PDFStructElem elem = structElemCache.get((int) parent.getObjectNumber()); if (elem == null) { elem = createAndRegisterStructElem(parent); copyElemEntries(parent, elem); extra.add(parent); createKids(kid, parentDict, elem, false); } } } else { assert kid instanceof COSInteger; COSDictionary parentDict = (COSDictionary) parent.getObject(); if (checkPageEntryInAncestorsRecursively(parentDict)) { PDFStructElem elem = structElemCache.get((int) parent.getObjectNumber()); if (elem == null) { elem = createAndRegisterStructElem(parent); copyElemEntries(parent, elem); createKids(kid, parentDict, elem, false); } } } }
From source file:org.apache.fop.render.pdf.pdfbox.StructureTreeMerger.java
License:Apache License
private void fillKidsWithNull(PDFStructElem elem, COSDictionary baseElem) { COSBase baseArray = baseElem.getItem(COSName.K); if (baseArray instanceof COSArray) { COSArray array = (COSArray) baseArray; int size = array.size(); for (int i = 0; i < size; i++) { elem.addKid(null);/*from w ww. j av a2s . c om*/ } } }
From source file:org.apache.padaf.preflight.font.Type3FontValidator.java
License:Apache License
/** * CharProcs is a dictionary where the key is a character name and the value * is a Stream which contains the glyph representation of the key. * /*from w w w .java 2 s .co m*/ * This method checks that all character code defined in the Widths Array * exist in the CharProcs dictionary. If the CharProcs doesn't know the * Character, it is mapped with the .notdef one. * * For each character, the Glyph width must be the same as the Width value * declared in the Widths array. * * @param errors * @return */ private boolean checkCharProcsAndMetrics() throws ValidationException { COSDocument cDoc = this.handler.getDocument().getDocument(); // ---- the Widths value can be a reference to an object // ---- Access the object using the COSkey COSArray wArr = COSUtils.getAsArray(this.widths, cDoc); if (wArr == null) { this.fontContainer.addError( new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "The Witdhs array is unreachable")); return false; } COSDictionary charProcsDictionary = COSUtils.getAsDictionary(this.charProcs, cDoc); if (charProcsDictionary == null) { this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "The CharProcs element isn't a dictionary")); return false; } // ---- firstChar and lastChar must be integer. int fc = ((COSInteger) this.firstChar).intValue(); int lc = ((COSInteger) this.lastChar).intValue(); // ---- wArr length = (lc - fc) +1 and it is an array of int. // ---- If FirstChar is greater than LastChar, the validation will fail // because of // ---- the array will have an expected size <= 0. int expectedLength = (lc - fc) + 1; if (wArr.size() != expectedLength) { this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "The length of Witdhs array is invalid. Expected : \"" + expectedLength + "\" Current : \"" + wArr.size() + "\"")); return false; } // ---- Check width consistency for (int i = 0; i < expectedLength; i++) { int cid = fc + i; COSBase arrContent = wArr.get(i); if (COSUtils.isNumeric(arrContent, cDoc)) { float width = COSUtils.getAsFloat(arrContent, cDoc); String charName = null; try { charName = this.type3Encoding.getName(cid); } catch (IOException e) { // shouldn't occur throw new ValidationException("Unable to check Widths consistency", e); } COSBase item = charProcsDictionary.getItem(COSName.getPDFName(charName)); COSStream charStream = COSUtils.getAsStream(item, cDoc); if (charStream == null) { /* There are no character description, we declare the Glyph as Invalid. * If the character is used in a Stream, the GlyphDetail will throw an exception. */ GlyphException glyphEx = new GlyphException(ERROR_FONTS_METRICS, cid, "The CharProcs \"" + charName + "\" doesn't exist, the width defines in the Font Dictionary is " + width); GlyphDetail glyphDetail = new GlyphDetail(cid, glyphEx); this.fontContainer.addKnownCidElement(glyphDetail); } else { try { // --- Parse the Glyph description to obtain the Width PDFAType3StreamParser parser = new PDFAType3StreamParser(this.handler); PDResources pRes = null; if (this.resources != null) { COSDictionary resAsDict = COSUtils.getAsDictionary(this.resources, cDoc); if (resAsDict != null) { pRes = new PDResources(resAsDict); } } parser.resetEngine(); parser.processSubStream(null, pRes, charStream); if (width != parser.getWidth()) { GlyphException glyphEx = new GlyphException(ERROR_FONTS_METRICS, cid, "The CharProcs \"" + charName + "\" should have a width equals to " + width); GlyphDetail glyphDetail = new GlyphDetail(cid, glyphEx); this.fontContainer.addKnownCidElement(glyphDetail); } else { // Glyph is OK, we keep the CID. GlyphDetail glyphDetail = new GlyphDetail(cid); this.fontContainer.addKnownCidElement(glyphDetail); } } catch (ContentStreamException e) { this.fontContainer.addError(new ValidationError(e.getValidationError())); return false; } catch (IOException e) { this.fontContainer.addError(new ValidationError(ERROR_FONTS_TYPE3_DAMAGED, "The CharProcs references an element which can't be read")); return false; } } } else { this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "The Witdhs array is invalid. (some element aren't integer)")); return false; } } return true; }
From source file:org.apache.padaf.preflight.font.Type3FontValidator.java
License:Apache License
/** * If the Resources entry is present, this method check its content. Only * fonts and Images are checked because this resource describes glyphs. REMARK * : The font and the image aren't validated because they will be validated by * an other ValidationHelper.//from ww w .ja va 2 s. c om * * @return */ private boolean checkResources() throws ValidationException { if (this.resources == null) { // ---- No resources dictionary. return true; } COSDocument cDoc = this.handler.getDocument().getDocument(); COSDictionary dictionary = COSUtils.getAsDictionary(this.resources, cDoc); if (dictionary == null) { this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "The Resources element isn't a dictionary")); return false; } COSBase cbImg = dictionary.getItem(COSName.getPDFName(DICTIONARY_KEY_XOBJECT)); COSBase cbFont = dictionary.getItem(COSName.getPDFName(DICTIONARY_KEY_FONT)); if (cbImg != null) { // ---- the referenced objects must be present in the PDF file COSDictionary dicImgs = COSUtils.getAsDictionary(cbImg, cDoc); Set<COSName> keyList = dicImgs.keySet(); for (Object key : keyList) { COSBase item = dictionary.getItem((COSName) key); COSDictionary xObjImg = COSUtils.getAsDictionary(item, cDoc); if (xObjImg == null) { this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "The Resources dictionary of type 3 font is invalid")); return false; } if (!XOBJECT_DICTIONARY_VALUE_SUBTYPE_IMG .equals(xObjImg.getString(COSName.getPDFName(DICTIONARY_KEY_SUBTYPE)))) { this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "The Resources dictionary of type 3 font is invalid")); return false; } } } if (cbFont != null) { // ---- the referenced object must be present in the PDF file COSDictionary dicFonts = COSUtils.getAsDictionary(cbFont, cDoc); Set<COSName> keyList = dicFonts.keySet(); for (Object key : keyList) { COSBase item = dictionary.getItem((COSName) key); COSDictionary xObjFont = COSUtils.getAsDictionary(item, cDoc); if (xObjFont == null) { this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "The Resources dictionary of type 3 font is invalid")); return false; } if (!FONT_DICTIONARY_VALUE_FONT .equals(xObjFont.getString(COSName.getPDFName(DICTIONARY_KEY_TYPE)))) { this.fontContainer.addError(new ValidationError(ERROR_FONTS_DICTIONARY_INVALID, "The Resources dictionary of type 3 font is invalid")); return false; } try { PDFont aFont = PDFontFactory.createFont(xObjFont); // FontContainer aContainer = this.handler.retrieveFontContainer(aFont); AbstractFontContainer aContainer = this.handler.getFont(aFont.getCOSObject()); // ---- another font is used in the Type3, check if the font is valid. if (aContainer.isValid() != State.VALID) { this.fontContainer.addError(new ValidationError(ERROR_FONTS_TYPE3_DAMAGED, "The Resources dictionary of type 3 font contains invalid font")); return false; } } catch (IOException e) { throw new ValidationException("Unable to valid the Type3 : " + e.getMessage()); } } } List<ValidationError> errors = new ArrayList<ValidationError>(); ExtGStateContainer extGStates = new ExtGStateContainer(dictionary, cDoc); boolean res = extGStates.validateTransparencyRules(errors); for (ValidationError err : errors) { this.fontContainer.addError(err); } return res && validateShadingPattern(dictionary, errors); }
From source file:org.apache.padaf.preflight.helpers.BookmarkValidationHelper.java
License:Apache License
/** * return true if Count entry > 0/* w ww . j ava2 s. c o m*/ * @param outline * @param doc * @return */ private boolean isCountEntryPositive(COSDictionary outline, COSDocument doc) { COSBase countBase = outline.getItem(COSName.getPDFName("Count")); return COSUtils.isInteger(countBase, doc) && (COSUtils.getAsInteger(countBase, doc) > 0); }
From source file:org.apache.padaf.preflight.helpers.CatalogValidationHelper.java
License:Apache License
/** * This method checks the content of each OutputIntent. The S entry must * contain GTS_PDFA1. The DestOuputProfile must contain a valid ICC Profile * Stream./*from w w w .j a v a 2s. c o m*/ * * If there are more than one OutputIntent, they have to use the same ICC * Profile. * * This method returns a list of ValidationError. It is empty if no errors * have been found. * * @param handler * @return * @throws ValidationException */ public List<ValidationError> validateOutputIntent(DocumentHandler handler) throws ValidationException { List<ValidationError> result = new ArrayList<ValidationError>(0); PDDocument pdDocument = handler.getDocument(); PDDocumentCatalog catalog = pdDocument.getDocumentCatalog(); COSDocument cDoc = pdDocument.getDocument(); COSBase cBase = catalog.getCOSDictionary() .getItem(COSName.getPDFName(DOCUMENT_DICTIONARY_KEY_OUTPUT_INTENTS)); COSArray outputIntents = COSUtils.getAsArray(cBase, cDoc); Map<COSObjectKey, Boolean> tmpDestOutputProfile = new HashMap<COSObjectKey, Boolean>(); for (int i = 0; outputIntents != null && i < outputIntents.size(); ++i) { COSDictionary dictionary = COSUtils.getAsDictionary(outputIntents.get(i), cDoc); if (dictionary == null) { result.add(new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY, "OutputIntent object is null or isn't a dictionary")); } else { // ---- S entry is mandatory and must be equals to GTS_PDFA1 String sValue = dictionary.getNameAsString(COSName.getPDFName(OUTPUT_INTENT_DICTIONARY_KEY_S)); if (!OUTPUT_INTENT_DICTIONARY_VALUE_GTS_PDFA1.equals(sValue)) { result.add(new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_S_VALUE_INVALID, "The S entry of the OutputIntent isn't GTS_PDFA1")); continue; } // ---- OutputConditionIdentifier is a mandatory field String outputConditionIdentifier = dictionary .getString(COSName.getPDFName(OUTPUT_INTENT_DICTIONARY_KEY_OUTPUT_CONDITION_IDENTIFIER)); if (outputConditionIdentifier == null) {// empty string is autorized (it may be an application specific value) result.add(new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY, "The OutputIntentCondition is missing")); continue; } // ---- If OutputConditionIdentifier is "Custom" or a non Standard ICC Characterization : // ---- DestOutputProfile and Info are mandatory // ---- DestOutputProfile must be a ICC Profile // ---- Because of PDF/A conforming file needs to specify the color characteristics, the DestOutputProfile // ---- is checked even if the OutputConditionIdentifier isn't "Custom" COSBase dop = dictionary .getItem(COSName.getPDFName(OUTPUT_INTENT_DICTIONARY_KEY_DEST_OUTPUT_PROFILE)); ValidationError valer = validateICCProfile(dop, cDoc, tmpDestOutputProfile, handler); if (valer != null) { result.add(valer); continue; } // TODO [LAZY] When Lazy mode will be added, this block should be uncommented to set result as warning. // if (!isStandardICCCharacterization(outputConditionIdentifier)) { // String info = dictionary.getString(COSName.getPDFName(OUTPUT_INTENT_DICTIONARY_KEY_INFO)); // if (info == null || "".equals(info)) { // result.add(new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY, // "The Info entry of a OutputIntent dictionary is missing")); // continue; // } // } } } return result; }
From source file:org.mustangproject.ZUGFeRD.ZUGFeRDExporter.java
License:Open Source License
/** * Embeds an external file (generic - any type allowed) in the PDF. * * @param doc PDDocument to attach the file to. * @param filename name of the file that will become attachment name in the PDF * @param relationship how the file relates to the content, e.g. "Alternative" * @param description Human-readable description of the file content * @param subType type of the data e.g. could be "text/xml" - mime like * @param data the binary data of the file/attachment * @throws java.io.IOException//w w w . j a va2 s . co m */ public void PDFAttachGenericFile(PDDocument doc, String filename, String relationship, String description, String subType, byte[] data) throws IOException { fileAttached = true; PDComplexFileSpecification fs = new PDComplexFileSpecification(); fs.setFile(filename); COSDictionary dict = fs.getCOSObject(); dict.setName("AFRelationship", relationship); dict.setString("UF", filename); dict.setString("Desc", description); ByteArrayInputStream fakeFile = new ByteArrayInputStream(data); PDEmbeddedFile ef = new PDEmbeddedFile(doc, fakeFile); ef.setSubtype(subType); ef.setSize(data.length); ef.setCreationDate(new GregorianCalendar()); ef.setModDate(GregorianCalendar.getInstance()); fs.setEmbeddedFile(ef); // In addition make sure the embedded file is set under /UF dict = fs.getCOSObject(); COSDictionary efDict = (COSDictionary) dict.getDictionaryObject(COSName.EF); COSBase lowerLevelFile = efDict.getItem(COSName.F); efDict.setItem(COSName.UF, lowerLevelFile); // now add the entry to the embedded file tree and set in the document. PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog()); PDEmbeddedFilesNameTreeNode efTree = names.getEmbeddedFiles(); if (efTree == null) { efTree = new PDEmbeddedFilesNameTreeNode(); } Map<String, PDComplexFileSpecification> namesMap = new HashMap<>(); Map<String, PDComplexFileSpecification> oldNamesMap = efTree.getNames(); if (oldNamesMap != null) { for (String key : oldNamesMap.keySet()) { namesMap.put(key, oldNamesMap.get(key)); } } namesMap.put(filename, fs); efTree.setNames(namesMap); names.setEmbeddedFiles(efTree); doc.getDocumentCatalog().setNames(names); // AF entry (Array) in catalog with the FileSpec COSArray cosArray = (COSArray) doc.getDocumentCatalog().getCOSObject().getItem("AF"); if (cosArray == null) { cosArray = new COSArray(); } cosArray.add(fs); COSDictionary dict2 = doc.getDocumentCatalog().getCOSObject(); COSArray array = new COSArray(); array.add(fs.getCOSObject()); // see below dict2.setItem("AF", array); doc.getDocumentCatalog().getCOSObject().setItem("AF", cosArray); }
From source file:uk.ac.liverpool.thumbnails.PDFService.java
License:Open Source License
@Override public FontInformation[] extractFontList(URI u, File fff) throws MalformedURLException, IOException { SortedSet<FontInformation> ret = new TreeSet<FontInformation>(); PDDocument document = getPages(u, fff); List pages = document.getDocumentCatalog().getAllPages(); int i = 0;/*from ww w . j av a 2 s. c om*/ // The code down here is easier as it gets all the fonts used in the document. Still, this would inlcude unused fonts, so we get the fonts page by page and add them to a Hash table. for (COSObject c : document.getDocument().getObjectsByType(COSName.FONT)) { if (c == null || !(c.getObject() instanceof COSDictionary)) continue; //System.out.println(c.getObject()); COSDictionary fontDictionary = (COSDictionary) c.getObject(); // System.out.println(dic.getNameAsString(COSName.BASE_FONT)); // } // } // int pagen = document.getNumberOfPages(); // i=0; // for (int p=0;p<pagen;p++){ // PDPage page = (PDPage)pages.get(p); // PDResources res = page.findResources(); // //for each page resources // if (res==null) continue; // // get the font dictionary // COSDictionary fonts = (COSDictionary) res.getCOSDictionary().getDictionaryObject( COSName.FONT ); // for( COSName fontName : fonts.keySet() ) { // COSObject font = (COSObject) fonts.getItem( fontName ); // // if the font has already been visited we ingore it // long objectId = font.getObjectNumber().longValue(); // if (ret.get(objectId)!=null) // continue; // if( font==null || ! (font.getObject() instanceof COSDictionary) ) // continue; // COSDictionary fontDictionary = (COSDictionary)font.getObject(); // Type MUSt be font if (!fontDictionary.getNameAsString(COSName.TYPE).equals("Font")) continue; // get the variables FontInformation fi = new FontInformation(); fi.fontType = fontDictionary.getNameAsString(COSName.SUBTYPE); String baseFont = fontDictionary.getNameAsString(COSName.BASE_FONT); if (baseFont == null) continue; if (Arrays.binarySearch(standard14, baseFont) >= 0) continue; COSDictionary fontDescriptor = (COSDictionary) fontDictionary.getDictionaryObject(COSName.FONT_DESC); COSBase enc = fontDictionary.getItem(COSName.ENCODING); COSBase uni = fontDictionary.getItem(COSName.TO_UNICODE); int firstChar = fontDictionary.getInt(COSName.FIRST_CHAR); int lastChar = fontDictionary.getInt(COSName.LAST_CHAR); String encoding; boolean toUnicode = uni != null; if (enc == null) { encoding = "standard14"; } if (enc instanceof COSString) { encoding = ((COSString) enc).getString(); } else { encoding = "table"; } fi.isSubset = false; boolean t = true; // Type one and TT can have subsets defineing the basename see 5.5.3 pdfref 1.6 // if (fi.fontType.lastIndexOf(COSName.TYPE1.getName())!=-1 || fi.fontType.equals(COSName.TRUE_TYPE.getName()) ) if (baseFont != null) { if (baseFont.length() > 6) { for (int k = 0; k < 6; k++) if (!Character.isUpperCase(baseFont.charAt(k))) t = false; if (baseFont.charAt(6) != '+') t = false; } else t = false; fi.isSubset = t; if (fi.isSubset) baseFont = baseFont.substring(7); } fi.fontFlags = 0; if (fi.fontType.equals(COSName.TYPE0) || fi.fontType.equals(COSName.TYPE3)) fi.isEmbedded = true; if (fontDescriptor != null) { // in Type1 charset indicates font is subsetted if (fontDescriptor.getItem(COSName.CHAR_SET) != null) fi.isSubset = true; if (fontDescriptor.getItem(COSName.FONT_FILE) != null || fontDescriptor.getItem(COSName.FONT_FILE3) != null || fontDescriptor.getItem(COSName.FONT_FILE2) != null) fi.isEmbedded = true; fi.fontFlags = fontDescriptor.getInt(COSName.getPDFName("Flags")); fi.fontFamily = fontDescriptor.getString(COSName.FONT_FAMILY); fi.fontStretch = fontDescriptor.getString(COSName.FONT_STRETCH); } fi.charset = encoding; fi.fontName = baseFont; fi.isToUnicode = toUnicode; ret.add(fi); } // for all fonts // } // for all pages Iterator<FontInformation> it = ret.iterator(); FontInformation prev = null; LinkedList<FontInformation> toDelete = new LinkedList<FontInformation>(); while (it.hasNext()) { FontInformation current = it.next(); if (prev != null && prev.fontName.equals(current.fontName) && prev.fontType.startsWith("CIDFontType")) toDelete.add(current); prev = current; } ret.removeAll(toDelete); FontInformation[] retArray = ret.toArray(new FontInformation[0]); return retArray; }