Two issues with adding watermarks in Aspose.PDF

First, I’ll post the code I wrote, and then explain the issues at the end:

/// <summary>
        /// 为pdf添加水印
        /// </summary>
        /// <param name="DocuementFileName">文档名称</param>
        /// <param name="SaveFileName">保存后的名称</param>
        /// <param name="WaterMarkContent">水印文本</param>
        /// <param name="WaterMarkOpactiyNum">不透明度</param>
        /// <param name="WaterMarkRotateDegNum">旋转角度</param>
        /// <param name="WaterMarkHorAlign">水平对齐方式</param>
        /// <param name="WaterMarkVerAlign">垂直对齐方式</param>
        /// <param name="WaterMarkFontColor">字体颜色</param>
        /// <param name="WaterMarkFontName">字体名称</param>
        /// <param name="WaterMarkFontSize">字体大小</param>
        public static void AddWaterMark2Pdf(
            string DocuementFileName,
            string SaveFileName,
            string WaterMarkContent,
            float WaterMarkOpactiyNum,
            string WaterMarkRotateDegNum,
            int WaterMarkHorAlignIndex,
            int WaterMarkVerAlignIndex,
            System.Drawing.Color WaterMarkFontColor,
            string WaterMarkFontName = "宋体",
            float WaterMarkFontSize = 12,
            string AddImageFileName = "",
            string AddImageHeight = "100",
            string AddImageWidth = "100",
            bool FullScreenMode = false,
            int WaterDmi_Hor = 25,
            int WaterDmi_Ver = 25,
            bool CustomPostion = false,
            double CustomPostion_X = 0,
            double CustomPostion_Y = 0)
        {
            using Aspose.Pdf.Document TempLoadDocument = new Aspose.Pdf.Document(DocuementFileName);
            #region foreach添加水印
            foreach (Aspose.Pdf.Page TempPage in TempLoadDocument.Pages)
            {
                double PageWidth = TempPage.Rect.Width;
                double PageHeight = TempPage.Rect.Height;

                int PageRow = (int)(PageHeight / WaterDmi_Hor);
                int PageCol = (int)(PageWidth / WaterDmi_Ver);

                if (!string.IsNullOrEmpty(AddImageFileName))
                {
                    lock (TempPage)
                    {
                        if (FullScreenMode)
                        {
                            for (int i = 0; i <= PageRow; i++)
                            {
                                for (int j = 0; j <= PageCol; j++)
                                {
                                    WatermarkArtifact TempAddArtifact = new WatermarkArtifact();

                                    TempAddArtifact.ArtifactHorizontalAlignment = GetHorizontalAlignment(
                                        WaterMarkHorAlignIndex);
                                    TempAddArtifact.ArtifactVerticalAlignment = GetVerticalAlignment(
                                        WaterMarkVerAlignIndex);
                                    TempAddArtifact.Rotation = Convert.ToInt32(WaterMarkRotateDegNum);
                                    TempAddArtifact.Opacity = WaterMarkOpactiyNum;
                                    TempAddArtifact.IsBackground = false;
                                    TempAddArtifact.SetImage(AddImageFileName);
                                    TempAddArtifact.Position = new Aspose.Pdf.Point(j * WaterDmi_Hor, i * WaterDmi_Ver);
                                    lock (TempAddArtifact)
                                    {
                                        TempPage.Artifacts.Add(TempAddArtifact);
                                    }
                                }
                            }
                        }
                        else
                        {
                            ImageStamp TempAddArtifact = new ImageStamp(AddImageFileName)
                            {
                                Opacity = WaterMarkOpactiyNum,
                                RotateAngle = Convert.ToInt32(WaterMarkRotateDegNum),
                                Width = double.Parse(AddImageWidth),
                                Height = double.Parse(AddImageHeight),
                                HorizontalAlignment = GetHorizontalAlignment(WaterMarkHorAlignIndex),
                                VerticalAlignment = GetVerticalAlignment(WaterMarkVerAlignIndex),
                                XIndent = 100,
                                YIndent = 100
                            };
                            TempPage.AddStamp(TempAddArtifact);
                        }
                    }
                }
                //text watermark
                else
                {
                    lock (TempPage)
                    {
                        WatermarkArtifact TempAddArtifact = new WatermarkArtifact();

                        TextState TempTextState = new TextState
                        {
                            Font = FontRepository.FindFont(WaterMarkFontName),
                            FontSize = WaterMarkFontSize,
                            ForegroundColor =
                                Aspose.Pdf.Color
                                    .FromArgb(WaterMarkFontColor.R, WaterMarkFontColor.G, WaterMarkFontColor.B)
                        };

                        TempAddArtifact.SetTextAndState(WaterMarkContent, TempTextState);
                        TempAddArtifact.ArtifactHorizontalAlignment = GetHorizontalAlignment(WaterMarkHorAlignIndex);
                        TempAddArtifact.ArtifactVerticalAlignment = GetVerticalAlignment(WaterMarkVerAlignIndex);
                        TempAddArtifact.Rotation = Convert.ToInt32(WaterMarkRotateDegNum);
                        TempAddArtifact.Opacity = WaterMarkOpactiyNum;
                        TempAddArtifact.IsBackground = false;

                        if (CustomPostion)
                        {
                            TempAddArtifact.Position = new Aspose.Pdf.Point(CustomPostion_X, CustomPostion_Y);
                        }

                        lock (TempAddArtifact)
                        {
                            if (FullScreenMode)
                            {
                                for (int i = 0; i <= PageRow; i++)
                                {
                                    for (int j = 0; j <= PageCol; j++)
                                    {
                                        TempAddArtifact.Position = new Aspose.Pdf.Point(
                                            j * WaterDmi_Hor,
                                            i * WaterDmi_Ver);
                                        TempPage.Artifacts.Add(TempAddArtifact);
                                        Debug.WriteLine($"已经添加第{i}行第{j}列的水印...");
                                    }
                                }
                            }
                            else
                            {
                                TempPage.Artifacts.Add(TempAddArtifact);
                            }
                        }
                    }
                }
            }
            #endregion
            TempLoadDocument.Save(SaveFileName, Aspose.Pdf.SaveFormat.Pdf);
        }

        private static Aspose.Pdf.HorizontalAlignment GetHorizontalAlignment(int index)
        {
            return index switch
            {
                0 => Aspose.Pdf.HorizontalAlignment.None,
                1 => Aspose.Pdf.HorizontalAlignment.Left,
                2 => Aspose.Pdf.HorizontalAlignment.Center,
                3 => Aspose.Pdf.HorizontalAlignment.Right,
                4 => Aspose.Pdf.HorizontalAlignment.Justify,
                5 => Aspose.Pdf.HorizontalAlignment.FullJustify,
                _ => Aspose.Pdf.HorizontalAlignment.None,
            };
        }

        private static Aspose.Pdf.VerticalAlignment GetVerticalAlignment(int index)
        {
            return index switch
            {
                0 => Aspose.Pdf.VerticalAlignment.None,
                1 => Aspose.Pdf.VerticalAlignment.Top,
                2 => Aspose.Pdf.VerticalAlignment.Center,
                3 => Aspose.Pdf.VerticalAlignment.Bottom,
                _ => Aspose.Pdf.VerticalAlignment.None,
            };
        }

