Цифровые подписи можно использовать для многих типов документов, где в прошлом использовались традиционные подписи пером и чернилами. Однако само наличие цифровой подписи не является достаточной гарантией того, что документ является тем, чем он кажется. Более того, государственным и корпоративным учреждениям часто приходится накладывать дополнительные ограничения на рабочие процессы подписи, например, ограничивать выбор пользователей и поведение документов во время и после подписания.
В следующем примере показано, как создать PDF-документ с цифровой подписью.
Исходный файл: digitalsignature.docx
Файл сертификата: sautinsoft.pfx
Файл подписи: signature.png
// Path to a loadable document.
string loadPath = @"..\..\digitalsignature.docx";
Создайте новую невидимую фигуру для цифровой подписи. Поместите фигуру в верхний левый угол (0 мм, 0 мм)
страницы.
Shape signatureShape = new Shape(dc, Layout.Floating(new HorizontalPosition(0f, LengthUnit.Millimeter, HorizontalPositionAnchor.LeftMargin),
new VerticalPosition(0f, LengthUnit.Millimeter, VerticalPositionAnchor.TopMargin), new Size(1, 1)));
((FloatingLayout)signatureShape.Layout).WrappingStyle = WrappingStyle.InFrontOfText;
signatureShape.Outline.Fill.SetEmpty();
// Find a first paragraph and insert our Shape inside it.
Paragraph firstPar = dc.GetChildElements(true).OfType<Paragraph>().FirstOrDefault();
firstPar.Inlines.Add(signatureShape);
Добавьте рукописное изображение для нашей цифровой подписи и укажите позицию на странице:
// Picture which symbolizes a handwritten signature.
Picture signaturePict = new Picture(dc, @"..\..\signature.png");
// Signature picture will be positioned:
// 14.5 cm from Top of the Shape.
// 4.5 cm from Left of the Shape.
signaturePict.Layout = Layout.Floating(
new HorizontalPosition(4.5, LengthUnit.Centimeter, HorizontalPositionAnchor.Page),
new VerticalPosition(14.5, LengthUnit.Centimeter, VerticalPositionAnchor.Page),
new Size(20, 10, LengthUnit.Millimeter));
И последний шаг - указать сертификат (*.pfx) и его характеристики: Пароль от сертификата,
Контактная информация и т.д.
PdfSaveOptions options = new PdfSaveOptions();
// Path to the certificate (*.pfx).
options.DigitalSignature.CertificatePath = @"..\..\sautinsoft.pfx";
// The password for the certificate.
// Each certificate is protected by a password.
// The reason is to prevent unauthorized the using of the certificate.
options.DigitalSignature.CertificatePassword = "123456789";
// Additional information about the certificate.
options.DigitalSignature.Location = "World Wide Web";
options.DigitalSignature.Reason = "Document.Net by SautinSoft";
options.DigitalSignature.ContactInfo = "info@sautinsoft.com";
// Placeholder where signature should be visualized.
options.DigitalSignature.SignatureLine = signatureShape;
// Visual representation of digital signature.
options.DigitalSignature.Signature = signaturePict;
В результате Вы получите с цифровой подписью
PDF документ.Полный код
using System.IO;
using System.Linq;
using SautinSoft.Document;
using SautinSoft.Document.Drawing;
namespace Sample
{
class Sample
{
static void Main(string[] args)
{
// Get your free 100-day key here:
// https://sautinsoft.com/start-for-free/
DigitalSignature();
}
/// <summary>
/// Load an existing document (*.docx, *.rtf, *.pdf, *.html, *.txt, *.pdf) and save it in a PDF document with the digital signature.
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/document/help/net/developer-guide/digital-signature-net-csharp-vb.php
/// </remarks>
public static void DigitalSignature()
{
// Path to a loadable document.
string loadPath = @"..\..\..\digitalsignature.docx";
string savePath = "Result.pdf";
DocumentCore dc = DocumentCore.Load(loadPath);
// Create a new invisible Shape for the digital signature.
// Place the Shape into top-left corner (0 mm, 0 mm) of page.
Shape signatureShape = new Shape(dc, Layout.Floating(new HorizontalPosition(0f, LengthUnit.Millimeter, HorizontalPositionAnchor.LeftMargin),
new VerticalPosition(0f, LengthUnit.Millimeter, VerticalPositionAnchor.TopMargin), new Size(1, 1)));
((FloatingLayout)signatureShape.Layout).WrappingStyle = WrappingStyle.InFrontOfText;
signatureShape.Outline.Fill.SetEmpty();
// Find a first paragraph and insert our Shape inside it.
Paragraph firstPar = dc.GetChildElements(true).OfType<Paragraph>().FirstOrDefault();
firstPar.Inlines.Add(signatureShape);
// Picture which symbolizes a handwritten signature.
Picture signaturePict = new Picture(dc, @"..\..\..\signature.png");
// Signature picture will be positioned:
// 14.5 cm from Top of the Shape.
// 4.5 cm from Left of the Shape.
signaturePict.Layout = Layout.Floating(
new HorizontalPosition(4.5, LengthUnit.Centimeter, HorizontalPositionAnchor.Page),
new VerticalPosition(14.5, LengthUnit.Centimeter, VerticalPositionAnchor.Page),
new Size(20, 10, LengthUnit.Millimeter));
PdfSaveOptions options = new PdfSaveOptions();
// Path to the certificate (*.pfx).
options.DigitalSignature.CertificatePath = @"..\..\..\sautinsoft.pfx";
// The password for the certificate.
// Each certificate is protected by a password.
// The reason is to prevent unauthorized the using of the certificate.
options.DigitalSignature.CertificatePassword = "123456789";
// Additional information about the certificate.
options.DigitalSignature.Location = "World Wide Web";
options.DigitalSignature.Reason = "Document.Net by SautinSoft";
options.DigitalSignature.ContactInfo = "info@sautinsoft.com";
// Placeholder where signature should be visualized.
options.DigitalSignature.SignatureLine = signatureShape;
// Visual representation of digital signature.
options.DigitalSignature.Signature = signaturePict;
dc.Save(savePath, options);
// Open the result for demonstration purposes.
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(savePath) { UseShellExecute = true });
}
}
}
Imports System
Imports System.Linq
Imports System.IO
Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing
Module Sample
Sub Main()
DigitalSignature()
End Sub
''' Get your free 100-day key here:
''' https://sautinsoft.com/start-for-free/
''' <summary>
''' Load an existing document (*.docx, *.rtf, *.pdf, *.html, *.txt, *.pdf) and save it in a PDF document with the digital signature.
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/digital-signature-net-csharp-vb.php
''' </remarks>
Sub DigitalSignature()
' Path to a loadable document.
Dim loadPath As String = "..\..\..\digitalsignature.docx"
Dim savePath As String = "Result.pdf"
Dim dc As DocumentCore = DocumentCore.Load(loadPath)
' Create a new invisible Shape for the digital signature.
' Place the Shape into top-left corner (0 mm, 0 mm) of page.
Dim signatureShape As New Shape(dc, Layout.Floating(New HorizontalPosition(0F, LengthUnit.Millimeter, HorizontalPositionAnchor.LeftMargin), New VerticalPosition(0F, LengthUnit.Millimeter, VerticalPositionAnchor.TopMargin), New Size(1, 1)))
CType(signatureShape.Layout, FloatingLayout).WrappingStyle = WrappingStyle.InFrontOfText
signatureShape.Outline.Fill.SetEmpty()
' Find a first paragraph and insert our Shape inside it.
Dim firstPar As Paragraph = dc.GetChildElements(True).OfType(Of Paragraph)().FirstOrDefault()
firstPar.Inlines.Add(signatureShape)
' Picture which symbolizes a handwritten signature.
Dim signaturePict As New Picture(dc, "..\..\..\signature.png")
' Signature picture will be positioned:
' 14.5 cm from Top of the Shape.
' 4.5 cm from Left of the Shape.
signaturePict.Layout = Layout.Floating(New HorizontalPosition(4.5, LengthUnit.Centimeter, HorizontalPositionAnchor.Page), New VerticalPosition(14.5, LengthUnit.Centimeter, VerticalPositionAnchor.Page), New Size(20, 10, LengthUnit.Millimeter))
Dim options As New PdfSaveOptions()
' Path to the certificate (*.pfx).
options.DigitalSignature.CertificatePath = "..\..\..\sautinsoft.pfx"
' The password for the certificate.
' Each certificate is protected by a password.
' The reason is to prevent unauthorized the using of the certificate.
options.DigitalSignature.CertificatePassword = "123456789"
' Additional information about the certificate.
options.DigitalSignature.Location = "World Wide Web"
options.DigitalSignature.Reason = "Document.Net by SautinSoft"
options.DigitalSignature.ContactInfo = "info@sautinsoft.com"
' Placeholder where signature should be visualized.
options.DigitalSignature.SignatureLine = signatureShape
' Visual representation of digital signature.
options.DigitalSignature.Signature = signaturePict
dc.Save(savePath, options)
' Open the result for demonstration purposes.
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(savePath) With {.UseShellExecute = True})
End Sub
End Module
Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.com или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже: