将Response.OutputStream复制到MemoryStream

时间:2016-04-12 02:24:05

标签: c# pdf outputstream memorystream pdfstamper

如何将Response.Outputstream复制到MemoryStream?错误是:

  

Stream不支持阅读。

以下是代码:

string pdfTemplate = Server.MapPath(@"~\Pdf\132331.pdf");

Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ".pdf");
Response.Clear();

PdfReader pdfReader = new PdfReader(pdfTemplate);                        
PdfStamper pdfStamper = new PdfStamper(pdfReader, Response.OutputStream);

pdfStamper.FormFlattening = true;
iTextSharp.text.pdf.PdfContentByte pdfPage = pdfStamper.GetOverContent(1);

AcroFields pdfFormFields = pdfStamper.AcroFields;

string myString = Sessions.AccountCode.Trim() + "-" + Sessions.MemberID.Trim() + "-" + Sessions.CardNo + "-" + txtBDateModal.Text; // Joseph new 20151228 Sessions
BarcodeQRCode qrcode = new BarcodeQRCode(myString.Trim(), 1, 1, null);
iTextSharp.text.Image qrcodeImage = qrcode.GetImage();
qrcodeImage.SetAbsolutePosition(480, 640);
qrcodeImage.ScalePercent(325);


iTextSharp.text.Image qrcodeImage2 = qrcode.GetImage();
qrcodeImage2.SetAbsolutePosition(485, 38);
qrcodeImage2.ScalePercent(165);

var pdfContentByte = pdfStamper.GetOverContent(1);
pdfContentByte.AddImage(qrcodeImage);


var pdfContentByte2 = pdfStamper.GetOverContent(2);
pdfContentByte2.AddImage(qrcodeImage2);

pdfFormFields.SetField("lastname", txtLastNameModal.Text.Trim());
pdfFormFields.SetField("firstname", txtFirstNameModal.Text.Trim());
pdfFormFields.SetField("middlename", txtMiddleNameModal.Text.Trim());

string cardno = Sessions.CardNo.Trim(); // Joseph new 20151228 Sessions
                                        //string cardno0 = cardno.Substring(0, 4);
                                        //string cardno1 = cardno.Substring(4, 4);
                                        //string cardno2 = cardno.Substring(8, 4);
                                        //string cardno3 = cardno.Substring(12, 4);
string cardno0 = CutString(cardno, 0, 4, "CARDNO");
string cardno1 = CutString(cardno, 4, 4, "CARDNO");
string cardno2 = CutString(cardno, 8, 4, "CARDNO");
string cardno3 = CutString(cardno, 12, 4, "CARDNO");
pdfFormFields.SetField("cardno0", cardno0);
pdfFormFields.SetField("cardno1", cardno1);
pdfFormFields.SetField("cardno2", cardno2);
pdfFormFields.SetField("cardno3", cardno3);

string tin = txtTINModal.Text.Trim();
//string tin0 = tin.Substring(0, 3);
//string tin1 = tin.Substring(3, 3);
//string tin2 = tin.Substring(6, 3);
string tin0 = CutString(tin, 0, 3, "TIN");
string tin1 = CutString(tin, 3, 3, "TIN");
string tin2 = CutString(tin, 6, 3, "TIN");
string tin3 = "000";
pdfFormFields.SetField("tin0", tin0);
pdfFormFields.SetField("tin1", tin1);
pdfFormFields.SetField("tin2", tin2);
pdfFormFields.SetField("tin3", tin3);

if (txtSSSModal.Text != "")
{
    string sss = txtSSSModal.Text.Trim();
    //string sss0 = sss.Substring(0, 3);
    //string sss1 = sss.Substring(3, 3);
    //string sss2 = sss.Substring(6, 3);
    //string sss3 = sss.Substring(9);
    string sss0 = CutString(sss, 0, 3, "SSS");
    string sss1 = CutString(sss, 3, 3, "SSS");
    string sss2 = CutString(sss, 6, 3, "SSS");
    string sss3 = CutString(sss, 9, 1, "SSS");
    pdfFormFields.SetField("sss0", sss0);
    pdfFormFields.SetField("sss1", sss1);
    pdfFormFields.SetField("sss2", sss2);
    pdfFormFields.SetField("sss3", sss3);
}

