翻譯|使用教程|編輯:胡濤|2024-07-25 11:19:57.567|閱讀 116 次
概述:本文將演示如何使用Spire.PDF for .NET以編程方式在 PDF 文件中添加、隱藏或刪除圖層,歡迎查閱~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
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 包中包含的 DLL 文件作為引用添加到您的 .NET 項目中。 可以從此鏈接下載 DLL 文件,也可以通過安裝。
PM> Install-Package Spire.PDF
Spire.PDF for .NET 提供了PdfDocument.Layers.AddLayer()方法在 PDF 文檔中添加圖層,然后您可以在 PDF 圖層上繪制文本、線條、圖像或形狀。詳細步驟如下。
【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.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
要設置現有圖層的可見性,您需要使用PdfDocument.Layers屬性通過其索引或名稱獲取指定的圖層,然后使用PdfLayer.Visibility屬性顯示或隱藏該圖層。詳細步驟如下。
【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.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
Spire.PDF for .NET 還允許您使用PdfDocument.Layers.RemoveLayer(String)方法按名稱刪除現有圖層。但請注意,PDF 圖層的名稱可能不是唯一的,此方法將刪除所有具有相同名稱的 PDF 圖層。詳細步驟如下。
【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.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
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn