How to split PDF by size using C#, VB.NET in WinForms?
The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. With this library, you can split the PDF pages into multiple PDF documents based on the size using C# and VB.NET.
Steps to programmatically split the PDF pages into multiple PDF documents based on the size
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Form.cs file.
// [C# Code] using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf;
' [VB Code] Imports Syncfusion.Pdf.Parsing Imports Syncfusion.Pdf
- Use the following code sample to draw the table one after another in PDF.
// [C# Code] //Document size 1MB. float sizeinKB = 1024; //Loads input PDF document. PdfLoadedDocument ldoc = new PdfLoadedDocument("../../data/pdf_document_reference.PDF"); //Output document count. int documentCount = 1; int beginPageIndex = 0; //Initialize the PDF document. PdfDocument document = new PdfDocument(); //Iterate the pages in the existing document. for (int i = 0; i < ldoc.Pages.Count; i++) { //Importing existing page to new document. document.ImportPage(ldoc, i); //Initialize memory stream. MemoryStream ms = new MemoryStream(); //Save the document to stream. document.Save(ms); ms.Position = 0; if (sizeinKB * 1024 < ms.Length) { if (i == 0) { //Create a new file. FileStream fs = new FileStream(documentCount.ToString() + ".pdf", FileMode.Create); ms.CopyTo(fs); ms.Dispose(); fs.Dispose(); } else { //Initialize PDF document. document = new PdfDocument(); //Import pages to the new document. document.ImportPageRange(ldoc, beginPageIndex, i - 1); //Save the document. document.Save(documentCount.ToString() + ".pdf"); FileStream fileStream= File.OpenRead(documentCount.ToString() + ".pdf"); //Close the document. document.Close(true); } documentCount++; beginPageIndex = i; //Initialize the document. document = new PdfDocument(); if (i > 0) { //Import last page to new document. document.ImportPage(ldoc, i); } } else if (i == ldoc.Pages.Count - 1) { //Create a new file. FileStream fs = new FileStream(documentCount.ToString() + ".pdf", FileMode.Create); ms.CopyTo(fs); ms.Close(); fs.Close(); } }
'[VB Code] 'Document size 1MB. Dim sizeinKB As Single = 1024 'Loads input PDF document. Dim ldoc As New PdfLoadedDocument("../../data/pdf_document_reference.PDF") 'Output document count. Dim documentCount As Integer = 1 Dim beginPageIndex As Integer = 0 'Initialize the PDF document. Dim document As New PdfDocument() 'Iterate the pages in the existing document. For i As Integer = 0 To ldoc.Pages.Count - 1 'Importing existing page to the new document. document.ImportPage(ldoc, i) 'Initialize memory stream. Dim ms As New MemoryStream() 'Save the document to stream. document.Save(ms) ms.Position = 0 If sizeinKB * 1024 < ms.Length Then If i = 0 Then 'Create a new file. Dim fs As New FileStream(documentCount.ToString() + ".pdf", FileMode.Create) ms.CopyTo(fs) ms.Dispose() fs.Dispose() Else 'Initialize PDF document. document = New PdfDocument() 'Import pages to the new document. document.ImportPageRange(ldoc, beginPageIndex, i - 1) 'Save the document. document.Save(documentCount.ToString() + ".pdf") Dim fileStream As FileStream = File.OpenRead(documentCount.ToString() + ".pdf") 'Close the document. document.Close(True) End If documentCount += 1 beginPageIndex = i 'Initialize the document. document = New PdfDocument() If i > 0 Then 'Import last page to new document. document.ImportPage(ldoc, i) End If ElseIf i = ldoc.Pages.Count - 1 Then 'Create a new file. Dim fs As New FileStream(documentCount.ToString() + ".pdf", FileMode.Create) ms.CopyTo(fs) ms.Close() fs.Close() End If Next
A complete working sample can be downloaded from SplitPDFC#-Sample.zip
By executing the program, the document has been spitted into multiple PDFs based on the given size. The console for output PDF size as follows,
Take a moment to peruse the documentation. You can find other options like insert, import, and splitting-a-pdf-file-to-individual-pages with Syncfusion® .NET PDF Library.
Refer here to explore a rich set of Syncfusion® .NET PDF features.
An online sample link explains how to split the PDF pages using C# and VB.NET into multiple PDF documents.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to split PDF by size using C#, VB.NET in WinForms.
You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!