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");

添加文本水印
添加文本水印时,需要先绘制文本并设置文本格式如字体、颜色及排列方式等,然后将其添加到页面作为水印。
//加载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");

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");

将文档作为注释附加到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");

获取附件
根据附件添加方式的不同,获取附件也分为以下两种相应的方式。
获取文件附件
//加载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();

获取注释附件
//加载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进行处理,非常感谢!