首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过文件下载PDFSharp文件

通过文件下载PDFSharp文件
EN

Stack Overflow用户
提问于 2016-03-02 13:17:00
回答 1查看 4K关注 0票数 1

我是这个库的新手,但是我找不到任何关于下载到这个库的filestream的东西,我只能找到一个不总是被允许的document.Save(filepath)选项。我想要创建一个filestream,以便文件被下载到定向下载文件夹。

有人能直接指给我看吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-02 13:57:33

有一个Save重载,它接受一个MemoryStream对象,而不是文件的路径。

PDFSharp网站有一个示例,展示了如何做到这一点:

代码语言:javascript
复制
private void Page_Load(object sender, System.EventArgs e)
{
    // Create new PDF document
    PdfDocument document  = new PdfDocument();
    this.time             = document.Info.CreationDate;
    document.Info.Title   = "PDFsharp Clock Demo";
    document.Info.Author  = "Stefan Lange";
    document.Info.Subject = "Server time: " + 
    this.time.ToString("F", CultureInfo.InvariantCulture);

    // Create new page
    PdfPage page = document.AddPage();
    page.Width  = XUnit.FromMillimeter(200);
    page.Height = XUnit.FromMillimeter(200);

    // Create graphics object and draw clock
    XGraphics gfx = XGraphics.FromPdfPage(page);
    RenderClock(gfx);

    // Send PDF to browser
    MemoryStream stream = new MemoryStream();
    document.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();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35748319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档