Add multi-line watermark on header or top-left

Document.Doc2PdfSaveWithWaterMarkOut636608796292274550.pdf (43.4 KB)

When I tried to add multiple watermark on top left in header portion, I was unable to do that. It’s repeating one over another. Below is my code and I have attached the output file too.

static void AddWaterMarktoHeader()
{
Document doc = new Document(@“I:\Document\PDFConversion\testFile.docx”);
DocumentBuilder builder = new DocumentBuilder(doc);
Section currentSection = builder.CurrentSection;

        PageSetup pageSetup = currentSection.PageSetup;

        Shape watermark = new Shape(doc, ShapeType.TextPlainText);
        Shape watermark1 = new Shape(doc, ShapeType.TextPlainText);
        Shape watermark2 = new Shape(doc, ShapeType.TextPlainText);
        Shape watermark3 = new Shape(doc, ShapeType.TextPlainText);

        // Set up the text of the watermark.

        String waterText = "Approved";

        watermark.TextPath.Text = waterText;
        watermark.TextPath.FontFamily = "Arial";
        watermark.Width = 50;
        watermark.Height = 20;
        watermark.Fill.Color = System.Drawing.Color.Red; // Try LightGray to get more Word-style watermark
        watermark.StrokeColor = System.Drawing.Color.Red; // Try LightGray to get more Word-style watermark
        watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.LeftMargin;
        watermark.DistanceLeft = 200.0;
        watermark.DistanceTop = 200.0;
        watermark.RelativeVerticalPosition = RelativeVerticalPosition.TopMargin;
        watermark.WrapType = WrapType.None;
        watermark.VerticalAlignment = VerticalAlignment.Top;
        watermark.HorizontalAlignment = HorizontalAlignment.Left;




        watermark1.TextPath.Text = "Approved1";
        watermark1.TextPath.FontFamily = "Arial";
        watermark1.Width = 50;
        watermark1.Height = 20;
        watermark1.Fill.Color = System.Drawing.Color.Green; // Try LightGray to get more Word-style watermark
        watermark1.StrokeColor = System.Drawing.Color.Green; // Try LightGray to get more Word-style watermark
        watermark1.RelativeHorizontalPosition = RelativeHorizontalPosition.LeftMargin;
        watermark1.RelativeVerticalPosition = RelativeVerticalPosition.TopMargin;
        watermark1.WrapType = WrapType.None;
        watermark1.VerticalAlignment = VerticalAlignment.Top;
        watermark1.HorizontalAlignment = HorizontalAlignment.Left;

        watermark2.TextPath.Text = "Approved2";
        watermark2.TextPath.FontFamily = "Arial";
        watermark2.Width = 50;
        watermark2.Height = 20;
        watermark2.Fill.Color = System.Drawing.Color.Green; // Try LightGray to get more Word-style watermark
        watermark2.StrokeColor = System.Drawing.Color.Green; // Try LightGray to get more Word-style watermark
        watermark2.RelativeHorizontalPosition = RelativeHorizontalPosition.LeftMargin;
        watermark2.RelativeVerticalPosition = RelativeVerticalPosition.TopMargin;
        watermark2.WrapType = WrapType.None;
        watermark2.VerticalAlignment = VerticalAlignment.Top;
        watermark2.HorizontalAlignment = HorizontalAlignment.Left;

        //watermark3.TextPath.Text = waterText;
        //watermark3.TextPath.FontFamily = "Arial";
        //watermark3.Width = 50;
        //watermark3.Height = 20;
        //watermark3.Fill.Color = System.Drawing.Color.Green; // Try LightGray to get more Word-style watermark
        //watermark3.StrokeColor = System.Drawing.Color.Green; // Try LightGray to get more Word-style watermark
        //watermark3.RelativeHorizontalPosition = RelativeHorizontalPosition.LeftMargin;
        //watermark3.RelativeVerticalPosition = RelativeVerticalPosition.TopMargin;
        //watermark3.WrapType = WrapType.None;
        //watermark3.VerticalAlignment = VerticalAlignment.Top;
        //watermark3.HorizontalAlignment = HorizontalAlignment.Left;

        // Insert the watermark into all headers of each document section.
        Paragraph watermarkPara = new Paragraph(doc);
        watermarkPara.AppendChild(watermark);
        watermarkPara.AppendChild(watermark1);
        watermarkPara.AppendChild(watermark2);
        //watermarkPara.AppendChild(watermark3);
        foreach (Section sect in doc.Sections)
        {

            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);

            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);

            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
        }
        doc.Save(@"I:\Document\PDFConversion\" + "Document.Doc2PdfSaveWithWaterMarkOut" + DateTime.Now.Ticks + ".pdf", SaveFormat.Pdf);
    }

Thanks
Satyendra Kumar


This Topic is created by sohail.aspose using the Email to Topic plugin.

@itsathere,

Thanks for your inquiry. To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your input Word document
  • Your expected Word document (DOCX file) showing the correct output. Please create this document by using MS Word. We will investigate the structure of your expected document as to how you want your final output be generated like.

