为什么MigraDoc在我的asp.net应用程序中生成空白pdf? [英] Why is MigraDoc generating a blank pdf in my asp.net application?

查看:70
本文介绍了为什么MigraDoc在我的asp.net应用程序中生成空白pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试创建PDF时,我具有以下代码:

I have the following code in my attempts to create a PDF:

    public static MemoryStream Test()
    {
        var document = new Document();
        document.Info.Title = "Test Report";
        document.Info.Subject = "blah";
        document.Info.Author = "Me";
        //new CoverPageSummarySection().AddToDocument(document, new int[0], 2004);

        Style style = document.Styles["Normal"];
        style.Font.Name = "Times New Roman";
        style = document.Styles["Heading1"];
        style.Font.Name = "Tahoma";
        style.Font.Size = 14;
        style.Font.Bold = true;
        style.Font.Color = Colors.DarkBlue;
        style.ParagraphFormat.PageBreakBefore = true;
        style.ParagraphFormat.SpaceAfter = 6;

        var section = document.AddSection();
        var p = section.AddParagraph("test");
        p.AddText("Testing 1234");

        var renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
        renderer.Document = document;
        renderer.RenderDocument();

        var ms = new MemoryStream();
        renderer.PdfDocument.Save(ms, false);
        return ms;
    }

生成的pdf为空白.我可以查看属性,并且 document.Info 字段在我的PDF中正确显示,但是在页面上看不到任何文本.

The resulting pdf is blank. I can view the properties and the document.Info fields are showing correctly in my PDF, but I can't see any text on my page.

我在做什么错了?


因此,问题似乎与保存到内存流有关.当我将 renderer.PdfDocument.Save(ms,false); 替换为 renderer.PdfDocument.Save("e:\\ test.pdf"); 时,它会正确保存在test.pdf.


So it appears that the issue has something to do with saving to a memory stream. When I replace renderer.PdfDocument.Save(ms, false); to renderer.PdfDocument.Save("e:\\test.pdf"); it saves it correctly at test.pdf.

我将内存流保存到asp.net输出的代码是:

My code to save the memory stream to the asp.net output is:

        var stream = TestReportGen.Test();

        // Set the content headers
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ContentType = "application/pdf";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=testReport.pdf");

        stream.WriteTo(HttpContext.Current.Response.OutputStream);
        stream.Close();

        HttpContext.Current.Response.End();

我是如何发送回内存流还是什么问题?

Is the issue with how I'm sending back the memorystream or what?

推荐答案

假设您有一个有效的MigraDoc文档,则应该可以进行以下操作:

Assuming you have a valid MigraDoc document, the following should work:

PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfFontEmbedding.Always);
renderer.Document = document;
renderer.RenderDocument();

// Send PDF to browser
MemoryStream stream = new MemoryStream();
renderer.PdfDocument.Save(stream, false);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", stream.Length.ToString());
Response.BinaryWrite(stream.ToArray());
Response.Flush();
stream.Close();
Response.End();

这篇关于为什么MigraDoc在我的asp.net应用程序中生成空白pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