if (txtUnifiedID.Text != "")
{
    string unified = txtUnifiedID.Text.Trim();
    //string unified0 = unified.Substring(0, 4);
    //string unified1 = unified.Substring(4, 7);
    //string unified2 = unified.Substring(11, 1);
    string unified0 = CutString(unified, 0, 4, "UNIFIEDID");
    string unified1 = CutString(unified, 4, 7, "UNIFIEDID");
    string unified2 = CutString(unified, 11, 1, "UNIFIEDID");
    pdfFormFields.SetField("uni0", unified0);
    pdfFormFields.SetField("uni1", unified1);
    pdfFormFields.SetField("uni2", unified2);
}

if (txtPassportNoForeigner.Text != "")
{
    string passport = txtPassportNoForeigner.Text.Trim().ToUpper();
    pdfFormFields.SetField("passportno", passport);
}

pdfFormFields.SetField("position", txtPositionModal.Text.Trim());

string[] date = txtBDateModal.Text.Trim().Split('/');
string mdate = date[0].ToString();
string ddate = date[1].ToString();
string ydate = date[2].ToString();
pdfFormFields.SetField("mdate", mdate);
pdfFormFields.SetField("ddate", ddate);
pdfFormFields.SetField("ydate", ydate);

//string pobcity = txtPoBCityModal.Text.Trim();
////string pobprovince = "";
//string pobzipcode = txtPoBZipModal.Text.Trim();

//pdfFormFields.SetField("birthplace", pobcity + ", " + pobzipcode);

string pobcity = string.Empty;
if (ddlCityPOBModal.SelectedValue == "Others")
{
    if (ddlCountryPOBModal.SelectedValue != "Philippines")
    {
        pobcity = txtCityOthersPOB.Text.Trim() + ", " + ddlCountryPOBModal.SelectedValue.ToString() + ", " + txtPoBZipModalOther.Text;
    }
    else
    {
        pobcity = txtCityOthersPOB.Text.Trim();
    }
}
else
{
    if (ddlCountryPOBModal.SelectedValue != "Philippines")
    {
        pobcity = txtCityOthersPOB.Text.Trim() + ", " + ddlCountryPOBModal.SelectedValue.ToString() + ", " + txtPoBZipModalOther.Text;
    }
    else
    {
        pobcity = ddlCityPOBModal.SelectedValue.ToString().Trim();
    }
}

string pobzipcode = txtPoBZipModal.Text.Trim();
if (pobzipcode == "" || pobzipcode == null)
{
    pdfFormFields.SetField("birthplace", pobcity);
}
else
{
    pdfFormFields.SetField("birthplace", pobcity + ", " + pobzipcode);
}

string maiden1 = txtMaidenNameLNModal.Text.Trim();
string maiden2 = txtMaidenNameFNModal.Text.Trim();
string maiden3 = txtMaidenNameMNModal.Text.Trim();
pdfFormFields.SetField("maidenname", maiden1 + ", " + maiden2 + ", " + maiden3);

pdfFormFields.SetField("email", txtEmailModal.Text.Trim());

string street = txtStreetModal.Text.Trim();
string town = txtTownModal.Text.Trim();

string city1 = string.Empty;
if (ddlCountry.SelectedValue == "Philippines")
{
    city1 = ddlCity.SelectedValue.ToString();
    if (city1 == "Others")
    {
        city1 = txtCityModal.Text.ToString();
    }
}
else if (ddlCountry.SelectedValue != "Philippines")
{
    city1 = txtCityModal.Text.ToString();
}

