Sommario
Installato tramite NuGet
PM> Install-Package Spire.PDF
Link correlati
Il livello PDF è una funzione che dispone il contenuto di un file PDF in livelli, che consente agli utenti di impostare selettivamente alcuni contenuti in modo che siano visibili e altri invisibili nello stesso file PDF. I livelli PDF sono un elemento comune utilizzato nella grafica a più livelli, nelle mappe e nei disegni CAD. Questo articolo dimostrerà come eseguire a livello di codice aggiungere, nascondere o eliminare livelli in un file PDF utilizzando Spire.PDF for .NET.
- Aggiungi livelli a un documento PDF in C# e VB.NET
- Imposta la visibilità dei livelli in un documento PDF in C# e VB.NET
- Elimina livelli in un documento PDF in C# e VB.NET
Installa Spire.PDF for .NET
Per cominciare, è necessario aggiungere i file DLL inclusi nel pacchetto Spire.PDF for.NET come riferimenti nel progetto .NET. I file DLL possono essere scaricati da questo link o installato tramite NuGet.
PM> Install-Package Spire.PDF
Aggiungi livelli a un documento PDF in C# e VB.NET
Spire.PDF for .NET fornisce il metodo PdfDocument.Layers.AddLayer() per aggiungere un livello in un documento PDF e puoi quindi disegnare testo, linee, immagini o forme sul livello PDF. I passaggi dettagliati sono i seguenti.
- Creare un'istanza PdfDocument.
- Carica un file PDF di esempio utilizzando il metodo PdfDocument.LoadFromFile().
- Aggiungi un livello con il nome specificato nel PDF utilizzando il metodo PdfDocument.Layers.AddLayer(String) oppure puoi anche impostare la visibilità del livello durante l'aggiunta utilizzando il metodo PdfDocument.Layers.AddLayer(String, PdfVisibility).
- Crea una tela per il livello usando il metodo PdfLayer.CreateGraphics().
- Disegna testo, immagine o altri elementi sulla tela.
- Salvare il documento risultato utilizzando il metodo PdfDocument.SaveToFile().
- C#
- VB.NET
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)));
}
}
}
}

Imposta la visibilità dei livelli in un documento PDF in C# e VB.NET
Per impostare la visibilità di un layer esistente, è necessario ottenere un layer specificato in base al suo indice o nome utilizzando la proprietà PdfDocument.Layers, quindi mostrare o nascondere il layer utilizzando la proprietà PdfLayer.Visibility.I passaggi dettagliati sono i seguenti.
- Creare un'istanza PdfDocument.
- Carica un documento PDF di esempio utilizzando il metodo PdfDocument.LoadFromFile().
- Imposta la visibilità di un layer specificato utilizzando la proprietà PdfDocument.Layers.Visibility.
- Salvare il documento risultato utilizzando il metodo PdfDocument.SaveToFile().
- C#
- VB.NET
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");
}
}
}

Elimina livelli in un documento PDF in C# e VB.NET
Spire.PDF for .NET ti consente anche di rimuovere un livello esistente in base al suo nome utilizzando il metodo PdfDocument.Layers.RemoveLayer(String).Ma tieni presente che i nomi dei livelli PDF potrebbero non essere univoci e questo metodo rimuoverà tutti i livelli PDF con lo stesso nome I passaggi dettagliati sono i seguenti.
- Creare un'istanza PdfDocument.
- Carica un documento PDF di esempio utilizzando il metodo PdfDocument.LoadFromFile().
- Elimina un layer specificato in base al suo nome utilizzando il metodo PdfDocument.Layers.RemoveLayer(String).
- Salvare il documento risultato utilizzando il metodo PdfDocument.SaveToFile().
- C#
- VB.NET
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);
}
}
}

Richiedi una licenza temporanea
Se desideri rimuovere il messaggio di valutazione dai documenti generati o eliminare le limitazioni delle funzioni, per favore richiedere una licenza di prova di 30 giorni per te.