PDF管理控件Aspose.PDF for .Net使用教程(四十五):在PDF文件中添加图章

Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用AdobeAcrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。

>>Aspose.PDF for .NET更新至最新版v20.8,欢迎下载体验。


使用Aspose.PDF for .NET可以使用ImageStamp类将图像图章添加到PDF文件。ImageStamp类提供创建基于图像的图章所需的属性,例如高度,宽度,不透明度等。

要添加图像图章:

  • 使用必需的属性创建一个Document对象和一个ImageStamp对象。
  • 调用Page类的AddStamp方法将图章添加到PDF。

以下代码段显示了如何在PDF文件中添加图章。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks();// Open documentDocument pdfDocument = new Document(dataDir+ "AddImageStamp.pdf");// Create image stampImageStamp imageStamp = new ImageStamp(dataDir + "aspose-logo.jpg");imageStamp.Background = true;imageStamp.XIndent = 100;imageStamp.YIndent = 100;imageStamp.Height = 300;imageStamp.Width = 300;imageStamp.Rotate = Rotation.on270;imageStamp.Opacity = 0.5;// Add stamp to particular pagepdfDocument.Pages[1].AddStamp(imageStamp);dataDir = dataDir + "AddImageStamp_out.pdf";// Save output documentpdfDocument.Save(dataDir);

添加图章时控制图像质量

将图像添加为图章对象时,可以控制图像的质量。ImageStamp类的Quality属性用于此目的。它以百分比表示图像质量(有效值为0..100)。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks();// Open documentDocument pdfDocument = new Document(dataDir+ "AddImageStamp.pdf");// Create image stampImageStamp imageStamp = new ImageStamp(dataDir + "aspose-logo.jpg");imageStamp.Quality = 10;pdfDocument.Pages[1].AddStamp(imageStamp);pdfDocument.Save(dataDir + "ControlImageQuality_out.pdf");

在PDF文件中添加PDF页面图章

可以使用PdfPageStamp类将PDF页面作为图章添加到PDF文件中。PdfPageStamp类提供创建基于PDF页面的图章所需的属性。可以将任何PDF文件的特定页面传递给PdfPageStamp类的构造函数。为了添加基于页面的图章,需要使用必需的属性创建Document对象和PdfPageStamp对象。之后,可以调用Page的AddStamp方法在PDF中添加图章。以下代码段显示了如何在PDF文件中添加PDF页面图章。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks();// Open documentDocument pdfDocument = new Document(dataDir+ "PDFPageStamp.pdf");// Create page stampPdfPageStamp pageStamp = new PdfPageStamp(pdfDocument.Pages[1]);pageStamp.Background = true;pageStamp.XIndent = 100;pageStamp.YIndent = 100;pageStamp.Rotate = Rotation.on180;// Add stamp to particular pagepdfDocument.Pages[1].AddStamp(pageStamp);dataDir = dataDir + "PDFPageStamp_out.pdf";// Save output documentpdfDocument.Save(dataDir);

在PDF文件中添加页码戳

可以使用PageNumber Stamp类在PDF文件中添加页码戳。PageNumber Stamp类提供了创建基于页码的图章所需的属性,如格式,边距,对齐方式,起始编 等。为了添加页码图章,您需要使用必需的属性来创建Document对象和PageNumber Stamp对象。之后,可以调用Page的AddStamp方法在PDF中添加图章。还可以设置页码标记的字体属性。以下代码段显示了如何在PDF文件中添加页码。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks();// Open documentDocument pdfDocument = new Document(dataDir+ "PageNumberStamp.pdf");// Create page number stampPageNumberStamp pageNumberStamp = new PageNumberStamp();// Whether the stamp is backgroundpageNumberStamp.Background = false;pageNumberStamp.Format = "Page # of " + pdfDocument.Pages.Count;pageNumberStamp.BottomMargin = 10;pageNumberStamp.HorizontalAlignment = HorizontalAlignment.Center;pageNumberStamp.StartingNumber = 1;// Set text propertiespageNumberStamp.TextState.Font = FontRepository.FindFont("Arial");pageNumberStamp.TextState.FontSize = 14.0F;pageNumberStamp.TextState.FontStyle = FontStyles.Bold;pageNumberStamp.TextState.FontStyle = FontStyles.Italic;pageNumberStamp.TextState.ForegroundColor = Color.Aqua;// Add stamp to particular pagepdfDocument.Pages[1].AddStamp(pageNumberStamp);dataDir = dataDir + "PageNumberStamp_out.pdf";// Save output documentpdfDocument.Save(dataDir);

