在 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
 
 
	
		申請臨時許可證
 若想從生成的文檔中刪除評估信息,或解除功能限制,請許可證 
	

 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線