First Issue:With version 24.9.0, the above code works for batch-adding multiple watermarks (specifically text watermarks). However, after upgrading to the latest version, it seems only one watermark is added—even for the same document. Could there be an issue with the code?

Second Issue: For PDFs exported from Word, the watermark transparency settings work fine. But for PDFs exported from CorelDRAW (CDR), the transparency doesn’t seem to take effect—even when set to 0.5, it appears fully opaque (as if it were 1.0). However, if I convert the CDR-generated PDF to Word and then back to PDF, the transparency behaves correctly. Could you help identify the problem?

Attached is a sample document related to the second issue, with notes on the export method:

Aspose.Pdf的问题文档.zip (2.0 MB)

@jidesheng

To address the issues you’re experiencing with adding watermarks in Aspose.PDF, let’s break down each problem:

First Issue: Multiple Watermarks Not Being Added

It seems that after upgrading to the latest version, your code is only adding one watermark instead of multiple. This could be due to how the WatermarkArtifact is being reused in your loop. When you add the same instance of WatermarkArtifact to multiple pages or positions, it may not behave as expected.

To resolve this, ensure that you create a new instance of WatermarkArtifact for each watermark you want to add. Here’s a modified snippet of your code that creates a new instance inside the loop:

if (FullScreenMode)
{
    for (int i = 0; i <= PageRow; i++)
    {
        for (int j = 0; j <= PageCol; j++)
        {
            WatermarkArtifact TempAddArtifact = new WatermarkArtifact(); // Create a new instance here

            TempAddArtifact.ArtifactHorizontalAlignment = GetHorizontalAlignment(WaterMarkHorAlignIndex);
            TempAddArtifact.ArtifactVerticalAlignment = GetVerticalAlignment(WaterMarkVerAlignIndex);
            TempAddArtifact.Rotation = Convert.ToInt32(WaterMarkRotateDegNum);
            TempAddArtifact.Opacity = WaterMarkOpactiyNum;
            TempAddArtifact.IsBackground = false;
            TempAddArtifact.SetTextAndState(WaterMarkContent, TempTextState); // Assuming you are adding text watermark
            TempAddArtifact.Position = new Aspose.Pdf.Point(j * WaterDmi_Hor, i * WaterDmi_Ver);
            TempPage.Artifacts.Add(TempAddArtifact);
        }
    }
}

Second Issue: Transparency Not Working with CDR Exported PDFs

The issue with transparency settings not taking effect for PDFs exported from CorelDRAW (CDR) could be related to how the PDF is structured or how the transparency is handled in the original document. Different applications may handle transparency differently, which can lead to inconsistencies.

To troubleshoot this, consider the following steps:

  1. Check PDF Version: Ensure that the PDF version being generated from CorelDRAW supports transparency. Some older PDF versions may not handle transparency correctly.
  2. Test with Different Settings: Experiment with different opacity values and see if there’s any change. Sometimes, values that seem correct may not render as expected due to the PDF’s internal structure.
  3. Convert and Test: As you mentioned, converting the CDR-generated PDF to Word and back to PDF resolves the issue. This indicates that the original PDF may have compatibility issues. If possible, continue using this conversion method as a workaround.

If the problem persists, consider reaching out to Aspose support with the specific PDF file for further analysis.

Conclusion

By ensuring that you create new instances of WatermarkArtifact for each watermark and checking the PDF settings for transparency, you should be able to resolve the issues you’re facing. If you need further assistance, feel free to ask!

Sources:
[1]: AddTextStamp.cs