【问题标题】:Stretch watermark from bottom left to top right on PDF c#在PDF c#上从左下角到右上角拉伸水印
【发布时间】:2020-05-01 08:58:56
【问题描述】:

这听起来像是一个非常简单的问题,但我似乎无法弄清楚。

我想在我的 PDF 顶部放置一个水印,并希望它从左下角开始并在右上角结束。我的开始问题是我找不到左下角。我有一种感觉,取决于我打开 0,0 坐标的 PDF 文档。

这是我目前得到的:

 static string fontname = "Calibri";
        public void WaterMarkPDF3(string sourceFileName)
        {
            PdfDocument doc = PdfReader.Open(sourceFileName, PdfDocumentOpenMode.Modify);

            foreach (PdfPage page in doc.Pages)
            {
                //First calculate the cross angle from bottom left to top right
                double Angle= -Math.Atan(page.Height / page.Width) * 180 / Math.PI;
                
                //here I calculate how long the diagonal is so that my string can have the same length
                double watermarkWidth = page.Height / Math.Sin(Angle);

                XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);

                double emSize = 300;
                XFont font = new XFont(fontname, emSize, XFontStyle.Bold);
                XSize size = gfx.MeasureString("Teststring", font);

                gfx.TranslateTransform(page.Width / 2, page.Height / 2);
                
                gfx.RotateTransform(Angle);
                gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);

                XGraphicsPath path = new XGraphicsPath();

                path.AddString("Teststring", font.FontFamily, XFontStyle.Bold, emSize,
                new XPoint(0, page.Height), XStringFormats.Default);

                // add to color to the outline and filling
                XPen pen = new XPen(XColor.FromArgb(75, 0, 152, 163), 2);
                XBrush brush = new XSolidBrush(XColor.FromArgb(50, 0, 152, 163));

                gfx.DrawPath(pen, brush, path);

            }
            doc.Save(sourceFileName);
        }

据我了解,0,0 将是左上角。通常,如果我从new XPoint(0, page.Height) 开始我的水印,我希望它在左下角。正如您在所附图片中看到的那样,它位于一个“随机”的位置。

【问题讨论】:

  • 您能帮我们解释一下您正在使用的数学吗?有没有可能有你到目前为止的截图?我很乐意为您提供帮助。
  • 通常 0,0 在左上角,我不是特别了解你的所有代码,因为我从未使用过这个库,但它很容易混淆,因为它与通常的数学 x,y 不同协调。所以你可以检查一下。
  • 在这里查看:pdfsharp.com/PDFsharp/… pdfsharp 中的页面大小
  • 我在代码中添加了一些 cmets 来帮助解释数学。我也不太清楚如何使用emSize。我想要完成的是,无论上传什么大小的pdf,它都可以对角放置水印。
  • 这听起来像是一个关于一个相当简单的问题的不清楚的问题。

标签: c# pdf pdfsharp


【解决方案1】:

水印样本可以在这里找到:
https://github.com/empira/PDFsharp-samples/blob/master/samples/core/Watermark/Program.cs

示例和您的代码之间的一个区别是XStringFormat。这定义了字符串如何相对于给定点对齐。该示例只是将字符串的中心与页面的中心对齐。

您来自 cmets 的“问题”:emSize 是字体的大小(以磅为单位) - 72 磅为 1 英寸。您的 300 点是 105.833 毫米或 4.167 英寸。

您的代码 sn-p 围绕页面中心旋转。我想这也会改变页面角落的坐标。如果您想从页面的角落开始字符串,那么围绕页面的角落旋转可能更简单。
该示例在页面中心绘制水印,这是大多数人会要求的。

【讨论】:

    【解决方案2】:

    谢谢大家的帮助。

    我将文本更改为图像(公司徽标)。该代码现在适用于所有类型的页面(A4、A3、....)

    代码如下:

    public void WaterMarkPDF4(string sourceFileName)
            {
                PdfDocument doc = PdfReader.Open(sourceFileName, PdfDocumentOpenMode.Modify);
    
                foreach (PdfPage page in doc.Pages)
                {
                    double hoek = Math.Atan(page.Height / page.Width);
                    double watermarkWidth = page.Height / Math.Sin(hoek);
    
    
                    XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
                    XImage image = XImage.FromFile(TransparentImage.png");
    
                    double iWidth = image.PixelWidth * 72 / image.HorizontalResolution;
                    double iHeight = image.PixelHeight * 72 / image.HorizontalResolution;
                    double factor = watermarkWidth * 0.8 / iWidth;
    
                    XPoint punt = new XPoint();
                    punt.X = iWidth / 2;
                    punt.Y = iHeight / 2;
    
                    gfx.TranslateTransform(-iWidth / 2, -iHeight / 2);
                    gfx.RotateAtTransform(-hoek * 180 / Math.PI, punt);
                    gfx.ScaleAtTransform(factor,factor, iWidth / 2, iHeight / 2);
    
    
                    double pwidth = page.Width;
                    double pheight = page.Height;
    
                    double dx = Math.Cos(hoek) * pwidth / 2 - Math.Sin(hoek) * pheight / 2;
                    double dy = Math.Sin(hoek) * pwidth / 2 + Math.Cos(hoek) * pheight / 2;
    
                    double newHeight = (image.PixelHeight / page.Width) * watermarkWidth;
                    double y = page.Height/2  - newHeight / 2;
    
    
                    gfx.DrawImage(image, dx/factor, dy/factor);
    
                    gfx.Dispose();
    
    
                }
                doc.Save(sourceFileName);
    
            }
    

    【讨论】:

      猜你喜欢
      • 2019-03-06
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 2020-06-09
      • 2013-12-22
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多