Foxit PDF SDK for Linux Foxit PDF SDK for Mac Foxit PDF SDK for Windows

How to Combine PDFs with PDF SDK

Foxit PDF SDK has the ability to combine or merge multiple PDFs into one. Please use the following sample codes for the different languages below to get started.

Simple code for C++

#include "include/pdf/fs_combination.h"
  
...// SDK has already been initialized successfully.
 
CombineDocumentInfoArray info_array;
info_array.Add(CombineDocumentInfo(input_path + L"AboutFoxit.pdf", L""));
info_array.Add(CombineDocumentInfo(input_path + L"Annot_all.pdf", L""));
info_array.Add(CombineDocumentInfo(input_path + L"SamplePDF.pdf", L""));
 
WString savepath = output_directory + L"Test_Combined.pdf";
uint32 option = Combination::e_CombineDocsOptionBookmark | Combination::e_CombineDocsOptionAcroformRename |
    Combination::e_CombineDocsOptionStructrueTree | Combination::e_CombineDocsOptionOutputIntents |
    Combination::e_CombineDocsOptionOCProperties | Combination::e_CombineDocsOptionMarkInfos |
    Combination::e_CombineDocsOptionPageLabels | Combination::e_CombineDocsOptionNames |
    Combination::e_CombineDocsOptionObjectStream | Combination::e_CombineDocsOptionDuplicateStream;
 
Progressive progress = Combination::StartCombineDocuments(savepath, info_array, option);
Progressive::State progress_state = Progressive::e_ToBeContinued;
while (Progressive::e_ToBeContinued == progress_state) {
    progress_state = progress.Continue();
}

Simple code for C# (dotnet or dotnetcore))

using foxit.pdf;
  
...// SDK has already been initialized successfully.
 
CombineDocumentInfoArray info_array = new CombineDocumentInfoArray();
info_array.Add(new CombineDocumentInfo(input_path + "AboutFoxit.pdf", ""));
info_array.Add(new CombineDocumentInfo(input_path + "Annot_all.pdf", ""));
info_array.Add(new CombineDocumentInfo(input_path + "SamplePDF.pdf", ""));
 
String savepath = output_path + "Test_Combined.pdf";
int option = (int)(Combination.CombineDocsOptions.e_CombineDocsOptionBookmark | Combination.CombineDocsOptions.e_CombineDocsOptionAcroformRename |
    Combination.CombineDocsOptions.e_CombineDocsOptionStructrueTree | Combination.CombineDocsOptions.e_CombineDocsOptionOutputIntents |
    Combination.CombineDocsOptions.e_CombineDocsOptionOCProperties | Combination.CombineDocsOptions.e_CombineDocsOptionMarkInfos |
    Combination.CombineDocsOptions.e_CombineDocsOptionPageLabels | Combination.CombineDocsOptions.e_CombineDocsOptionNames |
    Combination.CombineDocsOptions.e_CombineDocsOptionObjectStream | Combination.CombineDocsOptions.e_CombineDocsOptionDuplicateStream);
 
Progressive progress = Combination.StartCombineDocuments(savepath, info_array, option, null);
Progressive.State progress_state = Progressive.State.e_ToBeContinued;
while (Progressive.State.e_ToBeContinued == progress_state)
{
    progress_state = progress.Continue();
}

Simple code for Java

import com.foxit.sdk.common.Progressive;
import com.foxit.sdk.pdf.CombineDocumentInfo;
import com.foxit.sdk.pdf.CombineDocumentInfoArray;
import com.foxit.sdk.pdf.Combination;
import static com.foxit.sdk.pdf.Combination.e_CombineDocsOptionBookmark;
import static com.foxit.sdk.pdf.Combination.e_CombineDocsOptionAcroformRename;
import static com.foxit.sdk.pdf.Combination.e_CombineDocsOptionStructrueTree;
import static com.foxit.sdk.pdf.Combination.e_CombineDocsOptionOutputIntents;
import static com.foxit.sdk.pdf.Combination.e_CombineDocsOptionOCProperties;
import static com.foxit.sdk.pdf.Combination.e_CombineDocsOptionMarkInfos;
import static com.foxit.sdk.pdf.Combination.e_CombineDocsOptionPageLabels;
import static com.foxit.sdk.pdf.Combination.e_CombineDocsOptionNames;
import static com.foxit.sdk.pdf.Combination.e_CombineDocsOptionObjectStream;
import static com.foxit.sdk.pdf.Combination.e_CombineDocsOptionDuplicateStream;
import static com.foxit.sdk.pdf.Combination.e_CombineDocsOptionDuplicateStream;
  
...// SDK has already been initialized successfully.
 
CombineDocumentInfoArray info_array = new CombineDocumentInfoArray();
info_array.add(new CombineDocumentInfo(input_path + "AboutFoxit.pdf", ""));
info_array.add(new CombineDocumentInfo(input_path + "Annot_all.pdf", ""));
info_array.add(new CombineDocumentInfo(input_path + "SamplePDF.pdf", ""));
 
