Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,22 @@
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Input.pdf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Data\Intermediate0.cer">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Data\Intermediate1.cer">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Data\Root.cer">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Output\gitkeep.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,31 @@
using System.Security.Cryptography.X509Certificates;
using System.Text;

string dataPath = Path.GetFullPath(@"Data/");

// Load signed PDF document
//Open the signed PDF document.
using PdfLoadedDocument ldoc = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"));

// Get signature field
//Retrieve the signature field from the document form.
PdfLoadedSignatureField lSigFld = ldoc.Form.Fields[0] as PdfLoadedSignatureField;

// Root and Intermediate Certificates
//Create a collection to store the root and intermediate certificates.
X509CertificateCollection collection = new X509CertificateCollection();

string[] certificates =
{
"Root.cer",
"Intermediate0.cer",
"Intermediate1.cer"
};

//Load certificates and add them to the certificate collection.
foreach (string certFile in certificates)
{
byte[] certData = File.ReadAllBytes(Path.Combine(dataPath, certFile));

X509Certificate2 certificate = new X509Certificate2(certData);

collection.Add(certificate);
collection.Add(new X509Certificate2(File.ReadAllBytes(Path.GetFullPath(@"Data/" + certFile))));
}

// Validate signature
//Verify the digital signature using the certificate chain.
PdfSignatureValidationResult result = lSigFld.ValidateSignature(collection);

StringBuilder builder = new StringBuilder();

builder.AppendLine("Signature is " + result.SignatureStatus);
builder.AppendLine();
builder.AppendLine("----------Validation Summary----------");
builder.AppendLine();

