2013-03-12 113 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