ASP.NET中的邮件附件,PDF生成 [英] Mail Attachment in ASP.NET, Pdf generation

查看:75
本文介绍了ASP.NET中的邮件附件,PDF生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我没有一点英语经验
在我的应用程序上转到要发送的邮件上的附件Pdf时遇到问题.

我使用OpenXml创建带有db信息的.docx文档,然后尝试使用Interop Word将.docx转换为Pdf,并创建带有Pdf附件和发送的邮件,但文件已损坏,请勿t打开.

我有将此代码转换为转换器

Hi,
I don''t have very english experience
I have a problem when I goes to Attachment a Pdf on a Mail for Send on my application.

I''m usign OpenXml for create a .docx document with information of a Db, tnen that .docx try to converter in a Pdf using Interop Word, and create a Mail with the Pdf Attachment and Send but the file is damaged and don´t open.

I have this code to converter

public static bool ConvertDocument(string file)
        {
            object missing = System.Reflection.Missing.Value;

            Word.Application word = null;
            Word.Document doc = null;

            try
            {
                word = new Word.Application();
                word.Visible = false;
                word.ScreenUpdating = false;

                Object filename = (Object)file;

                doc = word.Documents.Open(ref filename, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing);
                doc.Activate();

                if (Path.GetExtension(file) == ".docx")
                    file = file.Replace(".docx", ".pdf");
                else
                    file = file.Replace(".doc", ".pdf");

                object fileFormat = Word.WdSaveFormat.wdFormatPDF;

                doc.ExportAsFixedFormat(file, Word.WdExportFormat.wdExportFormatPDF, false, Word.WdExportOptimizeFor.wdExportOptimizeForPrint,
                    Word.WdExportRange.wdExportAllDocument, 1, 1, Word.WdExportItem.wdExportDocumentContent, true, true, Word.WdExportCreateBookmarks.wdExportCreateNoBookmarks,
                    true, true, false, ref missing);
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                object saveChanges = Word.WdSaveOptions.wdSaveChanges;
                if (doc != null)
                {
                    ((Word._Document)doc).Close(ref saveChanges, ref missing, ref missing);
                    doc = null;
                }

                if (word != null)
                {
                    ((Word._Application)word).Quit(ref saveChanges, ref missing, ref missing);
                    word = null;
                }
            }

            return true;
        }



感谢哥伦比亚的任何人都可以提供帮助:)



Thanks from Colombia if anyone can help :)

推荐答案

我使用另存为"而不是导出,并且效果很好.我有Office 2007和Windowx XP/VS 2010. br/>
I used Save As instead of export and it works perfectly.I have Office 2007 and Windowx XP/VS 2010.

public bool ConvertDocument(string file)
       {
           object missing = System.Reflection.Missing.Value;

           Word.Application word = null;
           Word.Document doc = null;

           try
           {
               word = new Word.Application();
               word.Visible = false;
               word.ScreenUpdating = false;

               Object filename = (Object)file;

               doc = word.Documents.Open(ref filename, ref missing,
                   ref missing, ref missing, ref missing, ref missing, ref missing,
                   ref missing, ref missing, ref missing, ref missing, ref missing,
                   ref missing, ref missing, ref missing, ref missing);
               doc.Activate();

               if (Path.GetExtension(file) == ".docx")
                   file = file.Replace(".docx", ".pdf");
               else
                   file = file.Replace(".doc", ".pdf");

               object pdf = Word.WdSaveFormat.wdFormatPDF;

               doc.SaveAs(file, ref pdf, ref missing, ref missing,
                   ref missing, ref missing, ref missing, ref missing,
                   ref missing, ref missing, ref missing, ref missing,
                   ref missing, ref missing, ref missing, ref missing);

           }
           catch (Exception ex)
           {
               return false;
           }
           finally
           {

                   ((Word._Document)doc).Close(ref missing, ref missing, ref missing);
                   doc = null;



                   ((Word._Application)word).Quit(ref missing, ref missing, ref missing);
                   word = null;

           }

           return true;
       }


这篇关于ASP.NET中的邮件附件,PDF生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