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 example demonstrates how to load a template DOCX file and modify its contents using GemBox.Document. It shows how to find and replace certain content, mail merge and how to modify bookmarks. At the end, modified document is saved in PDF format.

Notes

  • GemBox.Document assembly needs to be registered in order for this example to work. You can find more information about the COM registration here.
  • Python code requires the pywin32 extension 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 the advantage of GemBox.Document's full capabilites.

Screenshot

COM Example Screenshot

See the full code below.

' 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 & "'")
// 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");
$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");
$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.
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 + "'")

Check next sample or find out more about GemBox.Document and GemBox Software.