Java/PDF RTF/Page Footer Header

Материал из Java эксперт
Перейти к: навигация, поиск

Adding Page Footer

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
public class PageFooterPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 70, 70);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("PageFooterPDF.pdf"));
      document.open();
      document.add(new Paragraph("text"));
      Rectangle page = document.getPageSize();
      PdfPTable foot = new PdfPTable(1);
      foot.addCell("Page 1");
      foot.setTotalWidth(page.width() - document.leftMargin() - document.rightMargin());
      foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
            writer.getDirectContent());
      document.close();
    } catch (Exception de) {
      de.printStackTrace();
    }
  }
}





Adding Page Header

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
public class PageHeaderPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A4, 50, 50, 70, 70);
    try {
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("PageHeaderPDF.pdf"));
      document.open();
      document.add(new Paragraph("text"));
      Rectangle page = document.getPageSize();
      PdfPTable head = new PdfPTable(1);
        head.addCell("Page 1");
      head.setTotalWidth(page.width() - document.leftMargin() - document.rightMargin());
      head.writeSelectedRows(0, -1, document.leftMargin(), page.height() - document.topMargin()
          + head.getTotalHeight(), writer.getDirectContent());
      document.close();
    } catch (Exception de) {
      de.printStackTrace();
    }
  }
}