ItextSharp html to pdf with signature

时间:2017-08-08 21:23:04

标签: c# .net itext

我想从html生成一个pdf并在其中包含签名块。 html可以是任何格式,但有时可能是1页,有时可能是50页。签名将是页面上的唯一输入。 html的一个例子可能是。

<div>
    <input type="text" />
</div>

有谁知道怎么做?我目前正在使用ItextSharp生成pdf。

1 个答案:

答案 0 :(得分:1)

技术组合将产生您想要的结果。

  1. 使用pdfHTML(iText7插件)将HTML(HTML5 + CSS3)转换为PDF
  2. 您可以为这些代码添加自定义代码和处理程序。您可以创建一个注入签名表单域
  3. 使用iText对PDF文档进行数字签名
  4. 数字签名的简短示例:

    public void sign(String src, String dest,
                 Certificate[] chain,
                 PrivateKey pk, String digestAlgorithm, String provider,
                 PdfSigner.CryptoStandard subfilter,
                 String reason, String location)
        throws GeneralSecurityException, IOException {
    // Creating the reader and the signer
    PdfReader reader = new PdfReader(src);
    PdfSigner signer = new PdfSigner(reader, new FileOutputStream(dest), false);
    // Creating the appearance
    PdfSignatureAppearance appearance = signer.getSignatureAppearance()
            .setReason(reason)
            .setLocation(location)
            .setReuseAppearance(false);
    Rectangle rect = new Rectangle(36, 648, 200, 100);
    appearance
            .setPageRect(rect)
            .setPageNumber(1);
    signer.setFieldName("sig");
    // Creating the signature
    IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, provider);
    IExternalDigest digest = new BouncyCastleDigest();
    signer.signDetached(digest, pks, chain, null, null, null, 0, subfilter);
    }
    

    pdfHTML的简短示例:

    public void createPdf(String src, String dest, String resources) throws IOException {
        try {
            FileOutputStream outputStream = new FileOutputStream(dest);
    
            WriterProperties writerProperties = new WriterProperties();
            //Add metadata
            writerProperties.addXmpMetadata();
    
            PdfWriter pdfWriter = new PdfWriter(outputStream, writerProperties);
    
            PdfDocument pdfDoc = new PdfDocument(pdfWriter);
            pdfDoc.getCatalog().setLang(new PdfString("en-US"));
            //Set the document to be tagged
            pdfDoc.setTagged();
            pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
    
            //Set meta tags
            PdfDocumentInfo pdfMetaData = pdfDoc.getDocumentInfo();
            pdfMetaData.setAuthor("Samuel Huylebroeck");
            pdfMetaData.addCreationDate();
            pdfMetaData.getProducer();
            pdfMetaData.setCreator("iText Software");
            pdfMetaData.setKeywords("example, accessibility");
            pdfMetaData.setSubject("PDF accessibility");
            //Title is derived from html
    
            // pdf conversion
            ConverterProperties props = new ConverterProperties();
            FontProvider fp = new FontProvider();
            fp.addStandardPdfFonts();
            fp.addDirectory(resources);//The noto-nashk font file (.ttf extension) is placed in the resources
    
            props.setFontProvider(fp);
            props.setBaseUri(resources);
            //Setup custom tagworker factory for better tagging of headers
            DefaultTagWorkerFactory tagWorkerFactory = new AccessibilityTagWorkerFactory();
            props.setTagWorkerFactory(tagWorkerFactory);
    
            HtmlConverter.convertToPdf(new FileInputStream(src), pdfDoc, props);
            pdfDoc.close();
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    http://itextpdf.com/itext7/pdfHTML

    的更多信息