用ASP_Net实现将Word文档转换为PDF格式

 

public class WordConverterPDF
    {
        object paramMissing = Type.Missing;
        public string errormessage;
        private bool wordavailable = false;
        private bool checkedword = false;

        public bool convert(string source, string output)
        {
            if (System.IO.File.Exists(source))
            {
               
                try
                {
                    ApplicationClass wordApplication = new ApplicationClass();

                    Document wordDocument = null;
                    object paramSourceDocPath = source;

                    string paramExportFilePath = output;


                    #region 设置导出格式为PDF格式

                    //WdExportFormat 值之一,指定是以 PDF 还是 XPS 格式保存文档。
                    WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;

                    //如果要自动打开新文件,则为 true;否则为 false。
                    bool paramOpenAfterExport = false;

                    //WdExportOptimizeFor 值之一,指定进行屏幕优化还是打印优化。
                    WdExportOptimizeFor paramExportOptimizeFor =
                        WdExportOptimizeFor.wdExportOptimizeForPrint;

                    //WdExportRange 值之一,指定导出范围是整个文档、当前页、文本范围还是当前选定内容。 默认设置为导出整个文档。
                    WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;

                    //指定起始页码
                    int paramStartPage = 0;

                    //指定结束页码
                    int paramEndPage = 0;

                    //Microsoft.Office.Interop.Word.WdExportItem 值之一,指定导出过程是仅包括文本,还是同时包括文本和标记
                    WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;

                    //如果要在新文件中包含文档属性,则为 true;否则为 false。
                    bool paramIncludeDocProps = true;

                    //如果要在源文档具有信息权限管理 (IRM) 保护时将 IRM 权限复制到 XPS 文档,则为 true;否则为 false。 默认值为 true。
                    bool paramKeepIRM = true;

                    //WdExportCreateBookmarks 值之一,指定是否导出书签以及要导出的书签类型。
                    WdExportCreateBookmarks paramCreateBookmarks =
                        WdExportCreateBookmarks.wdExportCreateWordBookmarks;

                    //如果要包含额外数据(如有关内容的流和逻辑组织的信息)来协助使用屏幕读取器,则为 true;否则为 false。 默认值为 true。
                    bool paramDocStructureTags = true;

                    //如果要包含文本的位图,则为 true;如果要引用文本字体,则为 false。 如果字体许可证不允许在 PDF 文件中嵌入某种字体,则将此参数设置为 true。 如果将此参数设置为 false,则当指定字体不可用时,查看者的计算机会替换合适的字体。 默认值为 true。
                    bool paramBitmapMissingFonts = true;

                    //如果要将 PDF 使用范围限制为按照 ISO 19005-1 进行标准化的 PDF 子集,则为 true;否则为 false。 如果将此参数设置为 true,则生成的文件会是更加可靠的独立文件,但这些文件可能会更大,或者由于格式限制而显示更多的视觉瑕疵。 默认值为 false。
                    bool paramUseISO19005_1 = false;
                    #endregion
                    try
                    {
                        //打开源文件。
                        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);

                        // 在指定的格式导出。
                        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)
                    {
                        // 响应错误
                        errormessage = ex.Message;
                    }
                    finally
                    {
                        // 关闭并释放文件对象。
                        if (wordDocument != null)
                        {
                            wordDocument.Close(ref paramMissing, ref paramMissing,
                                               ref paramMissing);
                            wordDocument = null;
                        }

                        // 退出Word和释放ApplicationClass对象。
                        if (wordApplication != null)
                        {
                            wordApplication.Quit(ref paramMissing, ref paramMissing,
                                                 ref paramMissing);
                            wordApplication = null;
                        }

                     
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                    }
                }
                catch (Exception err)
                {
                    errormessage = err.Message;
                }
                return true;

            }
            else
            {
                errormessage = "ERROR: 输入文件未找到";
            }

            return false;

        }
    }

 

 

  public partial class Work :BasePage
    {

 WordConverterPDF converter=new WordConverterPDF ();
        private string storefolder = @"D:\PDFConverter\";

  protected void Button1_Click(object sender, EventArgs e)
        {
            //pdf pdf = new pdf();
            //string file = "d:\\1.doc";
            //pdf.ConvertWord2Pdf(file);

            //if (FileUpload1.HasFile)
            //{

            try
            {
                //工作目录不存在就创建
                if (!Directory.Exists(storefolder))
                {
                    Directory.CreateDirectory(storefolder);
                }

                //支持office2003和office2007
                if (FileUpload1.FileName.Contains(".doc") || FileUpload1.FileName.Contains(".docx"))
                {
                    string sourcefile = storefolder + FileUpload1.FileName;
                    string outputfile = "";

                    outputfile = storefolder + FileUpload1.FileName.Replace(".doc", ".pdf").Replace("x", "");

                    //保存原始文件
                    FileUpload1.SaveAs(sourcefile);


                    //调用转换器的方法
                    converPdf = converter.convert(sourcefile, outputfile);

                    删除原始文件
                    //if (System.IO.File.Exists(sourcefile))
                    //    System.IO.File.Delete(sourcefile);

                    if (converPdf)
                    {
                        //回送一个可下载的文件
                        System.IO.FileInfo downloadFile = new System.IO.FileInfo(outputfile);
                        HttpContext.Current.Response.Clear();
                        HttpContext.Current.Response.ContentType = "application/pdf";
                        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;

                        HttpContext.Current.Response.AddHeader("Content-Disposition",
                                                               string.Format("attachment; filename={0}",
                                                                             HttpUtility.UrlEncode(downloadFile.Name), System.Text.Encoding.UTF8));
                        HttpContext.Current.Response.AddHeader("Content-Length", downloadFile.Length.ToString());
                        HttpContext.Current.Response.WriteFile(downloadFile.FullName);
                        HttpContext.Current.Response.End();
                        HttpContext.Current.Response.Close();
                    }
                    else {
                        BasePage.JavascriptHelper.Alert(this.Page, "转换失败!");
                    }
                    //在这一点上,PDF文件仍然是在现有的存储文件夹。
                    //因为我们不知道需要多长时间,对于用户来说,下载文件,我们不能在这一点上删除PDF文件

                }
            }catch(Exception ex){

                BasePage.JavascriptHelper.Alert(this.Page, "转换失败!");
                return;
            }
           
        }

}

 

 

---实现转换必须安装SaveAsPDFandXPS.exe

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值