COM

GemBox.Pdf can be used in other programming languages through COM. Due to the COM limitations, GemBox.Pdf exposes a ComHelper class that provides overrides for some API members not supported through COM.

The following example shows how you can read a PDF file, merge multiple PDF files into a single PDF and split a single PDF into multiple PDF files.

Notes

  • GemBox.Pdf assembly needs to be registered in order for this example to work. You can find more information about COM registration here.
  • Python code requires the pywin32 extension to be installed.

Wrapper assembly

If you need to use many GemBox.Pdf classes and methods via COM Interop, consider creating a .NET wrapper assembly that does all the work within and exposes a minimal set of classes and methods to the unmanaged code. This will enable you to avoid various COM limitations and take advantage of GemBox.Pdf's full capabilities.

PDF file generated with GemBox.Pdf through COM
Screenshot of PDF created with GemBox.Pdf via COM
# Create ComHelper object and set license.
# NOTE: If you're using a Professional version you'll need to put your serial key below.
import win32com.client as COM
comHelper = COM.Dispatch("GemBox.Pdf.ComHelper")
comHelper.ComSetLicense("FREE-LIMITED-KEY")

fileNames = ["\\%#MergeFile01.pdf%", "\\%#MergeFile02.pdf%", "\\%#MergeFile03.pdf%"]

# EXAMPLE 1: Load PDF file and read text content from each page.
import os
document1 = comHelper.Load(os.getcwd() + fileNames[0])
pages1 = document1.Pages

for i1 in range(pages1.Count):
    page = pages1.Item(i1)
    print(page.Content.ToString() + "\n")

document1.Dispose()

# EXAMPLE 2: Merge multiple PDF files into a single PDF file.
document2 = COM.Dispatch("GemBox.Pdf.PdfDocument")

for fileName in fileNames:
    sourceDocument = comHelper.Load(os.getcwd() + fileName)
    sourcePages = sourceDocument.Pages

    for i2 in range(sourcePages.Count):
        document2.Pages.AddClone(sourcePages.Item(i2))

    sourceDocument.Dispose()

comHelper.Save(document2, os.getcwd() + "\\Merge Files.pdf")
document2.Dispose()

# EXAMPLE 3: Split a single PDF file into multiple PDF files.
document3 = comHelper.Load(os.getcwd() + "\\Merge Files.pdf")
pages3 = document3.Pages

for i3 in range(pages3.Count):
    destinationDocument = COM.Dispatch("GemBox.Pdf.PdfDocument")
    destinationDocument.Pages.AddClone(pages3.Item(i3))
    
    comHelper.Save(destinationDocument, os.getcwd() + "\\Page" + str(i3) + ".pdf")
    destinationDocument.Dispose()

document3.Dispose()
// Create ComHelper object and set license. 
// NOTE: If you're using a Professional version you'll need to put your serial key below.
$comHelper = new Com("GemBox.Pdf.ComHelper", null, CP_UTF8);
$comHelper->ComSetLicense("FREE-LIMITED-KEY");

$fileNames = array("\\%#MergeFile01.pdf%", "\\%#MergeFile02.pdf%", "\\%#MergeFile03.pdf%");

// EXAMPLE 1: Load PDF file and read text content from each page.
$document1 = $comHelper->Load(getcwd() . $fileNames[0]);
$pages1 = $document1->Pages;

for ($i1 = 0; $i1 < $pages1->Count; $i1++) {
    $page = $pages1->Item($i1);
    echo $page->Content->ToString() . "<br>";
}

$document1.Dispose();

// EXAMPLE 2: Merge multiple PDF files into a single PDF file.
$document2 = new Com("GemBox.Pdf.PdfDocument", null, CP_UTF8);

foreach ($fileNames as $fileName) {
    $sourceDocument = $comHelper->Load(getcwd() . $fileName);
    $sourcePages = $sourceDocument->Pages;
    
    for ($i2 = 0; $i2 < $sourcePages->Count; $i2++) {
        $document2->Pages->AddClone($sourcePages->Item($i2));
    }
    
    $sourceDocument->Dispose();
}

$comHelper->Save($document2, getcwd() . "\\Merge Files.pdf");
$document2->Dispose();

// EXAMPLE 3: Split a single PDF file into multiple PDF files.
$document3 = $comHelper->Load(getcwd() . "\\Merge Files.pdf");
$pages3 = $document3->Pages;

for ($i3 = 0; $i3 < $pages3->Count; $i3++) {
    $destinationDocument = new Com("GemBox.Pdf.PdfDocument", null, CP_UTF8);
    $destinationDocument->Pages->AddClone($pages3->Item($i3));
    
    $comHelper->Save($destinationDocument, getcwd() . "\\Page" . $i3 . ".pdf");
    $destinationDocument->Dispose();
}

$document3->Dispose();
' Create ComHelper object and set license.
' NOTE: If you're using a Professional version you'll need to put your serial key below.
Set comHelper = Server.CreateObject("GemBox.Pdf.ComHelper")
comHelper.SetLicense("FREE-LIMITED-KEY")

Dim fileNames : fileNames = Array("\%#MergeFile01.pdf%", "\%#MergeFile02.pdf%", "\%#MergeFile03.pdf%")

' EXAMPLE 1: Load PDF file and read text content from each page.
Set document1 = comHelper.Load(Server.MapPath(".") & fileNames(0))
Set pages1 = document1.Pages

For i1 = 0 To pages1.Count - 1
    Set page = pages1.Item(i1)
    Response.write(page.Content.ToString() & "<br>")
Next

document1.Dispose()

' EXAMPLE 2: Merge multiple PDF files into a single PDF file.
Set document2 = Server.CreateObject("GemBox.Pdf.PdfDocument")

For i2 = 0 To UBound(fileNames)
    Set sourceDocument = comHelper.Load(Server.MapPath(".") & fileNames(i2))
    Set sourcePages = sourceDocument.Pages

    For j2 = 0 To sourcePages.Count - 1
        document2.Pages.AddClone(sourcePages.Item(j2))
    Next

    sourceDocument.Dispose()
Next

comHelper.Save document2, Server.MapPath(".") & "\Merge Files.pdf"
document2.Dispose()

' EXAMPLE 3: Split a single PDF file into multiple PDF files.
Set document3 = comHelper.Load(Server.MapPath(".") & "\Merge Files.pdf")
Set pages3 = document3.Pages

For i3 = 0 To pages3.Count - 1
    Set destinationDocument = Server.CreateObject("GemBox.Pdf.PdfDocument")
    destinationDocument.Pages.AddClone(pages3.Item(i3))
    
    comHelper.Save destinationDocument, Server.MapPath(".") & "\Page" & i3 & ".pdf"
    destinationDocument.Dispose()
Next

document3.Dispose()

Want more?

GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Pdf yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.