0

In my current application i'm able to load total pdf document by using html object and view in jsp page. but i need to show every single page in the document seperately like thumbnail view in adobe reader.

Here is my jsp code

<div class="showpdf" style="margin-left: 10%;">
    <object id="pdfPage"
        data="${pageContext.request.contextPath}/<%=session.getAttribute("fileName")%>"
        type="application/pdf" width="191" height="207" title="">
    </object>
</div>

here is my servlet code

File file = new File("D:/IIV 3 Project/Documents/Invoices/invoices/"+fileName+"");
    response.setHeader("Content-Type",    getServletContext().getMimeType(file.getName()));
    response.setHeader("Content-Length", String.valueOf(file.length()));
    response.setHeader("Content-Disposition", "inline; filename=\""+fileName+"\"");
         Files.copy(file.toPath(), response.getOutputStream());

Here is code to get a single page from pdf using pdfbox

File PDF_Path = new File("C:\\PDF.PDF");
    PDDocument inputPDF = PDDocument.load(PDF_Path);
    List<PDPage> allPages = inputPDF.getDocumentCatalog().getAllPages();
    inputPDF.close();
    PDPage testPage = (PDPage)allPages.get(0);
Nike
  • 5
  • 5
  • Your text is contradictory, the title mentions a single page, the body mentions "every single page". So what is it? If you want a single page, you'd need to pass the page number to the server. (Assuming you don't want to use PDF.js) And do you want to get an image, or do you want to get a new PDF that has a single page only? – Tilman Hausherr Oct 14 '16 at 11:41
  • both(image,PDF) are fine. – Nike Oct 14 '16 at 12:15
  • @TilmanHausherr how can we pass page number to the server? and also if image is possible, how can i get it ? – Nike Oct 17 '16 at 06:53
  • Oh oh. These are really basic JSP skills taught in courses, i.e. passing parameters and returning different file types. Google "jsp pass parameter to servlet" and "jsp display image from servlet". – Tilman Hausherr Oct 17 '16 at 07:34
  • @TilmanHausherr can i do that using pdfbox? In pdfbox i'm able to get a single page from the pdf document, now how can i display it in my jsp page ? – Nike Oct 17 '16 at 07:57
  • It is possible to split PDFs with the Splitter class, see the javadoc. https://pdfbox.apache.org/docs/2.0.0/javadocs/org/apache/pdfbox/multipdf/Splitter.html Display it in JSP would be done like you would display an ordinary PDF. – Tilman Hausherr Oct 17 '16 at 08:00
  • @TilmanHausherr Sorry for not mentioning this "i should not use splitter", i've edited my question, i'm getting a page from the document. Now i've to show it in the jsp page. One solution i'm having is read that page as image and display in jsp(i don't know if this solution works fine or not). Is there any other solution? – Nike Oct 17 '16 at 08:09
  • 1
    It's what I mentioned earlier. The alternatives are 1) using splitter to create a new (temporary) PDF file with 1 page only and pass this to the client, 2) convert to image (see https://stackoverflow.com/questions/23326562/apache-pdfbox-convert-pdf-to-images) and pass this to the client. I can't write JSP code quickly myself anymore (it's been a few years already so I would need to long), so google for "jsp display image from servlet". Note that you can't return a PDPage object. – Tilman Hausherr Oct 17 '16 at 08:13
  • @TilmanHausherr Fine Tilman, thanks for your answer and patience. – Nike Oct 17 '16 at 08:20
  • `inputPDF.close();` will close the PDF under your feet. Close it only when done, i.e. after converting to image. – Tilman Hausherr Oct 17 '16 at 08:20

0 Answers0