iTextSharp生成损坏的PDF为“pdf.pdf”

时间:2016-06-22 17:39:06

标签: c# pdf itext

我有一段简单的代码,我从Good Header处获取了相同的主题,损坏的PDF文件。

我已尝试与another question一起实施所描述的解决方案,但尚未成功实现。

以下是生成示例PDF的两个函数:

private void ShowPdf (byte[] str)
{
    var Response = HttpContext.Current.Response;

    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now);

    Response.BinaryWrite(str);
    Response.End();
    Response.Flush();
    Response.Clear();
}
private byte[] CreatePDF2()
{
    Document doc = new Document(PageSize.LETTER, 50, 50, 50, 50);

    using (MemoryStream output = new MemoryStream())
    {
        PdfWriter wri = PdfWriter.GetInstance(doc, output);
        doc.Open();

        Paragraph header = new Paragraph("Test bug") { Alignment = Element.ALIGN_CENTER };
        Paragraph paragraph = new Paragraph("test.");
        Phrase phrase = new Phrase("testnewline. \nnewline hapenned.");
        Chunk chunk = new Chunk("Chucnk cauncuanocnaacoocsinasiocniocsanacsoi chunk.");

        doc.Add(header);
        doc.Add(paragraph);
        doc.Add(phrase);
        doc.Add(chunk);

        doc.Close();
        return output.ToArray();
    }
}

然后在请求中,我只是按照以下方式使用它:

ShowPdf(CreatePDF2());

问题是这会生成一个名为“Response.pdf.pdf”的文件,该文件已损坏且无法打开。

我该如何解决这个问题?

Obs。:我目前正在使用other references

1 个答案:

答案 0 :(得分:1)

尝试输出变量

FileStream output = new FileStream(Server.MapPath("MyFirstPDF.pdf"), FileMode.Create);
相关问题