String savepath = output_path + "Test_Combined.pdf";
int option = (int)(e_CombineDocsOptionBookmark | e_CombineDocsOptionAcroformRename |
    e_CombineDocsOptionStructrueTree | e_CombineDocsOptionOutputIntents |
    e_CombineDocsOptionOCProperties | e_CombineDocsOptionMarkInfos |
    e_CombineDocsOptionPageLabels | e_CombineDocsOptionNames |
    e_CombineDocsOptionObjectStream | e_CombineDocsOptionDuplicateStream);
 
Progressive progress = Combination.startCombineDocuments(savepath, info_array, option, null);
int progress_state = Progressive.e_ToBeContinued;
while (Progressive.e_ToBeContinued == progress_state)
{
    progress_state = progress.resume();
}

Simple code for Objective-c

#include "include/FSPDFObjC.h"
  
...// SDK has already been initialized successfully.
 
FSCombineDocumentInfoArray* info_array = [[FSCombineDocumentInfoArray alloc]init];
[info_array add:[[FSCombineDocumentInfo alloc]initWithFile_path:[input_path stringByAppendingPathComponent:@"AboutFoxit.pdf"] password:@""]];
[info_array add:[[FSCombineDocumentInfo alloc]initWithFile_path:[input_path stringByAppendingPathComponent:@"Annot_all.pdf"] password:@""]];
[info_array add:[[FSCombineDocumentInfo alloc]initWithFile_path:[input_path stringByAppendingPathComponent:@"SamplePDF.pdf"] password:@""]];
  
NSString* savepath = [output_directory stringByAppendingPathComponent:@"Test_Combined.pdf"];
int option = (int)(FSCombinationCombineDocsOptionBookmark | FSCombinationCombineDocsOptionAcroformRename |
    FSCombinationCombineDocsOptionStructrueTree | FSCombinationCombineDocsOptionOutputIntents |
    FSCombinationCombineDocsOptionOCProperties | FSCombinationCombineDocsOptionMarkInfos |
    FSCombinationCombineDocsOptionPageLabels | FSCombinationCombineDocsOptionNames |
    FSCombinationCombineDocsOptionObjectStream | FSCombinationCombineDocsOptionDuplicateStream);
  
FSProgressive* progress = [FSCombination startCombineDocuments:savepath document_array:info_array options:option pause:nil];
int progress_state = FSProgressiveToBeContinued;
while (FSProgressiveToBeContinued == progress_state)
{
    progress_state = [progress resume];
}

Simple code for C

#include "include/fs_common_c.h"
#include "include/fs_combination_c.h"
#include <string>
using namespace std;
...// SDK has already been initialized successfully.
int info_array_size = 3;
FS_COMBINEDOCUMENTINFO_HANDLE* info_array = new FS_COMBINEDOCUMENTINFO_HANDLE[info_array_size];
FS_WSTR password;
password.str = L"";
password.len = 0;
auto item = info_array[0];
FSDK_CombineDocumentInfo_Create((input_path + wstring(L"AboutFoxit.pdf")).c_str(), &password, info_array[0]);
item = info_array[0];
FSDK_CombineDocumentInfo_Create((input_path + wstring(L"Annot_all.pdf")).c_str(), &password, info_array[1]);
item = info_array[1];
FSDK_CombineDocumentInfo_Create((input_path + wstring(L"SamplePDF.pdf")).c_str(), &password, info_array[2]);
item = info_array[2];
FS_UINT32 option = e_FSCombineDocsOptionBookmark | e_FSCombineDocsOptionAcroformRename
| e_FSCombineDocsOptionStructrueTree | e_FSCombineDocsOptionOutputIntents
| e_FSCombineDocsOptionOCProperties | e_FSCombineDocsOptionMarkInfos
| e_FSCombineDocsOptionPageLabels | e_FSCombineDocsOptionNames
| e_FSCombineDocsOptionObjectStream | e_FSCombineDocsOptionDuplicateStream;
FS_PROGRESSIVE_HANDLE progressive = NULL;
FSDK_Combination_StartCombineDocuments((output_directory + L"Test_Combined.pdf").c_str(), info_array, info_array_size, option, NULL, progressive);
FSState progress_state = e_FSToBeContinued;
while (e_FSToBeContinued == progress_state) {
    FSDK_Progressive_Continue(progressive, progress_state);
}
for (int i = 0; i < info_array_size; i++) {
    FSDK_CombineDocumentInfo_Release(info_array[i]);
}
delete[] info_array;

And that’s it. Enjoy our new merging feature!

Updated on December 9, 2020

Was this article helpful?

Notice: Undefined offset: 1 in /usr/share/nginx/htdocs/wp-content/themes/foxit/includes/common/page-rating.php on line 51
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: