ItextSharp HTML to PDF转换如何使用上标和下标原样转换文本

时间:2015-09-02 03:15:24

标签: c# pdf-generation itextsharp

嗨,这是我将HTML格式转换为PDF格式的实现

            private static PdfContent PdfContentA()
                    {
                        var cssText = System.IO.File.ReadAllText(@"C:\Users\kalanamithm\Desktop\Ground Operations\MvcApplication1\MvcApplication1\Resources\CSS\FTL.css");
                        //var htmlText = System.IO.File.ReadAllText(@"C:\Users\kalanamithm\Desktop\Ground Operations\MvcApplication1\MvcApplication1\Resources\HTML\grace.html");

                        var htmlText = System.IO.File.ReadAllText(@"C:\Users\kalanamithm\Desktop\Ground Operations\MvcApplication1\MvcApplication1\Resources\HTML\FLT.html");

                        var memoryStream = new MemoryStream();
                        var document = new Document();
                        var writer = PdfWriter.GetInstance(document, memoryStream);
                        document.Open();

                        var xmlWorkerFontProvider = new XMLWorkerFontProvider();
                        xmlWorkerFontProvider.Register("http://localhost:58045/Resources/Fonts/TequilaSunset-Regular.ttf");
                        xmlWorkerFontProvider.Register("http://localhost:58045/Resources/Fonts/Mf Sippin On Sunshine.ttf");
                        using (var cssMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssText)))
                        {
                            using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlText)))
                            {
                                XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, htmlMemoryStream, cssMemoryStream);
                            }
                        }


                        document.Close();
                        var pdfContent = new PdfContent
                        {
                            MemoryStream = memoryStream,
                            FileName = "SomeName"
                        };
                        return pdfContent;
                    }
你在这里面临一个问题,当你转换它不会在Pdf中显示的SuperScript pr下标时。

ITextSharp中是否有解决方案?

2 个答案:

答案 0 :(得分:0)

我想出了一个解决方案 在html我手动创建一个上标如下,它的工作原理 (根据我的情况)

 "<span style=\"width:500px;\">" +
                            "<span>500 m</span>" +
                            "<span style=\"vertical-align:super;font-size:8px;\">2</span>" +
                        "</span>" +

答案 1 :(得分:0)

由于你的问题提到“原样”,你可以在你的css文件中添加sub和sup标签的css。然后所有下标和上标标签都将以pdf格式呈现。无需更改初始html。

<style>
sup {
vertical-align: super;
font-size: 9px;
}

sub {
vertical-align: sub;
font-size: 9px;
}
</style>