Showing posts with label OpenXML. Show all posts
Showing posts with label OpenXML. Show all posts

Saturday, April 2, 2016

Find and Replace Placeholders in a Word Template

We have a template file that we use as a baseline in our report generation (see here for help creating a new Word document from a template). The template file has some placeholders for title, subtitle, author, etc. I wanted to be able to dynamically replace those with content when the rest of the document was generated. This took me way longer than I expected to figure out so here is hopefully some info to help the next person. First I found the relevant section in the XML (the Open XML Productivity Tools are a great help for this). Then with a bunch of trial and error I figured out how to find and replace the '[Document title]' placeholder.

Monday, November 3, 2014

Creating Word documents with Open XML SDK


Creating a simple Word document and adding content is relatively easy using the Open XML SDK (2.5). Here's a basic Hello World example:

public static void CreateWordDocument(string filename) {
    // Create a document by supplying the filename. 
    using (var wordDocument = WordprocessingDocument.Create(filename, WordprocessingDocumentType.Document)) {
        // Add the MainDocumentPart, root Document and the Body.
        var mainPart = wordDocument.AddMainDocumentPart();
        var document = mainPart.Document = new Document();
        var body = document.AppendChild(new Body());

        // Add a Paragraph and a Run with the specified Text
        var para = body.AppendChild(new Paragraph());
        var run = para.AppendChild(new Run());
        run.AppendChild(new Text("Hello World"));
    }
}

That's great for creating a simple document but what about when you want to add some real content and style it. Once you try and do that you quickly learn that what you are creating with the Open XML SDK truly is an empty document. This is not like opening Word and selecting "New Blank document" which has a bunch of preset styles available. To start having your document look like a real Word document (with things like styles and bullet lists) you have to start adding in classes like StyleDefinitionsPart and NumberingDefinitionsPart (and lots of additional stuff). Even when you add in those additional elements it's hard to make a document look as a good as Word out of the box.

An easier solution is to create your own template and use that as the basis for your new document. That way you will get a bunch of stuff already defined. The simplest way to do this is to open Word and create a new blank document and then save this as a Word template (*.dotx). You can also go into Word and customize the styles and add items like a header page and use that in your template. Once you have a template the code to create that basic Hello World example can be altered like so to use it:

public static void CreateFromTemplate(string templateFilename, string documentFilename) {
    // WordprocessingDocument.Create will overwrite an existing file. 
    // If we are using Open we have to delete the file first 
    // if we want to copy that behavior.
    if (File.Exists(documentFilename)) {
        File.Delete(documentFilename);
    }

    // Copy the template to the output file name.
    File.Copy(templateFilename, documentFilename);

    // Now open the copied file
    using (var wordDocument = WordprocessingDocument.Open(documentFilename, true)) {
        // We need to change the file type from template to document.
        wordDocument.ChangeDocumentType(WordprocessingDocumentType.Document);

        // MainDocumentPart, root Document and Body already exist just access them
        var mainPart = wordDocument.MainDocumentPart;
        var document = mainPart.Document;
        var body = document.Body;

        // Add a Paragraph and a Run with the specified Text
        var para = body.AppendChild(new Paragraph());
        var run = para.AppendChild(new Run());
        run.AppendChild(new Text("Hello World"));

        document.Save();
    }
}

It's a little more verbose but now you have a document with lots of predefined style ready to add content.

Friday, October 10, 2014

Adding a formatted hyperlink to a Word document using Open XML

I was looking for a way to insert a hyperlink into a Word document I am creating using the Open XML SDK (v2.5). I found a couple of examples on the web and got them to work. The link that was created was clickable but it wasn't styled like a hyperlink. After some trial and error I figured out the issue. Since I was creating the document from scratch it has no styles defined in it. So I created a link in word and copied the hyperlink style.

Here is the example code:

const string outputFilename = @"c:\temp\linked.docx";
						
using (var wordDocument = WordprocessingDocument.Create(outputFilename, WordprocessingDocumentType.Document)) {
	var mainPart = wordDocument.AddMainDocumentPart();

	// Create the document structure and add some text.
	var doc = mainPart.Document = new Document();
	var body = doc.AppendChild(new Body());

	// Start a paragraph with some text
	var para = body.AppendChild(new Paragraph());
	var run = para.AppendChild(new Run());
	run.AppendChild(new Text("This is a formatted hyperlink: ") {
		Space = SpaceProcessingModeValues.Preserve // Need this so the trailing space is preserved.
	});

	// Create a hyperlink relationship. Pass the relationship id to the hyperlink below.
	var rel = wordDocument.MainDocumentPart.AddHyperlinkRelationship(new Uri("http://www.example.com/"), true);
				
	// Append the hyperlink with formatting.
	para.AppendChild(
		new Hyperlink(
			new Run(
				new RunProperties(
					// This should be enough if starting with a template
					new RunStyle { Val = "Hyperlink", }, 
					// Add these settings to style the link yourself
					new Underline { Val = UnderlineValues.Single },
					new Color { ThemeColor = ThemeColorValues.Hyperlink }),
				new Text { Text = "Click Here"}
			)) { History = OnOffValue.FromBoolean(true), Id = rel.Id });

	doc.Save();
}