开发者

Print a PDF into a div

Whene I open a PDF on the browser I want to print it in a div not all the page. How can I do that? Here is my JSP source code:

<%@ page language="java" import="com.search.ts.*
                                ,java.io.*
                                ,java.net.*
                                ,javax.xml.namespace.QName
                                ,javax.jws.*
                                ,javax.xml.ws.* "
                                                   contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Ebook reader</title>

<%@ page language="java" import="com.search.ts.CallSEI_CallSPort_Client,java.util.*,com.search.ts.Links,com.search.ts.LinksResponse"  %>

<link rel="stylesheet" type="text/css" href="style.css" />

</head>
<body>

            <div id="right_section">
              <div class="right_box">

<% 

        String filename= request.getParameter("err");
        //String filename =(String) request.getAttribute("linkbook");
        File file = new File("F:/fichiers/", filename+".pdf");

        response.setContentType(getServletContext().getMimeType(file.getName()));
        response.setContentLength((int) file.length());
        response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");

        BufferedInputStream input = null;
        BufferedOutputStream output = null;

        try {
            input = new BufferedInputStream(new FileIn开发者_如何学PythonputStream(file));
            output = new BufferedOutputStream(response.getOutputStream());

            byte[] buffer = new byte[8192];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
        } finally {
            if (output != null) try { output.close(); } catch (IOException ignore) {}
            if (input != null) try { input.close(); } catch (IOException ignore) {}
        }


        %>


          </div>
 </div>


</body>
</html>


i was in the seem prbm you have to send from the 1st pdf page a link with the name of the pdf (myfile.pdf), like that:

<a href="pdfread.jsp?err=<%=filename %>"><%=bookName %> </a> 

to the page pdfread.jsp and in this page put that

<%
    String filename= request.getParameter("err");
%>
<embed src="${pageContext.request.contextPath}/pdfreader/<%=filename %>#toolbar=0&navpanes=0&scrollbar=0" width="500" height="375">
  </embed>

and this code you must put it in a servlet with a do get

    String filename= request.getParameter("err");
    //String filename =(String) request.getAttribute("linkbook");
    File file = new File("F:/fichiers/", filename+".pdf");

    response.setContentType(getServletContext().getMimeType(file.getName()));
    response.setContentLength((int) file.length());
    response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");

    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try {
        input = new BufferedInputStream(new FileInputStream(file));
        output = new BufferedOutputStream(response.getOutputStream());

        byte[] buffer = new byte[8192];
        int length;
        while ((length = input.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
    } finally {
        if (output != null) try { output.close(); } catch (IOException ignore) {}
        if (input != null) try { input.close(); } catch (IOException ignore) {}
    }

see this link to do it

How to use doGet in jsp with Servlet


there are simpler options with less code involved....have a look http://www.webdeveloper.com/forum/showthread.php?t=152923


What you want is an object/embed tag.

Check this out

http://blogs.adobe.com/pdfdevjunkie/2007/08/using_the_html_embed_tag_to_di.html

The synopsis is, you want to source the pdf like you'd source an image:

<object type="application/pdf" data="myfile.pdf" width="500" height="650" ><embed src="myfile.pdf"/>
</object>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