private void mergePage(COSArray array, PDPage page) { int layoutPageNum = pageCount % layoutPages.size(); LayoutPage layoutPage = (LayoutPage) layoutPages.get(layoutPageNum); PDResources resources = page.findResources(); if (resources == null) { resources = new PDResources(); page.setResources(resources); } COSDictionary docResDict = resources.getCOSDictionary(); COSDictionary layoutResDict = layoutPage.res; mergeArray(COSName.PROC_SET, docResDict, layoutResDict); mergeDictionary(COSName.FONT, docResDict, layoutResDict, layoutPage.objectNameMap); mergeDictionary(COSName.XOBJECT, docResDict, layoutResDict, layoutPage.objectNameMap); mergeDictionary(COSName.EXT_G_STATE, docResDict, layoutResDict, layoutPage.objectNameMap); // we are going to wrap the existing content around some save/restore // graphics state, so the result is // // <save graphics state> // <all existing content streams> // <restore graphics state> // <overlay content> array.add(0, saveGraphicsStateStream); array.add(restoreGraphicsStateStream); array.add(layoutPage.contents); }
private void collectLayoutPages(List pages) throws IOException { Iterator pagesIter = pages.iterator(); while (pagesIter.hasNext()) { PDPage page = (PDPage) pagesIter.next(); COSBase contents = page.getCOSDictionary().getDictionaryObject(COSName.CONTENTS); PDResources resources = page.findResources(); if (resources == null) { resources = new PDResources(); page.setResources(resources); } COSDictionary res = resources.getCOSDictionary(); if (contents instanceof COSStream) { COSStream stream = (COSStream) contents; Map objectNameMap = new TreeMap(); stream = makeUniqObjectNames(objectNameMap, stream); layoutPages.add(new LayoutPage(stream, res, objectNameMap)); } else if (contents instanceof COSArray) { throw new UnsupportedOperationException( "Layout pages with COSArray currently not supported."); // layoutPages.add(new LayoutPage(contents, res)); } else { throw new IOException("Contents are unknown type:" + contents.getClass().getName()); } } }
private void processPages(List pages) throws IOException { Iterator pageIter = pages.iterator(); while (pageIter.hasNext()) { PDPage page = (PDPage) pageIter.next(); COSDictionary pageDictionary = page.getCOSDictionary(); COSBase contents = pageDictionary.getDictionaryObject(COSName.CONTENTS); if (contents instanceof COSStream) { COSStream contentsStream = (COSStream) contents; // System.err.println("stream"); COSArray array = new COSArray(); array.add(contentsStream); mergePage(array, page); pageDictionary.setItem(COSName.CONTENTS, array); } else if (contents instanceof COSArray) { COSArray contentsArray = (COSArray) contents; mergePage(contentsArray, page); } else { throw new IOException("Contents are unknown type:" + contents.getClass().getName()); } pageCount++; } }