C#使用Aspose将Word\HTML 转换成PDF文件
写在前面
- Aspose 这个是收费的,直接使用是有水印的
 - 需要用到的dll文件 ==> Aspose.Words.dll、Aspose.HTML.dll、Aspose.Total.lic(授权文件)
 - 我使用的是.NET Framework 4.0 ,.NET Core 使用nuget安装
 
Word转换成PDF
1、引用 Aspose.Words.dll 文件
    
2、授权。先将 Aspose.Total.lic 添加到项目中,然后设置文件属性
    
3、转换代码
public static class PdfHelper
{
static PdfHelper()
{
Aspose.Words.License license = new Aspose.Words.License();
license.SetLicense("Aspose.Total.lic"); //授权
} /// <summary>
/// word 转换成 pdf 文件
/// </summary>
/// <param name="sourcePath">原文件</param>
/// <param name="targetPath">转换后的pdf存储路径</param>
/// <param name="error"></param>
/// <returns></returns>
private static string WordToPdf(string sourcePath, string targetPath)
{
string error = "";
try
{
using (FileStream localFileStream = new FileInfo(sourcePath).Open(FileMode.Open, FileAccess.Read, FileShare.Read))
{
var opt = new Aspose.Words.LoadOptions();
opt.Encoding = Encoding.UTF8;
var doc = new Aspose.Words.Document(localFileStream, opt); //如果word文档有自己安装的字体需要追加一下,不然转换出来的pdf文件字体格式会错误
string userfontsfoloder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Microsoft\\Windows\\Fonts\\"; ArrayList fontSources = new ArrayList(Aspose.Words.Fonts.FontSettings.DefaultInstance.GetFontsSources());
//将用户目录字体添加到字体源中
Aspose.Words.Fonts.FolderFontSource folderFontSource = new Aspose.Words.Fonts.FolderFontSource(userfontsfoloder, true);
fontSources.Add(folderFontSource);
Aspose.Words.Fonts.FontSourceBase[] updatedFontSources = (Aspose.Words.Fonts.FontSourceBase[])fontSources.ToArray(typeof(Aspose.Words.Fonts.FontSourceBase));
Aspose.Words.Fonts.FontSettings.DefaultInstance.SetFontsSources(updatedFontSources); //doc.Sections[0].PageSetup.PageWidth = 2479 / 2.5;
//doc.Sections[0].PageSetup.PageHeight = 3508 / 2.5;
//doc.Sections[0].PageSetup.OddAndEvenPagesHeaderFooter = false;
doc.Save(targetPath, Aspose.Words.SaveFormat.Pdf);
}
}
catch (Exception ex)
{
error = "转换pdf失败,error:" + ex.Message;
}
return error;
}
}
HTML转换成PDF
1、引用Aspose.HTML.dll文件
参考Word转换PDF步骤1
2、授权
参考Word转换PDF步骤2
3、转换代码
在Word转换PDF的Helper类里添加代码
在构造函数追加授权代码
static PdfHelper()
{
Aspose.Words.License license = new Aspose.Words.License();
license.SetLicense("Aspose.Total.lic"); //授权
Aspose.Html.License licenseHtml = new Aspose.Html.License();
licenseHtml.SetLicense("Aspose.Total.lic"); //授权
}
新增HTML转换PDF代码
private static string HtmlToPdf(string sourcePath, string targetPath)
{
string error = "";
try
{
var doc = new Aspose.Html.HTMLDocument(sourcePath);
var opt = new Aspose.Html.Saving.PdfSaveOptions();
Aspose.Html.Converters.Converter.ConvertHTML(doc, opt, targetPath);
}
catch (Exception ex)
{
error = "转换pdf失败,error:" + ex.Message;
}
return error;
}
4、如果html 转换成pdf文件格式有问题也可以尝试使用【Select.HtmlToPdf】
C#使用Aspose将Word\HTML 转换成PDF文件的更多相关文章
- OpenOffice将MS docx转换成pdf文件偶数页眉不显示问题解决办法
		
OpenOffice版本:4.0(Windows.Linux下测试都出现问题) MS Office版本:2007 问题描述 使用OpenOffice将MS的docx文件转换为pdf文件时,docx文件 ...
 - Nodejs 中将html转换成pdf文件
		
