Java implementation pdf file download

Source: Internet
Author: User

Click the hyperlink download PDF file, the content of the PDF file with the JSP page data. The essence is a print bar.

If you print a PDF of this template, first load the template, that is, the contents of the template read out.

Pdfreader reader = new Pdfreader ("Simpleregistrationform.pdf");

Set the size of a page

Rectangle rectpagesize = reader.getpagesizewithrotation (1);

Document doc = new document (Rectpagesize, 40, 40, 40, 40);//The remaining 4 parameters, set the page's 4 margins

Emptying response, not doing so sometimes has an impact on the program, because before this may also use the response, if not reset behind the setContentType () may not be valid.

Response.reset ();

setContentType () set file type as PDF
Response.setcontenttype ("Application/pdf");

Set the name of the download file
Response.setheader ("Content-disposition", "attachment; Filename=test.pdf ");
Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); Set font
Basefont font = Basefont.createfont ("heiseimin-w3", "Unijis-ucs2-hw-h",
basefont.not_embedded);
Font font = Fontfactory.getfont (Fontfactory.helvetica, Font.Bold,
Color.Blue);

PDFWriter pw = pdfwriter.getinstance (doc, BAOs);

Doc.open ();

Pdfcontentbyte PCB = Pw.getdirectcontent ();

Join business logic here

Pcb.begintext ();
Pcb.setfontandsize (font, 10);
String page = string.valueof (1);
pcb.showtextaligned (Pdfcontentbyte.align_left, page, 530, 40, 0);
Pcb.endtext ();

Doc.close ();

Send PDF to Client
Baos.writeto (Response.getoutputstream ());
Baos.close ();

The above is the output of the file with the flow of content, than to generate temporary file output should be simpler and faster.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.