As soon as you get these pieces of information ready, we will start further investigation into your issue and provide you more information. Thanks for your cooperation.

PDFConversion.zip (1.7 MB)
I have attached word file and output pdf file.Kindly, Check it.

Note:
In output file there are two watermarks

  • In header top-left multi-line in multi color. Current text is
    Approved
    Orca_Code_added-16162
    Revision: D-1234
  • In background Hello!! is there as watermark too.

@itsathere,

You can meet this requirement by using the following code (please also see 18.5.pdf (42.1 KB)):

Document doc = new Document(MyDir + @"PDFConversion\testFile.docx");
PageSetup pageSetup = doc.FirstSection.PageSetup;
// Create One Shape
Shape watermark = new Shape(doc, ShapeType.TextPlainText);
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
watermark.Width = 72*4;
watermark.Height = 72* 1;
watermark.Top = 72 * 0.5;
watermark.Left = 72 * 0.25;
// Create Paragraph to hold above Shape
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.AppendChild(watermark);
// Add three Paragraphs to one Shape
Paragraph para1 = new Paragraph(doc);
para1.ParagraphFormat.SpaceBefore = 0;
para1.ParagraphFormat.SpaceAfter = 0;
Paragraph para2 = (Paragraph) para1.Clone(false);
Paragraph para3 = (Paragraph)para1.Clone(false);
// Three Run nodes to hold different formatting for each line
Run run1 = new Run(doc, "Approved");
Run run2 = new Run(doc, "Orca_Code_added-16162");
Run run3 = new Run(doc, "Revision: D-1234");
// Specify formatting for each Run
run1.Font.Color = Color.Red;
run2.Font.Color = Color.Green;
run3.Font.Color = Color.Blue;
// Add pne Run to each Paragraph
para1.AppendChild(run1);
para2.AppendChild(run2);
para3.AppendChild(run3);
// Append three paragraphs to one Shape
watermark.AppendChild(para1);
watermark.AppendChild(para2);
watermark.AppendChild(para3);
// insert watermark para to each header in sections
foreach (Section sect in doc.Sections)
{
    insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
    insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
    insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
}

doc.Save(MyDir + @"PDFConversion\18.5.pdf");

I need one more Hello!! watermark in background. Can you do that too. I am attaching the reference fileFile1.pdf (1.7 MB)

@itsathere,

Yes you can insert Hello watermark at the center of pages. For this, you need to create a separate Shape object and specify the following options:

watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
watermark.WrapType = WrapType.None;
watermark.VerticalAlignment = VerticalAlignment.Center;
watermark.HorizontalAlignment = HorizontalAlignment.Center;

Thanks for response.

Just have change in requirement. I want to print top-left watermark label(Approved/Orca_Code_added-16162/Revision: D-1234) one by one.On each request it will print one above mentioned Run run1 = new Run(doc, “Approved”); method at a time but watermark label should not overlap on multiple request.Each request will add watermark below the last added watermark.
I am using below method and it’s overlapping watermark label

