showing a large pdf file embedded on a browser

Anjali Agarwal 1,366 Reputation points
2023-02-08T23:54:49.76+00:00

I am trying to show a pdf file embedded in my browser window. My code shows a small pdf file on the browser, but it does not show the large pdf file. Below is my code:

Model class:

public class FileModel

{

    public int FileModelId { get; set; }

    public byte[]? FileName { get; set; }

    public string? Url { get; set; }

    public string InlineMarkup

    {

        get

        {

            return String.Format("data:application/pdf;base64,{0}", Convert.ToBase64String(this.FileName));

        }

    }

}

My controller class Index method:

public async Task<IActionResult> Index()

    {

        using (MemoryStream stream = new MemoryStream())

        {

            string filePath = Path.Combine("FilledPDFFiles//Package2022.pdf");

            string filePath2 = Path.Combine(_environment.WebRootPath, "FilledPDFFiles//Package2022.pdf");

            byte[] bytes = System.IO.File.ReadAllBytes(filePath2);

            FileModel profile = new FileModel

            {

                Url = filePath,

                FileName = bytes

            };

            return View(profile);

        }

My razor View:

@model AckPackage.Models.FileModel

<embed src="@Model.InlineMarkup" width="100% " height="1000px" type="application/pdf">

Above code works fine if the pdf file is around 373 KB. I am trying to embed a 2330 KB file on the web browser. Is their any way to display large pdf files. Below is my directory structure

User's image

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,157 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,252 questions
{count} votes

Accepted answer
  1. Farid Uddin Kiron MSFT 456 Reputation points Microsoft Vendor
    2023-02-10T01:06:53.89+00:00

    I am trying to embed a 2330 KB file on the web browser. Is their any way to display large pdf files.

    Well, seems you are having issue on displaying large PDF file on browser. Obviously, you can. In fact, the way is much cleaner and efficient no matter how large the pdf is. I have checked with up to 10,000 KB size. Here is the implementation below:

    Embed Large PDF:

    Controller:

    public IActionResult DisplayLargePdf()
    
                {
    
                   
    
                        using (MemoryStream stream = new MemoryStream())
    
                    {
    
        
    
        
                        string embed = "<object data="{0}" type="application/pdf" width="1000px" height="600px">";
    
                        embed += "If you are unable to view file, you can download from 
    
                        embed += " or download 
    
                        embed += "</object>";
    
        
    
                        TempData["Embed"] = string.Format(embed, "/YourFolderName/YourFileName.pdf");
    
                        return View();
    
                    }
    
        
    
                }
    
    

    View:

    
        @Html.Raw(TempData["Embed"])
    
    

    Output:

    LargePDF

    Browser Settings:

    If your browser restricts you, in that scenario, you should configure your browser setting. For Edge you can configure as following.

    enter image description here

    Note:

    The key part is <object data> which would display PDF for you. I have tested just google chrome as well and working accordingly.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,601 Reputation points
    2023-02-09T16:57:38.45+00:00

    with embed, the source url is passed to the adobe reader, but there is a size limit. your approach will not work for large pdf's.

    you can download as suggested or switch to a javascript pdf viewer (though you might need to convert the base64 to a blob).

    0 comments No comments