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));
}
相关推荐
贾斯汀frank7 小时前
C# 15 类型系统改进:Union Types
开发语言·windows·c#
时代的狂9 小时前
如何理解 C# 的 async 和 await
c#·.netcore·async·await
xiaoshuaishuai812 小时前
C# AI实现PR处理、单元测试
开发语言·c#·log4j
LONGZHIQIN12 小时前
C#基础复习笔记
开发语言·笔记·c#
时代的狂14 小时前
如何理解 C#/.NET 的依赖注入与生命周期
c#·.net·依赖注入·控制反转
品尚公益团队15 小时前
C#在asp.net网页开发中的带cookie登录实现
前端·c#·asp.net
吴可可12315 小时前
C#自定义CAD多段线零件实体
c#
CallmeFoureyes15 小时前
使用 C# 提取 Word 文档中的表格数据
开发语言·c#·word
思麟呀15 小时前
在C++基础上理解CSharp-7
java·jvm·c++·c#
杰佛史彦明117 小时前
ElasticSearch中的分词器详解
大数据·elasticsearch·c#