PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri May 14, 2021 11:03 am

All times are UTC

Forum rules

Please read this before posting on this forum: Forum Rules

Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed Oct 17, 2018 3:34 am 

Joined: Fri Jul 27, 2018 3:29 am
Posts: 19
Hi, Im trying to use PDFSharp with Asp.Net Core 2.1. It working great, creating the pdf and save it to the path.
But it didn't show anything in the browser that the file has been downloaded to the browser default download path.
Here is my code:

namespace CreatePDF.Controllers
    public class CreatePDFController : Controller
        // GET: api/<controller>
        public IActionResult CreatePDF()

            return Ok("Successfully generate pdf.");

public class PDFGenerator
        public static string GeneratePDF()
            GlobalFontSettings.FontResolver = new FontResolver();

            var document = new PdfDocument();
            var page = document.AddPage();
            var gfx = XGraphics.FromPdfPage(page);
            var font = new XFont("OpenSans", 20, XFontStyle.Bold);

            gfx.DrawString("Hello World!", font, XBrushes.Black, new XRect(20, 20, page.Width, page.Height), XStringFormats.Center);


            return "pdf generated";

So, when user click button. It will generate the pdf and download it through browser like below. How do I achieve this using PDFSharp and .Net Core?

10-PDF-downloaded-file.png [ 2.72 KiB | Viewed 5670 times ]
Reply with quote  
PostPosted: Wed Oct 17, 2018 5:40 am 
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 636
Location: CCAA

The controller must send the PDF to the browser.

Here is a sample that does it with ASPX:

With ASP.NET MVC return the PDF file as the action result.
Maybe this helps you get started:

Best regards
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)

Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC

Who is online

Users browsing this forum: Google [Bot] and 16 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group