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

Free Support Forum - aspose.com

Insert Documents - Retain Watermarks

I am combining a number of Word documents using code similar to that below.

var srcDoc = new Aspose.Words.Document(tempFilePath);
docBuilder.InsertDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);

Some of the source documents contain a water mark. The water mark does not appear in the target document. How can I get the watermarks to flow through into the target document.

Thanks,

David
Hi David,

Thanks for your inquiry. Could you please attach your input Word documents here for testing? We will investigate the issue on our side and provide you more information.

Hi Tahir,


I have attached a very simple sample that demonstrates what I am talking about. I am wanting the “Confidential” watermark from “DocumentWithWatermark.docx” to appear on page 2 of “BuiltDocument.docx”.

Regards,

David
Hi David,

Thanks for sharing the detail. In your case, we suggest you please use Document.AppendDocument method to achieve your requirements. Please read the following article. Hope this helps you.

Joining and Appending Documents

var dstDoc = new Aspose.Words.Document("BaseDocument.docx");

var srcDoc = new Aspose.Words.Document("DocumentWithWatermark.docx");

dstDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);

dstDoc.Save("BuiltDocument.docx");

Hi Tahir,


Thanks for your suggestion. However, I have a whole document assembly engine built using the DocumentBuilder. The sample project that I sent was just a very simplified example of the problem that I was encountering. Is there any way to achieve this with the DocumentBuilder.

Thanks,

David
Hi David,

Thanks for your inquiry. DocumentBuilder.InsertDocument method mimics the MS Word behavior, as if CTRL+'A' (select all content) was pressed, then CTRL+'C' (copy selected into the buffer) inside one document and then CTRL+'V' (insert content from the buffer) inside another document.

In your case, you need to import the document into Aspose.Words' DOM and then append it into another document. Please use Document.AppendDocument to get the desired output.

Hi Tahir,


The problem with Document.AppendDocument is that I do not want to start the inserted document on a new page. We print a heading and then want the content from the inserted document to flow on under the heading. I don’t think that I can achieve that with Document.AppendDocument.

Thanks,

David
Hi David,

Thanks for your inquiry. Please set the value of PageSetup.SectionStart property as SectionStart.Continuous if you do not want to start the content on next page.

You may use DocumentBuilder.InsertDocument method and insert the header/footer contents from one document into another document. After inserting the contents of one document into another using DocumentBuilder.InsertDocument, please use MergeHeaderFooter method to merge header/footer contents of documents. Hope this helps you.

If you still face problem, please share your expected output document here for our reference. We will then provide you more information about your query along with code.


Document doc1 = new Document(MyDir + "doc1.docx");

Document doc2 = new Document(MyDir + "doc2.docx");

MergeHeaderFooter(doc1, doc2, HeaderFooterType.HeaderPrimary);

MergeHeaderFooter(doc1, doc2, HeaderFooterType.FooterPrimary);

doc2.Save(MyDir + "Out.docx");

public void MergeHeaderFooter(Document srcDoc, Document dstDoc, HeaderFooterType headerType)

{

foreach (Section section in dstDoc.Sections)

{

HeaderFooter header = section.HeadersFooters[headerType];

if (header == null)

{

// There is no header of the specified type in the current section, create it.

header = new HeaderFooter(section.Document, headerType);

section.HeadersFooters.Add(header);

}

foreach (Node srcNode in srcDoc.FirstSection.HeadersFooters[headerType].ChildNodes)

{

Node dstNode = dstDoc.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);

header.AppendChild(dstNode);

}

}

}

Hi Tahir,


Could you please give me an example of how to use PageSetup.SectionStart property as SectionStart.Continuous with AppendDocument?

Thanks,

David

Hi Tahir,


I found an example here: Appending 2 documents with SectionStart.Continuous changes justification on first document

However when I do this the document that I am appending always starts on a new page. I have attached a sample project. What do I need to do to get the appended document to start on the current page.

Kind regards,

David
Hi David,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. If you perform the same scenario using MS Word with your documents, you will get the same output.

In your case, we suggest you please remove the section breaks after joining the documents as shown below. Hope this helps you.

var destDoc = new Aspose.Words.Document("BaseDocument.docx");

var docBuilder = new DocumentBuilder(destDoc);

var srcDoc = new Aspose.Words.Document("DocumentWithWatermark.docx");

docBuilder.MoveToDocumentEnd();

docBuilder.ParagraphFormat.ClearFormatting();

docBuilder.Writeln("Just some text to insert using the document builder");

srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;

destDoc.AppendDocument(srcDoc, ImportFormatMode.UseDestinationStyles);

RemoveSectionBreaks(destDoc);

destDoc.Save("BuiltDocument.docx");

private static void RemoveSectionBreaks(Document doc)

{

// Loop through all sections starting from the section that precedes the last one

// and moving to the first section.

for (int i = doc.Sections.Count - 2; i >= 0; i--)

{

// Copy the content of the current section to the beginning of the last section.

doc.LastSection.PrependContent(doc.Sections[i]);

// Remove the copied section.

doc.Sections[i].Remove();

}

}

Hi Tahir,


Thanks for your help on this - I can now achieve what I need to. Your help is appreciated.

Kind regards,

David