How do we create Watermark using Apspose.Words? Also How to merge a document with a template?
Hi
Thanks for your inquiry.
- You can insert watermark as an image behind text. Here is code example that could help you to achieve this.
// Create document
Document doc = new Document();
// Create documentbuilder
DocumentBuilder builder = new DocumentBuilder(doc);
// Move cursor to header
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
// Insert image and configure it
Shape watermark = builder.InsertImage("test.jpg", RelativeHorizontalPosition.Page, 0, RelativeVerticalPosition.Page, 0, -1, -1, WrapType.None);
watermark.VerticalAlignment = VerticalAlignment.Center;
watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
watermark.BehindText = true;
// Save document
doc.Save("out.doc");
- Do you mean mail merge? If so please see the following link for more information.
https://docs.aspose.com/words/java/mail-merge-and-reporting/
If you would like to merge documents together then see the following link.
https://docs.aspose.com/words/net/insert-and-append-documents/
Best regards.
Thanks for the quick response Alexey. We need to insert a text watermark into the document. I have attached a sample document. Is it possible to achieve this?
Also by document merge I mean that we need to use the header and footer of one document and apply that header and footer to a different document.
Thanks again for your help.
Hi
Thanks for your inquiry.
- Actually watermark text in your document is an image. So you can use code I provided.
- Yes you can import headers/footers from one document to another. For example you can use the following code:
// Open first document
Document srcDoc = new Document(@"Test248\in.doc");
// Create empty document
Document doc = new Document();
// Apply headers footer feom first document
foreach (HeaderFooter hf in srcDoc.FirstSection.HeadersFooters)
{
// Import header/footer
Node dsthf = doc.ImportNode(hf, true, ImportFormatMode.KeepSourceFormatting);
// Insert header/footer into dst document
doc.FirstSection.HeadersFooters.Add(dsthf);
}
// save output
doc.Save(@"Test248\out.doc");
Best regards.
Hi Alexey,
Thanks for your help. I got most of my questions cleared. I have just one issue that I am not able to fix.
I am generating the watermark in the document using
Document doc = new Document(documentPath);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
// Insert the image
Shape watermark = builder.InsertImage("Watermark.gif", RelativeHorizontalPosition.Page, 0, RelativeVerticalPosition.Page, 0, -1, -1, WrapType.None);
watermark.VerticalAlignment = VerticalAlignment.Center;
watermark.HorizontalAlignment = HorizontalAlignment.Center;
watermark.BehindText = true;
This code works well if there is no existing header in the document. If the document has a header with a few items on it then its putting the watermark on the header itself rather than on the center of the page. Is there a work around for this?
Also I was trying to put a text watermark using the code
Shape shape = new Shape(doc, ShapeType.TextSlantUp);
shape.TextPath.Size = 72;
shape.TextPath.Text = "DRAFT";
shape.WrapType = WrapType.None;
shape.BehindText = true;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
shape.HorizontalAlignment = HorizontalAlignment.Center;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
shape.VerticalAlignment = VerticalAlignment.Center;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.InsertNode(shape);
But this never works.
Thanks
Mahesh
Hi
Thanks for your inquiry. Would you please attach your document for testing?
Also here is code that you can use to insert text watermark.
Document doc = new Document("in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = new Shape(doc, ShapeType.TextBox);
shape.Height = 100;
shape.Width = 300;
shape.WrapType = WrapType.None;
shape.BehindText = true;
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
shape.HorizontalAlignment = HorizontalAlignment.Center;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
shape.VerticalAlignment = VerticalAlignment.Center;
shape.Stroked = false;
Paragraph par = new Paragraph(doc);
Run run = new Run(doc, "DRAFT");
par.AppendChild(run);
shape.AppendChild(par);
run.Font.Size = 72;
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.InsertNode(shape);
doc.Save("out.doc");
Hope this helps.
Best regards.
Hi Alexey,
I have attached the document you requested. The document has a first page header and a regular header. Here is the code I am using
Document doc = new Document("C:\\Sample Document.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
// Create list of Headers types
ArrayList headerTypeList = new ArrayList();
headerTypeList.Add(HeaderFooterType.HeaderFirst);
headerTypeList.Add(HeaderFooterType.HeaderPrimary);
foreach (HeaderFooterType headerType in headerTypeList)
{
// Move to the header
builder.MoveToHeaderFooter(headerType);
// Insert the image
Shape watermark = builder.InsertImage("C:\\Watermark.gif", RelativeHorizontalPosition.Page, 0, RelativeVerticalPosition.Page, 0, -1, -1, WrapType.None);
watermark.VerticalAlignment = VerticalAlignment.Center;
watermark.HorizontalAlignment = HorizontalAlignment.Center;
watermark.BehindText = true;
}
doc.Save("C:\\Sample Document WM.doc");
Here it works fine for the first page header but not for the other headers. I have attched the resulting document also. For some documents it doesn’t work on the first page header also.
Also thanks for the text watermark code. It works but have the same issues with the placement of the header. Also text watermark doesn’t appear on the first page header for some reason.
Also I have one more question hopefully the last one
How do we delete the generated watermark?
Thanks for all your help and time
Mahesh
Hi
Thank you for additional information. Please try using the following code:
Document doc = new Document(@"Test270\Sample Document.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
// Create list of Headers types
ArrayList headerTypeList = new ArrayList();
headerTypeList.Add(HeaderFooterType.HeaderFirst);
headerTypeList.Add(HeaderFooterType.HeaderPrimary);
foreach (HeaderFooterType headerType in headerTypeList)
{
// Insert the image
Shape watermark = builder.InsertImage(@"Test270\Watermark.gif", RelativeHorizontalPosition.Page, 0, RelativeVerticalPosition.Page, 0, -1, -1, WrapType.None);
watermark.VerticalAlignment = VerticalAlignment.Center;
watermark.HorizontalAlignment = HorizontalAlignment.Center;
watermark.BehindText = true;
doc.FirstSection.HeadersFooters[headerType].LastParagraph.AppendChild(watermark);
}
doc.Save(@"Test270\out.doc");
Hope this helps.
You can use DeleteShapes method to remove all images from header.
doc.FirstSection.HeadersFooters[headerType].DeleteShapes()
also you can get collection of Shapes in the header/footer and remove particular node from the collection.
Best regards.
Hi,
is there a posibility to insert a watermark like you do in Word by using the “background function”?
Best regards,
Jochen
Hi
Thanks for your inquiry. Please see the following link to learn how to insert watermark
https://docs.aspose.com/words/net/working-with-watermark/
Best regards,
Hi,
I’m using the following code to create the watermark:
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
Shape shapeFirst = builder.insertImage(watermark, RelativeHorizontalPosition.PAGE, 0, RelativeVerticalPosition.PAGE, 0, 607, 822, WrapType.NONE);
shapeFirst.setVerticalAlignment(VerticalAlignment.CENTER);
shapeFirst.setHorizontalAlignment(HorizontalAlignment.CENTER);
shapeFirst.setBehindText(true);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
Shape shapeRest = builder.insertImage(watermark, RelativeHorizontalPosition.PAGE, 0, RelativeVerticalPosition.PAGE, 0, 607, 822, WrapType.NONE);
shapeRest.setVerticalAlignment(VerticalAlignment.CENTER);
shapeRest.setHorizontalAlignment(HorizontalAlignment.CENTER);
shapeRest.setBehindText(true);
doc.save(source);
As far as I know, Aspose creates an image and puts it behind the text. But this is not the original way to create an watermark in Word. In Word there is a special function to insert a watermark. Is there a way to use this function?
The reason why I’m asking ist, that we use the NOA API to create .pdf files. When we export the document with the watermark the watermark in the .pdf file has a border!
Hi
Thank you for additional information. In the article from my previous post you can learn how to insert a watermark in the document as a WordArt object, exactly as MS Word does. As I can see you insert a watermark as an image.
In additional, Aspose.Words for Java will support converting to PDF before the end of this year. Your request has been linked to the appropriate issue. You will be notified as soon as this feature is supported.
Best regards,
We are happy to inform you that the first auto-ported version of Aspose.Words for Java is ready. This version supports converting documents to PDF. You can get it from here.
Best regards,
Aspose.Words team
(111)