2013-03-12 112 views
-1

我的文档存储在我想用附件发送邮件的数据库中。将docx转换为pdf

我想将存储的docx转换为pdf。

var result = from c in valinor.documents 
      select new 
      { 
       c.document_name, 
       c.document_size, 
       c.document_content 
      }; 

var kk = result.ToList(); 
for (int i = 0; i<kk.Count; i++) 
{ 
    MemoryStream stream = new MemoryStream(kk[i].document_content); 
    Attachment attachment = new Attachment(stream, kk[i].document_name + ".pdf", "application/pdf"); 
    mail.Attachments.Add(attachment); 
} 

如何将document_content转换为pdf?

+1

你试过了什么?这里有一种可能性,搜索功能返回http://stackoverflow.com/questions/8817623/converting-docx-to-pdf-using-openxml-and-pdfcreator-in-c-sharp – Chuck 2013-03-12 14:17:14

+0

'流'内容是文档或pdf – 2013-03-12 14:19:03

+0

流内容一docx – user1065131 2013-03-12 14:38:54

回答

0

您将需要第三方组件,例如安装在机器上的ABCpdf和(可能)Word,并使用该组件将docx转换为pdf。

1

您需要在MIcrosoft office dll中使用Microsoft.Office.Interop.Word

  • 添加引用到你的项目Microsoft.Office.Interop.Word
  • 检查我的代码的样本。

这很好,很容易。 100%为我工作。

Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); 
wordDocument = word.Documents.Open(savedFileName, ReadOnly: true); 
wordDocument.ExportAsFixedFormat(attahcmentPath + "/pdf" + attachment.BetAttachmentCode + ".pdf", Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF); 
word.Quit(false); 
+0

保存我的一天!这适用于32位和64位版本 – 2018-01-19 05:49:23