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

Asked

Viewed 344 times

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 given page and not on the first page of the file. How can I do this?

  • Hi @Anthonyaccioly all right? Is that with this code my pdf opens normally in the browser but always on the first page of the file, and I need to be able to determine the page that this file will open.

  • Um... have you ever tried to put #page=NumeroDaPagina no request? Works with pdf files directly. Have a look at: Acrobat / Create a URL to open a PDF file at a specific page. If it works give me a tap and I turn in response.

  • 2

    @R.Santos you have changed the question as answers are being asked. And worse, the edited question is invalidating the answers given. This is not a forum. It’s a question and answer site. You ask a well-thought-out, well-formulated question, with a specific, well-defined problem, and you get answers to it. If it is not satisfactory, you can ask new questions, but you cannot keep changing the existing one to invalidate the answers. If you need to change the question too much, it’s because the question is not good and you need to think more before posting a question.I hope you understand.Look at [tour]

  • I do understand @bigown and fully agree, I know what’s wrong. Anthony explained this to me as well. I apologize for making unnecessary changes.

3 answers

3


My original comment, to reply from José and the response of Cleidimar show the way to point a client-side page. That is, make the PDF reader open the document on a specific page.

Another possible solution is to "crop" the pdf from the server side and send only the page you need. To do this you will need to use your own library for PDF manipulation like Apache Pdfbox.

Assuming you will pass the page number as a parameter to the Servlet (e.g., calling 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
    }
}

Both approaches have advantages and disadvantages:

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

From the point of view of performance, point to the client side page means there will be no heavier processing on the server side, getting your Servlet limited I/O capacity. On the other hand you keep transmitting the entire PDF, which consumes more bandwidth and potentially makes the document take a lot more time to load.

Clipping the server-side PDF means more complex and flawed processing (the structure of the original PDF file may be "corrupted" or this may potentially be a protected / encrypted PDF). On top of the time of I/O you will also spend a little CPU in this game. On the other hand the customer will receive a single page, which costs less bandwidth and potentially means less charging time.

If a certain page is called several times it may be better 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.


Source: Soen - Reading a particular page from a PDF Document using Pdfbox

  • Okay @Anthonyaccioly, thanks for your help.

1

0

There’s a way you can do it using page using this parameter #page=[pagina específica], then it would look like this if the file is at a specific local address:

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

If you are on the web:

http://www.meusite.meuarquivo.pdf#page=3
  • It gives error so, it does not find the way if placed in the way you suggested;

  • @R.Santos Try to put there in your response something like setParameter and put page as parameter and page number.

  • @R.Santos would be like this response.setParameter("#page", 3)

  • @R.Santos actually face, give a modified one there on your service, acescentando HttpSession session = request.getSession();
 session.setAttribute("#page",3); so you’d be setting an attribute in your request. Oh yeah it has to work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.