C#/VB.NET: 텍스트 또는/및 이미지로 PDF에 디지털 서명

2023-08-07 01:52:52 zaki zou

디지털 서명은 서명된 문서를 작성자 이외의 다른 사람이 변경할 수 없도록 합니다. 서명을 추가하는 것은 문서 내용의 신뢰성을 보장하는 가장 일반적인 방법입니다. PDF 문서의 시각적 디지털 서명은 텍스트 또는 이미지(예: 자필 서명)를 표시할 수 있습니다. 이 문서에서는 방법을 소개합니다 PDF에 디지털 서명 다음 세 가지 측면에서 Spire.PDF for .NET 사용합니다.

Spire.PDF for .NET 설치

먼저 Spire.PDF for .NET 패키지에 포함된 DLL 파일을 .NET 프로젝트의 참조로 추가해야 합니다. DLL 파일은 이 링크에서 다운로드하거나 NuGet을 통해 설치할 수 있습니다.

PM> Install-Package Spire.PDF

텍스트로 PDF에 디지털 서명

다음은 PDF 문서에 일반 텍스트 서명을 추가하는 단계입니다.

  • PdfDocument 개체를 만들고 PdfDocument.LoadFromFile() 메서드를 사용하여 샘플 PDF 파일을 로드합니다.
  • PdfCertificate 개체를 초기화하는 동안 .pfx 인증서 파일을 로드합니다.
  • 문서에서 위치와 크기를 지정하여 PdfSignature 객체를 만듭니다.
  • 서명 그래픽 모드를 SignDetail로 설정하면 서명 세부 정보가 일반 텍스트로 표시됩니다.
  • PdfSignature 개체 아래의 속성을 통해 이름, 연락처 정보, 이유, 날짜 등 서명 세부 정보를 설정합니다.
  • 인증된 문서의 권한을 ForbidChangesAllowFormFill로 설정합니다.
  • PdfDocument.SaveToFile() 메서드를 사용하여 문서를 다른 파일로 저장합니다.
  • C#
  • VB.NET
using Spire.Pdf;
    using Spire.Pdf.Security;
    using System;
    using System.Drawing;
    using Spire.Pdf.Graphics;
    
    namespace AddTextSignature
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Create a PdfDocument object
                PdfDocument doc = new PdfDocument();
    
                //Load a sample PDF file
                doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
    
                //Load the certificate
                PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");
    
                //Create a PdfSignature object and specify its position and size
                PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count-1], cert, "MySignature");
                RectangleF rectangleF = new RectangleF(doc.Pages[0].ActualSize.Width - 340, 150, 290, 100);
                signature.Bounds = rectangleF;
                signature.Certificated = true;
    
                //Set the graphics mode to sign detail
                signature.GraphicsMode = GraphicMode.SignDetail;
    
                //Set the signature content
                signature.NameLabel = "Signer:";
                signature.Name = "Gary";
                signature.ContactInfoLabel = "Phone:";
                signature.ContactInfo = "0123456";
                signature.DateLabel = "Date:";
                signature.Date = DateTime.Now;
                signature.LocationInfoLabel = "Location:";
                signature.LocationInfo = "USA";
                signature.ReasonLabel = "Reason:";
                signature.Reason = "I am the author";
                signature.DistinguishedNameLabel = "DN:";
                signature.DistinguishedName = signature.Certificate.IssuerName.Name;
    
                //Set the signature font
                signature.SignDetailsFont = new PdfTrueTypeFont(new Font("Arial Unicode MS",12f,FontStyle.Regular));
    
                //Set the document permission to forbid changes but allow form fill
                signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill;
    
                //Save to file
                doc.SaveToFile("TextSignature.pdf");
                doc.Close();
            }
        }
    }

C#/VB.NET: Digitally Sign PDF with Text or/and Image

이미지로 PDF에 디지털 서명

다음은 PDF 문서에 이미지 서명을 추가하는 단계입니다.

  • PdfDocument 개체를 만들고 PdfDocument.LoadFromFile() 메서드를 사용하여 샘플 PDF 파일을 로드합니다.
  • PdfCertificate 개체를 초기화하는 동안 .pfx 인증서 파일을 로드합니다.
  • 문서에서 위치와 크기를 지정하여 PdfSignature 객체를 만듭니다.
  • 서명 그래픽 모드를 SignImageOnly로 설정하면 서명 이미지만 표시됩니다.
  • PdfSignature.SignImageSource 속성을 통해 서명 이미지를 설정합니다.
  • 인증된 문서의 권한을 ForbidChangesAllowFormFill로 설정합니다.
  • PdfDocument.SaveToFile() 메서드를 사용하여 문서를 다른 파일로 저장합니다.
  • C#
  • VB.NET
