showing a large pdf file embedded on a browser

Anjali Agarwal 1,531 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

Developer technologies ASP.NET ASP.NET Core
Developer technologies ASP.NET Other
{count} votes

Accepted answer
  1. Farid Uddin Kiron MSFT 456 Reputation points Microsoft External Staff
    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) 77,686 Reputation points Volunteer Moderator
    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).

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.