We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to get rid of extra lines and section breaks

Hello. I’m writing an application that takes a user-created template and inserts data into it. I am accomplishing this using merge fields and merge field regions. There are multiple templates in each file, for different types of data so the user can decide how they want each type to be displayed. Here is an example template region:


<span style=“mso-field-code:” MERGEFIELD TemplateStart:DEFAULTTEMPLATE \* MERGEFORMAT"">«TemplateStart:DEFAULTTEMPLATE»

«description»

Relationships:«relationships»

«TemplateEnd:DEFAULTTEMPLATE»


I iterate through the document and strip out these templates and put them into seperate Aspose Words Documents in memory, in a map. I then iterate through all the data that needs to be put into the document. I copy the template I extracted to a temporary Document, execute a merge, and then copy the result into the original document. This is so that the title page, table of contents, header/footer, etc... of the original document are retained.


This all works reasonably well, styles are correctly maintained, etc... However, a lot of extra paragraph breaks are inserted. I believe it has something to do with the fact that I am copying full Documents into the original Document multiple times.

Also, part of my copyDocument method sets the sectionStart to SectionStart.CONTINUOUS, because otherwise it defaults to a page break. I would prefer that everything be in the same section. Again, I think this has something to do with copying to a temporary document and back.


So I guess my question is whether or not there is an easy way to remove these extra paragraphs and merge sections, or if there is a better way for me to extract/store/insert these templates. I've attached the relevant code and an example template and output. Thank you in advance for your help!


-Dylan Gulick

Jama Software

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. You can try using the following code to put all content into one section and remove unnecessary empty paragraphs:

Document doc = new Document(@"Test001\Result.doc");

// Remove unnecessary sections.

while (doc.Sections.Count>=2)

{

doc.FirstSection.AppendContent(doc.Sections[1]);

doc.Sections[1].Remove();

}

// Remove all empty paragraphs.

Node[] paragraphs = doc.GetChildNodes(NodeType.Paragraph, true).ToArray();

foreach (Paragraph paragraph in paragraphs)

{

if(!paragraph.HasChildNodes)

paragraph.Remove();

}

doc.Save(@"Test001\out.doc");

Hope this helps. Please let me know if you need more assistance, I will be glad to help you.

Best regards,

Thank you, this helps remove some extra space. I’m now running into an issue with inserting a table to the same document. The template I start with has a table formatted as a title page (large border right, alignment, etc…). It also has text-wrapping set to “Around”. When I try to insert a table later in the document it appears to be inheriting the title page table’s properties, which is causing problems with the text wrapping. Is there a way to disable this style inheritance? Or to clear all table formatting? Thank you


-Dylan

Hi Dylan,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Could you please attach your input document (with table that causes the problem) and output document? I will check the issue and provide you more information.

Also, do you use table styles to format the table? Have you tried using table properties to format the table in the template? This might help you to resolve the problem.

Best regards,