简体   繁体   English

在C#中使用CutePdf将Word文档转换为pdf

[英]Convert word document to pdf using CutePdf programatically in C#

I have the following code to convert word document to pdf using CutePdf Writer 我有以下代码使用CutePdf Writer将Word文档转换为pdf

        PrintDialog pDia = new PrintDialog();
        PrinterSettings ps = new PrinterSettings();
        pDia.Document = printDocumentMessageBoxTest;
        pDia.Document.DocumentName = "C:\\FinalGap.doc";

        ps.PrinterName = "CutePDF Writer";
        ps.PrintToFile = true;

        ps.PrintFileName = "C:\\" + Path.GetFileNameWithoutExtension(pDia.Document.DocumentName) + ".pdf";

        // take printer settings from the dialog and set into the PrintDocument object
        pDia.Document.OriginAtMargins = true;
        ps.DefaultPageSettings.Margins.Left = 2;
        //printDocumentMessageBoxTest.PrinterSettings = ps;

        // start the printing process, catch exceptions
        try
        {
            printDocumentMessageBoxTest.Print();
        }
        catch (Exception exc)
        {
            MessageBox.Show("Printing error!\n" + exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

When I run the application, it is printing the word document bu the output file is not generated. 当我运行该应用程序时,它正在打印word文档,但不会生成输出文件。 Can anyone tell me how to convert word document to pdf using CUTEPDF programatically and waht's wrong with the above code? 谁能告诉我如何通过CUTEPDF程序将Word文档转换为pdf,而上述代码有错吗?

CutePdf Writer does not support automation. CutePdf Writer不支持自动化。 You can't use it the manner you are trying to use it. 您不能以尝试使用它的方式使用它。 You can purchase Custom Pdf Writer from them, then you code will be something like this: 您可以从他们那里购买Custom Pdf Writer ,然后编写如下代码:

        string regKey = @"HKEY_CURRENT_USER\Software\Custom PDF Printer";
        Registry.SetValue(regKey, "OutputFile", @"C:\Sample.pdf", RegistryValueKind.String);
        Registry.SetValue(regKey, "BypassSaveAs", @"1", RegistryValueKind.String);
        Application wordApp = new Word.Application();
        Document wordDoc = wordApp.Documents.Open(@"C:\test.doc");
        wordApp.ActivePrinter = "Custom PDF Printer";
        wordApp.PrintOut();
        wordDoc.Close();
        Registry.SetValue(regKey, "BypassSaveAs", @"0", RegistryValueKind.String);

Also see: 另请参阅:

I personally used ABCPdf for a project and I liked it, however my goal was convert not from doc but from html to pdf and the component were not free. 我个人将ABCPdf用于一个项目,但我很喜欢,但是我的目标不是从doc而是从html转换为pdf,并且该组件不是免费的。

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

相关问题 未在 Windows 服务应用程序 C# 中使用 CutePDF writer 生成 PDF 文档 - Not generating a PDF documents using CutePDF writer in windows service application C# 使用OpenXML SDK C#以编程方式将FootNotes添加到Word文档 - Add FootNotes to Word Document Programatically using OpenXML SDK C# 使用C#将SharePoint库XML文档转换为PDF文档 - Convert SharePoint Library XML document to PDF document using C# 使用C#将Word文档或pdf上载到mysql数据库 - Upload word document or pdf to mysql database using c# c# Word-AddIn 将 activeDocument 转换为虚拟 PDF 并将它们合并为一个 PDF 文档 - c# Word-AddIn convert activeDocument to a virtual PDF and merge them into one PDF document 如何在没有office.word.interop C#的情况下将带有图表的MS Word文档转换为PDF - How to convert MS word document with chart in it to PDF without office.word.interop c# 使用c#将word文件(.docx和doc)转换为.pdf无效 - Convert of word file(.docx & doc) to .pdf using c# is not working 使用C#将SSRS 2005报告流转换为Word文档 - Convert SSRS 2005 Report stream into a word document using C# C#中将Word文档转换为PDF出错 - Error Converting Word Document to PDF in C# 使用ASP.NET C#将网页转换为PDF文档 - Convert webpage to PDF document using ASP.NET C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM