use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.
the class Splitter method processPage.
/**
* Interface to start processing a new page.
*
* @param page The page that is about to get processed.
*
* @throws IOException If there is an error creating the new document.
*/
protected void processPage(PDPage page) throws IOException {
createNewDocumentIfNecessary();
PDPage imported = getDestinationDocument().importPage(page);
imported.setResources(page.getResources());
// remove page links to avoid copying not needed resources
processAnnotations(imported);
}
use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.
the class Overlay method getLayoutPage.
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(), page.getRotation());
}
use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.
the class Overlay method processPages.
private void processPages(PDDocument document) throws IOException {
int pageCounter = 0;
for (PDPage page : document.getPages()) {
pageCounter++;
COSDictionary pageDictionary = page.getCOSObject();
COSBase originalContent = pageDictionary.getDictionaryObject(COSName.CONTENTS);
COSArray newContentArray = new COSArray();
LayoutPage layoutPage = getLayoutPage(pageCounter, document.getNumberOfPages());
if (layoutPage == null) {
continue;
}
switch(position) {
case FOREGROUND:
// save state
newContentArray.add(createStream("q\n"));
addOriginalContent(originalContent, newContentArray);
// restore state
newContentArray.add(createStream("Q\n"));
// overlay content last
overlayPage(page, layoutPage, newContentArray);
break;
case BACKGROUND:
// overlay content first
overlayPage(page, layoutPage, newContentArray);
addOriginalContent(originalContent, newContentArray);
break;
default:
throw new IOException("Unknown type of position:" + position);
}
pageDictionary.setItem(COSName.CONTENTS, newContentArray);
}
}
use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.
the class Overlay method getLayoutPages.
private Map<Integer, LayoutPage> getLayoutPages(PDDocument doc) throws IOException {
int numberOfPages = doc.getNumberOfPages();
Map<Integer, LayoutPage> layoutPages = new HashMap<Integer, Overlay.LayoutPage>(numberOfPages);
for (int i = 0; i < numberOfPages; i++) {
PDPage page = doc.getPage(i);
COSBase contents = page.getCOSObject().getDictionaryObject(COSName.CONTENTS);
PDResources resources = page.getResources();
if (resources == null) {
resources = new PDResources();
}
layoutPages.put(i, new LayoutPage(page.getMediaBox(), createCombinedContentStream(contents), resources.getCOSObject(), page.getRotation()));
}
return layoutPages;
}
use of com.tom_roush.pdfbox.pdmodel.PDPage in project PdfBox-Android by TomRoush.
the class PDFMergerUtility method optimizedMergeDocuments.
private void optimizedMergeDocuments(MemoryUsageSetting memUsageSetting) throws IOException {
PDDocument destination = null;
try {
destination = new PDDocument(memUsageSetting);
PDFCloneUtility cloner = new PDFCloneUtility(destination);
for (Object sourceObject : sources) {
PDDocument sourceDoc = null;
try {
if (sourceObject instanceof File) {
sourceDoc = PDDocument.load((File) sourceObject, memUsageSetting);
} else {
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());
PDResources resources = page.getResources();
if (resources != null) {
// this is smart enough to just create references for resources that are used on multiple pages
newPage.setResources(new PDResources((COSDictionary) cloner.cloneForNewDocument(resources)));
} else {
newPage.setResources(new PDResources());
}
destination.addPage(newPage);
}
} finally {
IOUtils.closeQuietly(sourceDoc);
}
}
if (destinationStream == null) {
destination.save(destinationFileName);
} else {
destination.save(destinationStream);
}
} finally {
IOUtils.closeQuietly(destination);
}
}
Aggregations