Itext PDF with headers and footers plus pages (ii)

Source: Internet
Author: User

New class:

Import java.io.IOException;
Import com.itextpdf.text.Document;
Import com.itextpdf.text.DocumentException;
Import Com.itextpdf.text.pdf.BaseFont;
Import Com.itextpdf.text.pdf.PdfContentByte;
Import Com.itextpdf.text.pdf.PdfPageEventHelper;
Import Com.itextpdf.text.pdf.PdfTemplate;
Import Com.itextpdf.text.pdf.PdfWriter;
/**
* Set the header and bottom information for the printed document
*
* @author
*
*/
public class Headfootinfopdfpageevent extends Pdfpageeventhelper {
Custom Pass Parameters
Public String pdfname;//The name of the warehouse
Public String date;//Date
Public String type;//Dan Number type
Public String code;//Dan
Public Pdftemplate TPL;
Basefont Bfchinese;

Non-parametric construction method
Public Headfootinfopdfpageevent () {
Super ();
}

A method of parametric construction
Public headfootinfopdfpageevent (String pdfname,string date,string type,string Code) {
Super ();
This.pdfname=pdfname;
This.date = date;
This.type=type;
This.code = code;
try {
Bfchinese = Basefont.createfont ("C:/windows/fonts/simsun"). ttc,1 ", Basefont.identity_h, basefont.embedded);
catch (Documentexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

public void OnOpenDocument (PDFWriter writer, document document) {
TPL = Writer.getdirectcontent (). Createtemplate (100, 20);
}

Ways to implement headers and footers
public void OnEndPage (PDFWriter writer, document document) {
try {
string[] Riqi = Date.split ("-");
Pdfcontentbyte headandfootpdfcontent = Writer.getdirectcontent ();
Headandfootpdfcontent.savestate ();
Headandfootpdfcontent.begintext ();
Set up Chinese
Headandfootpdfcontent.setfontandsize (Bfchinese, 12);
Document page header information settings
float x = document.top (-20);
float x1 = document.top (-5);
middle of page header information
Headandfootpdfcontent.showtextaligned (Pdfcontentbyte.align_center, Pdfname, (document.right () + document.left ())/2 , x, 0);
Page header information left
Headandfootpdfcontent.showtextaligned (Pdfcontentbyte.align_left, riqi[0] + "year" + riqi[1] + "month" + riqi[2] + "Day",
Document.left () + x1, 0);
middle of page header information
Headandfootpdfcontent.showtextaligned (Pdfcontentbyte.align_center, type+ "library number:" +code+ "",
(Document.right () + document.left ())/2, x1, 0);
Page header information to the right
Headandfootpdfcontent.showtextaligned (pdfcontentbyte.align_right, "unit: Book", Document.right ()--0, X1;
Document footer Information Settings
Float y = document.bottom (-20);
float y1 = document.bottom (-35);
Footer Information Left
Headandfootpdfcontent.showtextaligned (Pdfcontentbyte.align_left, "Head of Transportation Department:", Document.left () +, y, 0);
middle of footer information
Headandfootpdfcontent.showtextaligned (Pdfcontentbyte.align_center, "Librarian:", (Document.right () + document.left ())/2 , y, 0);
Footer information to the right
Headandfootpdfcontent.showtextaligned (Pdfcontentbyte.align_right, "the person who handles:", document.right ()-----0;
Add page numbers
middle of footer information
Headandfootpdfcontent.showtextaligned (Pdfcontentbyte.align_center, "--the first" + Document.getpagenumber (),
(Document.right () + document.left ())/2, y1, 0);
At the end of each page, write "page x" to specify the location of the template
Headandfootpdfcontent.addtemplate (TPL, (document.right () + document.left ())/2 +, y1)//Locate "y page" to change the XY coordinates at specific page debugging
Headandfootpdfcontent.endtext ();
Headandfootpdfcontent.restorestate ();
catch (Exception de) {
De.printstacktrace ();
}
}

public void OnCloseDocument (PDFWriter writer, document document) {
Gets the total number of pages when the document is closed, and the number of pages reserved before the template is written
Tpl.begintext ();
Tpl.setfontandsize (Bfchinese, 12);
Tpl.showtext ("page, total" + integer.tostring (Writer.getpagenumber ()-1) + "page-");
Tpl.endtext ();
Tpl.closepath ();//sanitycheck ();
}
}

The method call is as follows: The following document's Open is written before:

Set the headers and footers for each page of the PDF
Writer.setpageevent (New Headfootinfopdfpageevent ("header name", Stolist.get (0). GetDate ("Stsu_data"). ToString (), "in", Stsu.getstr ("Stsu_code"));

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.