你好,這是我的C#代碼:未能在C#中加載PDF文檔
Byte[] bytes;
int orderID = Convert.ToInt32(e.CommandArgument);
var templateData = ordersBL.GetTemplateDetails(orderID);
using (MemoryStream ms = new MemoryStream())
{
using (Document document = new Document(PageSize.A4, 10, 10, 10, 10))
{
PdfWriter writer = PdfWriter.GetInstance(document, ms);
foreach (var temp in templateData.ToList())
{
string message = temp.Message;
string tempimage = Convert.ToBase64String(temp.logo);
string base64 = tempimage;
byte[] imageBytes = Convert.FromBase64String(base64);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageBytes);
if (image.Height > image.Width)
{
float percentage = 0.0f;
percentage = 700/image.Height;
image.ScalePercent(percentage * 100);
}
else
{
float percentage = 0.0f;
percentage = 140/image.Width;
image.ScalePercent(percentage * 100);
}
if (!document.IsOpen())
{
document.Open();
}
document.Add(image);
using (var htmlWorker = new HTMLWorker(document))
{
using (var sr = new StringReader(message))
{
htmlWorker.Parse(sr);
}
}
Paragraph paragraph = new Paragraph();
paragraph.SpacingBefore = 10;
paragraph.SpacingAfter = 10;
paragraph.Alignment = Element.ALIGN_LEFT;
// paragraph.Font = FontFactory.GetFont(FontFactory.HELVETICA, 12f, BaseColor.GREEN);
document.Add(paragraph);
document.NewPage();
}
bytes = ms.ToArray();
document.Close();
}
}
Response.ContentType = "application/pdf";
string pdfName = "User";
Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfName + ".pdf");
Response.Buffer = true;
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
Response.Flush();
}
全文下載,但它顯示未能加載PDF文檔。
無法加載PDF文檔中的C#
我沒有找到,我在代碼中的錯誤。
請給我正確的代碼
謝謝。
你''End''Close'代碼Flush'會不喜歡這樣的工作。請閱讀:https://blogs.msdn.microsoft.com/aspnetue/2010/05/25/response-end-response-close-and-how-customer-feedback-helps-us-improve-msdn-documentation/ 和 http://stackoverflow.com/questions/2816194/use-of-response-flush-before-response-end – Alexander