using Spire.Pdf;
    using Spire.Pdf.Graphics;
    using Spire.Pdf.Security;
    using System.Drawing;
    
    namespace AddImageSignature
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Create a PdfDocument object
                PdfDocument doc = new PdfDocument();
    
                //Load a sample PDF file
                doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
    
                //Load the certificate
                PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");
    
                //Create a PdfSignature object and specify its position and size
                PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count - 1], cert, "MySignature");
                RectangleF rectangleF = new RectangleF(doc.Pages[0].ActualSize.Width - 200, 150, 130, 130);
                signature.Bounds = rectangleF;
                signature.Certificated = true;
    
                //Set the graphics mode to image only
                signature.GraphicsMode = GraphicMode.SignImageOnly;
    
                //Set the sign image source
                signature.SignImageSource = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\verified.png");
    
                //Set the signature font
                signature.SignDetailsFont = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular));
    
                //Set the document permission to forbid changes but allow form fill
                signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill;
    
                //Save to file
                doc.SaveToFile("ImageSignature.pdf");
                doc.Close();
            }
        }
    }

C#/VB.NET: Digitally Sign PDF with Text or/and Image

텍스트와 이미지로 PDF에 디지털 서명

다음은 텍스트와 이미지가 포함된 PDF 문서에 디지털 서명하는 단계입니다.

  • PdfDocument 개체를 만들고 PdfDocument.LoadFromFile() 메서드를 사용하여 샘플 PDF 파일을 로드합니다.
  • PdfCertificate 개체를 초기화하는 동안 .pfx 인증서 파일을 로드합니다.
  • 문서에서 위치와 크기를 지정하여 PdfSignature 객체를 만듭니다.
  • 서명 그래픽 모드를 SignImageAndSignDetail로 설정하면 서명 이미지와 세부 정보가 모두 표시됩니다.
  • PdfSignature.SignImageSource 속성을 통해 서명 이미지를 설정하고, PdfSignature 객체 아래의 다른 속성을 통해 이름, 연락처, 이유, 날짜 등의 서명 세부 정보를 설정합니다.
  • 인증된 문서의 권한을 ForbidChanges 및 AllowFormFill로 설정합니다.
  • PdfDocument.SaveToFile() 메서드를 사용하여 문서를 다른 파일로 저장합니다.
  • C#
  • VB.NET
using Spire.Pdf;
    using Spire.Pdf.Security;
    using System;
    using System.Drawing;
    using Spire.Pdf.Graphics;
    
    namespace AddTextAndImageSignature
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Create a PdfDocument object
                PdfDocument doc = new PdfDocument();
    
                //Load a sample PDF file
                doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
    
                //Load the certificate
                PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");
    
                //Create a PdfSignature object and specify its position and size
                PdfSignature signature = new PdfSignature(doc, doc.Pages[doc.Pages.Count - 1], cert, "MySignature");
                RectangleF rectangleF = new RectangleF(doc.Pages[0].ActualSize.Width - 320, 150, 260, 110);
                signature.Bounds = rectangleF;
                signature.Certificated = true;
    
                //Set the graphics mode to image and sign detail
                signature.GraphicsMode = GraphicMode.SignImageAndSignDetail;
    
                //Set the signature content
                signature.NameLabel = "Signer:";
                signature.Name = "Gary";
                signature.ContactInfoLabel = "Phone:";
                signature.ContactInfo = "0123456";
                signature.DateLabel = "Date:";
                signature.Date = DateTime.Now;
                signature.LocationInfoLabel = "Location:";
                signature.LocationInfo = "USA";
                signature.ReasonLabel = "Reason:";
                signature.Reason = "I am the author";
                signature.DistinguishedNameLabel = "DN:";
                signature.DistinguishedName = signature.Certificate.IssuerName.Name;
    
                //Set the signature image source
                signature.SignImageSource = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\handSignature.png");
    
                //Set the signature font
                signature.SignDetailsFont = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular));
    
                //Set the document permission to forbid changes but allow form fill
                signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges | PdfCertificationFlags.AllowFormFill;
    
                //Save to file
                doc.SaveToFile("TextAndImageSignature.pdf");
                doc.Close();
            }
        }
    }

C#/VB.NET: Digitally Sign PDF with Text or/and Image

임시 면허 신청

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

또한보십시오