簡體   English   中英

如何從字符串創建PDF並將其發送給用戶?

[英]How to create a PDF from a string and send it to the user?

大家早上好,

我有一個應該部署在的項目

sharepoint 2007(wss 3.0)作為自定義Web部件。 它是

只需將特定字符串轉換為pdf的按鈕

文件並將其發送給用戶。我正在使用c#.NET。 我有

以下代碼:

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 =新Document();

Web部件在部署時給我一個錯誤(文件

未找到)。

現在我被卡住了。 什么是適當的轉換方式

將此字符串轉換為pdf,然后不發送給用戶

隨時隨地存儲!

任何幫助都將受到高度贊賞,並提前感謝您:)

看看這是否有助於創建字節數組,我正在使用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();
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM