use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PageExtractor method extract.
/**
* This will take a document and extract the desired pages into a new
* document. Both startPage and endPage are included in the extracted
* document. If the endPage is greater than the number of pages in the
* source document, it will go to the end of the document. If startPage is
* less than 1, it'll start with page 1. If startPage is greater than
* endPage or greater than the number of pages in the source document, a
* blank document will be returned.
*
* @return The extracted document
* @throws IOException If there is an IOError
*/
public PDDocument extract() throws IOException {
PDDocument extractedDocument = new PDDocument();
extractedDocument.setDocumentInformation(sourceDocument.getDocumentInformation());
extractedDocument.getDocumentCatalog().setViewerPreferences(sourceDocument.getDocumentCatalog().getViewerPreferences());
for (int i = startPage; i <= endPage; i++) {
PDPage page = sourceDocument.getPage(i - 1);
PDPage imported = extractedDocument.importPage(page);
imported.setCropBox(page.getCropBox());
imported.setMediaBox(page.getMediaBox());
imported.setResources(page.getResources());
imported.setRotation(page.getRotation());
}
return extractedDocument;
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class Splitter method createNewDocument.
/**
* Create a new document to write the split contents to.
*
* @return the newly created PDDocument.
* @throws IOException If there is an problem creating the new document.
*/
protected PDDocument createNewDocument() throws IOException {
PDDocument document = memoryUsageSetting == null ? new PDDocument() : new PDDocument(memoryUsageSetting);
document.getDocument().setVersion(getSourceDocument().getVersion());
document.setDocumentInformation(getSourceDocument().getDocumentInformation());
document.getDocumentCatalog().setViewerPreferences(getSourceDocument().getDocumentCatalog().getViewerPreferences());
return document;
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class Overlay method close.
/**
* Close all input documents which were used for the overlay and opened by this class.
*
* @throws IOException if something went wrong
*/
@Override
public void close() throws IOException {
if (defaultOverlay != null) {
defaultOverlay.close();
}
if (firstPageOverlay != null) {
firstPageOverlay.close();
}
if (lastPageOverlay != null) {
lastPageOverlay.close();
}
if (allPagesOverlay != null) {
allPagesOverlay.close();
}
if (oddPageOverlay != null) {
oddPageOverlay.close();
}
if (evenPageOverlay != null) {
evenPageOverlay.close();
}
for (PDDocument doc : openDocuments) {
doc.close();
}
openDocuments.clear();
specificPageOverlayPage.clear();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDFMergerUtility method legacyMergeDocuments.
/**
* Merge the list of source documents, saving the result in the destination
* file.
*
* @param memUsageSetting defines how memory is used for buffering PDF streams;
* in case of <code>null</code> unrestricted main memory is used
*
* @throws IOException If there is an error saving the document.
*/
private void legacyMergeDocuments(MemoryUsageSetting memUsageSetting) throws IOException {
PDDocument destination = null;
if (sources != null && sources.size() > 0) {
// Make sure that:
// - first Exception is kept
// - destination is closed
// - all PDDocuments are closed
// - all FileInputStreams are closed
// - there's a way to see which errors occurred
List<PDDocument> tobeclosed = new ArrayList<PDDocument>();
try {
MemoryUsageSetting partitionedMemSetting = memUsageSetting != null ? memUsageSetting.getPartitionedCopy(sources.size() + 1) : MemoryUsageSetting.setupMainMemoryOnly();
destination = new PDDocument(partitionedMemSetting);
for (Object sourceObject : sources) {
PDDocument sourceDoc = null;
if (sourceObject instanceof File) {
sourceDoc = PDDocument.load((File) sourceObject, partitionedMemSetting);
} else {
sourceDoc = PDDocument.load((InputStream) sourceObject, partitionedMemSetting);
}
tobeclosed.add(sourceDoc);
appendDocument(destination, sourceDoc);
}
// optionally set meta data
if (destinationDocumentInformation != null) {
destination.setDocumentInformation(destinationDocumentInformation);
}
if (destinationMetadata != null) {
destination.getDocumentCatalog().setMetadata(destinationMetadata);
}
if (destinationStream == null) {
destination.save(destinationFileName);
} else {
destination.save(destinationStream);
}
} finally {
if (destination != null) {
IOUtils.closeAndLogException(destination, "PDDocument", null);
}
for (PDDocument doc : tobeclosed) {
IOUtils.closeAndLogException(doc, "PDDocument", null);
}
}
}
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class COSWriter method write.
/**
* This will write the pdf document.
*
* @throws IOException If an error occurs while generating the data.
* @param doc The document to write.
*/
public void write(COSDocument doc) throws IOException {
PDDocument pdDoc = new PDDocument(doc);
write(pdDoc);
}
Aggregations