C#3.0使用MemoryStream将itextsharp pdf保存到数据库 [英] C# 3.0 Save itextsharp pdf to database using MemoryStream

查看:237
本文介绍了C#3.0使用MemoryStream将itextsharp pdf保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将divxtsharp生成的pdf文件保存到数据库。但是,到目前为止我还没有成功。

I'm trying to save to databse a pdf file generated by itextsharp. But, I haven't been successfully so far.

我正在使用Linq来实现sql。

I'm using Linq to sql.

以下是代码:

            MemoryStream ms = new MemoryStream();
            Document d = new Document(PageSize.A4, 60, 60, 40, 40);
            PdfWriter w = PdfWriter.GetInstance(d, ms);
            w.CloseStream = false;

            string txtTemplate = "";
            Encoding en = Encoding.GetEncoding("iso-8859-1");
            StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath("~/Content/templates/CessaoDireitosDica.txt"), en);
            txtTemplate  = sr.ReadToEnd();
            sr.Close();
            string conselhos = "";

            Font font = new Font(Font.HELVETICA, 11, Font.NORMAL);
            font.SetColor(0xC6, 0xC6, 0xC6);

            Paragraph txtBody = new Paragraph(txtTemplate, font);

            txtBody .SetAlignment(ElementTags.ALIGN_JUSTIFIED);

            d.Open();
            d.Add(txtBody);
            d.Close();

            byte[] pdfDone = ms.ToArray();
            w.Flush();
            ms.Flush();
            ms.Close();

            return pdfDone;

它不会抛出任何错误,但它不会在DB中保存任何内容。 DB字段是图像字段类型。
我也使用这段代码动态渲染pdf(我切断了byte [] pdfDone ...并返回MemoryStream)。

It throws no error, but it doesn't save nothing in DB. The DB field is an "image" field type. I also use this code to render a pdf on the fly (I cut off the byte[] pdfDone... and return the MemoryStream).

I不知道什么是错的...而且调试时,我还可以看到byte [] pdfDone有一个值(类似于3487),但没有任何内容保存到DB。

I don't know what can be wrong... And debugging, I could also see that byte[] pdfDone has a value (something like 3487), but nothing is saved to DB.

提前致谢!

推荐答案

function byte[] CreatePdf(){
            byte[] result;
            using (MemoryStream ms = new MemoryStream())
            {
                Document pDoc = new Document(PageSize.A4, 0, 0, 0, 0);
                PdfWriter writer = PdfWriter.GetInstance(pDoc, ms);
                pDoc.Open();

                //here you can create your own pdf.

                pDoc.Close();
                result = ms.GetBuffer();
            }

            return result;
}

这篇关于C#3.0使用MemoryStream将itextsharp pdf保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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