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));
}
相关推荐
lzhdim3 小时前
C#开发的提示显示例子 - 开源研究系列文章
开发语言·c#
人工智能AI技术3 小时前
【C#程序员入门AI】向量数据库入门:C#集成Chroma/Pinecone,实现AI知识库检索(RAG基础)
人工智能·c#
xb11323 小时前
C# 定时器和后台任务
开发语言·c#
A_nanda5 小时前
c# 用VUE+elmentPlus生成简单管理系统
javascript·vue.js·c#
wuguan_6 小时前
C#之线程
开发语言·c#
gc_22998 小时前
学习C#调用OpenXml操作word文档的基本用法(21:学习嵌入对象类)
c#·word·openxml·ole
老骥伏枥~8 小时前
C# if / else 的正确写法与反例
开发语言·c#
老骥伏枥~9 小时前
C# 运算符优先级易踩坑
c#
SunnyDays10119 小时前
C# 实战:从 Word 文档中提取指定页面
c#·提取word文档页面·将word页面复制到另一个文档
骆驼爱记录9 小时前
Word通配符技巧:高效文档处理指南
开发语言·c#·自动化·word·excel·wps·新人首发