Asp.net 使用itextsharpe将pdf保存到sql数据库

Asp.net 使用itextsharpe将pdf保存到sql数据库,asp.net,Asp.net,我正在使用这个教程,它使用PDF模板,让用户使用文本框输入字段。文件下载到客户端的pc上,但我想将文件的副本也保存到sql数据库中,或者只将文件保存到数据库中,而不是两者都保存 任何帮助都将不胜感激 保罗 protectedvoid btngeneratedf_单击(对象发送方,事件参数e) { var pdfPath=Path.Combine(Server.MapPath(“~/PDFTemplates/fw9.pdf”); //获取此PDF的表单字段并填写它们! var formFieldM

我正在使用这个教程,它使用PDF模板,让用户使用文本框输入字段。文件下载到客户端的pc上,但我想将文件的副本也保存到sql数据库中,或者只将文件保存到数据库中,而不是两者都保存

任何帮助都将不胜感激

保罗

protectedvoid btngeneratedf_单击(对象发送方,事件参数e)
{
var pdfPath=Path.Combine(Server.MapPath(“~/PDFTemplates/fw9.pdf”);
//获取此PDF的表单字段并填写它们!
var formFieldMap=PDFHelper.GetFormFieldNames(pdfPath);
formFieldMap[“topmostSubform[0].Page1[0].f1\u 01\u 0\u[0]“]=txtName.Text;
formFieldMap[“topmostSubform[0]。第1页[0]。f1\U 02\U 0\U[0]“]=txtBusinessName.Text;
如果(rblTaxClassification.SelectedValue!=null)
{
var formFieldName=string.Format(“topmostSubform[0].Page1[0].c1_01[{0}]”,rblTaxClassification.SelectedIndex);
formFieldMap[formFieldName]=(rblTaxClassification.SelectedIndex+1).ToString();
}
如果(chkExemptPayee.Checked)
formFieldMap[“topmostSubform[0]。第1页[0]。c1_01[7]“]=“8”;
formFieldMap[“topmostSubform[0]。第1页[0]。f1\U 04\U 0\U[0]“]=txtAddress.Text;
formFieldMap[“topmostSubform[0]。第1页[0]。f1_05_0_[0]“]=txtCityStateZIP.Text;
formFieldMap[“topmostSubform[0]。第1页[0]。f1_07_0_[0]“]=txtAccountNumbers.Text;
//请求者的姓名和地址(硬编码)
formFieldMap[“topmostSubform[0]。第1页[0]。f1_06_0_[0]”]=“Acme网站\n123 Anywhere Lane\n斯普林菲尔德,美国”;
//SSN
如果(!string.IsNullOrEmpty(txtSSN1.Text))
{
formFieldMap[“topmostSubform[0]。Page1[0]。社交[0]。TextField1[0]“]=txtSSN1.Text;
formFieldMap[“topmostSubform[0]。Page1[0]。社交[0]。TextField2[0]“]=txtSSN2.Text;
formFieldMap[“topmostSubform[0]。Page1[0]。社交[0]。TextField2[1]“]=txtSSN3.Text;
}
如果(!string.IsNullOrEmpty(txtEIN1.Text))为else
{
formFieldMap[“topmostSubform[0]。Page1[0]。社交[0]。TextField2[2]“]=txtEIN1.Text;
formFieldMap[“topmostSubform[0]。Page1[0]。社交[0]。TextField2[3]“]=txtEIN2.Text;
}
var pdfContents=PDFHelper.GeneratePDF(pdfPath,formFieldMap);
PDFHelper.ReturnPDF(pdfContents,“Completed-W9.pdf”);
FileStream fs=newfilestream(pdfPath,FileMode.Open,FileAccess.Read);
BinaryReader br=新的BinaryReader(fs);
Byte[]bytes=br.ReadBytes((Int32)fs.Length);
br.Close();
fs.Close();
//将文件插入数据库
string strQuery=“插入tblFiles(名称、内容类型、数据)值(@Name、@ContentType、@Data)”;
SqlCommand cmd=新的SqlCommand(strQuery);
cmd.Parameters.Add(“@Name”,SqlDbType.VarChar).Value=“Completed-W9132.pdf”;
cmd.Parameters.Add(“@ContentType”,SqlDbType.VarChar).Value=“application/pdf”;
cmd.Parameters.Add(“@Data”,SqlDbType.Binary).Value=bytes;
插入更新数据(cmd);
应用程序代码/pdfHelper.cs
使用制度;
使用System.Collections.Generic;
使用系统集合;
使用System.Linq;
使用System.Web;
使用System.IO;
使用iTextSharp.text.pdf;
使用系统数据;
使用System.Data.SqlClient;
使用系统配置;
公共级PDFHelper
{
公共静态字典GetFormFieldName(字符串pdfPath)
{
var fields=newdictionary();
变量读取器=新的PDF读取器(pdfPath);
foreach(reader.AcroFields.Fields中的DictionaryEntry条目)
fields.Add(entry.Key.ToString(),string.Empty);
reader.Close();
返回字段;
}
公共静态字节[]GeneratePDF(字符串pdfPath,字典formFieldMap)
{
var输出=新的MemoryStream();
变量读取器=新的PDF读取器(pdfPath);
var stamper=新的PdfStamper(读卡器,输出);
var formFields=stamper.AcroFields;
foreach(formFieldMap.Keys中的变量fieldName)
SetField(fieldName,formFieldMap[fieldName]);
stamper.FormFlatting=真;
压模关闭();
reader.Close();
返回输出.ToArray();
}
//看http://stackoverflow.com/questions/4491156/get-the-export-value-of-a-checkbox-using-itextsharp/
公共静态字符串GetExportValue(AcroFields.Item)
{
var valueDict=项目.GetValue(0);
var appearanceDict=valueDict.GetAsDict(PdfName.AP);
如果(显示命令!=null)
{
var normalappearancess=appearanceDict.GetAsDict(PdfName.N);
///D表示“向下”外观。
//如果有正常的外观,一个键将为“Off”,另一个键将为“Off”
//将是出口值…应该只有两个。
如果(正常外观!=null)
{
foreach(normalappearancess.Keys中的var curKey)
如果(!PdfName.OFF.Equals(curKey))
返回curKey.ToString().Substring(1);//字符串将有一个前导“/”字符,请将其删除!
}
}
//如果这不起作用,那么可能有一个/AS键,其值是带有
//导出值,同样带有前导“/”,因此将其删除!
var curVal=valueDict.GetAsName(PdfName.AS);
if(curVal!=null)
返回curVal.ToString()子字符串(1);
其他的
返回字符串。空;
}
公共静态void ReturnPDF(字节[]内容)
{
ReturnPDF(目录,空);
}
公共静态void ReturnPDF(字节[]内容,字符串attachmentFilename)
{
var response=HttpContext.Current.response;
如果(!string.IsNullOrEmpty(attachmentFilename))
response.AddHeader(“内容处置”、“附件;文件名=“+attachmentFilename”);
response.ContentType=“application/pdf”;
响应.二进制写入(内容);
response.End();
}
}   
在这里输入代码

在这种情况下,您要查找的教程部分如下所示:

Response.ContentType = "application/pdf";
Response.BinaryWrite(output.ToArray());
将“文件”保存到数据库时
Response.ContentType = "application/pdf";
Response.BinaryWrite(output.ToArray());