public ActionResult CreatePdf(IFormCollection collection) { // create a PDF document PdfDocument document = new PdfDocument(); // set a demo serial number document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; // create a page in document PdfPage page1 = document.AddPage(); // create the true type fonts that can be used in document text Font sysFont = new Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point); PdfFont pdfFont = document.CreateFont(sysFont); PdfFont pdfFontEmbed = document.CreateFont(sysFont, true); float crtYPos = 20; float crtXPos = 5; // add a title to PDF document PdfText titleTextTransImage = new PdfText(crtXPos, crtYPos, "Click the image below to open the digital signature", pdfFontEmbed); titleTextTransImage.ForeColor = Color.Navy; PdfLayoutInfo textLayoutInfo = page1.Layout(titleTextTransImage); crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // layout a PNG image with alpha transparency PdfImage transparentPdfImage = new PdfImage(crtXPos, crtYPos, m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Images\HiQPdfLogo_small.png"); PdfLayoutInfo imageLayoutInfo = page1.Layout(transparentPdfImage); // apply a digital sgnature over the image PdfCertificatesCollection pdfCertificates = PdfCertificatesCollection.FromFile(m_hostingEnvironment.ContentRootPath + @"\wwwroot" + @"\DemoFiles\Pfx\hiqpdf.pfx", "hiqpdf"); PdfDigitalSignature digitalSignature = new PdfDigitalSignature(pdfCertificates[0]); digitalSignature.SigningReason = "My signing reason"; digitalSignature.SigningLocation = "My signing location"; digitalSignature.SignerContactInfo = "My contact info"; document.AddDigitalSignature(digitalSignature, imageLayoutInfo.LastPdfPage, imageLayoutInfo.LastPageRectangle); try { // write the PDF document to a memory buffer byte[] pdfBuffer = document.WriteToMemory(); FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf"); fileResult.FileDownloadName = "DigitalSignatures.pdf"; return(fileResult); } finally { document.Close(); } }
/// <summary> /// Sign document with digital signature applying PDF document-specific options /// Please be aware that this example works only on licensed product due to limitation with Pdf processing /// </summary> public static void Run() { Console.WriteLine("\n--------------------------------------------------------------------------------------------------------------------"); Console.WriteLine("[Example Advanced Usage] # SignWithDigitalAdvancedPdfCertificate : Sign document with digital signature applying PDF certificate options\n"); // The path to the documents directory. string filePath = Constants.SAMPLE_PDF; string certificatePath = Constants.CertificatePfx; string outputFilePath = Path.Combine(Constants.OutputPath, "SignWithDigitalAdvancedPdf", "digitallyCertified.pdf"); //Certify pdf document with digital signature using (Signature signature = new Signature(filePath)) { PdfDigitalSignature pdfDigitalSignature = new PdfDigitalSignature() { ContactInfo = "Contact", Location = "Location", Reason = "Reason", // Setting pdf digital signature type as Certificate Type = PdfDigitalSignatureType.Certificate }; //Create digital signing options DigitalSignOptions options = new DigitalSignOptions(certificatePath) { // certificate password Password = "******", // Setting document-specific options Signature = pdfDigitalSignature, // Page position VerticalAlignment = VerticalAlignment.Bottom, HorizontalAlignment = HorizontalAlignment.Right }; SignResult signResult = signature.Sign(outputFilePath, options); Console.WriteLine($"\nSource document signed successfully with {signResult.Succeeded.Count} signature(s).\nFile saved at {outputFilePath}."); Console.WriteLine("\nList of newly created signatures:"); int number = 1; foreach (BaseSignature temp in signResult.Succeeded) { Console.WriteLine($"Signature #{number++}: Type: {temp.SignatureType} Id:{temp.SignatureId}, Location: {temp.Left}x{temp.Top}. Size: {temp.Width}x{temp.Height}"); } } }
protected void buttonCreatePdf_Click(object sender, EventArgs e) { // create a PDF document PdfDocument document = new PdfDocument(); // set a demo serial number document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; // create a page in document PdfPage page1 = document.AddPage(); // create the true type fonts that can be used in document text Font sysFont = new Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point); PdfFont pdfFont = document.CreateFont(sysFont); PdfFont pdfFontEmbed = document.CreateFont(sysFont, true); float crtYPos = 20; float crtXPos = 5; // add a title to PDF document PdfText titleTextTransImage = new PdfText(crtXPos, crtYPos, "Click the image below to open the digital signature", pdfFontEmbed); titleTextTransImage.ForeColor = Color.Navy; PdfLayoutInfo textLayoutInfo = page1.Layout(titleTextTransImage); crtYPos += textLayoutInfo.LastPageRectangle.Height + 10; // layout a PNG image with alpha transparency PdfImage transparentPdfImage = new PdfImage(crtXPos, crtYPos, Server.MapPath("~") + @"\DemoFiles\Images\HiQPdfLogo_small.png"); PdfLayoutInfo imageLayoutInfo = page1.Layout(transparentPdfImage); // apply a digital sgnature over the image PdfCertificatesCollection pdfCertificates = PdfCertificatesCollection.FromFile(Server.MapPath("~") + @"\DemoFiles\Pfx\hiqpdf.pfx", "hiqpdf"); PdfDigitalSignature digitalSignature = new PdfDigitalSignature(pdfCertificates[0]); digitalSignature.SigningReason = "My signing reason"; digitalSignature.SigningLocation = "My signing location"; digitalSignature.SignerContactInfo = "My contact info"; document.AddDigitalSignature(digitalSignature, imageLayoutInfo.LastPdfPage, imageLayoutInfo.LastPageRectangle); try { // write the PDF document to a memory buffer byte[] pdfBuffer = document.WriteToMemory(); // inform the browser about the binary data format HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf"); // let the browser know how to open the PDF document and the file name HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=DigitalSignatures.pdf; size={0}", pdfBuffer.Length.ToString())); // write the PDF buffer to HTTP response HttpContext.Current.Response.BinaryWrite(pdfBuffer); // call End() method of HTTP response to stop ASP.NET page processing HttpContext.Current.Response.End(); } finally { document.Close(); } }