Thanks for your inquiry. Please call Document.UpdatePageLayout method before saving the PDF document. If you still face problem, attach the following resources here for testing:
Your input Word document.
Please attach the output PDF file that shows the undesired behavior.
Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.
As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.
PS: To attach these resources, please zip and upload them.
Thanks for sharing the detail. We have tested the scenario using the latest version of Aspose.Words for .NET 18.12 and have not found the shared issue. So, please upgrade to the latest version of Aspose.Words for .NET 18.12. We have attached the output document with this post for your kind reference. output.zip (9.9 KB)
Thanks for your inquiry. Please note that Aspose.Words mimics the behavior of MS Word. If you insert the same watermark in the header of document and set its position using MS Word, you will get the same output.
In your case, we suggest you please insert the watermark in the footer of document instead of header. We have attached the modified code and output document with this post for your kind reference. Code and Document.zip (9.1 KB)
Thanks for your inquiry. You are facing the expected behavior. In this case, we suggest you please set the watermark’s (shape) position as shown below to get the desired output.
Aspose.Words.Drawing.Shape watermark = new Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
watermark.TextPath.Text = "First Line watermark text sample. \n Additional text for the second line that is not printed correctly when printed. azerty azerty.\n";
watermark.TextPath.Size = 12;
watermark.Width = 500;
watermark.Height = 30;
watermark.Fill.Color = System.Drawing.Color.Red;
watermark.TextPath.FontFamily = "Arial"; ;
watermark.Left = 0;
watermark.Top = -doc.FirstSection.PageSetup.BottomMargin;
Thank you very much for all your answers.
It is working and your last reply helped me a lot understanding how to position the watermark.
I understand watermarks are “under” the document content (table, text or image). It is the normal Word behavior. Is there a way to place it upon the content with Aspose ?
Thanks for your inquiry. Yes, you can place the watermark at any place on the page using Aspose.Words. Could you please share some more detail about your query along with expected output Word document? We will then provide you more information about your query.
Thanks for your inquiry. Please note that Aspose.Words mimics the behavior of MS Word. If you add the same watermark in your document, you will get the same output. However, you can achieve your requirement by inserting the watermark on each page of Word document instead of Header/Footer.
Following code example shows how to insert the watermark on each page of document. Hope this helps you.
public static void InsertWatermarkImageAtEachPage(Document doc, string watermarkImagePath)
{
DocumentBuilder builder = new DocumentBuilder(doc);
LayoutCollector collector = new LayoutCollector(doc);
int pageIndex = 1;
foreach (Section section in doc.Sections)
{
NodeCollection paragraphs = section.Body.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph para in paragraphs)
{
if (collector.GetStartPageIndex(para) == pageIndex)
{
builder.MoveToParagraph(paragraphs.IndexOf(para), 0);
builder.StartBookmark("BM_Page" + pageIndex);
builder.EndBookmark("BM_Page" + pageIndex);
pageIndex++;
}
}
}
collector = new LayoutCollector(doc);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
const int PageRelativeY = 0;
const int PageRelativeX = 0;
foreach (Bookmark bookmark in doc.Range.Bookmarks)
{
if (bookmark.Name.StartsWith("BM_"))
{
Paragraph para = (Paragraph)bookmark.BookmarkStart.ParentNode;
Shape textbox = new Shape(doc, Aspose.Words.Drawing.ShapeType.Image);
textbox.Top = PageRelativeY;
textbox.Left = PageRelativeX;
textbox.ImageData.SetImage(watermarkImagePath);
//Set the width height according to your requirements
textbox.Width = 100;
textbox.Height = 50;
textbox.BehindText = false;
para.AppendChild(textbox);
textbox.RelativeHorizontalPosition = Aspose.Words.Drawing.RelativeHorizontalPosition.Page;
textbox.RelativeVerticalPosition = Aspose.Words.Drawing.RelativeVerticalPosition.Page;
bool isInCell = bookmark.BookmarkStart.GetAncestor(NodeType.Cell) != null;
if (isInCell)
{
var renderObject = collector.GetEntity(bookmark.BookmarkStart);
layoutEnumerator.Current = renderObject;
layoutEnumerator.MoveParent(LayoutEntityType.Cell);
RectangleF location = layoutEnumerator.Rectangle;
textbox.Top = PageRelativeY - location.Y;
textbox.Left = PageRelativeX - location.X;
}
}
}
}