Aspose.Pdf.Kit (Java Version) now supports a new feature to split a PDF document into single page PDF documents. This feature would also allow developers to gain more control over the PDF documents while editing using Aspose.Pdf.Kit (Java Version). Splitting a PDF into single page PDF documents becomes very handy task if you work with Aspose.Pdf.Kit (Java Version).
SplitToPages (Java Version ) method is added to PdfFileEditor (Java Version ) class to perform this task. Just follow these steps below to do so:
- Create PdfFileEditor (Java Version )object by calling its empty constructor
- Call SplitToPages (Java Version ) method of PdfFileEditor (Java Version ) object and pass the input PDF file as stream or just the file name as string
- SplitToPages (Java Version ) method will split all pages in the input PDF file to single page PDF documents and store each single page PDF document into an array of memory streams
- Finally, a developer can write code to convert that memory stream into a PDF file using file stream
Code Snippet
[C#]
//Store the input PDF file name into a variable to use later
string inFile1 = "example1.pdf";
//Instantiate PdfFileEditor object by calling its empty constructor
PdfFileEditor pdfEditor = new PdfFileEditor();
//Initialize a numeric variable to generate unique file names of output PDF files
int fileNum = 1;
/* Call SplitToPages method, pass input PDF file name as argument
* and save the returned single page PDF files into an array of memory streams
*/
MemoryStream[] outBuffer = pdfEditor.SplitToPages(inFile1);
//Iterate through all memory stream one by one
foreach(MemoryStream aStream in outBuffer)
{
//Instantiate FileStream object for an output single page PDF file
FileStream outStream = new FileStream("oneByone" + fileNum.ToString() + ".pdf",FileMode.Create);
/* Write the contents of an output PDF memory stream to a PDF file stream.
* An output single page PDF file will be created
*/
aStream.WriteTo(outStream);
//Close the output stream
outStream.Close();
//Increment the numeric variable to get a different value
fileNum++;
}
[VB.NET]
'Store the input PDF file name into a variable to use late
Dim inFile1 As String = "example1.pdf"
'Instantiate PdfFileEditor object by calling its empty constructor
Dim pdfEditor As PdfFileEditor = New PdfFileEditor()
'Initialize a numeric variable to generate unique file names of output PDF files
Dim fileNum As Integer = 1
'Call SplitToPages method, pass input PDF file name as argument and save the
'returned single page PDF files into an array of memory streams
Dim outBuffer() As MemoryStream = pdfEditor.SplitToPages(inFile1)
'Declare a temporary memory stream
Dim aStream As MemoryStream
'Iterate through all memory stream one by one
For Each aStream In outBuffer
'Instantiate FileStream object for an output single page PDF file
Dim outStream As FileStream = New FileStream("oneByone" + fileNum.ToString() + .pdf",FileMode.Create)
'Write the contents of an output PDF memory stream to a PDF file stream.
'An output single page PDF file will be created
aStream.WriteTo(outStream)
'Close the output stream
outStream.Close()
'Increment the numeric variable to get a different value
fileNum = fileNum + 1
Next
[Java]
//Store the input PDF file name into a variable to use later
String inFile1 = "example1.pdf";
//Instantiate PdfFileEditor object by calling its empty constructor
PdfFileEditor pdfEditor = new PdfFileEditor();
int fileNum = 0;
ByteArrayOutputStream[] outBuffer = pdfEditor.splitToPages(inFile1);
while(fileNum < outBuffer.length){
FileOutputStream outStream = new FileOutputStream(path + fileNum + ".pdf");
outStream.write(outBuffer[fileNum].toByteArray());
outStream.close();
fileNum++;
}