在PDF中添加日期时间戳

可以使用TextStamp类在PDF文件中添加文本戳。TextStamp类提供创建基于文本的图章所需的属性,例如字体大小,字体样式和字体颜色等。为了添加文本图章,需要使用必需的属性来创建Document对象和TextStamp对象。之后,可以调用Page的AddStamp方法在PDF中添加图章。以下代码段显示了如何在PDF文件中添加文本戳。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks();// Open documentDocument pdfDocument = new Document(dataDir+ "AddTextStamp.pdf");string annotationText = string.Empty;annotationText = DateTime.Now.ToString("MM/dd/yy hh:mm:ss tt ");// Create text stampTextStamp textStamp = new TextStamp(annotationText);// Set properties of the stamptextStamp.BottomMargin = 10;textStamp.RightMargin = 20;textStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;textStamp.VerticalAlignment = VerticalAlignment.Bottom;// Adding stamp on stamp collectionpdfDocument.Pages[1].AddStamp(textStamp);DefaultAppearance default_appearance = new DefaultAppearance("Arial", 6, System.Drawing.Color.Black);FreeTextAnnotation textAnnotation = new FreeTextAnnotation(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(0, 0, 0, 0), default_appearance);textAnnotation.Name = "Stamp";textAnnotation.Accept(new AnnotationSelector(textAnnotation));textAnnotation.Contents = textStamp.Value;// TextAnnotation.Open = true;// TextAnnotation.Icon = Aspose.Pdf.InteractiveFeatures.Annotations.TextIcon.Key;Border border = new Border(textAnnotation);border.Width = 0;border.Dash = new Dash(1, 1);textAnnotation.Border = border;textAnnotation.Rect = new Aspose.Pdf.Rectangle(0, 0, 0, 0);pdfDocument.Pages[1].Annotations.Add(textAnnotation);dataDir = dataDir + "AddDateTimeStamp_out.pdf";// Save output documentpdfDocument.Save(dataDir);

在PDF文件中添加文本图章

可以使用TextStamp类在PDF文件中添加文本戳。TextStamp类提供创建基于文本的图章所需的属性,例如字体大小,字体样式和字体颜色等。为了添加文本图章,需要使用必需的属性来创建Document对象和TextStamp对象。之后,可以调用Page的AddStamp方法在PDF中添加图章。以下代码段显示了如何在PDF文件中添加文本戳。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks();// Open documentDocument pdfDocument = new Document(dataDir+ "AddTextStamp.pdf");// Create text stampTextStamp textStamp = new TextStamp("Sample Stamp");// Set whether stamp is backgroundtextStamp.Background = true;// Set origintextStamp.XIndent = 100;textStamp.YIndent = 100;// Rotate stamptextStamp.Rotate = Rotation.on90;// Set text propertiestextStamp.TextState.Font = FontRepository.FindFont("Arial");textStamp.TextState.FontSize = 14.0F;textStamp.TextState.FontStyle = FontStyles.Bold;textStamp.TextState.FontStyle = FontStyles.Italic;textStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Aqua);// Add stamp to particular pagepdfDocument.Pages[1].AddStamp(textStamp);dataDir = dataDir + "AddTextStamp_out.pdf";// Save output documentpdfDocument.Save(dataDir);

还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年7月11日
下一篇 2020年7月11日

相关推荐

发表回复

登录后才能评论