string city = city1.Trim();
string province = txtProvinceModal.Text.Trim();
string zipcode = txtZipCodeModal.Text.Trim();
pdfFormFields.SetField("presentaddr", street + ", " + town + ", " + city + ", " + ddlCountry.SelectedValue + " " + zipcode);
pdfFormFields.SetField("permanentaddr", city + ", " + ddlCountry.SelectedValue + " " + zipcode);

string mobile = ddlMobilePrefix.SelectedValue.ToString() + txtMobileNumberModal.Text.Trim();
//string mobile0 = mobile.Substring(0, 4);
//string mobile1 = mobile.Substring(4, 3);
//string mobile2 = mobile.Substring(7, 4);
string mobile0 = CutString(mobile, 0, 4, "MOBILE");
string mobile1 = CutString(mobile, 4, 3, "MOBILE");
string mobile2 = CutString(mobile, 7, 4, "MOBILE");
pdfFormFields.SetField("mobile0", mobile0);
pdfFormFields.SetField("mobile1", mobile1);
pdfFormFields.SetField("mobile2", mobile2);

string homephone = Sessions.PrincipalTelNo.Trim(); // Joseph new 20151228 Sessions
if (homephone.Length == 9)
{
    //string homephone0 = homephone.Substring(2, 3);
    //string homephone1 = homephone.Substring(5, 4);
    string homephone0 = CutString(homephone, 2, 3, "TELNO");
    string homephone1 = CutString(homephone, 5, 4, "TELNO");
    pdfFormFields.SetField("homephone0", homephone0);
    pdfFormFields.SetField("homephone1", homephone1);
}
else if (homephone.Length == 7)
{
    //string homephone0 = homephone.Substring(0, 3);
    //string homephone1 = homephone.Substring(3, 4);
    string homephone0 = CutString(homephone, 0, 3, "TELNO");
    string homephone1 = CutString(homephone, 3, 4, "TELNO");
    pdfFormFields.SetField("homephone0", homephone0);
    pdfFormFields.SetField("homephone1", homephone1);
}

string civil = txtCivilStatusModal.Text;
if (civil == "Single")
{
    pdfFormFields.SetField("rdbcivil", "1");
}
else if (civil == "Married")
{
    pdfFormFields.SetField("rdbcivil", "2");
}
else if (civil == "Widowed")
{
    pdfFormFields.SetField("rdbcivil", "2");
}
else if (civil == "Separated")
{
    pdfFormFields.SetField("rdbcivil", "2");
}

string gender = rdbSexModal.SelectedValue.ToString().Trim();
if (gender == "Male")
{
    pdfFormFields.SetField("rdbgender", "2");
}
else if (gender == "Female")
{
    pdfFormFields.SetField("rdbgender", "1");
}

pdfFormFields.SetField("citizenship", ddlCitizenship.SelectedValue.ToString().Trim());

pdfFormFields.SetField("educational", ddlEducationalAttainment.SelectedItem.ToString().Trim());
pdfFormFields.SetField("occupation", ddlOccupation.SelectedValue.ToString().Trim());

pdfFormFields.SetField("employer", Sessions.AccountCode + " - " + txtCompanyModal.Text.Trim()); // Joseph new 20151228 Sessions
pdfFormFields.SetField("nature", txtNatureofWorkModal.Text.ToString());

string source = ddlSourceofFund.SelectedValue;
if (source == "Others")
{
    pdfFormFields.SetField("source", txtOtherSourceFund.Text);
}
else
{
    pdfFormFields.SetField("source", "Income");
}

