CSharp中文档转换为PDF或图片不依赖WPS或Office 您所在的位置:网站首页 wps导出pdf图片 CSharp中文档转换为PDF或图片不依赖WPS或Office

CSharp中文档转换为PDF或图片不依赖WPS或Office

#CSharp中文档转换为PDF或图片不依赖WPS或Office| 来源: 网络整理| 查看: 265

安装依赖Install-Package Aspose.Words -Version 19.10.0 Install-Package Aspose.Cells -Version 19.10.0 Install-Package Aspose.Slides.NET -Version 19.10.0 Install-Package Aspose.PDF -Version 19.10.0

添加系统引用

System.DrawingWord => PDF/IMGusing System; using System.Collections.Generic; using System.Drawing.Imaging; using System.IO; using Aspose.Words; using Aspose.Words.Saving; using Path = System.IO.Path; namespace document_converter.Utils { public class ConverterWord { public static void ToPdf( string sourcePath, string targetPath ) { SetLicense(); Document doc = new Document(sourcePath); doc.Save(targetPath, SaveFormat.Pdf); } /// /// 转换为图片 /// /// 原路径 /// 目标文件夹 /// 开始页面 /// 结束页面 /// 格式 /// 清晰度 /// public static List ToImage( string sourcePath, string targetPath, int startPageNum = 1, int endPageNum = int.MaxValue, ImageFormat imageFormat = null, int resolution = 128 ) { SetLicense(); string pdfPath = Path.ChangeExtension(sourcePath, "temp"); ToPdf(sourcePath, pdfPath); var imgList = ConverterPdf.ToImage(pdfPath, targetPath, startPageNum, endPageNum, imageFormat, resolution); if (File.Exists(pdfPath)) { File.Delete(pdfPath); } return imgList; } /// /// 转换为图片的方法 /// 你可以像这样调用该方法: ToImage("F:\\PdfFile.doc", "F:\\ImageFile", 1, 20, ImageFormat.Png, 256); /// /// /// /// 图片输出路径,如果为空,默认值为Word所在路径 /// /// /// 从PDF文档的第几页开始转换,如果为0,默认值为1 /// /// /// 从PDF文档的第几页开始停止转换,如果为0,默认值为Word总页数 /// /// /// 设置所需图片格式,如果为null,默认格式为JPEG /// /// /// 设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024 /// public static List ToImage2( string sourcePath, string targetPath, int startPageNum = 1, int endPageNum = int.MaxValue, ImageFormat imageFormat = null, float resolution = 256 ) { SetLicense(); string imageName = Path.GetFileNameWithoutExtension(sourcePath).Replace(" ", ""); // 返回的图片绝对路径集合 List images = new List(); try { // open word file Document doc = new Document(sourcePath); if (targetPath.Trim().Length == 0) { targetPath = Path.GetDirectoryName(sourcePath); } if (!Directory.Exists(targetPath)) { if (targetPath != null) { Directory.CreateDirectory(targetPath); } } if (startPageNum doc.PageCount || endPageNum endPageNum) { startPageNum = endPageNum; endPageNum = startPageNum; } if (imageFormat == null) { imageFormat = ImageFormat.Jpeg; } if (resolution PDF/IMGusing System; using System.Collections.Generic; using System.Drawing.Imaging; using System.IO; using Aspose.Slides; namespace document_converter.Utils { public class ConverterPpt { /// /// 转换为PDF /// /// 原路径 /// 目标路径 public static void ToPdf( string sourcePath, string targetPath ) { SetLicense(); using (var presentation = new Presentation(sourcePath)) { // 保存为PDF文件 presentation.Save(targetPath, Aspose.Slides.Export.SaveFormat.Pdf); } } /// /// 转换为图片 /// /// 原路径 /// 目标文件夹 /// 开始页面 /// 结束页面 /// 格式 /// 清晰度 /// public static List ToImage( string sourcePath, string targetPath, int startPageNum = 1, int endPageNum = int.MaxValue, ImageFormat imageFormat = null, int resolution = 128 ) { SetLicense(); string pdfPath = Path.ChangeExtension(sourcePath, "temp"); ToPdf(sourcePath, pdfPath); var imgList = ConverterPdf.ToImage(pdfPath, targetPath, startPageNum, endPageNum, imageFormat, resolution); if (File.Exists(pdfPath)) { File.Delete(pdfPath); } return imgList; } private static void SetLicense() { new License() .SetLicense( new MemoryStream( Convert.FromBase64String( "PExpY2Vuc2U+CiAgPERhdGE+CiAgICA8TGljZW5zZWRUbz5TdXpob3UgQXVuYm94IFNvZnR3YXJlIENvLiwgTHRkLjwvTGljZW5zZWRUbz4KICAgIDxFbWFpbFRvPnNhbGVzQGF1bnRlYy5jb208L0VtYWlsVG8+CiAgICA8TGljZW5zZVR5cGU+RGV2ZWxvcGVyIE9FTTwvTGljZW5zZVR5cGU+CiAgICA8TGljZW5zZU5vdGU+TGlta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ing System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using Aspose.Pdf; using Aspose.Pdf.Devices; namespace document_converter.Utils { public class ConverterPdf { /// /// 转换为图片的方法 /// 你可以像这样调用该方法: ToImage("D:\\PdfFile.pdf", "D:\\ImageFile", 1, 20, ImageFormat.Png, 256); /// /// /// /// 图片输出路径,如果为空,默认值为Word所在路径 /// /// /// 从PDF文档的第几页开始转换,如果为0,默认值为1 /// /// /// 从PDF文档的第几页开始停止转换,如果为0,默认值为Word总页数 /// /// /// 设置所需图片格式,如果为null,默认格式为JPEG /// /// /// 设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024 /// public static List ToImage( string sourcePath, string targetPath, int startPageNum = 1, int endPageNum = int.MaxValue, ImageFormat imageFormat = null, int resolution = 128 ) { SetLicense(); string imageName = Path.GetFileNameWithoutExtension(sourcePath).Replace(" ", ""); // 返回的图片绝对路径集合 List images = new List(); try { if (!Directory.Exists(targetPath)) { if (targetPath != null) { Directory.CreateDirectory(targetPath); } } using (var pdfDocument = new Document(sourcePath)) { if (startPageNum pdfDocument.Pages.Count || endPageNum endPageNum) { startPageNum = endPageNum; endPageNum = startPageNum; } if (imageFormat == null) { imageFormat = ImageFormat.Jpeg; } if (resolution endPageNum) { startPageNum = endPageNum; endPageNum = startPageNum; } for (int i = startPageNum; i PDF

document-converter.exe pdf "D:\\Tools\\Docs\\01.docx" "D:\\Tools\\Docs\\pdf\\01.pdf" document-converter.exe pdf "D:\\Tools\\Docs\\01.doc" "D:\\Tools\\Docs\\pdf\\01.pdf"

PPT => PDF

document-converter.exe pdf "D:\\Tools\\Docs\\02.pptx" "D:\\Tools\\Docs\\pdf\\02.pdf"

缺少字体文档测试

document-converter.exe pdf "D:\\Tools\\Docs\\more\\第一节 导数的概念及运算 定积分.ppt" "D:\\Tools\\Docs\\pdf\\第一节 导数的概念及运算 定积分.pdf"

Excel => PDF

document-converter.exe pdf "D:\\Tools\\Docs\\03.xlsx" "D:\\Tools\\Docs\\pdf\\03.pdf"

TXT => PDF

document-converter.exe pdf "D:\\Tools\\Docs\\04.txt" "D:\\Tools\\Docs\\pdf\\04.pdf"

XML => PDF

document-converter.exe pdf "D:\\Tools\\Docs\\05.xml" "D:\\Tools\\Docs\\pdf\\05.pdf"

HTML => PDF

document-converter.exe pdf "D:\\Tools\\Docs\\06.html" "D:\\Tools\\Docs\\pdf\\06.pdf"转为图片

PDF => IMG

document-converter.exe img "D:\\Tools\\Docs\\pdf\\01.pdf" "D:\\Tools\\Docs\\images\\pdf\\"

Word => IMG

document-converter.exe img "D:\\Tools\\Docs\\01.docx" "D:\\Tools\\Docs\\images\\word\\"

PPT => IMG

document-converter.exe img "D:\\Tools\\Docs\\02.pptx" "D:\\Tools\\Docs\\images\\ppt\\"

Excel => IMG

document-converter.exe img "D:\\Tools\\Docs\\03.xlsx" "D:\\Tools\\Docs\\images\\excel\\"

TXT => IMG

document-converter.exe img "D:\\Tools\\Docs\\04.txt" "D:\\Tools\\Docs\\images\\txt\\"

XML => IMG

document-converter.exe img "D:\\Tools\\Docs\\05.xml" "D:\\Tools\\Docs\\images\\xml\\"

HTML => IMG

document-converter.exe img "D:\\Tools\\Docs\\06.html" "D:\\Tools\\Docs\\images\\html\\"Aspose.PDF和O2S.Components.PDFRender4NET

Aspose.PDF是一个用于处理PDF文件的强大工具,它提供了一系列的功能,包括:

创建和编辑PDF文件:可以使用Aspose.PDF创建新的PDF文档、编辑和修改现有的PDF文档。 将PDF文件转换为其他格式:将PDF文件转换为多种文件格式,包括DOC、DOCX、HTML、XML、PPT和XPS等。 添加水印和加密:可以在PDF文档中添加水印、数字签名和加密等安全特性。 表单处理:用于创建、填写和提交PDF表单。 文本和图像处理:可以从PDF文档中提取文本和图像,还可以在文档中插入、更新和删除文本和图像等。 页面元素处理:可以在PDF页面上添加、更新、删除和移动页面元素,如链接、书签、注释、标签和旋转等。

总之,Aspose.PDF提供了广泛的功能,能够满足用户处理PDF文件的各种需求。

Aspose.PDF功能比较多所以文件比较大,有32.9M。 O2S.Components.PDFRender4NET在转图片上效果也不错,大小只有944K,还不到1M,推荐使用。

在网上看的

O2S.Components.PDFRender4NET依赖Ghostscript软件来渲染PDF文件。

但是实际测试的时候并不用安装Ghostscript,也能转换成功。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有