将word文档转换为pdf

发布于 2024-11-04 02:05:53 字数 1138 浏览 2 评论 0原文

我有一个 RadGridView ,应该导出为 doc、pdf、csv 和 xls 格式。导出到 doc、csv 和 xls 工作正常...但导出到 pdf 格式时出现问题。导出到 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);
        }
    }
}

I have a RadGridView which should be exported to doc,pdf, csv and xls format. The export to doc, csv and xls works fine... but there's something wrong in exporting it to pdf format. The export to pdf works but the columns that i hide still shows...

So i just thought of exporting the RadGridView to pdf by: exporting it to word then convert it to pdf programmatically...

By the way, here's my code for exporting to 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);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栩栩如生 2024-11-11 02:05:53

通过 OLE 自动化使用 word(注意:word 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");
        }
    }

Using word via OLE automation (Note: word COM is not re-entrant - make sure not to call this concurrently from different threads):

    /// <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");
        }
    }
贩梦商人 2024-11-11 02:05:53

使用 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);
    }

Using DevExpress (below code only works with RTF format - which doesn't necessarily work with all Word's functionality):

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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文