العمل مع التوقيع في ملف PDF

كيفية استخراج معلومات التوقيع

يدعم Aspose.PDF for .NET ميزة التوقيع الرقمي لملفات PDF باستخدام فئة PdfFileSignature. حاليًا، من الممكن أيضًا تحديد صلاحية الشهادة ولكن لا يمكننا استخراج الشهادة بالكامل. المعلومات التي يمكن استخراجها هي المفتاح العام، بصمة الإصبع، والجهة المصدرة، وما إلى ذلك.

لاستخراج معلومات التوقيع، قدمنا طريقة ExtractCertificate(..) إلى فئة PdfFileSignature. يرجى إلقاء نظرة على مقتطف الكود التالي الذي يوضح الخطوات لاستخراج الشهادة من كائن PdfFileSignature:

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExtractSignatureInfo()
{ 
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
    
    using (var pdfFileSignature = new Aspose.Pdf.Facades.PdfFileSignature())
    {
        // Bind PDF document
        pdfFileSignature.BindPdf(dataDir + "signed_rsa.pdf");
        // Get list of signature names
        var sigNames = pdfFileSignature.GetSignatureNames();
        if (sigNames.Count > 0)
        {
            SignatureName sigName = sigNames[0];            
            // Extract signature certificate
            Stream cerStream = pdfFileSignature.ExtractCertificate(sigName);
            if (cerStream != null)
            {
                using (cerStream)
                {
                    using (FileStream fs = new FileStream(dataDir + "extracted_cert.pfx", FileMode.CreateNew))
                    {
                        cerStream.CopyTo(fs);
                    }
                }
            }
            
        }
    }
}

استخراج الصورة من حقل التوقيع (PdfFileSignature)

يدعم Aspose.PDF for .NET ميزة التوقيع الرقمي لملفات PDF باستخدام فئة PdfFileSignature وأثناء توقيع المستند، يمكنك أيضًا تعيين صورة لمظهر التوقيع. الآن توفر هذه الواجهة البرمجية أيضًا القدرة على استخراج معلومات التوقيع بالإضافة إلى الصورة المرتبطة بحقل التوقيع.

لاستخراج معلومات التوقيع، قدمنا طريقة ExtractImage(..) إلى فئة PdfFileSignature. يرجى إلقاء نظرة على مقتطف الكود التالي الذي يوضح الخطوات لاستخراج الصورة من كائن PdfFileSignature:

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExtractSignatureImage()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
    
    using (var signature = new Aspose.Pdf.Facades.PdfFileSignature())
    {
        // Bind PDF document
        signature.BindPdf(dataDir + "ExtractingImage.pdf");

        if (signature.ContainsSignature())
        {
            // Get list of signature names
            foreach (string sigName in signature.GetSignatureNames())
            {                
                // Extract signature image
                using (Stream imageStream = signature.ExtractImage(sigName))
                {
                    if (imageStream != null)
                    {
                        imageStream.Position = 0;
                        using (FileStream fs = new FileStream(dataDir + "ExtractImages_out.jpg", FileMode.OpenOrCreate))
                        {
                            imageStream.CopyTo(fs);
                        }
                    }
                }
            }
        }
    }
}

كتم الموقع والسبب

تتيح وظيفة Aspose.PDF تكوينًا مرنًا لنسخة التوقيع الرقمية. توفر فئة PdfFileSignature القدرة على توقيع ملف PDF. يسمح تنفيذ طريقة التوقيع بتوقيع PDF وتمرير كائن التوقيع المحدد إلى هذه الفئة. تحتوي طريقة التوقيع على مجموعة من الخصائص لتخصيص التوقيع الرقمي الناتج. في حال كنت بحاجة إلى كتم بعض الخصائص النصية من نتيجة التوقيع، يمكنك تركها فارغة. يوضح مقتطف الكود التالي كيفية كتم الموقع والسبب من كتلة التوقيع.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SupressLocationReason()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
    
    using (var pdfFileSignature = new Aspose.Pdf.Facades.PdfFileSignature())
    {
        // Bind PDF document
        pdfFileSignature.BindPdf(dataDir + "input.pdf");

        // Create a rectangle for signature location
        System.Drawing.Rectangle rect = new System.Drawing.Rectangle(10, 10, 300, 50);
        // Set signature appearance
        pdfFileSignature.SignatureAppearance = dataDir + "aspose-logo.png";

        // Create any of the three signature types
        var signature = new Aspose.Pdf.Forms.PKCS1(dataDir + "rsa_cert.pfx", "12345"); // PKCS#1

        pdfFileSignature.Sign(1, string.Empty, "test01@aspose-pdf-demo.local", string.Empty, true, rect, signature);
        // Save PDF document
        pdfFileSignature.Save(dataDir + "DigitallySign_out.pdf");
    }
}

ميزات التخصيص للتوقيع الرقمي

