Free Support Forum - aspose.com

Issues copying a document with protected section into another document

Hi there,

When copying a document with a protected section into another document, we encounter 2 issues:

  1. the section which is protected in the original document is no longer protected
  2. continuous break gets converted into a page break

2018-10-12_16-40-11.png

Code sample:

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");
    }
}

Sample documents are attached.

Thanks,

Manish

@mdb123,

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.

@mdb123,

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.

Thank you Awais.

There are 2 problems:

  1. continuous section break getting converted to page break after copy
  2. protection getting lost

We have found a workaround for the second problem. Are you able to suggest any workaround for the first problem - with section breaks?

@mdb123,

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.

Thank you Awais

@mdb123,

Regarding WORDSNET-17578, we have completed the work on your issue and closed this bug as Not a Bug. Please see my previous post for details.