How to split and merge PDF in C# and .NET

Today, PDF document processing has become an integral part of business process automation, workflow, and application development. The ability to efficiently split and merge PDF files allows you to create powerful tools for working with documentation, reports, contracts, and other files. In this article, we will talk in detail about how to implement split and merge PDF using the popular SautinSoft. PDF Metamorphosis .NET library.
What is this functionality useful for:
- Document management automation. Splitting large PDFs into smaller parts, for example, by pages or sections.
- Creation of presentation materials. Combining multiple PDF files to form a single document.
- Legal and business documents. Highlighting specific sections or pages from contracts.
- Report processing. Splitting reports into parts for forwarding or storing.
Document management companies, law firms, financial organizations, and creators of accounting systems and electronic document management platforms use such tools on a daily basis. The ability to quickly split and merge PDF files helps automate routine tasks, speed up customer service, and improve the quality of work. There is often a situation where automatic PDF processing can significantly save time and resources by avoiding manual work with files.
Step-by-step guide:
- Add SautinSoft.PdfMetamorphosis from Nuget.
- Load a PDF file from RTF file.
- Specify page numbers.
- Split and merge PDF by pages.
- Save PDF file.
Complete code
using System;
using System.IO;
using System.Collections;
namespace Sample
{
class Test
{
static void Main(string[] args)
{
// Before starting, we recommend to get a free key:
// https://sautinsoft.com/start-for-free/
// Apply the key here:
// SautinSoft.PdfMetamorphosis.SetLicense("...");
SautinSoft.PdfMetamorphosis p = new SautinSoft.PdfMetamorphosis();
string rtfPath = @"..\..\..\example.rtf";
string pdfPath = @"..\..\..\test.pdf";
// Let's create a PDF file from RTF file
p.PageSettings.Orientation = SautinSoft.PdfMetamorphosis.PageSetting.Orientations.Landscape;
//Specify page numbers: {1 of N}, font: Verdana, 18
p.PageSettings.Numbering.Text = "{page} of {numpages}";
p.PageSettings.Numbering.FontFace = "Verdana";
p.PageSettings.Numbering.FontSize = 18;
p.RtfToPdfConvertFile(rtfPath, pdfPath);
#region split PDF file
//Split PDF by pages: 1st, 2nd, 3rd ...
p.SplitPDFFileToPDFFolder(pdfPath, Path.GetDirectoryName(pdfPath));
#endregion
#region merge PDF files
//Merge only 1st and 3rd pages
string[] pdfFiles = { @"..\..\..\test-00001.pdf", @"..\..\..\test-00003.pdf" };
p.MergePDFFileArrayToPDFFile(pdfFiles, @"..\..\..\test_Split_and_Merge_1and3page.pdf");
#endregion
//Show merged PDF (it doesn't have 2nd page)
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(@"..\..\..\test_Split_and_Merge_1and3page.pdf") { UseShellExecute = true });
}
}
}
Imports System.IO
Module sample
Sub Main()
' Before starting, we recommend to get a free key:
' https://sautinsoft.com/start-for-free/
' Apply the key here:
' SautinSoft.PdfMetamorphosis.SetLicense("...");
Dim p As New SautinSoft.PdfMetamorphosis()
Dim rtfPath As String = "..\..\..\example.rtf"
Dim pdfPath As String = "..\..\..\test.pdf"
' Let's create a PDF file from RTF file
p.PageSettings.Orientation = SautinSoft.PdfMetamorphosis.PageSetting.Orientations.Landscape
'Specify page numbers: {1 of N}, font: Verdana, 18
p.PageSettings.Numbering.Text = "{page} of {numpages}"
p.PageSettings.Numbering.FontFace = "Verdana"
p.PageSettings.Numbering.FontSize = 18
p.RtfToPdfConvertFile(rtfPath, pdfPath)
'Split PDF by pages: 1st, 2nd, 3rd ...
p.SplitPDFFileToPDFFolder(pdfPath, Path.GetDirectoryName(pdfPath))
'Merge only 1st and 3rd pages
Dim pdfFiles() As String = {"..\..\..\test-00001.pdf", "..\..\..\test-00003.pdf"}
p.MergePDFFileArrayToPDFFile(pdfFiles, "..\..\..\test_Split_and_Merge_1and3page.pdf")
'Show merged PDF (it doesn't have 2nd page)
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("..\..\..\test_Split_and_Merge_1and3page.pdf") With {.UseShellExecute = True})
End Sub
End Module
If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below: