C# PDF转HTML字符串

需要nuget安装Aspose.PDF插件,本文使用23.10.0版本

一、获取PDF文件,通过Aspose.Pdf.Document 以Html格式 保存到某个路径;再读取该html返回字符串。

cs 复制代码
//html文件保存路径
string filePath = dirPath + "xxx.html";
if (!File.Exists(filePath))
{
    //获取pdf文件流
    Byte[] pdfByte = ......;
    var document = new Aspose.Pdf.Document(new MemoryStream(pdfByte));
    document.Save(filePath, SaveFormat.Html);
}

//读取刚刚保存的文件  
string file = File.ReadAllText(filePath);
string oldPath = "xxx_files";
string newPath = ConfigurationManager.AppSettings["NurseHtmlIP"] + "/" + oldPath;
//剔除版权文字;替换本地css和图片路径 为 http路径,使得在第三方接口可调用
string targetHtml = file.Replace("Evaluation Only. Created with Aspose.PDF. Copyright 2002-2023 Aspose Pty Ltd.","").Replace(oldPath, newPath);
return targetHtml;

二、在用Base64加密,避免格式错误。

cs 复制代码
string base64Html = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(targetHtml)));

三、前端显示,js方法。

cs 复制代码
//base64转字符串
function base64ToString(str) {
    return decodeURIComponent(atob(str).split('').map(function (c) {
        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
    }).join(''));
}

var showInfo = function (htmlStr) {
    //把html显示到前端
    $("#ele_target").html(base64ToString(htmlStr));
}
相关推荐
czhc11400756631 小时前
c#w 1214
开发语言·c#
用户298698530141 小时前
C# 中如何从 URL 下载 Word 文档:基于 Spire.Doc 的高效解决方案
后端·c#·.net
wangbing11251 小时前
将swagger在线文档转为word
microsoft·c#·word
mangge082 小时前
定时刷新已经登录过的网页c#
c#
走错路的程序员3 小时前
C语言单片机与C#上位机之间传递大量参数比较好的实践方案
c语言·单片机·c#
辜月廿七4 小时前
C#字符串相关知识
c#
波波0075 小时前
使用.NET 四步玩转 AI 绘图|不用Python、不买显卡
开发语言·c#·.net
唐青枫5 小时前
深入理解 C#.NET 运算符重载:语法、设计原则与最佳实践
c#·.net
工程师0076 小时前
C# HSL 与欧姆龙 CIP 协议(EtherNet/IP)的详细通信
网络协议·tcp/ip·c#·欧姆龙cip协议·hsl
林杜雨都13 小时前
Action和Func
开发语言·c#