Thanks for your request and additional information. As a temporary workaround you can try resave your document to DOC format using the following code:
private void button4_Click(object sender, EventArgs e)
{
try
{
var template = new Document(textBox1.Text);
var textModule = new Document(textBox2.Text);
// If textModule document is DOCX we should resave it as DOC
if (textModule.OriginalLoadFormat == LoadFormat.Docx)
{
MemoryStream docStream = new MemoryStream();
// Save document as DOC to stream
textModule.Save(docStream, SaveFormat.Doc);
// Load DOC document
textModule = new Document(docStream, "", LoadFormat.Doc, "");
}
var bookmark = template.Range.Bookmarks[comboBox1.Text];
…
Or you can try using the following code to achieve the same:
var dstDoc = new Document("10000296.docx");
var srcDoc1 = new Document("10000291.docx");
var srcDoc2 = new Document("10000295.docx");
InsertDocumentAtBookmark("C5804Mod000000010000291R0000000002", dstDoc, srcDoc1);
InsertDocumentAtBookmark("C9052Tsr000000010000295R0000000001", dstDoc, srcDoc1);
InsertDocumentAtBookmark("C4792Phd111111111111111R0000000003", dstDoc, srcDoc2);
dstDoc.Save("out.docx");
public void InsertDocumentAtBookmark(string bookmarkName, Document dstDoc, Document srcDoc)
{
// Clear bookmark
dstDoc.Range.Bookmarks[bookmarkName].Text = string.Empty;
//Create DocumentBuilder
DocumentBuilder builder = new DocumentBuilder(dstDoc);
//Move cursor to bookmark and insert paragraph break
builder.MoveToBookmark(bookmarkName);
builder.Writeln();
//Content of srcdoc will be inserted after this node
Node insertAfterNode = builder.CurrentParagraph.PreviousSibling;
//Content of first paragraph of srcDoc will be apended to this parafraph
Paragraph insertAfterParagraph = (Paragraph)insertAfterNode;
//Content of last paragraph of srcDoc will be apended to this parafraph
Paragraph insertBeforeParagraph = builder.CurrentParagraph;
// We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.ParentNode;
// Loop through all sections in the source document.
foreach (Section srcSection in srcDoc.Sections)
{
// Loop through all block level nodes (paragraphs and tables) in the body of the section.
foreach (Node srcNode in srcSection.Body)
{
// Do not insert node if it is a last empty paragarph in the section.
Paragraph para = srcNode as Paragraph;
if (para != null && para.IsEndOfSection && !para.HasChildNodes)
break;
//If current paragraph is first paragraph of srcDoc
//then append its content to insertAfterParagraph
if (para != null && para.Equals(srcDoc.FirstSection.Body.FirstParagraph))
{
foreach (Node node in para.ChildNodes)
{
Node dstNode = dstDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);
insertAfterParagraph.AppendChild(dstNode);
}
}
//If current paragraph is last paragraph of srcDoc
//then append its content to insertBeforeParagraph
else
{
// This creates a clone of the node, suitable for insertion into the destination document.
Node newNode = dstDoc.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);
// Insert new node after the reference node.
dstStory.InsertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
}
if (!insertBeforeParagraph.HasChildNodes)
insertBeforeParagraph.Remove();
}
Hi Andrey!
Thank you very much for your quick reply and your proposal!
I assume that saving to DOC would work fine if there were no Office 2007 related or non supported features in use.
Our gold standard tests fail e.g. by the following messages:
Assert.AreEqual failed. Expected:<>. Actual:<>.
Assert.AreEqual failed. Expected:<>. Actual:<>.
Assert.AreEqual failed. Expected:<>. Actual:<>.
Assert.AreEqual failed. Expected:<>. Actual:<>.
For we explicitly recommend our customers e.g. to use table styles, I guess that the workaround won’t be suitable for our application. Sorry.
But nevertheless thanks a lot for your research!
I’ll try your other proposal - your code looks much more elegant than our code But I have to check its impact on our rendering logic as well.
So I’ll let you know if that helps.
Thanks a lot for your great support,
cheers,
Stephan
Hello Andrey!
I now integrated your proposed method InsertDocumentAtBookmark() in our code.
The results look quite good but do not fulfill our rendering requirements for 100% (our requirement is e.g. that font formatting is always determined by the text module; paragraph formatting is always determined by the template, only).
Could you please tell me what your intension was with this fix? Perhaps I can integrate it in our code.
Thanks a lot and cheers,
Stephan
Thank you for additional information. The only thing, I can suggest you in this case, is manipulating with ImportFormatMode. You can set UseDestinationStyles or KeepSourceFormatting.
Node dstNode = dstDoc.ImportNode(node, true, ImportFormatMode.UseDestinationStyles);
Best regards,
Hi Andrey!
Thank you very much for this proposal and sorry for answering quite late.
Unfortunately, it does not solve the font problem, either.
And additionally, it causes unit test failures.
But I think that we now know what the limits of this workaround are so that we can communicate them to our customers.
Please let us know if you have further ideas.
If new questions occur from my side, I would like to ask you again.
Thanks a lot for your help!
Cheers,
Stephan
Thanks for your inquiry. Unfortunately, it is difficult to provide you any reliable estimate regarding this issue at the moment. We will keep you informed on any developments regarding this issue.
Best regards,
Hi Andrey!
Fortunately, we found a way to work around this problem without changing our code.
If we change our template by adopting the “default paragraph font” from Normal.dot, everything works as expected (menu “Tools/Templates and Add-Ins…”, then “Organizer…”):
This will probably help us to provisionally solve our customer’s current problem.
This just for your information.
Thank you very much for your help,
have a nice Christmas and a happy new year!
Cheers,
Stephan
It is perfect, that you already found the way to resolve the problem. If you need more assistance, I will be glad to help you.
Merry Christmas and a Happy New Year!
Best regards,