Wim Immelman
Wim Immelman

Reputation: 31

Merging documents using OpenXml and section breaks causes empty paragraphs

I am stitching a couple of documents together with a requirement that each document should retain its header and footer information in the final document. Using AltChunk instead of raw OpenXml or DocumentBuilder saves a lot of effort with regards to styles, formatting, references, parts, etc.

Unfortunately, after a couple of days I can't seem to get a 100% working version due to a small and frustrating issue and I need some insight.

My code is loosly based on this article

I modify each sub document, prior to appending it (as an AltChunk) to a working document, by moving the last section properties into the last paragraph (in order to retain header and footer references), but Word seems to be adding a blank paragraph to each of these documents as it renders them in the final document. I end up with:

document 1 with correct header and footer section properties/break blank paragraph document 2 with correct header and footer section properties/break blank paragraph etc.

I cant remove the blank paragraphs afterwards, as I ideally don't want to use WAS to render the document first.

It seems as if you cannot have a next-page section break without a following paragraph?

Upvotes: 0

Views: 1746

Answers (3)

Victor Xie
Victor Xie

Reputation: 148

I have the similar empty paragraph issue while importing HTML files.

My solution is,

After inserting HTML AltChunk, I add a GUID place holder. After processing the file, I will open the file again, locate the GUID and check if there is a empty paragraph before it, if so remove the empty paragraph and GUID. it seems work perfectly in my solution.

Hope it helps.

Upvotes: 0

Gonçalo Dinis
Gonçalo Dinis

Reputation: 93

I'm using Document Builder and works perfectly for me!

 var sources = new List<OpenXmlPowerTools.Source>();

 sources.Add(new OpenXmlPowerTools.Source(new WmlDocument(@tempReportPart1)));
 sources.Add(new OpenXmlPowerTools.Source(new WmlDocument(@tempReportPart2)));        

 var outputPath = @"C:\Users\xpto\Documents\TestFolder\myNewDocument.docx";

 DocumentBuilder.BuildDocument(sources, outputPath);

Upvotes: 0

Wim Immelman
Wim Immelman

Reputation: 31

After further investigation, it seems that will not be away around my usage scenario. I would need to place the last section properties in the body element, but due to my way of processing with nested AltChunk, it would not work.

I have changed my approach completely and went back to a more detailed append procedure using OpenXml Power Tools and some LINQ to Xml.

Upvotes: 0

Related Questions