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));
}
相关推荐
初级代码游戏1 天前
C#:程序发布的大小控制 裁剪 压缩
c#·.net·dotnet·压缩·大小·发布·裁剪
量子物理学1 天前
Modbus TCP
c#·modbus tcp
人工智能AI技术1 天前
能用C#开发AI吗?
人工智能·c#
自己的九又四分之三站台2 天前
6. 简单将原生代码改为流式请求
c#
一叶星殇2 天前
C# .NET 如何解决跨域(CORS)
开发语言·前端·c#·.net
JQLvopkk2 天前
C#调用Unity实现设备仿真开发浅述
开发语言·unity·c#
zxy28472253012 天前
使用Topshelf部署window后台服务(C#)
c#·安装·topshelf·后台服务
缺点内向2 天前
C# 高效统计 Word 文档字数:告别手动,拥抱自动化
c#·自动化·word
skywalk81632 天前
介绍一下 Backtrader量化框架(C# 回测快)
开发语言·c#·量化
Never_Satisfied2 天前
C#数组去重方法总结
开发语言·c#