Document Editor
CPDFDocument
and CPDFPage
classes.
ComPDFKit PDF SDK benefits include:
- PDF manipulation
Split pages
Merge pages
Extract pages
- Page edit
Delete pages
Insert pages (choose from another document, a blank page or an image)
Move pages
Rotate pages
Replace pages
Exchange pages
- Access document information
- Extract image
PDF Manipulation
Split pages
CPDFDocument
can extract ranges of pages from one document and put them into another document. If you run this operation multiple times with different page indexes, you can effectively split a PDF into as many documents as you require.
To split a PDF document into multiple pages, please use the following method:
- Create a blank PDF document.
CPDFDocument newDocument = CPDFDocument.createDocument(context);
- Open PDF document that contains the pages you want to split.
// Open document from file path
CPDFDocument document = new CPDFDocument(context);
document.open(pdfPath);
- Extract specific pages from PDF document that you just opened, and import it into the blank PDF document.
// Pages that need to be split, e.g. 2 to 5 pages, start insert index is 0
newDocument.importPages(document, pageNums, 1, 4, 0);
- Save the document.
// Save path, don't remove password of pdf file
newDocument.saveAs(filePath, false);
Merge pages
ComPDFKit allows you to instantiate multiple CPDFDocument
, and you can use the CPDFDocument
API to merge multiple PDF files into a single one.
To merge PDF documents into one file, please use the following method:
- Create a blank PDF document.
CPDFDocument mergeDocument = CPDFDocument.createDocument(mContext);
- Open the PDF documents that contain the pages you want to merge.
// Open document1 from file path1
CPDFDocument document1 = new CPDFDocument(context);
document.open(pdfPath1);
// Open document2 from file path2
CPDFDocument document2 = new CPDFDocument(context);
document2.open(pdfPath2);
- Merge all the pages from the documents you just opened, and import it into the blank PDF document.
mergeDocument.importPages(document1, 0, document1.pageCount - 1, mergeDocument.pageCount);
mergeDocument.importPages(document2, 0, document2.pageCount - 1, mergeDocument.pageCount);
- Save the document.
// Save path, don't remove password of pdf file
mergeDocument.saveAs(destFilePath, false);
The sample code above allows you to merge all the pages from the two documents. If you’re looking to merge or add specific pages from one document to another, you can use importPages
of CPDFDocument.importPages(CPDFDocument otherDocument, int[] pages, int insertPosition)
to set specific pages.
Extract pages
CPDFDocument
can extract ranges of pages from one document and put them into a blank document. If you run this operation, you can effectively extract a PDF as you require. Refer to split pages for more details.