We often open and save PDF documents at work. To improve work efficiency, it is very necessary for PDF tools to provide users with different ways of opening documents under certain circumstances. We will introduce several ways to open and save PDF documents with ComPDFKit.
ComPDFKit supports open documents from local storage, a remote URL, and in-memory data. Before getting familiar with the following approach to opening PDFs, you need to know the logic of opening files. A file could be chosen by its URL. Before rendering the files stored in the device, the files must be read in the running memory. The methods to open PDFs would follow parts of or all the above steps.
Local PDFs: A local PDF is stored on the disk. All the steps above need to be done when opening a local PDF. You could open all the files on this device if you know the file’s storage path. But for large PDFs, it would be a little slow for rendering the whole document.
Open PDFs from In-Memory Data: This is a method to open PDFs from the running memory straightforwardly. But this file shouldn’t be too large. Because the available space of memory may be 8 GB of RAM generally. The data in memory is stored temporarily. Once the device is turned off, data stored in volatile memory will be deleted.
Open Online PDFs: When you want to open a PDF that is not stored on your device, you could open it by its remote URL. You also need to download the remote document and cache it.
The supported saving modes include saving in local storage or by remote URL. There are some saving methods below. Each method has its advantages and disadvantages. Choose the appropriate method according to your needs.
Save Incrementally: It is a fast way to save documents. It appends changes to the end of the document, including deleting annotations. But the file data will increase because it saves the history changes.
Full Save: It’s a good choice to save your PDF files in full save mode if you are going to modify and change your document a lot. The file size will be reduced after you delete some pages, annotations, etc.
Save and Optimize: When you are saving PDFs, there is an optimizing process which is provided by
writeOptimizeToURL: method to compress and clean up useless data. But this operation will take more time to save documents.
Follow the following code example in Objective-C to open a local PDF which is named “ComPDFKit developer guides”. Save it as a new PDF file in the same saving path after deleting page 9.
NSURL *url = [NSURL fileURLWithPath:@".../Desktop/ComPDFKit developer guides.pdf"]; CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url]; [document removePageAtIndex:8]; NSURL *saveUrl = [NSURL fileURLWithPath:@".../Desktop/ComPDFKit developer guides(1).pdf"]; [document writeToURL:saveUrl];
The methods we mentioned above are some different ways to open and save PDFs. For the code examples of opening and saving PDFs, you can refer to our other blog posts.