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.
– R.Santos
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.– Anthony Accioly
@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]
– Maniero
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.
– R.Santos