List of usage examples for org.apache.pdfbox.cos COSObject getDictionaryObject
public COSBase getDictionaryObject(COSName key)
From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox.PDFBoxFont.java
License:EUPL
private PDFont findCachedFont(PDFBOXObject pdfObject, FontInfoCache fontInfo) { try {/*from w w w .ja v a 2 s.co m*/ if (pdfObject.getFontCache().containsKey(fontInfo.fontPath)) { return pdfObject.getFontCache().get(fontInfo.fontPath); } List<COSObject> cosObjects = pdfObject.getDocument().getDocument().getObjectsByType(COSName.FONT); //COSName cosFontName = COSName.getPDFName(fontInfo.fontName); //COSName cosFontFamily = COSName.getPDFName(fontInfo.fontFamily); Iterator<COSObject> cosObjectIt = cosObjects.iterator(); while (cosObjectIt.hasNext()) { COSObject cosObject = cosObjectIt.next(); COSDictionary baseObject = (COSDictionary) cosObject.getObject(); if (baseObject instanceof COSDictionary) { COSDictionary fontDictionary = (COSDictionary) baseObject; COSBase subType = cosObject.getItem(COSName.SUBTYPE); COSDictionary fontDescriptor = (COSDictionary) cosObject.getDictionaryObject(COSName.FONT_DESC); if (fontDescriptor != null) { String fontName = fontDescriptor.getNameAsString(COSName.FONT_NAME); String fontFamily = fontDescriptor.getNameAsString(COSName.FONT_FAMILY); logger.trace("Inspecting Font {} - {}", fontFamily, fontName); if (COSName.TRUE_TYPE.equals(subType)) { if (fontInfo.fontName != null && fontInfo.fontName.equals(fontName) && fontInfo.fontFamily != null && fontInfo.fontFamily.equals(fontFamily)) { // Found it! :) logger.info("Found Font {}", fontInfo.fontName); return new PDTrueTypeFont(fontDictionary); } } else { logger.debug("Font not a TTF"); } } } else { logger.debug("Font not a COSDictionary"); } } } catch (Exception e) { logger.info("Failed to load existing TTF fonts!", e); } return null; }
From source file:net.padaf.preflight.helpers.MetadataValidationHelper.java
License:Apache License
/** * Return the xpacket from the dictionary's stream *///from w w w. j a v a 2 s .c om public static byte[] getXpacket(COSDocument cdocument) throws IOException, XpacketParsingException { COSObject catalog = cdocument.getCatalog(); COSBase cb = catalog.getDictionaryObject(COSName.METADATA); if (cb == null) { // missing Metadata Key in catalog ValidationError error = new ValidationError(ValidationConstants.ERROR_METADATA_FORMAT, "Missing Metadata Key in catalog"); throw new XpacketParsingException("Failed while retrieving xpacket", error); } // no filter key COSDictionary metadataDictionnary = COSUtils.getAsDictionary(cb, cdocument); if (metadataDictionnary.getItem(COSName.FILTER) != null) { // should not be defined ValidationError error = new ValidationError(ValidationConstants.ERROR_SYNTAX_STREAM_INVALID_FILTER, "Filter specified in metadata dictionnary"); throw new XpacketParsingException("Failed while retrieving xpacket", error); } PDStream stream = PDStream.createFromCOS(metadataDictionnary); ByteArrayOutputStream bos = new ByteArrayOutputStream(); InputStream is = stream.createInputStream(); IOUtils.copy(is, bos); is.close(); bos.close(); return bos.toByteArray(); }
From source file:org.apache.fop.render.pdf.PageParentTreeFinderTestCase.java
License:Apache License
@Test public void testGetPageParentTreeArray() throws IOException { File resource = new File(getClass().getResource(LINK).getFile()); PDDocument doc = PDDocument.load(resource); PDPage srcPage = doc.getPage(0);//w w w. ja v a 2 s. c om PageParentTreeFinder finder = new PageParentTreeFinder(srcPage); COSArray markedContentParents = finder.getPageParentTreeArray(doc); Assert.assertEquals(markedContentParents.size(), 3); COSObject firstObj = (COSObject) markedContentParents.get(0); COSObject secObj = (COSObject) markedContentParents.get(1); COSArray firstKids = (COSArray) firstObj.getDictionaryObject(COSName.K); COSDictionary firstKid = (COSDictionary) firstKids.get(0); int test = firstKid.getInt("MCID"); int expected = 0; Assert.assertEquals(test, expected); COSDictionary firstKidBrother = (COSDictionary) firstKids.get(2); test = firstKidBrother.getInt("MCID"); expected = 2; Assert.assertEquals(test, expected); COSArray secKidsArray = (COSArray) secObj.getDictionaryObject(COSName.K); COSDictionary secondKid = (COSDictionary) secKidsArray.get(0); test = secondKid.getInt("MCID"); expected = 1; Assert.assertEquals(test, expected); }
From source file:org.apache.fop.render.pdf.pdfbox.PageParentTreeFinder.java
License:Apache License
private void traverseKids(COSBase kids, int position, List<COSArray> numList) { COSArray pageParentTree;/*from www.ja va 2 s. c om*/ if (!numList.isEmpty()) { return; } if (kids instanceof COSArray) { COSArray kidsArray = (COSArray) kids; for (COSBase kid : kidsArray) { COSObject kidCOSObj = (COSObject) kid; traverseKids(kidCOSObj, position, numList); } } else if (kids instanceof COSObject) { COSObject kidCOSObj = (COSObject) kids; if (kidCOSObj.getDictionaryObject(COSName.NUMS) == null) { traverseKids(kidCOSObj.getDictionaryObject(COSName.KIDS), position, numList); } else { if (kidCOSObj.getDictionaryObject(COSName.LIMITS) != null) { COSArray kidCOSArray = (COSArray) kidCOSObj.getDictionaryObject(COSName.LIMITS); int lowerLimit = ((COSInteger) kidCOSArray.get(0)).intValue(); int upperLimit = ((COSInteger) kidCOSArray.get(1)).intValue(); if (lowerLimit <= position && position <= upperLimit) { COSArray nums = (COSArray) kidCOSObj.getDictionaryObject(COSName.NUMS); pageParentTree = (COSArray) nums.getObject(((position - lowerLimit) * 2) + 1); numList.add(pageParentTree); } } else { COSArray nums = (COSArray) kidCOSObj.getDictionaryObject(COSName.NUMS); numList.add(extractMarkedContentParents(nums, position)); } } } }
From source file:org.apache.fop.render.pdf.StructureTreeMergerTestCase.java
License:Apache License
@Test public void testCheckNullCOSObject() throws IOException { setUp();// www .ja va2 s. c o m PDDocument doc = PDDocument.load(new File(getClass().getResource(BrokenLink).getFile())); PDPage srcPage = doc.getPage(0); PageParentTreeFinder finder = new PageParentTreeFinder(srcPage); COSArray markedContentParents = finder.getPageParentTreeArray(doc); COSObject nullObj = new COSObject(null); nullObj.setObjectNumber(100); nullObj.setGenerationNumber(0); PDFStructElem elem = new PDFStructElem(); elem.setObjectNumber(2); COSObject parent = (COSObject) markedContentParents.get(1); COSArray kids = (COSArray) parent.getDictionaryObject(COSName.K); COSDictionary kid = (COSDictionary) kids.get(1); kid.setItem(COSName.OBJ, nullObj); adapter = new PDFBoxAdapter(pdfPage, new HashMap(), new HashMap<Integer, PDFArray>()); PDFLogicalStructureHandler handler = setUpPDFLogicalStructureHandler(); StructureTreeMerger merger = new StructureTreeMerger(elem, handler, adapter, srcPage); merger.copyStructure(markedContentParents); PDFArray array = handler.getPageParentTree(); PDFStructElem parentElem = (PDFStructElem) array.get(1); PDFDictionary objrDict = (PDFDictionary) parentElem.getKids().get(1); Assert.assertNull(objrDict.get("Obj")); }