使用c#将word文件(.docx&;doc)转换为.pdf
本文关键字:doc 转换 pdf amp word 文件 docx 使用 | 更新日期: 2023-09-27 17:57:36
在不使用SaveAs()
或Save()
方法的情况下,如何在c#
中将单词文件(.docx & doc
)转换为.pdf
?还是不在服务器上上传?
试试这个,它对我有用:
using Microsoft.Office.Interop.Word;
var appWord = new Application();
if (appWord.Documents != null)
{
//yourDoc is your word document
var wordDocument = appWord.Documents.Open(yourDoc);
string pdfDocName = "pdfDocument.pdf";
if (wordDocument != null)
{
wordDocument.ExportAsFixedFormat(pdfDocName,
WdExportFormat.wdExportFormatPDF);
wordDocument.Close();
}
appWord.Quit();
}
天冬氨酸。如果你能购买许可证,Words真的是一个很好的解决方案。免费版本在输出PDF中添加警告消息。
如果你想找免费的东西,我用过FreeSpire。Doc,免费版本有以下限制:
免费版本限制为500段和25个表格。此限制在读取或写入文件时强制执行。将word文档转换为PDF和XPS文件时,您只能获得PDF文件的前3页。升级至Spire的商业版。单据
MS Office,Office。互操作或Office自动化是不需要的
通过NuGet:安装
Install-Package FreeSpire.Doc -Version 7.11.0
代码示例:
using System;
using Spire.Doc;
using Spire.Doc.Documents;
namespace DoctoPDF
{
class toPDF
{
static void Main(string[] args)
{
//Load Document
Document document = new Document();
document.LoadFromFile(@"E:'work'documents'TestSample.docx");
//Convert Word to PDF
document.SaveToFile("toPDF.PDF", FileFormat.PDF);
//Launch Document
System.Diagnostics.Process.Start("toPDF.PDF");
}
}
}
此处的NuGet Package页面
试试这个,如果你的计算机上安装了MS Office Word,就不需要额外的编译器配置:
using System;
using System.IO;
using System.Reflection;
namespace KUtil
{
public class Word2PDF
{
static void Main(string[] args)
{
var word = Type.GetTypeFromProgID("word.application");
dynamic app = Activator.CreateInstance(word);
if (args.Length < 1)
{
return;
}
var path = args[0];
var outPath = Path.ChangeExtension(path, "pdf");
dynamic doc = app.Documents.Open(path);
doc.ExportAsFixedFormat(outPath,
ExportFormat:17/*pdf*/);
doc.Close(0/*DoNotSaveChanges*/);
app.Quit();
}
}
}
试试这个。在我看来,这是最有用、最简单的方法。在Spire的帮助下,只需遵循三个简单的步骤,您就可以轻松完成这项任务。的文档。NET。
要查看完整的技术博客文章,请单击此链接。
using Spire.Doc;
namespace ToPDF
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document document = new Document();
//Load a sample Word document
document.LoadFromFile(@"C:'Users'Administrator'Desktop'Test.docx");
//Save the document to PDF
document.SaveToFile("ToPDF.pdf", FileFormat.PDF);
}
}
}
基于王的回答,PDF转换带标题书签。它也在下工作。NET 7(类似于链接的帖子)。
public static void Convert(string inputFileName, string outputFileName)
{
// Microsoft.Office.Interop.Word.WdSaveFormat enum
const int wdFormatPDF = 17;
// Microsoft.Office.Interop.Word.WdExportCreateBookmarks enum
const int wdExportCreateHeadingBookmarks = 1;
// Microsoft.Office.Interop.Word.WdSaveOptions enum
const int wdDoNotSaveChanges = 0;
var word = Type.GetTypeFromProgID("word.application");
if (word == null)
{
throw new ArgumentException("Microsoft Word is not installed on the system.");
}
dynamic app = Activator.CreateInstance(word);
try
{
dynamic doc = app.Documents.Open(inputFileName);
doc.ExportAsFixedFormat(outputFileName, ExportFormat: wdFormatPDF, CreateBookmarks: wdExportCreateHeadingBookmarks);
doc.Close(wdDoNotSaveChanges);
}
finally
{
app.Quit();
}
}