簡體   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