在 PDF 中添加、隐藏或删除图层
Spire.PDF for .NET 是一款专门对 Word 文档进行操作的 .NET 类库。致力于在于帮助开发人员轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档,而无需安装 Microsoft Word。
行号用于在每行文本旁边显示 Word 自动计算的行数。当我们需要参考合同或法律文件等文档中的特定行时,它非常有用。word中的行号功能允许我们设置起始值、编号间隔、与文本的距离以及行号的编号方式。使用 Spire.Doc,我们可以实现上述所有功能。本文将介绍如何将 HTML 转换为 PDF。
欢迎加入spire技术交流群:767755948
PDF 图层是一种将 PDF 文件内容分层排列的功能,它允许用户有选择性地在同一 PDF 文件中将某些内容设置为可见,而将另一些内容设置为不可见。PDF 图层是分层艺术品、地图和 CAD 图纸中常用的元素。本文将演示如何使用 Spire.PDF for .NET 在 PDF 文件中以编程方式添加、隐藏或删除图层。
- 在 C# 和 VB.NET 中为 PDF 文档添加图层
- 在 C# 和 VB.NET 中设置 PDF 文档中图层的可见性
- 在 C# 和 VB.NET 中删除 PDF 文档中的图层
首先,您需要将 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() 方法保存结果文档。
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.Drawing Namespace 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 Sub Private 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 Sub Private 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()方法保存结果文档。
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.Layer Namespace 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()方法保存结果文档
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.Pdf Namespace 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
申请临时许可证
若想从生成的文档中删除评估信息,或解除功能限制,请许可证