2011-11-01 45 views
2

早上好大家好,如何從字符串創建PDF並將其發送給用戶?

我這是應該被部署在

的SharePoint 2007(WSS 3.0)作爲一個自定義Web部件的項目。這是

只需一個按鈕,某些字符串轉換爲PDF

文件,並使用C#.NET將其發送到user.I'm。我有

以下代碼:

HttpContext.Current.Response.AddHeader"ContentDispositio 
n", "attachment;filename=foo.pdf"); 
       HttpContext.Current.Response.AddHeader("Content-Length", bb.Length.ToString()); 
       HttpContext.Current.Response.ContentType = "application/pdf"; 
       HttpContext.Current.Response.BinaryWrite(bb); 

BB是一個字節數組。 這對於「將文件

發送給用戶」部分正常工作。

我正面臨的問題是創建字節

數組。我不知道如何從可以轉換爲PDF的字符串

創建一個字節數組。我嘗試使用

iTextSharp的,但由於某些原因,我總是面對

誤差與此行:

文獻d =新的文檔();

Web部件時,它的部署(未找到文件

)給我一個錯誤。

現在我卡住了。什麼是

這個字符串轉換爲PDF格式,並將其發送給用戶WITHOUT

將其存儲在任何地方適當的方式!

任何幫助,高度讚賞&預先感謝您:)

+0

燦你澄清:你的問題不是如何從一個流創建一個字節數組,但如何將文本字符串轉換爲一個pdf字節數組? –

回答

1

看看這有助於在創建字節數組,我使用HTML解析器到我的XML文檔轉換爲PDF -

// Using iTextSharp to construct a PDF document 
      // Create a document-object 
      Document document = new Document(PageSize.A4); 

      // Create a writer that listens to the document 
      // and directs a XML-stream to a MemoryStream 
      using (MemoryStream ms = new MemoryStream()) 
      { 
       PdfWriter.GetInstance(document, ms); 
       document.Open(); 


       System.Xml.XmlTextReader _xmlr; 
       if (String.IsNullOrEmpty(errorMsg)) 
        _xmlr = new System.Xml.XmlTextReader(new StringReader(GetTransferedData(content))); 
       else 
        _xmlr = new System.Xml.XmlTextReader(new StringReader(@"<html><body>Error Message:" + errorMsg + "</body></html>")); 
       iTextSharp.text.html.HtmlParser.Parse(document, _xmlr); 
       document.Close();     
       ms.Flush(); 
       byte[] data = ms.ToArray(); 

       Response.Clear(); 
       Response.ClearHeaders(); 
       Response.ClearContent(); 
       Response.Buffer = true; 
       Response.ContentType = "application/pdf"; 
       Response.BinaryWrite(data); 
       Response.End(); 
       ms.Close(); 
      } 
相關問題