为有中文需求的客户提供多渠道中文技术支持.

Sun Jul 03, 2022 7:21 am

你好,我在官网英文网站查看关于添加文字水印的教程
通过测试,可以在页面中间加入一个倾斜45度的文字水印
实例代码如下:

Code: Select all
//Create a PdfDocument object
            PdfDocument pdf = new PdfDocument();

            //Load a sample PDF document
            pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //Create a PdfTrueTypeFont object
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 50f), true);

            //Set the watermark text
            string text = "CONFIDENTIAL";

            //Measure the text size
            SizeF textSize = font.MeasureString(text);

            //Calculate the values of two offset variables,
            //which will be used to calculate the translation amount of the coordinate system
            float offset1 = (float)(textSize.Width * System.Math.Sqrt(2) / 4);
            float offset2 = (float)(textSize.Height * System.Math.Sqrt(2) / 4);

            //Traverse all the pages in the document
            foreach (PdfPageBase page in pdf.Pages)
            {
                //Set the page transparency
                page.Canvas.SetTransparency(0.8f);

                //Translate the coordinate system by specified coordinates
                page.Canvas.TranslateTransform(page.Canvas.Size.Width / 2 - offset1 - offset2, page.Canvas.Size.Height / 2 + offset1 - offset2);

                //Rotate the coordinate system 45 degrees counterclockwise
                page.Canvas.RotateTransform(-45);

                //Draw watermark text on the page
                page.Canvas.DrawString(text, font, PdfBrushes.DarkGray, 0, 0);
            }

            //Save the changes to another file
            pdf.SaveToFile("TextWatermark.pdf");


但是对代码看的不是很明白
位置似乎都是通过计算来实现的
没有类似于HorizontalAlignment.Center、VerticalAlignment.Top这种直接可调用的位置参数
如果我想在页面的左上角、顶部中间、右上角、正中间、左下角、底部中间、右下角7个位置同时加入文字水印
代码应该如何写呢?
谢谢!

howzehowze
 
Posts: 18
Joined: Wed Jan 08, 2020 1:33 am

Mon Jul 04, 2022 10:08 am

Hello,

您好,
感谢您的询问,是的,我们PDF绘制水印是根据具体位置计算来绘制到画布上的。针对你的需求,你可以参考下面代码来添加水印文本。如果有其它疑问请随时联系。
Code: Select all
    static void Main(string[] args)
     {
      //加载一个示例PDF文档
      PdfDocument pdf = new PdfDocument();
      pdf.LoadFromFile(@"Test.pdf");
      PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f), true);
      PdfPageBase page = pdf.Pages[0];
      //设置透明度
      page.Canvas.SetTransparency(0.5f, 0.5f, PdfBlendMode.Normal);
      //设置水印文本
      string text = "hello";
      //测量文本大小
      float textWidth = font.MeasureString(text).Width;
      float textHeight = font.MeasureString(text).Height;
      drawWaterText(page, "hello", textWidth, textHeight, font);
      pdf.SaveToFile(@"TextWatermark.pdf");
    }
      private static void drawWaterText(PdfPageBase page, String text, float width, float height, PdfTrueTypeFont font)
     {
     PdfBrush brush = PdfBrushes.Violet;
     //左上角
     PdfGraphicsState state = page.Canvas.Save(); 
     page.Canvas.RotateTransform(-25, new PointF(0, 10));
     page.Canvas.DrawString(text, font, brush, new PointF(0, 10));
     page.Canvas.Restore(state);
     //右上角
     PdfGraphicsState state2 = page.Canvas.Save();
     page.Canvas.RotateTransform(-25,new PointF(page.ActualSize.Width - width, 10));
     page.Canvas.DrawString(text, font, brush, new PointF(page.ActualSize.Width - width-5, 10));
     page.Canvas.Restore(state2);
     //左下角
     PdfGraphicsState state3 = page.Canvas.Save();
     page.Canvas.RotateTransform(-25, new PointF(0, page.ActualSize.Height - height));
     page.Canvas.DrawString(text, font, brush, new PointF(0, page.ActualSize.Height - height));
     page.Canvas.Restore(state3);
     //右下角
     PdfGraphicsState state4 = page.Canvas.Save();
     page.Canvas.RotateTransform(-25, new PointF(page.ActualSize.Width - width-5, page.ActualSize.Height - height));
     page.Canvas.DrawString(text, font, brush, new PointF(page.ActualSize.Width - width-5, page.ActualSize.Height - height));
     page.Canvas.Restore(state4);
     //顶部中间
     PdfGraphicsState state5 = page.Canvas.Save();
     page.Canvas.RotateTransform(-25, new PointF(page.ActualSize.Width / 2 - width / 2, 10));
     page.Canvas.DrawString(text, font, brush, new PointF(page.ActualSize.Width / 2 - width / 2, 10));
     page.Canvas.Restore(state5);
     //底部部中间
     PdfGraphicsState state6 = page.Canvas.Save();
     page.Canvas.RotateTransform(-25, new PointF(page.ActualSize.Width / 2 - width / 2, page.ActualSize.Height - height));
     page.Canvas.DrawString(text, font, brush, new PointF(page.ActualSize.Width / 2 - width / 2, page.ActualSize.Height - height));
     page.Canvas.Restore(state6);
     //正中间
     PdfGraphicsState state7 = page.Canvas.Save();
     page.Canvas.RotateTransform(-25, new PointF(page.ActualSize.Width / 2 - width / 2, page.ActualSize.Height / 2 - height / 2));
     page.Canvas.DrawString(text, font, brush, new PointF(page.ActualSize.Width / 2 - width / 2, page.ActualSize.Height / 2 - height / 2));
     page.Canvas.Restore(state7);
 }

Sincerely,
Simple
E-iceblue support team
User avatar

Simple.Li
 
Posts: 248
Joined: Fri Jul 01, 2022 2:33 am

Wed Jul 06, 2022 1:53 am

非常感谢你的解答

howzehowze
 
Posts: 18
Joined: Wed Jan 08, 2020 1:33 am

Wed Jul 06, 2022 9:12 am

Hello,

您好,感谢您的来信,如果您以后遇到什么问题,可以随时与我们联系。

Sincerely,
Simple
E-iceblue support team
User avatar

Simple.Li
 
Posts: 248
Joined: Fri Jul 01, 2022 2:33 am

Return to 中文技术支持