Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8318827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:06:44+00:00 2026-06-08T22:06:44+00:00

How can I read a local PDF file and offer it as a download

  • 0

How can I read a local PDF file and offer it as a download in the browser with iText?
This is what I tried, but the file always says:

Adobe Reader could not open “xxx.pdf” because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachement and wasn’t correclty decoded).

PdfReader reader = new PdfReader(filename);
byte[] streamBytes = reader.getPageContent(1);

response.setContentType("application/force-download");
response.setCharacterEncoding("UTF-8");
response.addHeader("Content-Disposition", "attachment; filename=" + filename);

BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());

bos.write(reader.getPageContent(1));
bos.write(streamBytes);
bos.flush();
bos.close();

I even made a test if iText recognizes the file as PDF, and this is the output:

System.out.println("PDF Version: " + reader.getPdfVersion());
System.out.println("Number of pages: " + reader.getNumberOfPages());
System.out.println("File length: " + reader.getFileLength());
System.out.println("Encrypted? " + reader.isEncrypted());
System.out.println("Rebuilt? " + reader.isRebuilt());

14:52:42,121 INFO  [STDOUT] PDF Version: 4
14:52:42,121 INFO  [STDOUT] Number of pages: 2
14:52:42,121 INFO  [STDOUT] File length: 186637
14:52:42,121 INFO  [STDOUT] Encrypted? false
14:52:42,121 INFO  [STDOUT] Rebuilt? false
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:06:45+00:00Added an answer on June 8, 2026 at 10:06 pm

    The content type should be “application/pdf”

      response.setContentType("application/pdf");
    

    EDIT: you don’t have to use PdfReader because you are not modifying the pdf, you want to do something like this:

                 FileInputStream baos = new FileInputStream("c:\\temp\\test.pdf");
    
                 response.setHeader("Expires", "0");
                 response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
                 response.setHeader("Pragma", "public");
                 response.setContentType("application/pdf");
                 response.addHeader("Content-Disposition", "attachment; filename=test.pdf");
    
                 OutputStream os = response.getOutputStream();
    
                 byte buffer[] = new byte[8192];
                 int bytesRead;
    
                 while ((bytesRead = baos.read(buffer)) != -1) {
                     os.write(buffer, 0, bytesRead);
                 }
    
                 os.flush();
                 os.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how to make php can read log file in /var/log/.... I tried this code
There is any way (from browser) to read local file without open dialog? Can
I can read XLS file with this code : string path =@c:\r\1.xlsx; OleDbConnection MyConnection
i want to store pdf file in local and read it . i want
I tried creating a pdf file out of another one(in my local drive) using
You can read this question where I ask about the best architecture for a
I can read unmanaged memory in C# using UnmanagedMemoryStream, but how can I do
I can read settings like this, for example: final String mytest = System.getString(this.getContentResolver(), System.AIRPLANE_MODE_ON);
In main I can read my config file, and supply it as runReader (somefunc)
I converted a local sound file to a resource, which built this in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.