繁体   English   中英

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

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

我有以下代码使用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);
        }

当我运行该应用程序时,它正在打印word文档,但不会生成输出文件。 谁能告诉我如何通过CUTEPDF程序将Word文档转换为pdf,而上述代码有错吗?

CutePdf Writer不支持自动化。 您不能以尝试使用它的方式使用它。 您可以从他们那里购买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);

另请参阅:

我个人将ABCPdf用于一个项目,但我很喜欢,但是我的目标不是从doc而是从html转换为pdf,并且该组件不是免费的。

暂无
暂无

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

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