// Modified check
//Check whether the signed document has been modified.
if (result.IsDocumentModified)
{
builder.AppendLine("The document has been altered or corrupted since the signature was applied.");
Expand All @@ -49,64 +36,49 @@
{
builder.AppendLine("The document has not been modified since the signature was applied.");
}

// Certificate details
//Add signer certificate information to the report.
builder.AppendLine("Digitally signed by: " + lSigFld.Signature.Certificate.IssuerName);

builder.AppendLine("Valid From: " + lSigFld.Signature.Certificate.ValidFrom);

builder.AppendLine("Valid To: " + lSigFld.Signature.Certificate.ValidTo);

builder.AppendLine("Signature Algorithm: " + result.SignatureAlgorithm);

builder.AppendLine("Hash Algorithm: " + result.DigestAlgorithm);

// Revocation details
//Include revocation status details.
builder.AppendLine("OCSP Revocation Status: " + result.RevocationResult.OcspRevocationStatus);

if (result.RevocationResult.OcspRevocationStatus == RevocationStatus.None && result.RevocationResult.IsRevokedCRL)
{
builder.AppendLine("CRL is revoked.");
}

builder.AppendLine();
builder.AppendLine("--------Revocation Information---------");
builder.AppendLine();

//Extract OCSP and CRL validation details for each signer certificate.
foreach (PdfSignerCertificate signerCertificate in result.SignerCertificates)
{
if (signerCertificate.OcspCertificate != null)
{
builder.AppendLine("------------OCSP Certificate-------------");

foreach (X509Certificate2 item in signerCertificate.OcspCertificate.Certificates)
{
builder.AppendLine("The OCSP Response was signed by: " + item.SubjectName.Name);
}

builder.AppendLine("Is Embedded: " + signerCertificate.OcspCertificate.IsEmbedded);

builder.AppendLine("Valid From: " + signerCertificate.OcspCertificate.ValidFrom);

builder.AppendLine("Valid To: " + signerCertificate.OcspCertificate.ValidTo);

builder.AppendLine();
}

if (signerCertificate.CrlCertificate != null)
{
builder.AppendLine("------------CRL Certificate--------------");

foreach (X509Certificate2 item in signerCertificate.CrlCertificate.Certificates)
{
builder.AppendLine("The CRL was signed by: " + item.SubjectName.Name);
}

builder.AppendLine("Is Embedded: " + signerCertificate.CrlCertificate.IsEmbedded);
builder.AppendLine("Valid From: " + signerCertificate.CrlCertificate.ValidFrom);
builder.AppendLine("Valid To: " + signerCertificate.CrlCertificate.ValidTo);
builder.AppendLine();
}
}
//Save to file
File.WriteAllText(Path.GetFullPath(@"Output/Output.txt"),builder.ToString());
//Write the validation report to a text file.
File.WriteAllText(Path.GetFullPath(@"Output/Output.txt"), builder.ToString());
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,39 @@

// Open the existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/SignatureFields.pdf"));

// Retrieve the first page from the PDF document.
PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage;

// Access the first signature field available in the PDF form.
PdfLoadedSignatureField signatureField1 = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField;

// Create a certificate object using the PFX file and its password.
PdfCertificate certificate1 = new PdfCertificate(Path.GetFullPath(@"Data/PDF.pfx"), "syncfusion");

// Digitally sign the first signature field.
signatureField1.Signature = new PdfSignature(loadedDocument, page, certificate1, "Signature1", signatureField1);

// Load the signature image for the first signer.
FileStream imageStream1 = new FileStream(Path.GetFullPath(@"Data/Student Signature.jpg"), FileMode.Open, FileAccess.Read);

PdfBitmap signatureImage = new PdfBitmap(imageStream1);


// Render the signature image within the signature field appearance.
signatureField1.Signature.Appearance.Normal.Graphics.DrawImage(signatureImage, 0, 0, signatureField1.Bounds.Width, signatureField1.Bounds.Height);

// Save the signed PDF into a memory stream.
MemoryStream stream = new MemoryStream();
loadedDocument.Save(stream);

// Close the original PDF document instance.
loadedDocument.Close(true);

// Reopen the partially signed PDF from the memory stream.
PdfLoadedDocument signedDocument = new PdfLoadedDocument(stream);

// Retrieve the first page of the signed document.
PdfLoadedPage loadedPage = signedDocument.Pages[0] as PdfLoadedPage;

// Access the second signature field in the PDF form.
PdfLoadedSignatureField signatureField2 = signedDocument.Form.Fields[1] as PdfLoadedSignatureField;

// Apply a digital signature to the second signature field.
signatureField2.Signature = new PdfSignature(signedDocument, loadedPage, certificate1, "Signature2", signatureField2);

// Load the signature image for the second signer.
FileStream imageStream2 = new FileStream(Path.GetFullPath(@"Data/Teacher Signature.png"), FileMode.Open, FileAccess.Read);

PdfBitmap signatureImage1 = new PdfBitmap(imageStream2);

// Display the signature image in the second signature field appearance.
signatureField2.Signature.Appearance.Normal.Graphics.DrawImage(signatureImage1, 0, 0, signatureField2.Bounds.Width, signatureField2.Bounds.Height);

// Save the fully signed PDF document.
signedDocument.Save(Path.GetFullPath(@"Output/Output.pdf"));

// Close the signed PDF document.
signedDocument.Close(true);
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,34 @@
<PackageReference Include="Syncfusion.PDF.OCR.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\ARIALUNI.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Data\Input.pdf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Output\gitkeep.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Tessdata\ara.traineddata">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Tessdata\deu.traineddata">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Tessdata\ell.traineddata">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Tessdata\eng.traineddata">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Tessdata\fra.traineddata">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Tessdata\grc.traineddata">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;

// Load the PDF document
//Open the existing PDF document.
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf")))
{
// Initialize OCR processor
//Create an OCR processor instance.
OCRProcessor processor = new OCRProcessor();
//Sets Unicode font to preserve the Unicode characters in a PDF document.
//Assign a Unicode font to retain multilingual characters in the OCR output PDF.
processor.UnicodeFont = new PdfTrueTypeFont(Path.GetFullPath(@"Data/ARIALUNI.ttf"), 8);
// Set OCR language
//Specify the languages to be recognized during OCR processing.
processor.Settings.Language = "eng+deu+ara+ell+fra"; // English, German, Arabic, Greek, French
// Set the path to the Tesseract language data folder
//Set the directory containing Tesseract language data files.
processor.TessDataPath = Path.GetFullPath(@"Tessdata");
// Perform OCR
//Run OCR on the PDF document and make its content searchable.
processor.PerformOCR(loadedDocument);
// Save the PDF document
//Save the OCR-processed PDF to the output location.
loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf"));
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
</ItemGroup>

<ItemGroup>
<None Update="Data\ARIALUNI.TTF">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Input.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
using Syncfusion.OCRProcessor;
using Syncfusion.Pdf.Graphics;

//Initialize the OCR processor by providing the path of the tesseract binaries.
//Create an OCR processor instance.
using (OCRProcessor processor = new OCRProcessor())
{
//Get stream from the image file.
//Open the input image as a file stream.
FileStream stream = new FileStream(Path.GetFullPath(@"Data/Input.jpg"), FileMode.Open);

//Set OCR language to process.
//Specify English as the language for text recognition.
processor.Settings.Language = Languages.English;

//Sets Unicode font to preserve the Unicode characters in a PDF document.
FileStream fontStream = new FileStream(Path.GetFullPath(@"Data/ARIALUNI.ttf"), FileMode.Open);

processor.UnicodeFont = new PdfTrueTypeFont(fontStream, 8);

//Perform the OCR process for an image steam.
//Extract text from the image using the OCR engine and tessdata path.
string ocrText = processor.PerformOCR(stream, processor.TessDataPath);

System.IO.File.WriteAllText(Path.GetFullPath(@"Output/Output.txt"), ocrText);
//Save the recognized text to a text file.
File.WriteAllText(Path.GetFullPath(@"Output/Output.txt"), ocrText);
}
Loading