public static bool AddWatermark(string filePath, double height, double width, double top, double left, double rotation, string position, string watermarkText, Color fontColor, string fontFamily, out string watermarkedfilepath, out string message)
    {
        //add watermark to pdf

        bool retval = false;
        watermarkedfilepath = string.Empty;
        message = string.Empty;
        try
        {
            Document doc = new Document(filePath);
            PageSetup pageSetup = doc.FirstSection.PageSetup;
            Paragraph watermarkPara = new Paragraph(doc);
            if (position == "top-left")
            {
                // Create One Shape
                Shape watermark = new Shape(doc, ShapeType.TextPlainText);
                watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
                watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
                watermark.TextPath.FontFamily = fontFamily ?? "Arial";
                watermark.Width = width;//72 * 4
                watermark.Height = height;//72 * 1
                watermark.Top = top; //72 * 0.5
                watermark.Left = left;//72 * 0.25
                                      // Create Paragraph to hold above Shape
                watermarkPara = new Paragraph(doc);
                watermarkPara.AppendChild(watermark);



                // Add three Paragraphs to one Shape
                Paragraph para1 = new Paragraph(doc);
                para1.ParagraphFormat.SpaceBefore = 0;
                para1.ParagraphFormat.SpaceAfter = 0;
                //Paragraph para2 = (Paragraph)para1.Clone(false);
                //Paragraph para3 = (Paragraph)para1.Clone(false);

                #region  // only one watermark at time
                // Three Run nodes to hold different formatting for each line
                Run run1 = new Run(doc, watermarkText); //"Approved"
                //Run run2 = new Run(doc, "Orca_Code_added-16162");//
                //Run run3 = new Run(doc, "Revision: D-1234");// 
                #endregion

                // Specify formatting for each Run
                run1.Font.Color = fontColor; //Color.Red
                //run2.Font.Color = Color.Green;
                //run3.Font.Color = Color.Blue;
                //fontname if not availe geru

                // Add pne Run to each Paragraph
                para1.AppendChild(run1);
                //para2.AppendChild(run2);
                //para3.AppendChild(run3);

                // Append three paragraphs to one Shape
                watermark.AppendChild(para1);
                //watermark.AppendChild(para2);
                //watermark.AppendChild(para3);
            }


            if (position == "centre")
            {
                #region Centred Watermark
                Shape watermark1 = new Shape(doc, ShapeType.TextPlainText);

                // Set up the text of the watermark.
                watermark1.TextPath.Text = watermarkText;
                watermark1.TextPath.FontFamily = fontFamily ?? "Arial";
                watermark1.Width = width; //600
                watermark1.Height = height;//100
                // Text will be directed from the bottom-left to the top-right corner.
                watermark1.Rotation = rotation;//-40

                // Remove the following two lines if you need a solid black text.
                watermark1.Fill.Color = fontColor; // Try LightGray to get more Word-style watermark(System.Drawing.Color.Gray)
                watermark1.StrokeColor = fontColor; // Try LightGray to get more Word-style watermark(System.Drawing.Color.Gray)

                // Place the watermark in the page center.
                watermark1.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
                watermark1.RelativeVerticalPosition = RelativeVerticalPosition.Page;
                watermark1.WrapType = WrapType.None;
                watermark1.VerticalAlignment = VerticalAlignment.Center;
                watermark1.HorizontalAlignment = HorizontalAlignment.Center;

                // Create a new paragraph and append the watermark to this paragraph.
                watermarkPara.AppendChild(watermark1);

                #endregion 
            }

            // insert watermark para to each header in sections
            foreach (Section sect in doc.Sections)
            {
                insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
                insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
                insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
            }

            //watermarkedfilepath = outputPath + "WaterMarked_" + DateTime.Now.Ticks + ".pdf";
            watermarkedfilepath = outputPath + "WaterMarked_" + DateTime.Now.Ticks + ".docx";
            doc.Save(watermarkedfilepath);
            retval = true;
        }
        catch (Exception ex)
        {
            retval = false;
            message = ex.Message + (ex.InnerException != null ? " More information: " + ex.InnerException.Message : "");
            throw;
        }
        return retval;
    }

for more you can check previously the attached documents.

and I want to add watermark in pdf too. And on each request it will print one watermark label but on multiple request it should not overlap watermark label.

@itsathere,

You can maintain an ArrayList containing only the watermark Shapes. Before adding a new watermark Shape, please first check the position (Top, Left) and dimensions (Width, Height) of previously added Shapes in ArrayList so that the new Shape to be inserted in Document does not overlap the previous Shapes. Hope, this helps.

Can you share a sample code snippet.

@itsathere,

Please try using the following code. Hope, this helps.

public static Shape CreateShape(Document doc, double left, double top, double height, double width, string text)
{
    Shape watermark = new Shape(doc, ShapeType.TextPlainText);
    watermark.TextPath.Text = text;

    watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
    watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    watermark.Width = width;
    watermark.Height = height;
    watermark.Top = top;
    watermark.Left = left;

    watermark.AlternativeText = "watermark";

    return watermark;
}

private static void InsertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
{
    HeaderFooter header = sect.HeadersFooters[headerType];

    if (header == null)
    {
        // There is no header of the specified type in the current section, create it.
        header = new HeaderFooter(sect.Document, headerType);
        sect.HeadersFooters.Add(header);
    }

    // Insert a clone of the watermark into the header.
    header.AppendChild(watermarkPara.Clone(true));
}

Document doc = new Document(MyDir + @"PDFConversion\testFile.docx");

// First lets insert some watermarks
Shape watermark1 = CreateShape(doc, 72 * 0.25, 72 * 0.5, 72 * .1, 72 * .5, "first text");
Shape watermark2 = CreateShape(doc, 72 * 0.25, 72 * 0.6, 72 * .1, 72 * .5, "second text");

doc.FirstSection.Body.FirstParagraph.AppendChild(watermark1);
doc.FirstSection.Body.FirstParagraph.AppendChild(watermark2);

// now you want to insert a new watermark such that it should not overlap with previous watermark Shapes
double maxTop = 0;
double height = 0;
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    if (shape.AlternativeText.Equals("watermark"))
    {
        if (maxTop < shape.Top)
        {
            maxTop = shape.Top;
            height = shape.Height;
        }
    }
}

Shape watermark3 = CreateShape(doc, 72 * 0.25, maxTop + height, 72 * .1, 72 * .5, "third text");
           
// Create Paragraph to hold above Shapes
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.AppendChild(watermark1);
watermarkPara.AppendChild(watermark2);
watermarkPara.AppendChild(watermark3);

// insert watermark para to each header in sections
foreach (Section sect in doc.Sections)
{
    InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
    InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
    InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
}

doc.Save(MyDir + @"PDFConversion\18.5.pdf");