PDF管理控件Spire.PDF使用教程:如何添加水印和PDF附件

Spire.PDF是一个专业的PDF组件,能够独立地创建、编写、编辑、操作和阅读PDF文件,支持 .NET、Java、WPF和Silverlight。本文将介绍如何添加水印以及添加、获取PDF附件。

Spire.PDF是一个专业的PDF组件,能够独立地创建、编写、编辑、操作和阅读PDF文件,支持 .NET、Java、WPF和Silverlight。Spire.PDF的PDF API拥有丰富的功能,如安全设置(包括数字签名)、PDF文本/附件/图片提取、PDF文件合并/拆分、元数据更新、章节和段落优化、图形/图像描绘和插入、表格创建和处理、数据导入等等。>>下载Spire.PDF最新试用版

C# 给 PDF 文档添加水印

添加图片水印

Spire.Pdf.PdfPageBase类提供了一个属性BackgroundImage,用户可以通过该属性来获取或设置当前页面的背景图,除此之外还可以通过BackgroundRegion属性设置背景图的位置及大小,最终达到图片水印的效果。

//加载PDF文档PdfDocument pdf = new PdfDocument();pdf.LoadFromFile("Spire.Presentation.pdf");//获取PDF文档的第一页PdfPageBase page = pdf.Pages[0];//获取图片并将其设置为页面的背景图Image img = Image.FromFile("Logo.png");page.BackgroundImage = img;//指定背景图的位置和大小page.BackgroundRegion = new RectangleF(200, 200, 200, 200);//保存文档pdf.SaveToFile("ImageWaterMark.pdf");

Spire.PDF使用教程:如何添加水印和PDF附件

添加文本水印

添加文本水印时,需要先绘制文本并设置文本格式如字体、颜色及排列方式等,然后将其添加到页面作为水印。

//加载PDF文档PdfDocument pdf = new PdfDocument();pdf.LoadFromFile("Spire.Presentation.pdf");//获取PDF文档的第一页PdfPageBase page = pdf.Pages[0];//绘制文本,设置文本格式并将其添加到页面PdfTilingBrush brush = new PdfTilingBrush(new SizeF(page.Canvas.ClientSize.Width / 2, page.Canvas.ClientSize.Height / 3));brush.Graphics.SetTransparency(0.3f);brush.Graphics.Save();brush.Graphics.TranslateTransform(brush.Size.Width / 2, brush.Size.Height / 2);brush.Graphics.RotateTransform(-45);PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 20f), true);brush.Graphics.DrawString("草稿", font, PdfBrushes.Red, 0, 0, new PdfStringFormat(PdfTextAlignment.Center));brush.Graphics.Restore();brush.Graphics.SetTransparency(1);page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.ClientSize));//保存文档pdf.SaveToFile("TextWaterMark.pdf");

Spire.PDF使用教程:如何添加水印和PDF附件

C# 添加、获取PDF 附件

Spire.PDF组件提供了两种将文档附加到PDF的方式。一种是通过调用PdfAttachmentCollection类的Add() 方法将文档作为文件附件添加到PDF,另一种则是将文档作为注释附加到PDF的页面中。

添加附件

将文档作为文件附件添加到PDF

//加载PDF文档PdfDocument pdf = new PdfDocument("Test.pdf");//加载需要附加的文档PdfAttachment attachment = new PdfAttachment("New.pdf");//将文档添加到原PDF文档的附件集合中pdf.Attachments.Add(attachment);//保存文档pdf.SaveToFile("Attachment1.pdf");

Spire.PDF使用教程:如何添加水印和PDF附件

将文档作为注释附加到PDF文档的页面

//加载PDF文档PdfDocument doc = new PdfDocument("Test.pdf");//给文档添加一个新页面PdfPageBase page = doc.Pages.Add();//添加文本到页面PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, System.Drawing.FontStyle.Bold));page.Canvas.DrawString("Attachments:", font1, PdfBrushes.CornflowerBlue, new Point(50, 50));//将文档作为注释添加到页面PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 12f, System.Drawing.FontStyle.Bold));PointF location = new PointF(52, 80);String label = "Report.docx";byte[] data = File.ReadAllBytes("Report.docx");SizeF size = font2.MeasureString(label);RectangleF bounds = new RectangleF(location, size);page.Canvas.DrawString(label, font2, PdfBrushes.MediumPurple, bounds);bounds = new RectangleF(bounds.Right + 3, bounds.Top, font2.Height / 2, font2.Height);PdfAttachmentAnnotation annotation1 = new PdfAttachmentAnnotation(bounds, "Report.docx", data);annotation1.Color = Color.Purple;annotation1.Flags = PdfAnnotationFlags.NoZoom;annotation1.Icon = PdfAttachmentIcon.Graph;annotation1.Text = "Report.docx";(page as PdfNewPage).Annotations.Add(annotation1);//保存文档doc.SaveToFile("Attachment2.pdf");

Spire.PDF使用教程:如何添加水印和PDF附件

获取附件

根据附件添加方式的不同,获取附件也分为以下两种相应的方式。

获取文件附件

//加载PDF文档PdfDocument pdf = new PdfDocument("Attachment1.pdf");//获取文档的第一个文件附件PdfAttachment attachment = pdf.Attachments[0];//获取该附件的信息Console.WriteLine("Name: {0}", attachment.FileName);Console.WriteLine("MimeType: {0}", attachment.MimeType);Console.WriteLine("Description: {0}", attachment.Description);Console.WriteLine("Creation Date: {0}", attachment.CreationDate);Console.WriteLine("Modification Date: {0}", attachment.ModificationDate);//将附件的数据写入到新文档File.WriteAllBytes(attachment.FileName, attachment.Data);Console.ReadKey();

Spire.PDF使用教程:如何添加水印和PDF附件

获取注释附件

//加载PDF文档PdfDocument pdf = new PdfDocument("Attachment2.pdf");//实例化一个list并将文档内所有页面的Attachment annotations添加到该listList attaches = new List();foreach (PdfPageBase page in pdf.Pages){    foreach (PdfAnnotation annotation in page.AnnotationsWidget)    {        attaches.Add(annotation as PdfAttachmentAnnotationWidget);    }}//遍历list,将附件数据写入到新文档for (int i = 0; i < attaches.Count; i++){    File.WriteAllBytes(attaches[i].FileName, attaches[i].Data);}

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年7月19日
下一篇 2019年7月19日

相关推荐

发表回复

登录后才能评论