我正在尝试将 itextsharp 生成的 pdf 文件保存到数据库中。但是,到目前为止我还没有成功。
我正在使用 Linq 进行 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 字段是“图像”字段类型。 我还使用此代码动态呈现 pdf(我切断了 byte[] pdfDone... 并返回 MemoryStream)。
我不知道哪里出了问题...调试时,我还可以看到 byte[] pdfDone 有一个值(类似于 3487),但没有任何内容保存到数据库中。
提前致谢!
最佳答案
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 保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1374184/