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));
}
相关推荐
田井中律.5 小时前
知识图谱(关系抽取方法)【第十章】
人工智能·c#·知识图谱
周杰伦fans7 小时前
C# CAD二次开发:RotatedDimension 文字边框设置完全指南
开发语言·c#
新缸中之脑11 小时前
用Claude for Word审查法律合同
开发语言·c#·word
xuxie9911 小时前
N27 数据库UI
linux·c#
唐青枫11 小时前
C#.NET TaskCompletionSource 深入解析:手动控制 Task、桥接回调事件与实战避坑
c#·.net
张人玉11 小时前
VisionPro 药物检测工具 学习笔记
算法·c#·机器视觉·vsionpro
周杰伦fans12 小时前
C# CAD二次开发:RotatedDimension 旋转标注完全指南
开发语言·c#
宝桥南山12 小时前
Azure - 尝试一下使用Azure Developer CLI去部署应用程序
microsoft·ai·微软·c#·aigc·azure
OctShop大型商城源码12 小时前
C#.NET多商户商城系统源码_OctShop:技术与机遇的融合
c#·.net·多商户商城系统源码·商城系统源码
张人玉13 小时前
VisionPro 书页查找工具 学习笔记
c#·机器视觉·vsionpro