목차
NuGet을 통해 설치됨
PM> Install-Package Spire.PDF
관련된 링크들
PDF 레이어는 PDF 파일의 내용을 레이어로 배열하는 기능으로, 동일한 PDF 파일에서 사용자가 선택적으로 일부 내용은 보이고 다른 내용은 보이지 않도록 설정할 수 있습니다. PDF 레이어는 레이어로 구성된 아트웍, 지도 및 CAD 도면에 사용되는 공통 요소입니다. 이 문서에서는 프로그래밍 방식으로 PDF 파일에서 레이어 추가, 숨기기 또는 삭제 Spire.PDF for .NET사용.
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#
- 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)));
}
}
}
}

C# 및 VB.NET에서 PDF 문서의 레이어 가시성 설정
기존 레이어의 가시성을 설정하려면 PdfDocument.Layers 속성을 사용하여 인덱스 또는 이름으로 지정된 레이어를 가져온 다음 PdfLayer.Visibility 속성을 사용하여 레이어를 표시하거나 숨길 필요가 있습니다. 자세한 단계는 다음과 같습니다.
- PdfDocument 인스턴스를 만듭니다.
- PdfDocument.LoadFromFile() 메서드를 사용하여 샘플 PDF 문서를 로드합니다.
- PdfDocument.Layers.Visibility 속성을 사용하여 지정된 레이어의 가시성을 설정합니다.
- 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");
}
}
}

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#
- 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);
}
}
}

임시 면허 신청
생성된 문서에서 평가 메시지를 제거하거나 기능 제한을 제거하려면 다음을 수행하십시오 30일 평가판 라이선스 요청 자신을 위해.