Saving Word 2007 Documents to PDF and XPS Formats

本文介绍了一种使用Microsoft Word Interop库将Word文档转换为PDF的方法。通过创建Word应用程序实例并打开指定的Word文件,可以将其导出为PDF格式。代码详细展示了如何设置导出参数,如优化打印、导出范围等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static void wordConvertPdf(string wordPath, string pdfPath)
        {               oWord.Application wordApplication =new Microsoft.Office.Interop.Word.Application ();
                oWord.Document wordDocument = null;

                object paramSourceDocPath = wordPath;//eg.@"C:\Temp\Test.docx";
                object paramMissing = Type.Missing;

                string paramExportFilePath =pdfPath;//eg.@"C:\Temp\Test.xps";

                oWord.WdExportFormat paramExportFormat = oWord.WdExportFormat.wdExportFormatPDF;
                bool paramOpenAfterExport = false;
                oWord.WdExportOptimizeFor paramExportOptimizeFor =
                    oWord.WdExportOptimizeFor.wdExportOptimizeForPrint;
                oWord.WdExportRange paramExportRange = oWord.WdExportRange.wdExportAllDocument;
                int paramStartPage = 0;
                int paramEndPage = 0;
                oWord.WdExportItem paramExportItem = oWord.WdExportItem.wdExportDocumentContent;
                bool paramIncludeDocProps = true;
                bool paramKeepIRM = true;
                oWord.WdExportCreateBookmarks paramCreateBookmarks =oWord.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
                bool paramDocStructureTags = true;
                bool paramBitmapMissingFonts = true;
                bool paramUseISO19005_1 = false;
                try
                {
                    // Open the source document.
                    wordDocument = wordApplication.Documents.Open(
                        ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing);

                    // Export it in the specified format.
                    if (wordDocument != null)
                        wordDocument.ExportAsFixedFormat(paramExportFilePath,
                            paramExportFormat, paramOpenAfterExport,
                            paramExportOptimizeFor, paramExportRange, paramStartPage,
                            paramEndPage, paramExportItem, paramIncludeDocProps,
                            paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                            paramBitmapMissingFonts, paramUseISO19005_1,
                            ref paramMissing);
                }
                catch (Exception ex)
                {}
                finally
                {
                    // Close and release the Document object.
                    if (wordDocument != null)
                    {
                        wordDocument.Close(ref paramMissing, ref paramMissing,
                            ref paramMissing);
                        wordDocument = null;
                    }

                    // Quit Word and release the ApplicationClass object.
                    if (wordApplication != null)
                    {
                        wordApplication.Quit(ref paramMissing, ref paramMissing,
                            ref paramMissing);
                        wordApplication = null;
                    }

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
       }

原帖地址:http://msdn.microsoft.com/en-us/library/bb412305.aspx


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值