嗨这是我的C#代码:
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#
中加载pdf文档我没有找到我在代码中的错误。
请给我正确的代码
感谢。
答案 0 :(得分:3)
您没有创建完整的PDF文件。请查看我对问题Trying to get a memory stream from a pdfstamper into a pdfreader but getting: "PDF startxref not found"
的回答关闭时,PDF文档已完成:
document.Close();
在执行此操作之前,
但是,您在关闭文档之前询问MemoryStream
:
bytes = ms.ToArray();
document.Close();
这意味着bytes
不包含完整的PDF。
此外,您正在使用HTMLWorker
。该课程多年前已被放弃,请阅读documentation about XML Worker。
HTMLWorker
可能会隐式关闭document
对象(我不记得,正如我所说:HTMLWorker
不再使用)。在这种情况下,将paragraph
对象添加到文档的代码将抛出异常,表示文档已关闭,并且您无法再添加任何额外内容。
答案 1 :(得分:2)
@shivakumar,请尝试使用此示例并根据您的数据进行更改。
Byte[] bytes = ImageToByteArray(System.Drawing.Image.FromFile(@"D:\Test\Sample1.png"));
//Converted Image to byte[]
using (MemoryStream ms = new MemoryStream())
{
Document document = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
byte[] imageBytes = bytes;
ms.Write(bytes, 0, bytes.Length);
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);
var htmlWorker = new HTMLWorker(document);
string message="hi";
using (var sr = new StringReader(message))
{
htmlWorker.Parse(sr);
}
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();
答案 2 :(得分:0)
如果您有一个包含PDF的有效流,并且您正在获取"无法加载PDF文档"来自Chrome,这肯定会导致问题:
Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfName + ".pdf");
'附件'具体告诉浏览器不在浏览器中显示此文档,而是直接发送文件。我花了几个小时才知道Chrome的PDF查看器在那种情况下会窒息,可能是设计出来了!
试试这个:
Response.AddHeader("Content-Disposition", "inline; filename=" + pdfName + ".pdf");
更多:Content-Disposition:What are the differences between "inline" and "attachment"?