COM

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

The following example shows how you can process documents using GemBox.Document in Python, PHP and VBScript. The example loads a template DOCX file, modifies its content (with Find and Replace, Mail Merge and Modify Bookmarks operations) and saves it in PDF format.

Notes

  • GemBox.Document 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 extensions to be installed.

Wrapper assembly

If you need to use many GemBox.Document 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.Document's full capabilities.

PDF file generated with GemBox.Document from template document through COM interop
Screenshot of output file created 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.Document.ComHelper")
comHelper.ComSetLicense("FREE-LIMITED-KEY")

# Load input document.
import os
document = comHelper.Load(os.getcwd() + "\\%#ComTemplate.docx%")

# Find and replace.
document.Content.Replace("PLACEHOLDER1", "Sample Value 1")
document.Content.Replace("PLACEHOLDER2", "Sample Value 2")
document.Content.Replace("PLACEHOLDER3", "Sample Value 3")

# Mail merge.
source = COM.Dispatch("System.Collections.Hashtable")
source.Add("Name", "John")
source.Add("Surname", "Doe")
source.Add("Age", 30)
document.MailMerge.Execute(source)

# Modify bookmarks.
document.Bookmarks.Item("Bookmark1").GetContent(True).LoadText("Sample Content 1.")
document.Bookmarks.Item("Bookmark2").GetContent(True).LoadText("Sample Content 2.")

# Get output path and save document as PDF file.
path = os.getcwd()  + "\\ComExample.pdf"

document.Save(path)
print("Document saved as '" + path + "'")
// 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.Document.ComHelper", null, CP_UTF8);
$comHelper->ComSetLicense("FREE-LIMITED-KEY");

// Load input document.
$document = $comHelper->Load(getcwd() . "\%#ComTemplate.docx%");

// Find and replace
$document->Content->Replace("PLACEHOLDER1", "Sample Value 1");
$document->Content->Replace("PLACEHOLDER2", "Sample Value 2");
$document->Content->Replace("PLACEHOLDER3", "Sample Value 3");

// Mail merge
$source = new Com("System.Collections.Hashtable", null, CP_UTF8);
$source->Add("Name", "John");
$source->Add("Surname", "Doe");
$source->Add("Age", 30);
$document->MailMerge->Execute($source);

// Modify bookmarks
$document->Bookmarks->Item("Bookmark1")->GetContent(true)->LoadText("Sample Content 1.");
$document->Bookmarks->Item("Bookmark2")->GetContent(true)->LoadText("Sample Content 2.");

// Get output path and save document as PDF document.
$path = getcwd() . "\ComExample.pdf";

$document->Save($path);
echo("Document saved as '" . $path . "'");
' 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 = CreateObject("GemBox.Document.ComHelper")
comHelper.ComSetLicense("FREE-LIMITED-KEY")

' Load input document.
Set document = comHelper.Load(Server.MapPath(".") & "\%#ComTemplate.docx%")

' Find and replace.
document.Content.Replace "PLACEHOLDER1", "Sample Value 1"
document.Content.Replace "PLACEHOLDER2", "Sample Value 2"
document.Content.Replace "PLACEHOLDER3", "Sample Value 3"

' Mail merge.
Set source = CreateObject("System.Collections.Hashtable")
source.Add "Name", "John"
source.Add "Surname", "Doe"
source.Add "Age", 30
document.MailMerge.Execute(source)

' Modify bookmarks.
document.Bookmarks.Item("Bookmark1").GetContent(True).LoadText("Sample Content 1.")
document.Bookmarks.Item("Bookmark2").GetContent(True).LoadText("Sample Content 2.")

' Get output path and save document as PDF file.
path = Server.MapPath(".") & "\ComExample.pdf"

document.Save(path)
Response.Write("Document saved as '" & path & "'")

Want more?

Next example 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.Document 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.