Nodejs 中将html转换成pdf文件,Nodejs Convert html into pdf 1. 下载phantomjs.exe,将该文件放在根目录 2. 编写pdf.js文件(在githu ...
 - Linux不用使用软件把纯文本文档转换成PDF文件的方法
		
当你有一大堆文本文件要维护的时候,把它们转换成PDF文档会好一些.比如,PDF更适合打印,因为PDF文档有预定义布局.除此之外,还可以减少文档被意外修改的风险. 要将文本文件转换成PDF格式,你要按照 ...
 - C#.net   word excel powerpoint  (ppt) 转换成 pdf  文件
		
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
 - Python将word文档转换成PDF文件
		
如题. 代码: ''' #將word文档转换为pdf文件 #用到的库是pywin32 #思路上是调用了windows和office功能 ''' #导入所需库 from win32com.client ...
 - 在Linux下将HTML文件转换成PDF文件
		
今天要写一个上交的作业,本来是想用Office Word来写的,但是,我的Office貌似不能用了,但是,Linux下的LibreOffice写出的文档,在打印的时候是经常出现乱码的.所以,后来想到可 ...
 - C# 将PowerPoint文件转换成PDF文件
		
PowerPoint的优势在于对演示文档的操作上,而用PPT查看资料,反而会很麻烦.这时候,把PPT转换成PDF格式保存,再浏览,不失为一个好办法.在日常编程中和开发软件时,我们也有这样的需要.本文旨 ...
 - 使用abcpdf将html转换成pdf文件
		
ABCpdf.NET使用介绍 最新做一个项目需要生成pdf文档以供打印,研究决定使用abcpdf这款组件,先针对其使用方法做一个简单的总结介绍以给有需要的朋友做参考. 一. ABCpdf.NET简单介 ...
 - JAVA使用aspose实现word文档转pdf文件
		
引入jar包 下载地址:https://yvioo.lanzous.com/iezpdno3mob 然后打开下载的目录打开cmd执行 mvn install:install-file -Dfile=a ...
 - 用java代码把docx转换成pdf文件
		
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio ...
 
随机推荐
- M3U8下载器加嗅探浏览器
			
M3U8下载器太多了,随便一抓一大把,没什么新奇的. 下载地址: https://www.zhaimaojun.cn/P/%e8%a7%86%e9%a2%91%e7%bd%91%e7%ab%99%e5 ...
 - Golang 爬虫02
			
验证邮箱 目标站点: https://movie.douban.com/top250
 - ContextCapture-硬件配置推荐
			
ContextCapture倾斜摄影的空三计算.三维建模应用.非常耗费硬件资源,适当调整硬件配置,可以显著提高模型处理时间. 硬件常见问题 随着倾斜摄影建模算法成熟,应用越来越广泛,数据量越来越大,需 ...
 - 复杂模式的两个List与Map合并为一个Map的拼接;笛卡尔乘积处理数据问题
			
简介 (Introduction): 背景 数据从多个表中获取,每个表的数据条数不是唯一的,最后结果要拼接成一个Map<String,Object>的模式封装所有数据,每个数据是一条. 结 ...
 - ansible使用详解
			
ansible执行,用户主机配置 免密同一个同一个用户执行命令 1 能免密登录的[root@mcw1 ~]$ ansible 10.0.0.132 -m shell -a "hostname ...
 - 使用systemctl管理服务(nginx)
			
首先调整好路径信息,修改配置文件vim /usr/lib/systemd/system/nginx.service [Unit]Description=The nginx HTTP and rever ...
 - spring boot整合maybatis plus 的 文件生成代码
			
/** * 代码生成 */public class AutoGenerator_ { public static void main(String[] args) { AutoGenerator ge ...
 - 拼接sql 参数化 where userId in(@userIds)的问题
			
这里@userIds 如果 写成101,202,301翻译后的sql的where部分会是: where userId in('101,202,301'): 而不是期待的: where userId i ...
 - 解决:ModuleNotFoundError: No module named ‘urllib3.packages.six.moves问题
			
解决方案: 第一步:把selenium版本降到3.3.1 配合urllib3版本1.26.2使用,成功解决 &在python interpreter安装availablePackages se ...
 - Android 12(S) MultiMedia(十一)从MPEG2TSExtractor到MPEG2-TS
			
本节主要学习内容是看看MPEG2TSExtractor是如何处理TS流的. 相关代码位置: frameworks/av/media/extractors/mpeg2/MPEG2TSExtractor. ...