C# Can';t将PDF文件从MemoryStream复制到FileStream
我做错什么了吗?请问为什么我不能打开保存的两个PDF文件 至于MemoryStream,它被保存到我的目录->从Chrome下载文件夹 对于FileStream,它保存到我在代码中指定的文件路径 谢谢你的帮助。多谢各位 代码:C# Can';t将PDF文件从MemoryStream复制到FileStream,c#,asp.net,pdf,filestream,memorystream,C#,Asp.net,Pdf,Filestream,Memorystream,我做错什么了吗?请问为什么我不能打开保存的两个PDF文件 至于MemoryStream,它被保存到我的目录->从Chrome下载文件夹 对于FileStream,它保存到我在代码中指定的文件路径 谢谢你的帮助。多谢各位 代码: protected void BTN_Assign_Click(object sender, EventArgs e) { //String id = DDL_PatientID.SelectedItem.ToString(); S
protected void BTN_Assign_Click(object sender, EventArgs e)
{
//String id = DDL_PatientID.SelectedItem.ToString();
String id = TB_PatientID.Text;
String name = TB_PatientName.Text;
String bedID = DDL_BedID.SelectedItem.ToString();
String classType = DDL_Class.SelectedItem.ToString();
String block = TB_Block.Text;
String level = TB_Level.Text;
String staffId = DDL_StaffID.SelectedItem.ToString();
DateTime dt = DateTime.Now;
String dtAssign = dt.ToString("dd MMM yyyy h:mm tt");
int result = 0;
//Assigning bed (In Beds Table)
/*BedsBLL add = new BedsBLL();
result = add.assignBed(bedID, id, name, classType, block, level, staffId, dtAssign);*/
//Updating status of Bed Selected (In BedsList Table)
String status = "Alloted";
BedsBLL update = new BedsBLL();
result = update.updateBedStatusToAlloted(status, bedID, id, name, staffId, dtAssign);
AdmissionBLL a = new AdmissionBLL();
int assignCounter = 0;
assignCounter = a.updateBedAssignedAdmission(id, bedID);
//PDF Creation with Admission Details from previous page
// --- START ---
DataTable admissionDetails = new DataTable();
admissionDetails.Columns.Add("Date & Time Admitted");
admissionDetails.Columns.Add("Reason");
admissionDetails.Columns.Add("Family Phone");
admissionDetails.Columns.Add("Family Email");
admissionDetails.Columns.Add("First Time Admitted");
DataRow dr;
dr = admissionDetails.NewRow();
dr["Date & Time Admitted"] = Session["DTAdmitted"].ToString();
dr["Reason"] = Session["Reason"].ToString();
dr["Family Phone"] = Session["FamilyPhone"].ToString();
dr["Family Email"] = Session["FamilyEmail"].ToString();
dr["First Time Admitted"] = Session["FirstTimeAdmitted"].ToString();
admissionDetails.Rows.Add(dr);
Document pdfDocument = new Document(PageSize.A4, 40f, 40f, 40f, 40f);
Font NormalFont = FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK);
Font BoldFontForHeader = FontFactory.GetFont("Arial", 10, Font.BOLD, BaseColor.BLACK);
String fileName = @"C:\Users\Dom\Downloads\WebFormApplication\WebFormApplication\WebFormApplication\Exported Documents\" + Session["PatientID"].ToString() + " Admission PDF" + ".pdf";
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
PdfPCell cell = null;
using (MemoryStream ms = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(pdfDocument, ms);
FileStream fs = new FileStream(fileName, FileMode.Create);
//Admission Record
PdfPTable admissionTable = new PdfPTable(admissionDetails.Columns.Count);
admissionTable.TotalWidth = 550f;
admissionTable.LockedWidth = true;
admissionTable.HorizontalAlignment = Element.ALIGN_CENTER;
admissionTable.SetWidths(new float[] { 0.35f, 0.35f, 0.35f, 0.35f, 0.35f });
admissionTable.SpacingBefore = 30f;
string[] headersName = { "Date Time Admitted", "Reason", "Family Phone", "Family Email", "First Time Admitted" };
for (int i = 0; i < headersName.Length; i++)
{
PdfPCell hCell = new PdfPCell(new Phrase(headersName[i].ToString(), BoldFontForHeader));
admissionTable.AddCell(hCell);
}
foreach (DataRow row in admissionDetails.Rows)
{
foreach (object tableCell in row.ItemArray)
{
cell = new PdfPCell(new Phrase(tableCell.ToString()));
cell.Padding = 5;
admissionTable.AddCell(cell);
}
}
//Beds Record
PdfPTable bedsRecord = new PdfPTable(5);
bedsRecord.TotalWidth = 550f;
bedsRecord.LockedWidth = true;
bedsRecord.HorizontalAlignment = Element.ALIGN_CENTER;
bedsRecord.SetWidths(new float[] { 0.35f, 0.35f, 0.35f, 0.35f, 0.35f });
bedsRecord.SpacingBefore = 30f;
string[] headersNameBed = { "Bed ID", "Class", "Block", "Level", "Staff Incharge ID" };
for (int i = 0; i < headersNameBed.Length; i++)
{
PdfPCell hCell = new PdfPCell(new Phrase(headersNameBed[i].ToString(), BoldFontForHeader));
bedsRecord.AddCell(hCell);
}
PdfPCell[] cells = new PdfPCell[] {
new PdfPCell(new Phrase(bedID.ToString())),
new PdfPCell(new Phrase(classType.ToString())),
new PdfPCell(new Phrase(block.ToString())),
new PdfPCell(new Phrase(level.ToString())),
new PdfPCell(new Phrase(staffId.ToString()))
};
PdfPRow bRow = new PdfPRow(cells);
bedsRecord.Rows.Add(bRow);
pdfDocument.Open();
//Top of Document
Paragraph p1 = new Paragraph("Mount Olympus Hospital", new Font(Font.FontFamily.HELVETICA, 18));
Paragraph p2 = new Paragraph("24 Jalan Kapal Street 42 Singapore 554524", new Font(Font.FontFamily.HELVETICA, 12));
Paragraph p3 = new Paragraph("Telephone: 6550-9514 Fax: 6550-9245", new Font(Font.FontFamily.HELVETICA, 12));
p1.Alignment = Element.ALIGN_CENTER;
p2.Alignment = Element.ALIGN_CENTER;
p3.Alignment = Element.ALIGN_CENTER;
pdfDocument.Add(p1);
pdfDocument.Add(p2);
pdfDocument.Add(p3);
Paragraph p4 = new Paragraph("Patient ID: " + Session["PatientID"], new Font(Font.FontFamily.TIMES_ROMAN, 11));
Paragraph p5 = new Paragraph("Patient Name: " + Session["PatientName"], new Font(Font.FontFamily.TIMES_ROMAN, 11));
p4.SpacingBefore = 20f;
p4.Alignment = Element.ALIGN_LEFT;
p5.Alignment = Element.ALIGN_LEFT;
p5.SpacingAfter = 20f;
pdfDocument.Add(p4);
pdfDocument.Add(p5);
Paragraph p6 = new Paragraph("Admission Details", new Font(Font.FontFamily.HELVETICA, 16));
p6.Alignment = Element.ALIGN_LEFT;
p6.SpacingBefore = 20f;
pdfDocument.Add(p6);
pdfDocument.Add(admissionTable);
Paragraph p7 = new Paragraph("Bed Details", new Font(Font.FontFamily.HELVETICA, 16));
p7.Alignment = Element.ALIGN_LEFT;
p7.SpacingBefore = 20f;
pdfDocument.Add(p7);
pdfDocument.Add(bedsRecord);
byte[] bytes = ms.ToArray();
ms.Position = 0;
ms.CopyTo(fs);
fs.CopyTo(ms);
pdfDocument.Close();
ms.Close();
fs.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + Session["PatientID"].ToString() + " Admission PDF" + ".pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
/*using (System.IO.FileStream fs = new System.IO.FileStream())
{
}*/
// --- END ---
//Showing succcesful panel.
if (result > 0 && assignCounter > 0)
{
Panel_ErrorMsg.Visible = true;
//Panel_ErrorMsg.ToolTip = "Click on the button to return to Beds List";
lb_ErrorMsg.Text = "Successfully assigned bed to Patient " + name
+ " and updated Bed " + bedID + " status to Alloted.";
BTN_Assign.Visible = false;
}
}
受保护的无效BTN\u分配\u单击(对象发送方,事件参数e)
{
//字符串id=DDL_PatientID.SelectedItem.ToString();
字符串id=TB_PatientID.Text;
字符串名称=TB_PatientName.Text;
字符串bedID=DDL_bedID.SelectedItem.ToString();
String classType=DDL_Class.SelectedItem.ToString();
String block=TB_block.Text;
字符串级别=TB_level.Text;
字符串staffId=DDL_staffId.SelectedItem.ToString();
DateTime dt=DateTime.Now;
字符串dtAssign=dt.ToString(“dd-MMM-yyy-h:mm-tt”);
int结果=0;
//分配床位(床位表中)
/*BEDBBLL add=新BEDBBLL();
结果=add.assignBed(bedID,id,name,classType,block,level,staffId,dtAssign)*/
//更新所选床位的状态(在床位列表表中)
字符串状态=“已分配”;
BEDBBLL update=新BEDBBLL();
结果=update.updateBedStatusToAlloted(状态、bedID、id、名称、staffId、dtAssign);
AdmissionBLL a=新的AdmissionBLL();
int赋值计数器=0;
assignCounter=a.UpdateBedAssignedMission(id,bedID);
//使用上一页中的准入详细信息创建PDF
//---开始---
DataTable admissionDetails=新DataTable();
录取详情。栏。添加(“录取日期和时间”);
允许详情。列。添加(“原因”);
允许详细信息。列。添加(“家庭电话”);
允许详细信息。列。添加(“家庭电子邮件”);
录取详情。栏。添加(“首次录取”);
数据行dr;
dr=许可详情。NewRow();
dr[“允许的日期和时间”]=会话[“允许的数据”].ToString();
dr[“Reason”]=会话[“Reason”].ToString();
dr[“家庭电话”]=会话[“家庭电话”]。ToString();
dr[“家庭电子邮件”]=会话[“家庭邮件”].ToString();
dr[“首次接纳”]=会话[“首次接纳”].ToString();
允许详细信息。行。添加(dr);
文档pdfDocument=新文档(PageSize.A4、40f、40f、40f、40f);
Font-NormalFont=FontFactory.GetFont(“Arial”,8,Font.NORMAL,BaseColor.BLACK);
Font BoldFontForHeader=FontFactory.GetFont(“Arial”,10,Font.BOLD,BASECLOR.BLACK);
字符串文件名=@“C:\Users\Dom\Downloads\WebFormApplication\WebFormApplication\WebFormApplication\Exported Documents\”+会话[“PatientID”].ToString()+“Admission PDF”+“.PDF”;
CreateDirectory(Path.GetDirectoryName(fileName));
PdfPCell=null;
使用(MemoryStream ms=new MemoryStream())
{
PdfWriter writer=PdfWriter.GetInstance(pdfDocument,ms);
FileStream fs=newfilestream(文件名,FileMode.Create);
//录取记录
PdfPTable admissionTable=新的PdfPTable(admissionDetails.Columns.Count);
容许总宽度=550f;
admissionTable.LockedWidth=true;
admissionTable.HorizontalAlignment=Element.ALIGN\u CENTER;
可接受的设置宽度(新浮动[]{0.35f,0.35f,0.35f,0.35f,0.35f});
容许表.SpacingBefore=30f;
字符串[]headersName={“入院日期时间”、“原因”、“家庭电话”、“家庭电子邮件”、“首次入院”};
for(int i=0;i pdfDocument.Close();
ms.Position = 0;
ms.CopyTo(fs);
pdfDocument.Close();
//ms.Position = 0;
//ms.CopyTo(fs);
//fs.CopyTo(ms);
byte[] bytes = ms.ToArray();
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
ms.Position = 0;
ms.CopyTo(fs);
}