يسمح Aspose.PDF for .NET بميزات التخصيص لتوقيع رقمي. يتم تنفيذ طريقة التوقيع في فئة SignatureCustomAppearance مع 6 تحميلات لراحتك. على سبيل المثال، يمكنك تكوين التوقيع الناتج فقط بواسطة مثيل فئة SignatureCustomAppearance وقيم خصائصها. يوضح مقتطف الكود التالي كيفية إخفاء عنوان “تم التوقيع رقميًا بواسطة” من التوقيع الرقمي الناتج لملف PDF الخاص بك.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CustomizationFeaturesForDigitalSign()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
    
    using (var pdfFileSignature = new Aspose.Pdf.Facades.PdfFileSignature())
    {
        // Bind PDF document
        pdfFileSignature.BindPdf(dataDir + "input.pdf");

        // Create a rectangle for signature location
        System.Drawing.Rectangle rect = new System.Drawing.Rectangle(10, 10, 300, 50);

        // Create any of the three signature types
        var signature = new Aspose.Pdf.Forms.PKCS1(dataDir + "rsa_cert.pfx", "12345"); // PKCS#1
        // Create signature appearance
        var signatureCustomAppearance = new Aspose.Pdf.Forms.SignatureCustomAppearance
        {
            FontSize = 6,
            FontFamilyName = "Times New Roman",
            DigitalSignedLabel = "Signed by:"
        };
        // Set signature appearance
        signature.CustomAppearance = signatureCustomAppearance;

        pdfFileSignature.Sign(1, true, rect, signature);
        // Save PDF document
        pdfFileSignature.Save(dataDir + "DigitallySign_out.pdf");
    }
}

كيفية عرض صورة للتوقيع

يمكنك تعيينها باستخدام خاصية PdfFileSignature.CustomAppearance. إذا قمت بتحديد لون خلفية، ستغطي الخلفية الصورة. لعرض الصورة فوق الخلفية، قم بتعيين خاصية SignatureCustomAppearance.IsForegroundImage = true.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CustomizationFeaturesForDigitalSign()
{
    // The path to the image.
    var imagePath = RunExamples.GetDataDir_AsposePdf_StampsWatermarks() + "aspose-logo.jpg";
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();

    using (var pdfFileSignature = new Aspose.Pdf.Facades.PdfFileSignature())
    {
        // Bind PDF document
        pdfFileSignature.BindPdf(dataDir + "input.pdf");

        // Create a rectangle for signature location
        System.Drawing.Rectangle rect = new System.Drawing.Rectangle(10, 10, 300, 50);

        // Create any of the three signature types
        var signature = new Aspose.Pdf.Forms.PKCS1(dataDir + "rsa_cert.pfx", "12345"); // PKCS#1
        // Create signature appearance
        var signatureCustomAppearance = new Aspose.Pdf.Forms.SignatureCustomAppearance
        {
            FontSize = 6,
            FontFamilyName = "Times New Roman",
            DigitalSignedLabel = "Signed by:",
            IsForegroundImage = true
        };
        // Set signature appearance
        signature.CustomAppearance = signatureCustomAppearance;
        // Set signature appearance
        pdfFileSignature.SignatureAppearance = imagePath;

        pdfFileSignature.Sign(1, true, rect, signature);
        // Save PDF document
        pdfFileSignature.Save(dataDir + "DigitallySign_out.pdf");
    }
}

تغيير اللغة في نص التوقيع الرقمي

باستخدام واجهة برمجة التطبيقات Aspose.PDF for .NET، يمكنك توقيع ملف PDF باستخدام أي من الأنواع الثلاثة التالية من التوقيعات:

  • PKCS#1.
  • PKCS#7.
  • PKCS#12.

تحتوي كل من التوقيعات المقدمة على مجموعة من خصائص التكوين المنفذة لراحتك (التعريب، تنسيق التاريخ والوقت، عائلة الخط، إلخ). توفر فئة SignatureCustomAppearance الوظائف المقابلة. يوضح مقتطف الكود التالي كيفية تغيير اللغة في نص التوقيع الرقمي.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ChangeLanguageInDigitalSignText()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();   
    
    using (var pdfFileSignature = new Aspose.Pdf.Facades.PdfFileSignature())
    {
        // Bind PDF document
        pdfFileSignature.BindPdf(dataDir + "input.pdf");
        // Create a rectangle for signature location
        System.Drawing.Rectangle rect = new System.Drawing.Rectangle(310, 45, 200, 50);

        // Create any of the three signature types
        var pkcs = new Aspose.Pdf.Forms.PKCS7(dataDir + "rsa_cert.pfx", "12345")
        {
            Reason = "Pruebas Firma",
            ContactInfo = "Contacto Pruebas",
            Location = "Población (Provincia)",
            Date = DateTime.Now
        };
        
        var signatureCustomAppearance = new Aspose.Pdf.Forms.SignatureCustomAppearance
        {
            DateSignedAtLabel = "Fecha",
            DigitalSignedLabel = "Digitalmente firmado por",
            ReasonLabel = "Razón",
            LocationLabel = "Localización",
            FontFamilyName = "Arial",
            FontSize = 10d,
            Culture = System.Globalization.CultureInfo.InvariantCulture,
            DateTimeFormat = "yyyy.MM.dd HH:mm:ss"
        };
        // Set signature appearance
        pkcs.CustomAppearance = signatureCustomAppearance;
        // Sign the PDF file
        pdfFileSignature.Sign(1, true, rect, pkcs);
        // Save PDF document
        pdfFileSignature.Save(dataDir + "DigitallySign_out.pdf");
    }
}