PDF 图层是一种将 PDF 文件的内容按图层排列的功能,允许用户在同一个 PDF 文件中选择性地将某些内容设置为可见,将其他内容设置为不可见。PDF 图层是分层艺术品、地图和 CAD 图纸中使用的常见元素。本文将演示如何使用Spire.PDF for .NET以编程方式在 PDF 文件中添加、隐藏或删除图层。
Spire.PDF for .NET 是一款独立 PDF 控件,用于 .NET 程序中创建、编辑和操作 PDF 文档。使用 Spire.PDF 类库,开发人员可以新建一个 PDF 文档或者对现有的 PDF 文档进行处理,且无需安装 Adobe Acrobat。
E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式
Spire.PDF for.net下载 Spire.PDF for java下载
安装 Spire.PDF for .NET
首先,您需要将 Spire.PDF for.NET 包中包含的 DLL 文件作为引用添加到您的 .NET 项目中。 可以从此链接下载 DLL 文件,也可以通过NuGet安装。
PM> Install-Package Spire.PDF
使用 C# 和 VB.NET 向 PDF 文档添加图层
Spire.PDF for .NET 提供了PdfDocument.Layers.AddLayer()方法在 PDF 文档中添加图层,然后您可以在 PDF 图层上绘制文本、线条、图像或形状。详细步骤如下。
- 创建一个PdfDocument实例。
- 使用PdfDocument.LoadFromFile()方法加载示例 PDF 文件。
- 使用PdfDocument.Layers.AddLayer(String)方法在 PDF 中添加指定名称的图层。或者,您也可以在添加图层时使用PdfDocument.Layers.AddLayer(String, PdfVisibility)方法设置图层的可见性。
- 使用PdfLayer.CreateGraphics()方法为该图层创建画布。
- 在画布上绘制文本、图像或其他元素。
- 使用PdfDocument.SaveToFile()方法保存结果文档。
【C#】
using Spire.Pdf; using Spire.Pdf.Graphics; using Spire.Pdf.Graphics.Layer; using System.Drawing;namespace AddLayersToPdf { class Program { static void Main(string[] args) { //Create a PdfDocument instance and load a sample PDF file PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.pdf");//Invoke AddLayerWatermark method to add a watermark layer AddLayerWatermark(pdf);//Invoke AddLayerHeader method to add a header layer AddLayerHeader(pdf);//Save to file pdf.SaveToFile("AddLayers.pdf"); pdf.Close(); }private static void AddLayerWatermark(PdfDocument doc) { //Create a layer named "Watermark" PdfLayer layer = doc.Layers.AddLayer("Watermark");//Create a font PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 48), true);//Specify the watermark text string watermarkText = "CONFIDENTIAL";//Get text size SizeF fontSize = font.MeasureString(watermarkText);//Calculate two offsets float offset1 = (float)(fontSize.Width * System.Math.Sqrt(2) / 4); float offset2 = (float)(fontSize.Height * System.Math.Sqrt(2) / 4);//Get page count int pageCount = doc.Pages.Count;//Declare two variables PdfPageBase page; PdfCanvas canvas;//Loop through the pages for (int i = 0; (i < pageCount); i++) { page = doc.Pages[i];//Create a canvas from layer canvas = layer.CreateGraphics(page.Canvas); canvas.TranslateTransform(page.Canvas.Size.Width / 2 - offset1 - offset2, page.Canvas.Size.Height / 2 + offset1 - offset2); canvas.SetTransparency(0.4f); canvas.RotateTransform(-45);//Draw sting on the canvas of layer canvas.DrawString(watermarkText, font, PdfBrushes.DarkBlue, 0, 0); } } private static void AddLayerHeader(PdfDocument doc) { // Create a layer named "Header" PdfLayer layer = doc.Layers.AddLayer("Header");//Get page size SizeF size = doc.Pages[0].Size;//Specify the initial values of X and y float x = 90; float y = 40;//Get page count int pageCount = doc.Pages.Count;//Declare two variables PdfPageBase page; PdfCanvas canvas;//Loop through the pages for (int i = 0; (i < pageCount); i++) { //Draw an image on the layer PdfImage pdfImage = PdfImage.FromFile(@"C:\Users\Administrator\Desktop\img.jpg"); float width = pdfImage.Width; float height = pdfImage.Height; page = doc.Pages[i]; canvas = layer.CreateGraphics(page.Canvas); canvas.DrawImage(pdfImage, x, y, width, height);//Draw a line on the layer PdfPen pen = new PdfPen(PdfBrushes.DarkGray, 2); canvas.DrawLine(pen, x, (y + (height + 5)), (size.Width - x), (y + (height + 2))); } } } }
【VB.NET】
Imports Spire.Pdf Imports Spire.Pdf.Graphics Imports Spire.Pdf.Graphics.Layer Imports System.DrawingNamespace AddLayersToPdf Class Program Private Shared Sub Main(ByVal args() As String)'Create a PdfDocument instance and load a sample PDF file Dim pdf As PdfDocument = New PdfDocument pdf.LoadFromFile("C:\Users\Administrator\Desktop\Sample.pdf")'Invoke AddLayerWatermark method to add a watermark layer Program.AddLayerWatermark(pdf)'Invoke AddLayerHeader method to add a header layer Program.AddLayerHeader(pdf)'Save to file pdf.SaveToFile("AddLayers.pdf") pdf.Close() End SubPrivate Shared Sub AddLayerWatermark(ByVal doc As PdfDocument)'Create a layer named "Watermark" Dim layer As PdfLayer = doc.Layers.AddLayer("Watermark")'Create a font Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Arial", 48), True)'Specify the watermark text Dim watermarkText As String = "CONFIDENTIAL"'Get text size Dim fontSize As SizeF = font.MeasureString(watermarkText)'Calculate two offsets Dim offset1 As Single = CSng((fontSize.Width * System.Math.Sqrt(2) / 4)) Dim offset2 As Single = CSng((fontSize.Height * System.Math.Sqrt(2) / 4))'Get page count Dim pageCount As Integer = doc.Pages.Count'Declare two variables Dim page As PdfPageBase Dim canvas As PdfCanvas'Loop through the pages Dim i As Integer = 0 While (i < pageCount) page = doc.Pages(i)'Create a canvas from layer canvas = layer.CreateGraphics(page.Canvas) canvas.TranslateTransform(page.Canvas.Size.Width / 2 - offset1 - offset2, page.Canvas.Size.Height / 2 + offset1 - offset2) canvas.SetTransparency(0.4F) canvas.RotateTransform(-45)'Draw sting on the canvas of layer canvas.DrawString(watermarkText, font, PdfBrushes.DarkBlue, 0, 0) i = (i + 1) i += 1 End While End SubPrivate Shared Sub AddLayerHeader(ByVal doc As PdfDocument)' Create a layer named "Header" Dim layer As PdfLayer = doc.Layers.AddLayer("Header")'Get page size Dim size As SizeF = doc.Pages(0).Size'Specify the initial values of X and y Dim x As Single = 90 Dim y As Single = 40'Get page count Dim pageCount As Integer = doc.Pages.Count'Declare two variables Dim page As PdfPageBase Dim canvas As PdfCanvas'Loop through the pages Dim i As Integer = 0 While (i < pageCount)'Draw an image on the layer Dim pdfImage As PdfImage = PdfImage.FromFile("C:\Users\Administrator\Desktop\img.jpg") Dim width As Single = pdfImage.Width Dim height As Single = pdfImage.Height page = doc.Pages(i) canvas = layer.CreateGraphics(page.Canvas) canvas.DrawImage(pdfImage, x, y, width, height)'Draw a line on the layer Dim pen As PdfPen = New PdfPen(PdfBrushes.DarkGray, 2) canvas.DrawLine(pen, x, (y + (height + 5)), (size.Width - x), (y + (height + 2))) i += 1 End While End Sub End Class End Namespace
在 C# 和 VB.NET 中设置 PDF 文档中图层的可见性
要设置现有图层的可见性,您需要使用PdfDocument.Layers属性通过其索引或名称获取指定的图层,然后使用PdfLayer.Visibility属性显示或隐藏该图层。详细步骤如下。
- 创建一个PdfDocument实例。
- 使用PdfDocument.LoadFromFile()方法加载示例 PDF 文档。
- 使用PdfDocument.Layers.Visibility属性设置指定图层的可见性。
- 使用PdfDocument.SaveToFile()方法保存结果文档。
【C#】
using Spire.Pdf; using Spire.Pdf.Graphics.Layer;namespace HideLayer { class Program { static void Main(string[] args) { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument();//Load a sample PDF document pdf.LoadFromFile("AddLayers.pdf");//Hide a specified layer by index pdf.Layers[0].Visibility = PdfVisibility.Off;//Hide a specified layer by name //pdf.Layers["Watermark"].Visibility = PdfVisibility.Off;//Save the result document pdf.SaveToFile("HideLayer.pdf"); } } }
【VB.NET】
Imports Spire.Pdf Imports Spire.Pdf.Graphics.LayerNamespace HideLayer Class Program Private Shared Sub Main(ByVal args() As String)'Create a PdfDocument instance Dim pdf As PdfDocument = New PdfDocument'Load a sample PDF document pdf.LoadFromFile("AddLayers.pdf")'Hide a specified layer by index pdf.Layers(0).Visibility = PdfVisibility.Off'Hide a specified layer by name 'pdf.Layers["Watermark"].Visibility = PdfVisibility.Off;'Save the result document pdf.SaveToFile("HideLayer.pdf") End Sub End Class End Namespace
使用 C# 和 VB.NET 删除 PDF 文档中的图层
Spire.PDF for .NET 还允许您使用PdfDocument.Layers.RemoveLayer(String)方法按名称删除现有图层。但请注意,PDF 图层的名称可能不是唯一的,此方法将删除所有具有相同名称的 PDF 图层。详细步骤如下。
- 创建一个PdfDocument实例。
- 使用PdfDocument.LoadFromFile()方法加载示例 PDF 文档。
- 使用PdfDocument.Layers.RemoveLayer(String)方法根据名称删除指定图层。
- 使用PdfDocument.SaveToFile()方法保存结果文档。
【C#】
using Spire.Pdf;namespace DeleteLayer { class Program { static void Main(string[] args) { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument();//Load a sample PDF document pdf.LoadFromFile("AddLayers.pdf");//Remove a layer by name pdf.Layers.RemoveLayer(("Watermark"));//Save the result document pdf.SaveToFile("DeleteLayer.pdf", FileFormat.PDF); } } }
【VB.NET】
Imports Spire.PdfNamespace DeleteLayer Class Program Private Shared Sub Main(ByVal args() As String) 'Create a PdfDocument instance Dim pdf As PdfDocument = New PdfDocument'Load a sample PDF document pdf.LoadFromFile("AddLayers.pdf")'Remove a layer by name pdf.Layers.RemoveLayer("Watermark")'Save the result document pdf.SaveToFile("DeleteLayer.pdf", FileFormat.PDF) End Sub End Class End Namespace