Skip to Main Content
Using Templates, Replacing Text and Converting to PDF

Using Templates, Replacing Text and Converting to PDF

Learn more about Xceed Words for .NET

 

Now that we have covered most of what can be added to a document, let’s look at how to use templates and automatically replacing text, as well as how to convert a document to PDF.

Templates

Applying a template to a document is done by calling the ApplyTemplate method on the document.

The ApplyTemplate method supports 4 overloads, depending on if you are passing a file path or a stream, and if you are including the optional parameter to indicate if the document template text content is to be copied to the document (true by default).

Example when using a file path:

// Get the path to a template document
var templatePath = MyResourcesDirectory + @"Template.docx";

// Apply the template to a document
document1.ApplyTemplate( templatePath );

// Apply the template to a document, but do not copy the content of the template to the document
document2.ApplyTemplate( templatePath, false );

 

Replacing Text

The ReplaceText method is available at both document and paragraph, allowing the flexibility of replacing text only in a specific paragraph or in the entire document.

The ReplaceText methods supports 2 overloads, one for providing a new string value, and another for providing a function to use as a regex match handler.

In both overloads, a search value (string) must be provided, as well as the corresponding new value depending on the overload used. There are also several optional parameters available, including the ability to track the changes or not, the RegexOptions, the new Formatting, the match Formatting, the MatchFormattingOptions and if empty paragraphs should be removed or not.

// Simple text replacement in a paragraph
p.ReplaceText("<FIRST_NAME>", "John");
p.ReplaceText("<LAST_NAME>", "Doe");

// Simple text replacement in a document
document.ReplaceText("<CODE>", "A3124x");

A more complex example is also available in the documentation here.

 

Replacing Text with objects

It is also possible to replace text with objects by using the ReplaceTextWithObject method, also available on both document and paragraph.

The ReplaceTextWithObject method expects the search value and the object to add as mandatory parameters and has several optional parameters similarly to the ReplaceText method.

// Replace text with an Image
document.ReplaceTextWithObject( "<YEAR_IMAGE>", picture, false, RegexOptions.IgnoreCase );

// Replace text with a Hyperlink
document.ReplaceTextWithObject( "<YEAR_LINK>", hyperlink );

// Replace text with a Table
document.ReplaceTextWithObject( "<YEAR_TABLE>", table );

 

Converting to PDF

To convert a document to PDF, the ConvertToPdf method is called.

The ConvertToPdf method has 2 overloads to support two possible outputs, one for a filename, the other for a stream. In both cases however, it expects the document to convert as the first parameter.

Example when using a filename as the output:

using( var document = DocX.Load( "DocumentToConvert.docx" ) )
{
	// Convert the Document to PDF and output to a pdf file
	DocX.ConvertToPdf( document, "ConvertedDocument.pdf" );
}

 

Notes:

  • You need the Xceed.Pdf.dll reference in order to convert to PDF.

  • To convert a Document with a specific font to PDF, the font (a ttf file) must be installed in "Control PanelAll Control Panel ItemsFonts", otherwise the default Arial font will be used to generate the PDF Document.

While most of the docx features will be converted to PDF, there may still be some elements that are not correctly converted. If you encounter a situation where elements are not correctly converter to PDF, please let us know and we will look into adding that functionality.

 

For more information, please refer to the documentation.

Join more than 100,000 satisfied customers now!

IBM
Deloitte
Microsoft
NASA
Bank of America
JP Morgan
Apple