简体   繁体   English

使用 c# 将 word 文件(.docx 和 doc)转换为 .pdf

[英]Convert word file(.docx & doc) to .pdf using c#

How i can convert word file ( .docx & doc ) to .pdf in c# without using SaveAs() or Save() method ?我如何在不使用SaveAs()Save()方法的情况下在c#中将 word 文件( .docx & doc )转换为.pdf or without uploading on server?还是不上传到服务器?

Try this, it works for me:试试这个,它对我有用:

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();
}

Aspose.Words is really good solution for this purpose if you can buy the license.如果您可以购买许可证,Aspose.Words 是用于此目的的非常好的解决方案。 The free version adds warning messages to the output PDF.免费版本将警告消息添加到输出 PDF。

If you're looking for something free, I have used FreeSpire.Doc, the free version has the following limits:如果您正在寻找免费的东西,我使用了 FreeSpire.Doc,免费版本有以下限制:

Free version is limited to 500 paragraphs and 25 tables.免费版仅限于 500 个段落和 25 个表格。 This limitation is enforced during reading or writing files.此限制在读取或写入文件期间强制执行。 When converting word documents to PDF and XPS files, you can only get the first 3 page of PDF file.将word文档转换为PDF和XPS文件时,只能得到PDF文件的前3页。 Upgrade to Commercial Edition of Spire.Doc升级到 Spire.Doc 商业版

MS Office, Office.Interop or Office automations are not required.不需要 MS Office、Office.Interop 或 Office 自动化。

Install via NuGet:通过 NuGet 安装:

Install-Package FreeSpire.Doc -Version 7.11.0

Code example:代码示例:

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 page here NuGet 包页面在这里

Try this, no extra compiler configuration needed if MS Office Word is installed on your computer:试试这个,如果您的计算机上安装了 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();
        }
    }
}

You can use this code to convert ms-word document to pdf using aspose您可以使用此代码使用 aspose 将 ms-word 文档转换为 pdf

    
public class Program
    {
        public static void Main(string[] args)
        {
            var content = File.ReadAllBytes("response.doc");
            var document = new Document(new MemoryStream(content));

            ClearFormat(document);

            var options = SaveOptions.CreateSaveOptions(SaveFormat.Pdf);

            options.PrettyFormat = true;
            options.UseAntiAliasing = true;
            options.UseHighQualityRendering = true;

            document.Save("response.pdf", options);
        }

        private static void ClearFormat(Document doc)
        {
            for (var i = 0; i < doc.Sections.Count; i++)
            {
                var nodes = doc.Sections[i].GetChildNodes(NodeType.Run, true);
                if (nodes == null || nodes.Count <= 0) continue;
                foreach (var item in (from Run item in nodes
                                      where item.Font.Name.ToLower().Contains("nastaliq")
                                      select item).ToList())
                {
                    item.Font.Name = "Times New Roman";
                    item.Font.Size = item.Font.Size > 12 ? 12 : item.Font.Size;
                }
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM