public static void Main()
{
var fromDocumentBytes = File.ReadAllBytes(@"ProtectedDoc.docx");
var toDocumentBytes = File.ReadAllBytes(@"EntireDocumentCopyPaste_Destination.docx");
var mailMergeFieldName = "ExternalDocumentGoesHere";
//SetAsposeLicence();
byte[] documentAfterCopyPasteBytes;
using (var toStream = new MemoryStream(toDocumentBytes))
{
var toDocument = new Document(toStream);
if (!toDocument.MailMerge.GetFieldNames().Contains(mailMergeFieldName))
throw new ArgumentException(string.Format("Mail merge field {0} not found.", mailMergeFieldName));
var toDocumentBuilder = new DocumentBuilder(toDocument);
toDocumentBuilder.MoveToMergeField(mailMergeFieldName, false, true);
using (var fromStream = new MemoryStream(fromDocumentBytes))
{
var fromDocument = new Document(fromStream);
toDocumentBuilder.InsertDocument(fromDocument, ImportFormatMode.KeepSourceFormatting);
}
// Remove the paragraph that contains the merge field to avoid an empty line.
if (toDocumentBuilder.CurrentParagraph.ToString(SaveFormat.Text).Trim() == "")
toDocumentBuilder.CurrentParagraph.Remove();
toDocument.Save("ProtectedCopy.docx");
}
}
I am afraid, we do not see any documents attached to this post. Please ZIP and attach your input Word documents, Aspose.Words generated output document showing the undesired behavior and your expected document showing the correct output here for testing. Please create your expected document by using MS Word. Please also list the complete steps that you performed in MS Word to create expected document. We will then investigate the issue on our end and provide you more information.
SampleDocuments.zip (30.1 KB)
I’m sorry for missing out the attachments earlier. Please find them attached now. The files were created using Word 2016.
We tested the scenario and have managed to reproduce the same problems on our end. For the sake of correction, we have logged this in our issue tracking system. Your ticket number is WORDSNET-17578. We will further look into the details and will keep you updated on the status of the linked issue. We apologize for your inconvenience.
Regarding WORDSNET-17578, we have completed the analysis of your issues and come to a conclusion that these issues are actually not bugs in Aspose.Words.
Protection is applied to the whole document, and Section just has attribute. To achieve what you need, you have to apply protection to the destination document:
Document toDocument = new Document(toStream);
if (!toDocument.MailMerge.GetFieldNames().Contains(mailMergeFieldName))
throw new ArgumentException(string.Format("Mail merge field {0} not found.", mailMergeFieldName));
// Apply protection
toDocument.Protect(ProtectionType.AllowOnlyFormFields);
About the second issue, please try to unprotect “ProtectedDoc.docx” and copy its content to the destination document using MS Word – you will get exactly the same result.