2011-04-28 181 views
0

我有一個RadGridView應該導出爲doc,pdf,csv和xls格式。導出到doc,csv和xls可以正常工作......但將其導出爲pdf格式時出錯。在導出爲PDF的作品,但列,我躲還表示...將word文檔轉換爲pdf

所以我只是想通過導出RadGridView到PDF的:其導出到Word然後將其轉換爲PDF格式編程...

通過方式,這是我的代碼導出到word。

 
//export to doc 
private void Export_Doc(object sender, System.Windows.RoutedEventArgs e) 
{ 
    ExportDialog("doc", "Word", ExportFormat.Html); 
} 
private void ExportDialog(string extension, string selectedItem, ExportFormat format) 
{ 
    SaveFileDialog dialog = new SaveFileDialog(); 
    dialog.DefaultExt = extension; 
    dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, selectedItem); 
    dialog.FilterIndex = 1; 
    if (dialog.ShowDialog() == true) 
    { 
     using (Stream stream = dialog.OpenFile()) 
     { 
      GridViewExportOptions exportOptions = new GridViewExportOptions(); 
      exportOptions.Format = format; 
      exportOptions.ShowColumnFooters = true; 
      exportOptions.ShowColumnHeaders = true; 
      exportOptions.ShowGroupFooters = true; 

      RadGridView1.Export(stream, exportOptions); 
     } 
    } 
} 

回答

0

通過OLE自動化用字(注:字COM不重入 - 確保不從不同的線程同時調用此):

/// <summary> 
    /// Interacts directly with Word via COM to convert a document to PDF 
    /// Must have the Microsoft enhancement installed to support Save As PDF 
    /// in the word file menu 
    /// "2007 Microsoft Office Add-in: Microsoft Save as PDF or XPS" - SaveAsPDFandXPS.exe 
    /// </summary> 
    private void ExportToPDFUsingWord(object sourceDoc, string destPDF) 
    { 
     object m = System.Reflection.Missing.Value; 
     object readOnly = true; 
     object myFalse = false; 
     object isVisible = false; 
     object matchCase = false; 
     object matchWholeWord = true; 
     object saveChangesWhenQuitting = false; 

     //Debug("Connecting to word"); 
     _Application oWordApp = new Application(); 
     _Document oWordDoc = null; 
     Documents oWordDocs = null; 

     try 
     { 
      // load the source file 
      //Debug("Opening " + sourceDoc); 
      oWordDocs = oWordApp.Documents; 
      oWordDoc = oWordDocs.Open(ref sourceDoc, ref m, ref readOnly, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref m, ref isVisible, ref m, ref m, ref m, ref m); 

      if (oWordDoc != null) 
      { 
       try 
       { 
        WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF; 
        bool paramOpenAfterExport = false; 
        WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint; 
        WdExportRange paramExportRange = WdExportRange.wdExportAllDocument; 
        int paramStartPage = 0; 
        int paramEndPage = 0; 
        WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent; 
        bool paramIncludeDocProps = true; 
        bool paramKeepIRM = true; 
        WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks; 
        bool paramDocStructureTags = true; 
        bool paramBitmapMissingFonts = true; 
        bool paramUseISO19005_1 = false; 

        // 
        // Export the file to PDF. 
        //Debug("Exporting to " + destPDF); 
        oWordDoc.ExportAsFixedFormat(destPDF, 
         paramExportFormat, paramOpenAfterExport, 
         paramExportOptimizeFor, paramExportRange, paramStartPage, 
         paramEndPage, paramExportItem, paramIncludeDocProps, 
         paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, 
         paramBitmapMissingFonts, paramUseISO19005_1, 
         ref m); 
       } 
       finally 
       { 
        //Debug("Closing " + sourceDoc); 
        oWordDoc.Close(ref saveChangesWhenQuitting, ref m, ref m); 
       } 
      } 
      else 
       throw new FileLoadException("Documents.Open returned null for file \'" + sourceDoc + "\', perhaps use comexp.msc and change DCOM->Word97-2003 Identity to Interactive user"); // if you are running from within a service then Word behaves differently - it is designed as a user application which must have a user interface 
     } 
     finally 
     { 
      // close word and cleanup    
      //Debug("Quitting Word"); 
      oWordApp.Quit(ref saveChangesWhenQuitting, ref m, ref m); 

      //Debug("Releasing RCWs"); 
      Release(oWordDocs); 
      oWordDocs = null; 
      Release(oWordDoc); 
      oWordDoc = null; 
      Release(oWordApp); 
      oWordApp = null; 

      //Debug("Collecting garbage"); 
      GC.Collect(); // forces the garbage collector to run and might release any references that the RCW still has 
      GC.WaitForPendingFinalizers(); 
      //Debug("Word objects finished"); 
     } 
    } 
0

使用的DevExpress(下面的代碼只適用於RTF格式 - 不一定適用於所有Word的功能):

private void ExportToPDFUsingDevExpress(string sourceDoc, string destPDF) 
    { 
     DevExpress.XtraPrinting.PrintingSystem printingSystem1 = new DevExpress.XtraPrinting.PrintingSystem(); 
     DevExpress.XtraRichEdit.RichEditControl richEditControl1 = new DevExpress.XtraRichEdit.RichEditControl(); 
     richEditControl1.LoadDocument(sourceDoc); 
     DevExpress.XtraPrinting.PrintableComponentLink link = new DevExpress.XtraPrinting.PrintableComponentLink(printingSystem1); 
     link.Component = richEditControl1; 
     link.CreateDocument(); 
     link.PrintingSystem.ExportToPdf(destPDF); 
    }