How can I open the generated PDF document directly into the browser window instead of displaying the Open or Save dialog in the browser
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can create a PDF document and open it directly into the browser window.
Steps to open the generated PDF document directly into the browser window:
- Create a new ASP.NET MVC application project.
- Install the Syncfusion.Pdf.AspNet.MVC NuGet package as a reference to your .NET Framework application from NuGet.org.
- A default controller with a name HomeController.cs gets added on the created ASP.NET MVC project. Include the following namespaces in that HomeController.cs.
C#
using Syncfusion.Pdf; using System.Drawing; using Syncfusion.Pdf.Graphics;
VB.NET
Imports Syncfusion.Pdf Imports System.Drawing Imports Syncfusion.Pdf.Graphics
- Add a new action method named CreateDocument in the HomeController.cs and include the following code snippet to create an PDF file and open it in browser.
C#
//Create an instance of PdfDocument. using (PdfDocument document = new PdfDocument()) { //Add a page to the document PdfPage page = document.Pages.Add(); //Create the PDF graphics for the page PdfGraphics graphics = page.Graphics; //Set the standard font PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20); //Draw the text graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0)); // Open the document in browser after saving it document.Save("Output.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open); }
VB.NET
'Create an instance of PDF document Using document As PdfDocument = New PdfDocument() 'Add a page to the document Dim page As PdfPage = document.Pages.Add() 'Create the graphics for the page Dim graphics As PdfGraphics = page.Graphics 'Set the standard font Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 20) 'Draw the text graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, New PointF(0, 0)) 'Open the document in the browser after saving it document.Save("Output.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open) End Using
A complete working sample can be download from CreatePDFSample.zip.
By executing the program, the generated PDF document directly open in the browser.
Take a moment to peruse the documentation, where you can find the options to like drawing right-to-left text, multi-column text, consuming TrueType fonts, Standard fonts, CJK fonts, etc., and features like PDF form filling, extract text, or images from PDF, and protect PDF documents, etc., with code examples.
Refer here to explore the rich set of Syncfusion Essential PDF features.
See Also:
Create a PDF file in Windows Forms
Create a PDF file in ASP.NET Core
Create a PDF file in Xamarin Forms
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Click this link to learn about generating and registering the Syncfusion license key in your application to use the components without trail message.