pdf文件转换成html文件方法

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/y1535623813/article/details/88870351
pdf文件转换成html文件方法
其中用到了Aspose.Pdf 这个插件

using System.IO;
using System.Text;
using Aspose.Pdf;

namespace System.Extensions
{
    public static class PdfConverter
    {
        public static string Read(Stream stream, string fullPath)
        {
            if (Directory.Exists(fullPath))
            {
                Directory.Delete(fullPath, true);
            }
            Directory.CreateDirectory(fullPath);
            try
            {
                var doc = new Document(stream);
                var saveOptions = new HtmlSaveOptions()
                {
                    FixedLayout = true,
                    SplitIntoPages = false,
                    SplitCssIntoPages = false,
                    RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground,
                    SpecialFolderForAllImages = fullPath
                };
                var path = Path.Combine(fullPath, "i.html");
                doc.Save(path, saveOptions);
                string html;
                using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    var reader = new StreamReader(fileStream, Encoding.UTF8);
                    html = reader.ReadToEnd();
                    reader.Close();
                }
                File.Delete(path);
                return html;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/y1535623813/article/details/88870351