如何从字符串创建PDF并将其发送给用户? [英] How to create a PDF from a string and send it to the user?

查看:83
本文介绍了如何从字符串创建PDF并将其发送给用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家早上好,

我有一个应该部署在

sharepoint 2007(wss 3.0)作为自定义Web部件.是

sharepoint 2007 (wss 3.0) as a custom web part . It is

只需一个按钮即可将特定字符串转换为pdf

simply a button to convert a certain string to a pdf

文件并将其发送给用户.我正在使用c#.NET.我有

file and send it to the user.I'm using c# .NET . I have

以下代码:

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是一个字节数组. 对于发送文件

bb is a bytes array. And this is working fine regarding the "sending the file

对用户"部分.

我面临的问题是字节的创建

The problem I'm facing is with the creation of the bytes

数组.我不知道如何从

字符串.我尝试使用

iTextSharp,但是由于某些原因,我总是面临着

iTextSharp, but for some reason i'm always facing an

此行有错误:

Document d = new Document();

Document d = new Document();

Web部件在部署时给我一个错误(文件

The web part gives me an error when it's deployed (File

未找到).

现在,我被卡住了.转换

Now i'm stuck. What is the appropriate way to convert

将此字符串转换为pdf并直接发送给用户

this string to pdf and send it to the user WITHOUT

随时随地存储!

我们非常感谢您的帮助与帮助;预先谢谢你:)

Any help is highly appreciated & thank you in advance :)

推荐答案

看看这是否有助于创建字节数组,我正在使用html解析器将我的xml文档转换为pdf-

See if this helps in creating the byte array, i am using html parser to convert my xml document to 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();
            }

这篇关于如何从字符串创建PDF并将其发送给用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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