How can I download each page of a PDF separately, and view them as a single PDF, using PDFNet?

Q:

I have a somewhat strange setup. I have a large library of PDF documents. These have been split up page-by-page, so that each individual PDF file only have one page. I want to customize the PDFNet viewer to download all pages of the document and reassemble it in the viewer. Is that possible?

A:

First of all, you probably should not organize your documents this way. Splitting up a document page-by-page means that resources shared between pages (such as an image in a header, or embedded fonts) must be duplicated for each document. (See http://www.pdftron.com/pdfnet/intro.html#adv_pg for further details.) That means your documents will be taking up a lot more space on your server’s drives, and also means you’ll be expending a lot more bandwidth downloading the files.

Additionally, PDFNet provides an optimized way to download PDFs from the Web: PDFViewCtrl.OpenURL. This method will start displaying a document as soon as it’s downloaded (assuming the document is linearized — a standard PDF optimization that makes it possible to display them during downloading). You can find out more at http://blog.pdftron.com/2013/08/24/streaming-a-pdf-from-the-web/.

That said, what you’ve described is certainly possible using the PDFNet SDK. You would be fetching each PDF individually and would be inserting them in an in-memory PDF document. The code would use PDFDoc.pagePushBack(page) or similar. For more details, kindly see the following ocde sample:
https://www.pdftron.com/pdfnet/samplecode.html#PDFPage

The in-memory PDF document would be associated with a PDFViewCtrl. You would periodically call pdfviewctrl.updatePageLayout(), so that new pages would be reflected in the view. When inserting pages into the doc you would need to make sure to lock the document (i.e. doc.lock()… insert … doc.unlock()) to avoid concurrency issues.

Here is relevant pseudocode:

`
PDFDoc dc = new PDFDoc();

For (….) {
singlePage = new PDFDoc(filename + i);
Page page = singlePage.getPage(1);
document.pagePushBack(page);
}

String filename = getFilename();
File dir = getDirectory();
if (!dir.exists()) {
boolean success = dir.mkdirs();
Log.d(App.PDF_TRON_TAG, "created file? " + success);
}

try {
document.save(filename, SDFDoc.e_compatibility, null);
} catch (PDFNetException e) {
e.printStackTrace();
}

`

Although some of our clients are using this approach, it’s not recommended since PDFViewCtrl.openURL usually works better and you have less to think about on your end.