string id = ddlId.SelectedValue.ToString().Trim();
if (id == "Drivers_License")
{
    pdfFormFields.SetField("driver", "1");
}
else if (id == "PRC_ID")
{
    pdfFormFields.SetField("prc", "1");
}
else if (id == "NBI_Clearance")
{
    pdfFormFields.SetField("nbi", "1");
}
else if (id == "Police_Clearance")
{
    pdfFormFields.SetField("police", "1");
}
else if (id == "Postal_ID")
{
    pdfFormFields.SetField("postal", "1");
}
else if (id == "Voters_ID")
{
    pdfFormFields.SetField("voter", "1");
}
else if (id == "GSIS_ecard")
{
    pdfFormFields.SetField("gsis", "1");
}
else if (id == "Drivers_License")
{
    pdfFormFields.SetField("driver", "1");
}
else if (id == "SSS_card")
{
    pdfFormFields.SetField("sss", "1");
}
else if (id == "Senior_Citizen_Card")
{
    pdfFormFields.SetField("senior", "1");
}
else if (id == "OWWA_ID")
{
    pdfFormFields.SetField("owwa", "1");
}
else if (id == "OFW_ID")
{
    pdfFormFields.SetField("ofw", "1");
}
else if (id == "Seamans_Book")
{
    pdfFormFields.SetField("seaman", "1");
}
else if (id == "Passport")
{
    pdfFormFields.SetField("passport", "1");
}
else if (id == "Company_ID")
{
    pdfFormFields.SetField("company", "1");
}
else if (id == "GOCC_ID")
{
    pdfFormFields.SetField("gocc", "1");
}
else if (id == "Alien_Certificate")
{
    pdfFormFields.SetField("alien", "1");
}
else if (id == "Student_ID")
{
    pdfFormFields.SetField("student", "1");
}
else if (id == "IBP_ID")
{
    pdfFormFields.SetField("integrated", "1");
}
else if (id == "Mariage_Certificate")
{
    pdfFormFields.SetField("Marriage", "1");
}
else if (id == "Birth_Certificate")
{
    pdfFormFields.SetField("Birth", "1");
}
else if (id == "Barangay_Clearance")
{
    pdfFormFields.SetField("Barangay", "1");
}
else if (id == "NCWDP_Certification")
{
    pdfFormFields.SetField("NCWDP", "1");
}
else if (id == "Philhealth_Card")
{
    pdfFormFields.SetField("Philhealth", "1");
}
else if (id == "DSWD_Certification")
{
    pdfFormFields.SetField("DSWD", "1");
}
else if (id == "NBI_Signature")
{
    pdfFormFields.SetField("NBIS", "1");
}
else if (id == "TIN_ID")
{
    pdfFormFields.SetField("TIN", "1");
}




Stream s = Response.OutputStream;
using (var memoryStream = new MemoryStream())
{
    s.CopyTo(memoryStream);
    byte[] test = memoryStream.ToArray();
}

pdfStamper.Close();
Response.Flush();
ms.Close();

提前谢谢

2 个答案:

答案 0 :(得分:0)

由于错误试图告诉您,您无法做到这一点。这是一个只写流。

您可能想直接写入MemoryStream,然后将其复制到回复中。

您可能也对Response.Filter感兴趣。

答案 1 :(得分:0)

为什么不写入内存流然后将其复制到响应输出流?

string pdfTemplate = Server.MapPath(@"~\Pdf\132331.pdf");            

Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName + ".pdf");
Response.Clear();

using (var memoryStream = new MemoryStream())
{
    using (PdfStamper pdfStamper = new PdfStamper(new PdfReader(pdfTemplate), memoryStream))
    {
        pdfStamper.FormFlattening = true;
        PdfContentByte pdfPage = pdfStamper.GetOverContent(1);
        AcroFields pdfFormFields = pdfStamper.AcroFields;                    

        // pdf stuff here                                    
    }

    // Memory stream is closed by PdfStamper, so we should read the inner buffer with ToArray()
    byte[] buffer = memoryStream.ToArray();

    Response.OutputStream.Write(buffer, 0, buffer.Length);
}

Response.Flush();

编辑1:

需要关闭PdfStamper才能获取内存流中的内容。隐式调用Close方法,因此当你退出PdfStamper的using语句时,它会自动关闭。

编辑2:

PdfStamper在处理时也会关闭内存流,因此您应该使用ToArray读取内存流上的缓冲区。