写在前面

  1. Aspose 这个是收费的,直接使用是有水印的
  2. 需要用到的dll文件 ==> Aspose.Words.dll、Aspose.HTML.dll、Aspose.Total.lic(授权文件)
  3. 我使用的是.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文件的更多相关文章

  1. OpenOffice将MS docx转换成pdf文件偶数页眉不显示问题解决办法

    OpenOffice版本:4.0(Windows.Linux下测试都出现问题) MS Office版本:2007 问题描述 使用OpenOffice将MS的docx文件转换为pdf文件时,docx文件 ...

  2. Nodejs 中将html转换成pdf文件

    Nodejs 中将html转换成pdf文件,Nodejs Convert html into pdf 1. 下载phantomjs.exe,将该文件放在根目录 2. 编写pdf.js文件(在githu ...

  3. Linux不用使用软件把纯文本文档转换成PDF文件的方法

    当你有一大堆文本文件要维护的时候,把它们转换成PDF文档会好一些.比如,PDF更适合打印,因为PDF文档有预定义布局.除此之外,还可以减少文档被意外修改的风险. 要将文本文件转换成PDF格式,你要按照 ...

  4. C#.net word excel powerpoint (ppt) 转换成 pdf 文件

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  5. Python将word文档转换成PDF文件

    如题. 代码: ''' #將word文档转换为pdf文件 #用到的库是pywin32 #思路上是调用了windows和office功能 ''' #导入所需库 from win32com.client ...

  6. 在Linux下将HTML文件转换成PDF文件

    今天要写一个上交的作业,本来是想用Office Word来写的,但是,我的Office貌似不能用了,但是,Linux下的LibreOffice写出的文档,在打印的时候是经常出现乱码的.所以,后来想到可 ...

  7. C# 将PowerPoint文件转换成PDF文件

    PowerPoint的优势在于对演示文档的操作上,而用PPT查看资料,反而会很麻烦.这时候,把PPT转换成PDF格式保存,再浏览,不失为一个好办法.在日常编程中和开发软件时,我们也有这样的需要.本文旨 ...

  8. 使用abcpdf将html转换成pdf文件

    ABCpdf.NET使用介绍 最新做一个项目需要生成pdf文档以供打印,研究决定使用abcpdf这款组件,先针对其使用方法做一个简单的总结介绍以给有需要的朋友做参考. 一. ABCpdf.NET简单介 ...

  9. JAVA使用aspose实现word文档转pdf文件

    引入jar包 下载地址:https://yvioo.lanzous.com/iezpdno3mob 然后打开下载的目录打开cmd执行 mvn install:install-file -Dfile=a ...

  10. 用java代码把docx转换成pdf文件

    import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio ...

随机推荐

  1. 大营销抽奖系统,DDD开发要如何建模?

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 大家好,我是技术UP主小傅哥. ‍ 经过5.1假期的一顿框框输出,终于完成了<大营销项目 ...

  2. sh角本操作数据库

    #!/bin/bash HOST="127.0.0.1" PORT="3306" USERNAME="root" PASSWORD=&quo ...

  3. python教程6.1-模块和包

    模块分类 1.内置标准模块(⼜称标准库)执⾏help('modules')查看所有python⾃带模块列表 2.第三⽅开源模块,可通过pip install 模块名 联⽹安装 3.⾃定义模块 模块导入 ...

  4. PageOffice 在线打开 word 文件并添加水印

    一.服务器端方法插入水印 1.插入文字水印 WordDocument doc = new WordDocument(); //添加水印 ,设置水印的内容 doc.getWaterMark().setT ...

  5. 使用 JS 实现在浏览器控制台打印图片 console.image()

    在前端开发过程中,调试的时候,我门会使用 console.log 等方式查看数据.但对于图片来说,仅靠展示的数据与结构,是无法想象出图片最终呈现的样子的. 虽然我们可以把图片数据通过 img 标签展示 ...

  6. ansible使用详解

    ansible执行,用户主机配置 免密同一个同一个用户执行命令 1 能免密登录的[root@mcw1 ~]$ ansible 10.0.0.132 -m shell -a "hostname ...

  7. Java的深浅拷贝认识

    目录 浅拷贝 深拷贝 分辨代码里的深浅拷贝 在Java中,深拷贝和浅拷贝是对象复制的两种方式,主要区别在于对对象内部的引用类型的处理上. 浅拷贝 定义: 浅拷贝是指创建一个新的对象,但这个新对象的属性 ...

  8. nohup Command [ Arg … ] [ & ]

    nohup 英文全称:no hang up 不挂断的意思.退出终端不挂断程序的运行.在默认的情况下,会输出一个名叫 nohup.out 的文件到当前目录下.nohup Command [ Arg - ...

  9. 珠排序算法C# 简单实现 奇葩排序中的算盘排序(算珠排序)算法

    Console.WriteLine("Hello World!"); int[] arr = { 1, 3, 4, 0, 22, 4,0, 6, 3,10,8,6,7 }; Con ...

  10. CSS置顶操作(z-index属性)

    z-index使用方法: 1.首先要把position设置为 absolute 或 relative 或 fixed,z-index才能生效 2.设置z-index的值(整数) # 值越大代表越置前, ...