Combine PDF Files
The code below shows you how to combine multiple PDF files into a single PDF.
It is recommended that you have already reviewed the Getting Started sample, since that includes Licensing and Framework initialization code required to make this sample run.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.IO;
using SolidFramework.Pdf;
namespace CSharp_Tutorials
{
public static partial class Tutorials
{
public static bool CombinePdfFiles(string inputFolder, string outputPath)
{
// Get the list of files to combine
var filesToCombine = Directory.GetFiles(inputFolder, "*.pdf");
if (filesToCombine.Length < 1)
{
Console.WriteLine("Could not find any pdf files to combine in " + inputFolder);
return false;
}
Console.WriteLine("Combining PDFs from " + inputFolder + " into " + outputPath);
// Create a PdfDocument and open the first PDF file
using (var document = new PdfDocument(filesToCombine[0]))
{
document.Open();
// Add the rest of the files
foreach (string path in filesToCombine)
{
// Create another PdfDocument and open the next file
using (var nextDocument = new PdfDocument(path))
{
nextDocument.Open();
// Append this PdfDocument to the first PdfDocument
document.Append(nextDocument);
}
}
// Save the combined PdfDocument
document.SaveAs(outputPath, SolidFramework.Plumbing.OverwriteMode.ForceOverwrite);
}
Console.WriteLine("Successfully combined PDFs from " + inputFolder + " into " + outputPath);
Console.WriteLine();
return true;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Imports System
Imports System.IO
Imports SolidFramework.Pdf
Namespace VBNet_Tutorials
Partial Module Tutorials
Function CombinePdfFiles(ByVal inputFolder As String, ByVal outputPath As String) As Boolean
' Get the list of files to combine
Dim filesToCombine As String() = Directory.GetFiles(inputFolder, "*.pdf")
If filesToCombine.Length < 1 Then
Console.WriteLine("Could not find any pdf files to combine in " & inputFolder)
Return False
End If
Console.WriteLine("Combining PDFs from " & inputFolder & " into " & outputPath)
' Create a PdfDocument and open the first PDF file
Using document As PdfDocument = New PdfDocument(filesToCombine(0))
document.Open()
' Add the rest of the files
For Each path As String In filesToCombine
' Create another PdfDocument and open the next file
Using nextDocument As PdfDocument = New PdfDocument(path)
nextDocument.Open()
' Append this PdfDocument to the first PdfDocument
document.Append(nextDocument)
End Using
Next
' Save the combined PdfDocument
document.SaveAs(outputPath, SolidFramework.Plumbing.OverwriteMode.ForceOverwrite)
End Using
Console.WriteLine("Successfully combined PDFs from " & inputFolder & " into " & outputPath)
Console.WriteLine()
Return True
End Function
End Module
End Namespace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "Tutorials.h"
#include <algorithm>
#include "NaturalSort.h"
// NaturalSort.h is used for the optional sort, it can be found in the full sample
using namespace SolidFramework::Platform;
using namespace SolidFramework::Pdf;
bool CombinePdfFiles(const wstring & inputFolder, const wstring & outputPath)
{
// Get the list of files to combine
vector<wstring> filesToCombine;
for (const wstring & file : Directory::GetFiles(inputFolder, false))
{
if (Path::GetExtension(file) == L".pdf")
{
filesToCombine.push_back(file);
}
}
if (filesToCombine.empty())
{
wcout << L"Could not find any pdf files to combine in " << inputFolder << endl;
return false;
}
// Optional: Sort the list of files
sort(filesToCombine.begin(), filesToCombine.end(), NaturalSort::Ascending);
wcout << L"Combining PDFs from " << inputFolder << L" into " << outputPath << endl;
// Create a PdfDocument and open the first PDF file
auto document = make_shared<PdfDocument>(filesToCombine[0]);
document->Open();
// Add the rest of the files
for (auto iterator = ++filesToCombine.begin(); iterator != filesToCombine.end(); ++iterator)
{
// Create another PdfDocument and open the next file
auto nextDocument = make_shared<PdfDocument>(*iterator);
nextDocument->Open();
// Append this PdfDocument to the first PdfDocument
document->Append(nextDocument);
}
// Save the combined PdfDocument
document->SaveAs(outputPath, SolidFramework::Plumbing::OverwriteMode::ForceOverwrite);
wcout << L"Successfully combined PDFs from " << inputFolder << L" into " << outputPath << endl << endl;
return true;
}