下面的代码在pdf页面的中心添加指定图像的水印。我希望图像被重复作为水印在整个pdf页面上,而不是只在中心。水印应该重复背景重复属性在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");
}
发布于 2018-06-02 09:00:16
您可以使用嵌套循环在PDF页面上添加图像戳记。下面的代码片段遍历页面,并根据您的要求添加图像戳。它假设页边距为零,如果您的文档包括页边距,那么您可以使用各自的页边距值而不是零来初始化X
和Y
变量,而且边距值也会影响For
循环的条件语句。下面的代码片段解释了如何在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一起工作,担任开发布道者。
https://stackoverflow.com/questions/50647625
复制相似问题