翻译|使用教程|编辑:李显亮|2020-08-14 10:07:20.607|阅读 530 次
概述:在本系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍如何在PDF文件中添加图章。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
Aspose.PDF for .NET是一种高PDF处理和解析API,用于在跨平台应用程序中执行文档管理和操作任务。API可以轻松用于生成、修改、转换、渲染、保护和打印PDF文档,而无需使用Adobe Acrobat。此外,API还提供PDF压缩选项,表格创建和操作,图形和图像功能,广泛的超链接功能,印章和水印任务,扩展的安全控制和自定义字体处理。
在接下来的系列教程中,将为开发者带来Aspose.PDF for .NET的一系列使用教程,例如进行文档间的转换,如何标记PDF文件,如何使用表单和图表等等。本文将介绍如何在PDF文件中添加图章。
>>Aspose.PDF for .NET更新至最新版v20.8,欢迎下载体验。
使用Aspose.PDF for .NET可以使用ImageStamp类将图像图章添加到PDF文件。ImageStamp类提供创建基于图像的图章所需的属性,例如高度,宽度,不透明度等。
要添加图像图章:
以下代码段显示了如何在PDF文件中添加图章。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); // Open document Document pdfDocument = new Document(dataDir+ "AddImageStamp.pdf"); // Create image stamp ImageStamp 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 page pdfDocument.Pages[1].AddStamp(imageStamp); dataDir = dataDir + "AddImageStamp_out.pdf"; // Save output document pdfDocument.Save(dataDir);
将图像添加为图章对象时,可以控制图像的质量。ImageStamp类的Quality属性用于此目的。它以百分比表示图像质量(有效值为0..100)。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); // Open document Document pdfDocument = new Document(dataDir+ "AddImageStamp.pdf"); // Create image stamp ImageStamp imageStamp = new ImageStamp(dataDir + "aspose-logo.jpg"); imageStamp.Quality = 10; pdfDocument.Pages[1].AddStamp(imageStamp); pdfDocument.Save(dataDir + "ControlImageQuality_out.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 document Document pdfDocument = new Document(dataDir+ "PDFPageStamp.pdf"); // Create page stamp PdfPageStamp pageStamp = new PdfPageStamp(pdfDocument.Pages[1]); pageStamp.Background = true; pageStamp.XIndent = 100; pageStamp.YIndent = 100; pageStamp.Rotate = Rotation.on180; // Add stamp to particular page pdfDocument.Pages[1].AddStamp(pageStamp); dataDir = dataDir + "PDFPageStamp_out.pdf"; // Save output document pdfDocument.Save(dataDir);
可以使用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 document
Document pdfDocument = new Document(dataDir+ "PageNumberStamp.pdf");
// Create page number stamp
PageNumberStamp pageNumberStamp = new PageNumberStamp();
// Whether the stamp is background
pageNumberStamp.Background = false;
pageNumberStamp.Format = "Page # of " + pdfDocument.Pages.Count;
pageNumberStamp.BottomMargin = 10;
pageNumberStamp.HorizontalAlignment = HorizontalAlignment.Center;
pageNumberStamp.StartingNumber = 1;
// Set text properties
pageNumberStamp.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 page
pdfDocument.Pages[1].AddStamp(pageNumberStamp);
dataDir = dataDir + "PageNumberStamp_out.pdf";
// Save output document
pdfDocument.Save(dataDir);
可以使用TextStamp类在PDF文件中添加文本戳。TextStamp类提供创建基于文本的图章所需的属性,例如字体大小,字体样式和字体颜色等。为了添加文本图章,需要使用必需的属性来创建Document对象和TextStamp对象。之后,可以调用Page的AddStamp方法在PDF中添加图章。以下代码段显示了如何在PDF文件中添加文本戳。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks();
// Open document
Document pdfDocument = new Document(dataDir+ "AddTextStamp.pdf");
string annotationText = string.Empty;
annotationText = DateTime.Now.ToString("MM/dd/yy hh:mm:ss tt ");
// Create text stamp
TextStamp textStamp = new TextStamp(annotationText);
// Set properties of the stamp
textStamp.BottomMargin = 10;
textStamp.RightMargin = 20;
textStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;
textStamp.VerticalAlignment = VerticalAlignment.Bottom;
// Adding stamp on stamp collection
pdfDocument.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 document
pdfDocument.Save(dataDir);
可以使用TextStamp类在PDF文件中添加文本戳。TextStamp类提供创建基于文本的图章所需的属性,例如字体大小,字体样式和字体颜色等。为了添加文本图章,需要使用必需的属性来创建Document对象和TextStamp对象。之后,可以调用Page的AddStamp方法在PDF中添加图章。以下代码段显示了如何在PDF文件中添加文本戳。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks();
// Open document
Document pdfDocument = new Document(dataDir+ "AddTextStamp.pdf");
// Create text stamp
TextStamp textStamp = new TextStamp("Sample Stamp");
// Set whether stamp is background
textStamp.Background = true;
// Set origin
textStamp.XIndent = 100;
textStamp.YIndent = 100;
// Rotate stamp
textStamp.Rotate = Rotation.on90;
// Set text properties
textStamp.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 page
pdfDocument.Pages[1].AddStamp(textStamp);
dataDir = dataDir + "AddTextStamp_out.pdf";
// Save output document
pdfDocument.Save(dataDir);
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn