Hi Vladimir,
Thanks for the code, but unfortunately it made no difference. Results (generated_doc.doc) are attached in ZIP along with the documents used to merge and assemble.
Best Regards
Fred
Well, I was wrong. It’s not Headers/Footers that cause the problem.
The problem is that Page Setup settings are different in your source documents. For example, Page Setup | Layout | From edge: | Footer: setting is 0.7 cm in grouping_document.doc and 1.27 cm in main_document.doc.
If you make Page Setup settings identical in the source documents or set similar values for PageSetup in resulting document then these unexpected page breaks will be gone.
I have tried executing the following procedure for generated_doc.doc and it worked:
foreach (Section section in doc.Sections)
{
// set the default values for PageSetup
section.PageSetup.ClearFormatting();
section.PageSetup.SectionStart = SectionStart.Continuous;
}
You’re the man!
That fixed the problem. However, as I don’t want the whole document to revert to the default formatting (is there any way to define the default) and rather have the inserted documents normalised to the parent, I used following code instead based on you’re findings:
foreach (Section section in dstDoc.Sections)
{
section.PageSetup.BottomMargin = dstSection.PageSetup.BottomMargin;
section.PageSetup.Gutter = dstSection.PageSetup.Gutter;
section.PageSetup.LeftMargin = dstSection.PageSetup.LeftMargin;
section.PageSetup.PaperSize = dstSection.PageSetup.PaperSize;
section.PageSetup.RightMargin = dstSection.PageSetup.RightMargin;
section.PageSetup.TopMargin = dstSection.PageSetup.TopMargin;
section.PageSetup.VerticalAlignment = dstSection.PageSetup.VerticalAlignment;
}
I had to copy property by property because the PageSetup object is read-only, so not all page properties are included but the most significant are. Will there be a way to clone a PageSetup in a future version?
Many thanks giving my Easter holiday another boost
Fred
Yes, I agree that readonly access to PageSetup is inconvenient. There are a number of other aggregate properties that are having readonly access too, like Font, ParagraphFormat, etc. We are considering adding write access to this properties in the future versions of Aspose.Words.
Hi Miklovan,
Once again I would like to call upon your expertise. The code you provided in this thread has served us very well so far. Now we are trying to insert the sections of a word document (containing only a small amount of text or a small table) into a table cell (because nesting region merges is not possible). This unfortunately creates a corrupted table in the end result.
The problem I think lies in the section of code below, but I’m not sure how to fix it. I was hoping you might come up with a fix once again
Many Thanks
Fred
if (node.GetAncestor(typeof(Cell)) != null || node.GetAncestor(typeof(Shape)) != null)
{
// Insertion point is tracked to Cell or Shape level:
// - insert appended document on node by node basis.
index = node.ParentNode.ChildNodes.IndexOf(node);
foreach (Section section in doc.Sections)
{
insertedSection = (Section)dstDoc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting);
foreach (Node insertedNode in insertedSection.Body.ChildNodes)
{
// Only Paragraph or Table nodes can be inserted into Cell or Shape
if (node is Paragraph || node is Table)
{
node.ParentNode.ChildNodes.Insert(index++, insertedNode);
}
}
}
}
Please attach a code snippet and a template document so that I could reproduce the error and may necessary corrections in the code above.
Hi,
Attached code_snippet.txt. Basically we use the merge field handler to check each field for specific tag identifying that field as a placeholder for a word document to be inserted at that location. So far it all works wonderfull, even in headers. Only issue is when we put such a tag in a table cell and as such try to insert a word documents sections in the table cell. This operation works fine when done manually (copy-paste sections between 2 Word documents), but when trying to do the same programatically with Aspose.Words I get corrupted tables.
I believe the problem to be in the below section of the insertDocument method.
if ((node.GetAncestor(typeof(Cell)) != null || node.GetAncestor(typeof(Shape)) != null) || node.GetAncestor(typeof(HeaderFooter)) != null)
{
// Insertion point is tracked to Cell or Shape or HeaderFooter level:
// - insert appended document on node by node basis.
index = node.ParentNode.ChildNodes.IndexOf(node);
foreach (Section section in doc.Sections)
{
insertedSection = (Section)dstDoc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting);
foreach (Node insertedNode in insertedSection.Body.ChildNodes)
{
// Only Paragraph or Table nodes can be inserted into Cell or Shape
if (node is Paragraph || node is Table)
{
node.ParentNode.ChildNodes.Insert(index++, insertedNode);
}
}
}
}
I attached a zip containing all documents involved in the test along with the code snippet.
code_snippet.txt --> code
insert_table_test.doc --> main document
inserted_table.doc --> table to be inserted in main document table
insert_table_test_result.doc --> resulting document, table is corrupted
Best Regards
Fred
Thanks for providing the additional info. I will try to fix the problem in 1-2 days.
It seems that I have made several mistakes in the initial code.
Here is a fixed part:
// Insertion point is tracked to Cell or Shape level:
// - insert appended document on node by node basis.
index = node.ParentNode.ChildNodes.IndexOf(node);
foreach (Section section in doc.Sections)
{
foreach (Node sourceNode in section.Body.ChildNodes)
{
// Only Paragraph or Table nodes can be inserted into Cell or Shape
if (sourceNode is Paragraph || sourceNode is Table)
{
Node insertedNode = dstDoc.ImportNode(sourceNode, true, ImportFormatMode.KeepSourceFormatting);
node.ParentNode.ChildNodes.Insert(index++, insertedNode);
}
}
}
If you have questions on this fix please don’t hesitate to ask. I will be glad to answer them.
Best regards,
Hi Miklovan,
The inserts now work perfectly. Only 1 small issue that I hope you could fix as well, after a block that was inserted (regardless in a table or in the main document) it seems that there is always a return line feed added. This is of no great concern, but I was wondering if there is a way to fix that too as it adds an crlf also in table cells when an insert was done there.
Many Thanks again
Fred
I have added a small check to the code. The last paragraph in the section is not inserted if it is empty.
// Insertion point is tracked to Cell or Shape level:
// - insert appended document on node by node basis.
index = node.ParentNode.ChildNodes.IndexOf(node);
foreach (Section section in doc.Sections)
{
foreach (Node sourceNode in section.Body.ChildNodes)
{
// Only Paragraph or Table nodes can be inserted into Cell or Shape
if (sourceNode is Paragraph || sourceNode is Table)
{
// Do not insert node if it is a last empty paragarph in the section.
if (sourceNode is Paragraph && sourceNode == section.Body.LastChild && !sourceNode.HasInnerText)
break;
Node insertedNode = dstDoc.ImportNode(sourceNode, true, ImportFormatMode.KeepSourceFormatting);
node.ParentNode.ChildNodes.Insert(index++, insertedNode);
}
}
}
Perfect!
I noticed that the CrLf still gets added add the end of the inserted text, but now I can easily circumvent this by encapsulating the to be inserted data in a table in the source document. This way, your latest changes remove the empty paragraph (and as such also the CrLf) that is trailing the table. Great!
Many Thanks!
Fred