scsfdev

Print Excel to PDF through C# with PDFCreator COM

Feb 10th, 2012
1,541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. private void GeneratePDFxls()
  2.         {
  3.             Excel.Application xlApp;
  4.             Excel.Workbook xlWorkBook;
  5.             Excel.Worksheet xlWorkSheet;
  6.             object misValue = System.Reflection.Missing.Value;
  7.  
  8.             xlApp = new Excel.Application();
  9.             xlApp.Visible = true;
  10.  
  11.             xlWorkBook = xlApp.Workbooks.Open(@"C:\temp\testb.xls");
  12.             xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
  13.  
  14.             PDF.clsPDFCreator creator = new PDF.clsPDFCreator();
  15.             string parameters = "/NoProcessingAtStartup";
  16.             if (!creator.cStart(parameters, false))
  17.             {
  18.                 Console.WriteLine("Unable to start PDFCreator.");
  19.             }
  20.  
  21.             creator.cOptions.UseAutosave = 1;
  22.             creator.cOptions.UseAutosaveDirectory = 1;
  23.             creator.cOptions.AutosaveDirectory = xlWorkBook.Path ;
  24.             creator.cOptions.AutosaveFilename = xlWorkSheet.Name;
  25.             creator.cOptions.AutosaveFormat = 0;
  26.             creator.cClearCache();
  27.  
  28.             xlWorkSheet.PrintOut(Type.Missing, Type.Missing, 1, true, "PDFCreator", false, false, Type.Missing);
  29.  
  30.             while (creator.cCountOfPrintjobs != 1)
  31.             {
  32.                 Application.DoEvents();
  33.                 System.Threading.Thread.Sleep(1000);
  34.             }
  35.  
  36.             creator.cPrinterStop = false;
  37.  
  38.             while (creator.cCountOfPrintjobs != 0)
  39.             {
  40.                 Application.DoEvents();
  41.                 System.Threading.Thread.Sleep(1000);
  42.             }
  43.  
  44.             creator.cPrinterStop = true;
  45.  
  46.             creator.cClose();
  47.             creator = null;
  48.  
  49.             xlWorkBook.Close(true, misValue, misValue);
  50.             xlApp.Quit();
  51.             releaseObject(xlWorkSheet);
  52.             releaseObject(xlWorkBook);
  53.             releaseObject(xlApp);
  54.         }
Advertisement
Add Comment
Please, Sign In to add comment