title: How to download a pdf file from project folder in java
body: <p>In my task i have to download a file which will be in my project folder it is working without using servlet i mean when i am using normal java class its working but when i am making it servlet its not working please anyone help me out here.. here is my code and SalarySlip is a folder in my project which is I have created manually ..</p>
  
  <p>I have updated error message</p>
  
  <pre><code>   public class main extends HttpServlet
       {
     public void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, java.io.IOException
    {
    try
  
       {
  
        Document document = new Document();
  
          PdfWriter writer = PdfWriter.getInstance(document,
              new FileOutputStream("D:/test/ReadPdf.pdf"));
          document.open();
          PdfReader reader = new PdfReader("SalarySlip/pivot.pdf");
          int n = reader.getNumberOfPages();
          PdfImportedPage page;
          // Go through all pages
          for (int i = 1; i &lt;= n; i++) {
            // only page number 2 will be included
  
              page = writer.getImportedPage(reader, i);
              Image instance = Image.getInstance(page);
              document.add(instance);
  
          }
          document.close();
  
            }
  
    catch (Exception e) 
      {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
     }
  
      } 
     &lt;body&gt;
    &lt;form name = "form" method="get" action="main"&gt;
     &lt;input type="submit" value="login" &gt;
    &lt;/body&gt;
  </code></pre>
  
  <p>The error message showing:</p>
  
  <pre><code>   New/pivot.pdf not found as file or resource.
      at  com.itextpdf.text.io.RandomAccessSourceFactory.
        createByReadingToMemory(RandomAccessSourceFactory.java:248)
            at  com.itextpdf.text.io.RandomAccessSourceFactory.
        createBestSource(RandomAccessSourceFactory.java:173)
        at com.itextpdf.text.pdf.PdfReader.&lt;init&gt;(PdfReader.java:219)
       at com.itextpdf.text.pdf.PdfReader.&lt;init&gt;(PdfReader.java:207)
     at com.itextpdf.text.pdf.PdfReader.&lt;init&gt;(PdfReader.java:197)
     at com.journaldev.servlet.main.doGet(main.java:63)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
     at org.apache.catalina.core.ApplicationFilterChain.         internalDoFilter(ApplicationFilterChain.java:305)
       at org.apache.catalina.core.ApplicationFilterChain
     .doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.
    invoke(StandardWrapperValve.java:222)
      at org.apache.catalina.core.StandardContextValve.
      invoke(StandardContextValve.java:123)
       at org.apache.catalina.authenticator.
     AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor
   .process(AbstractHttp11Processor.java:1023)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.
     process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
   at java.lang.Thread.run(Unknown Source)
  </code></pre>