【问题标题】:How to add image watermarks to pdf using Aspose ?如何使用 Aspose 将图像水印添加到 pdf 中?
【发布时间】:2018-11-11 20:14:55
【问题描述】:

以下代码在pdf页面的中心添加了指定图片的水印。我希望图像在整个 pdf 页面上重复作为水印,而不是仅在中心。水印应该重复 background-repeat 属性在 css 中的工作方式。

static void Main(string[] args)
{
    Document pdfDocument = new Document(@"C:\Users\code.wines\Downloads\old.pdf");
    pdfDocument.Pages.Add();

    ImageStamp imageStamp = new ImageStamp(@"C:\Users\code.wines\Desktop\image.jpg");
    imageStamp.Background = true;

    imageStamp.Height = 350;
    imageStamp.Width = 350;
    imageStamp.Opacity = 0.5;

    imageStamp.HorizontalAlignment = HorizontalAlignment.Center;
    imageStamp.VerticalAlignment = VerticalAlignment.Center;

    for (int j = 1; j <= pdfDocument.Pages.Count; j++)
    {
        pdfDocument.Pages[j].AddStamp(imageStamp);
    }

    pdfDocument.Save(@"C:\Users\code.wines\Desktop\new.pdf");
}

【问题讨论】:

  • 您需要获取每个页面的高度,并使用页面的 y 值将 y 轴向下移动 350 像素,直到您标记整个页面

标签: c# .net pdf watermark aspose


【解决方案1】:

您可以使用嵌套循环在 PDF 页面上添加图像图章。下面的代码 sn-p 遍历页面并根据您的要求添加图像标记。它假定页边距为零,如果您的文档包含边距,那么您可以使用各自边距的值而不是零来初始化 X 和 Y 变量,并且边距值也会影响 For 循环的条件语句.下面的代码 sn-p 解释了如何在 PDF 文档的整个页面上重复水印。

//load source document
Document pdfDocument = new Document();
//add a page
pdfDocument.Pages.Add();
//load source image
ImageStamp imageStamp = new ImageStamp(dataDir + @"aspose-logo.jpg");
imageStamp.Background = true;
//set different values
imageStamp.Height = 100;
imageStamp.Width = 100;
imageStamp.Opacity = 0.5;

foreach (Page page in pdfDocument.Pages)
{
    //assuming margins as zero
    page.PageInfo.Margin.Top = 0;
    page.PageInfo.Margin.Bottom = 0;
    page.PageInfo.Margin.Left = 0;
    page.PageInfo.Margin.Right = 0;
    for (double y = 0; y < page.PageInfo.Height; y = y + imageStamp.Height)
    {
        for (double x = 0; x < page.PageInfo.Width; x = x + imageStamp.Width)
        {
            imageStamp.XIndent = x;
            imageStamp.YIndent = y;
            page.AddStamp(imageStamp);
        }
    }
}
//save generated PDF document
pdfDocument.Save( dataDir + @"New_18.5.pdf");

我希望这会有所帮助。如果您需要任何进一步的帮助,请随时与我们联系。

PS:我与 Aspose 合作,担任开发人员宣传员。

【讨论】:

  • 这就是我想要的。非常感谢!
猜你喜欢
  • 2011-08-27
  • 1970-01-01
  • 2018-12-02
  • 2021-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-31
相关资源
最近更新 更多