0

I have the following code to convert word document to pdf using CutePdf Writer

        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. Can anyone tell me how to convert word document to pdf using CUTEPDF programatically and waht's wrong with the above code?

  • Try by remove comment of `printDocumentMessageBoxTest.PrinterSettings = ps;` statement. – Yog Dec 12 '15 at 11:56

1 Answers1

3

CutePdf Writer does not support automation. 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:

        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.

Community
  • 1
  • 1
Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158