Determine the page where the * .pdf file will open in the browser

1

I have a code to display a pdf file:

@WebServlet(urlPatterns = {"/teste"})
public class abrirPDF extends HttpServlet {

    byte[] arquivo = null;
    File file = new File("C:\testes\teste.pdf");

    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        try {
            arquivo = fileToByte(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.setContentType("application/pdf");
        response.setContentLength(arquivo.length);
        ServletOutputStream ouputStream = response.getOutputStream();
        ouputStream.write(arquivo, 0, arquivo.length);
        ouputStream.flush();
        ouputStream.close();
    }

    public static InputStream byteToInputStream(byte[] bytes) throws Exception {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        return bais;
    }

    public static byte[] fileToByte(File imagem) throws Exception {

        FileInputStream fis = new FileInputStream(imagem);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[8192];
        int bytesRead = 0;
        while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
            baos.write(buffer, 0, bytesRead);
        }
        return baos.toByteArray();
    }
}

I need to open this pdf on a specific page. That is, I want the browser to open the PDF directly on a particular page and not on the first page of the file. How can I do this?

    
asked by anonymous 19.08.2016 / 20:00

3 answers

3

My original comment , the Joseph's answer and Cleidimar's answer shows how to point a client-side page. That is, to make the PDF reader open the document on a specific page.

Another possible solution is to "cut" the server-side pdf and send only the page you need. To do this you will need to use your own PDF manipulation library such as Apache PDFBox .

Assuming you are going to pass the page number as a parameter to the Servlet (eg call teste?numeroPagina=10 ) here is an example of how to "crop" the PDF and return only one page as a response:

public void doGet(HttpServletRequest request,HttpServletResponse response) 
        throws ServletException, IOException {

    File pdfOriginal = new File("C:\testes\teste.pdf");

    PDDocument documentoOriginal = null; 
    PDDocument documentoModificado = null;

    // No código real leia o parâmetro de maneira mais robusta
    int numeroPagina = Integer.parseInt(request.getParameter("numeroPagina"));

    try {
        documentoOriginal = PDDocument.load(pdfOriginal);
        // cria documento apenas com a pagina requisitada
        documentoModificado = new PDDocument();
        documentoModificado.addPage(documentoOriginal.getPage(numeroPagina));

        // escreve o documento na resposta
        response.setContentType("application/pdf");
        response.setContentLength(pdfOriginal.length());
        documentoModificado.save(response.getOutputStream());  
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (documentoOriginal != null) {
            documentoOriginal.close();
        }
        if (documentoModificado != null) {
            documentoModificado.close();
        }
        // você pode também fazer flush e fechar a stream da resposta
        // mas isso não é necessário
    }
}

The two approaches have advantages and disadvantages:

In terms of usability, the first solution serves the whole file (lets the client view other pages), while the second serves only the page you want to display.

From a performance standpoint, pointing to the client-side page means that there will be no heavier processing on the server side, with your Servlet being limited to I / O. On the other hand you continue to transmit the entire PDF, which consumes more bandwidth and potentially causes the document to take much longer to load.

Cutting the server-side PDF means more complex and fault-prone rendering (the structure of the original PDF file may be "corrupted" or potentially potentially encrypted / encrypted PDF). Over time I / O you will also spend a little CPU on this game. On the other hand, the customer will receive a single page, which will spend less bandwidth and potentially mean less loading time.

If a given page is to be called multiple times it may be best to split the PDF per page and save everything to disk. In the end we have here several possible exchanges between band, disk and CPU, depending on your application one or another strategy may be better.

Font : Soen - Reading a particular page from a PDF document using PDFBox

    
22.08.2016 / 17:54
1

Try to put the #page=[numPag] argument in the Response Header. Here is the example on page 2 below:

response.setHeader("Content-Disposition","attachment;filename=\"teste.pdf#page=2\";");

Create a URL to open a PDF file at a specific page

    
22.08.2016 / 16:53
0

There is a way for you to do this using page using this parameter #page=[pagina específica] , so it would look like this if the file is in a specific local address:

file:///C:/meuarquivo.pdf#page=3

If you are on the web:

http://www.meusite.meuarquivo.pdf#page=3
    
22.08.2016 / 16:08