将HTML转换为PDF / A-1A

时间:2017-09-15 14:45:06

标签: c# pdf itext pdf-generation pdfa

我需要将HTML页面转换为PDF / A-1A。

我可以使用iTextSharp将文档从HTML转换为PDF。

这是我现在使用iText 5.5.12.0的代码。 我不介意使用iText 7 。我不能使用iText 7(见https://stackoverflow.com/a/44845757/5178

当我调用doc.Close();时,抛出NullReferenceException。

        Byte[] buffer;

        using (MemoryStream output_stream = new MemoryStream())
        using (Document doc = new Document())
        using (PdfAWriter writer = PdfAWriter.GetInstance(doc, output_stream, PdfAConformanceLevel.PDF_A_1A))
        {
            writer.SetTagged();
            FontFactory.RegisterDirectories();

            doc.Open();

            string example_html = @"<p>This <em>is </em><span class=""headline"" style=""text-decoration: underline;"">some</span> <strong>sample <em> text</em></strong><span style=""color: red;"">!!!</span></p>";
            string example_css = @".headline{font-size:200%}";;

            using (MemoryStream msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css)))
            using (MemoryStream msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html)))
            {
                iTextSharp.tool.xml.XMLWorkerHelper xml_worker = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();
                xml_worker.ParseXHtml(writer, doc, msHtml, msCss);
            }

            doc.AddAuthor("DKM");
            doc.AddSubject("DKM");
            doc.AddLanguage("en-gb");
            doc.AddCreationDate();
            doc.AddCreator("DKM");
            doc.AddTitle("DKM");
            writer.CreateXmpMetadata();

            doc.Close();

            buffer = output_stream.ToArray();
        }

        string target_pdf_file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
        System.IO.File.WriteAllBytes(target_pdf_file, buffer);

        System.Diagnostics.Process.Start(target_pdf_file);

如何在HTML中将HTML页面转换为PDF / A-1a?

0 个答案:

没有答案