ITEXT 頁首頁尾頁碼三件套
問題場景
利用itext寫PDF的過程中遇到生成頁首頁尾頁碼的問題,記錄下。
整體思路
- 編寫相應事件
- 在適當位置新增事件
效果
CODE
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
/**
* @author youxingyang
* @Date 2017-5-9 下午1:15:46
*/
public class pdfPage {
//頁碼事件
private static class PageXofYTest extends PdfPageEventHelper{
/** The PdfTemplate that contains the total number of pages. */
public PdfTemplate total;
public BaseFont bfChinese;
/**
* 重寫PdfPageEventHelper中的onOpenDocument方法
*/
@Override
public void onOpenDocument(PdfWriter writer, Document document) {
// 得到文件的內容併為該內容新建一個模板
total = writer.getDirectContent().createTemplate(500, 500);
try {
String prefixFont = "";
String os = System.getProperties().getProperty("os.name");
if(os.startsWith("win") || os.startsWith("Win")){
prefixFont = "C:\\Windows\\Fonts" + File.separator;
}else {
prefixFont = "/usr/share/fonts/chinese" + File.separator;
}
// 設定字型物件為Windows系統預設的字型
bfChinese = BaseFont.createFont(prefixFont + "simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
/**
* 重寫PdfPageEventHelper中的onEndPage方法
*/
@Override
public void onEndPage(PdfWriter writer, Document document) {
// 新建獲得使用者頁面文字和圖片內容位置的物件
PdfContentByte pdfContentByte = writer.getDirectContent();
// 儲存圖形狀態
pdfContentByte.saveState();
String text = writer.getPageNumber() + "/";
// 獲取點字串的寬度
float textSize = bfChinese.getWidthPoint(text, 9);
pdfContentByte.beginText();
// 設定隨後的文字內容寫作的字型和字號
pdfContentByte.setFontAndSize(bfChinese, 9);
// 定位'X/'
float x = (document.right() + document.left()) / 2;
float y = 56f;
pdfContentByte.setTextMatrix(x, y);
pdfContentByte.showText(text);
pdfContentByte.endText();
// 將模板加入到內容(content)中- // 定位'Y'
pdfContentByte.addTemplate(total, x + textSize, y);
pdfContentByte.restoreState();
}
/**
* 重寫PdfPageEventHelper中的onCloseDocument方法
*/
@Override
public void onCloseDocument(PdfWriter writer, Document document) {
total.beginText();
try {
String prefixFont = "";
String os = System.getProperties().getProperty("os.name");
if(os.startsWith("win") || os.startsWith("Win")){
prefixFont = "C:\\Windows\\Fonts" + File.separator;
}else {
prefixFont = "/usr/share/fonts/chinese" + File.separator;
}
bfChinese = BaseFont.createFont(prefixFont + "simsun.ttc,0",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
total.setFontAndSize(bfChinese, 9);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
total.setTextMatrix(0, 0);
// 設定總頁數的值到模板上,並應用到每個介面
total.showText(String.valueOf(writer.getPageNumber() - 1));
total.endText();
}
}
//頁首事件
private static class Header extends PdfPageEventHelper {
public static PdfPTable header;
public Header(PdfPTable header) {
Header.header = header;
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
//把頁首表格定位
header.writeSelectedRows(0, -1, 36, 806, writer.getDirectContent());
}
/**
* 設定頁首
* @param writer
* @param req
* @throws MalformedURLException
* @throws IOException
* @throws DocumentException
*/
public void setTableHeader(PdfWriter writer) throws MalformedURLException, IOException, DocumentException {
String imageAddress = "E://TESTPDF/";
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(555);
PdfPCell cell = new PdfPCell();
cell.setBorder(0);
Image image01;
image01 = Image.getInstance(imageAddress + "testhead.png"); //圖片自己傳
//image01.scaleAbsolute(355f, 10f);
image01.setWidthPercentage(80);
cell.setPaddingLeft(30f);
cell.setPaddingTop(-20f);
cell.addElement(image01);
table.addCell(cell);
Header event = new Header(table);
writer.setPageEvent(event);
}
}
//頁尾事件
private static class Footer extends PdfPageEventHelper {
public static PdfPTable footer;
@SuppressWarnings("static-access")
public Footer(PdfPTable footer) {
this.footer = footer;
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
//把頁尾表格定位
footer.writeSelectedRows(0, -1, 38, 50, writer.getDirectContent());
}
/**
* 頁尾是圖片
* @param writer
* @throws MalformedURLException
* @throws IOException
* @throws DocumentException
*/
public void setTableFooter(PdfWriter writer) throws MalformedURLException, IOException, DocumentException {
String imageAddress = "E://TESTPDF/";
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(523);
PdfPCell cell = new PdfPCell();
cell.setBorder(1);
Image image01;
image01 = Image.getInstance(imageAddress + "testfooter.png"); //圖片自己傳
image01.scaleAbsoluteWidth(523);
image01.scaleAbsoluteHeight(30f);
image01.setWidthPercentage(100);
cell.addElement(image01);
table.addCell(cell);
Footer event = new Footer(table);
writer.setPageEvent(event);
}
/**
* 頁尾是文字
* @param writer
* @param songti09
*/
public void setTableFooter(PdfWriter writer, Font songti09) {
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(520f);
PdfPCell cell = new PdfPCell();
cell.setBorder(1);
String string = "地址: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 網址: www.xxxxxxx.com 諮詢熱線: 400x-xxx-xxx";
Paragraph p = new Paragraph(string, songti09);
cell.setPaddingLeft(10f);
cell.setPaddingTop(-2f);
cell.addElement(p);
table.addCell(cell);
Footer event = new Footer(table);
writer.setPageEvent(event);
}
}
public static void main(String[] args) throws Exception {
Document document = new Document(PageSize.A4, 48, 48, 60, 65);
// add index page.
String path = "test.pdf";
String dir = "E://TEST";
File file = new File(dir);
if (!file.exists()) {
file.mkdir();
}
path = dir + File.separator + path;
FileOutputStream os = new FileOutputStream(path);
PdfWriter writer = PdfWriter.getInstance(document, os);
// 設定頁面佈局
writer.setViewerPreferences(PdfWriter.PageLayoutOneColumn);
// 為這篇文件設定頁面事件(X/Y)
writer.setPageEvent(new PageXofYTest());
String prefixFont = "";
String oss = System.getProperties().getProperty("os.name");
if(oss.startsWith("win") || oss.startsWith("Win")){
prefixFont = "C:\\Windows\\Fonts" + File.separator;
}else {
prefixFont = "/usr/share/fonts/chinese" + File.separator;
}
BaseFont baseFont1 = BaseFont.createFont(prefixFont + "simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font songti09 = new Font(baseFont1, 9f); //宋體 小五
document.open();
document.newPage();
PdfPTable pdfPTable = new PdfPTable(1);
// 為報告新增頁首,事件的發生是在生成報告之後,寫入到硬碟之前
Header headerTable = new Header(pdfPTable);
headerTable.setTableHeader(writer);
Footer footerTable = new Footer(pdfPTable);
footerTable.setTableFooter(writer, songti09);
document.add(pdfPTable);
for (int i = 0; i < 36; i++) {
document.add(new Paragraph("the first page"));
}
document.newPage();
document.add(new Paragraph("the second page"));
document.close();
os.close();
}
}
相關推薦
ITEXT 頁首頁尾頁碼三件套
問題場景 利用itext寫PDF的過程中遇到生成頁首頁尾頁碼的問題,記錄下。 整體思路 編寫相應事件 在適當位置新增事件 效果 CODE import java.io.File; import java.io.FileOutp
前端匯出多頁pdf 帶目錄 頁首 頁尾及頁碼
前段時間公司釋出新需求,要求使用者點選按鈕可以匯出pdf或者html到本地,pdf中要包含可點選跳轉的目錄,要分頁記錄頁碼,還有頁首和頁尾,和後臺的小哥哥配合試了好多方法,最終完成的效果還不錯,在這裡做個記錄。 需求 點選匯出html報告,則匯出html檔案到本地 點選
iText把jsp/html轉pdf,並支援新增頁首頁尾
公司的相關業務需要匯出pdf,找遍了各大網站論壇,然後自己又總結融合了不少其他程式碼,吃過不少虧,特把全部原始碼扔上去供大家做個參考,鄙人研發兩年還屬於小菜階段.程式碼不嚴謹的地方還望指正; 為了給pdf直接加上頁首頁尾,直接重寫了ITextRenderer 下面直接上原始
【Itext】7步製作Itext5頁首頁尾pdf實現第幾頁共幾頁
/** * Project Name:report * File Name:PdfReportM1HeaderFooter.java * Package Name:com.riambsoft.report.pdf * Date:2013-9-16上午08:59:00 * Copyright (c)
Itext筆記之pdf頁首頁尾
效果展示: 原始碼: public class App { public static void main( String[] args ) throws Exception { Document document = new Docum
C# 操作word總結(一)——建立文件和新增頁首頁尾
最近程式中經常使用到word的操作,我在網上查了一些資料,在這裡整理一下。 使用程式碼建立word文件: #region 新建Word文件 /// <summary> /// 動態生成Word文件並填充內容 /// </summa
iText + Freemarker實現pdf的匯出,支援中文、css以及圖片,頁首頁尾,頁首新增圖片
本文為了記錄自己的實現過程,具體的實現步驟是參考博文 https://www.cnblogs.com/youzhibing/p/7692366.html 來實現的,只是在他的基礎上添加了頁首頁尾及頁首圖片 原來是決定採用wk
PDF如何刪除頁首頁尾,編輯PDF的軟體怎麼用
PDF編輯工具是可以用來修改編輯PDF檔案的,PDF檔案的使用在工作中用的很多,那麼,PDF如何刪除頁首頁尾,編輯PDF的軟體怎麼用呢,估計有很多的小夥伴都想知道應該怎麼操作吧,那就看看下面的文章,沒準就會了哦。 1.開啟並執行迅捷PDF編輯器http://bianji.xjpdf.com/,在
PDF檔案頁首頁尾設定介紹
現在很多人對PDF文件的頁首頁尾設定不是很瞭解,不知道怎麼給我們的PDF文件新增頁首頁尾,今天給大家帶來的是PDF檔案頁首頁尾設定介紹,給PDF檔案新增頁首頁尾能夠方便我們檢視檔案,但PDF檔案是一種特殊的檔案格式,想要設定PDF檔案的頁首頁尾需要掌握特殊的方法,接下來就讓我們一起來學習吧。 操作軟體:
用PDF編輯軟體怎樣新增頁首頁尾
在很多的時候,都會使用到PDF檔案,工作中,學習中都會使用的到,而PDF檔案的修改編輯是需要用到PDF編輯軟體的,那麼,用PDF編輯軟體怎樣新增頁首頁尾呢,是不是有很多的小夥伴也想知道應該怎麼操作呢,那就看看下面的文章,沒準就會了哦。 1.開啟執行迅捷PDF編輯器,在編輯器中開啟需要修改的PDF
PDF頁首頁尾刪除用什麼方法
PDF頁首頁尾刪除用什麼方法。相信每一個小夥伴都在網路上下載過PDF檔案,當我們開啟PDF檔案後,發現檔案裡有著我們不需要的頁首與頁尾,我們應該如何將其刪除掉呢?如果螢幕前的你不清楚的話,那就和小編一起學習學習吧。 操作軟體:迅捷PDF編輯器 工具下載地址:https://www.xunjiepdf.
C# 調整Word頁首頁尾的位置高度
在Word中頁首頁尾有一個預設的高度,我們在製作Word文件時有時需要調整頁首頁尾的高度,這篇文章將介紹如何使用免費.NET Word元件Free Spire.Doc和C#調整Word頁首頁尾的位置高度。 在使用程式碼前,需要下載安裝Free Spire.Doc,並從安裝資料夾下引用Spir
利用PDF頁首頁尾及PDF加水印保護你的作品
在越來越注重保護智慧財產權的今天,在檔案上新增原創者的個人資訊就是宣誓“版權”的一個重要途徑。在PDF檔案中宣誓“版權”,最方便的就是在頁首上新增公司名,或者是給PDF加水印。 話不多說,開始我們今天的教程! 首先需要一款福昕PDF編輯器。 PDF頁首 用PDF編輯器開啟檔案。然後點選頂部選單欄“
PDF新增頁首頁尾用什麼工具
PDF新增頁首頁尾用什麼工具?我們在工作中所使用的各式各樣的檔案大多都會新增上頁首與頁尾,比如excel、word、PDF檔案等等,因為這樣會方便我們閱讀查閱檔案。相信excel和word檔案如何新增頁首頁尾大家都很清楚,但PDF檔案卻有可能許多朋友不知道怎麼新增頁首頁尾。所以今天小編就給大家介紹一下PDF新
jquery.PrintArea 區域性列印 去掉頁首頁尾,但在IE有些版本中不管用。
修改jquery.PrintArea.js 檔案中的getFormDate 函式 function getFormData(ele) { $("input,select,texta
PDF能編輯嗎,怎麼刪除PDF頁首頁尾
在很多的時候,大家都會使用到PDF檔案,對於PDF檔案的修改,則是需要使用到PDF編輯軟體的,不管是在學習中還是在工作中,都會使用到PDF檔案,那麼,PDF能編輯嗎,怎麼刪除PDF頁首頁尾呢,是不是有很多的小夥伴都想知道應該怎麼做呢,那就可以看看下面的
PDF怎麼修改,如何刪除PDF頁首頁尾
不管是在學習中還是在工作中,都會使用到PDF檔案,在很多的時候,大家都會使用到PDF檔案,對於PDF檔案的修改,則是需要使用到PDF編輯軟體的,那麼,PDF怎麼修改,如何刪除PDF頁首頁尾呢,是不是有很多的小夥伴都想知道應該怎麼做呢,那就可以看看下面的文章,說不定
js頁面列印去掉頁首頁尾
列印的時候有 時間和請求的網址,客戶不希望有這樣的資訊,所以再head中新增<style type="text/css"> @page { margin: 0; }</style>設定前效果設定後效果
Word2003頁首頁尾處自動插入檔名稱與路徑
常見的頁首頁尾都是一些文字說明或者是數字頁碼,其實,Word2003中可以將檔名稱和所在的路徑插入到頁首頁尾裡面,而且是自動生成,無需我們手動輸入。下面,帶著大家感受一下。①首先啟動Word2003,單擊選單欄--檢視--頁首和頁尾。②將游標定位到頁首頁尾裡面,然後插入--自
word中如何插入一頁沒有頁首頁尾的空白頁
1、如果想在當前頁前加入一頁空白頁,只需在當前頁的頁首插入分隔符(頁面佈局--分隔符--下一頁); 2、然後點選當前頁的頁首頁尾,在“設計”中點選“連結到前一條頁首或頁尾”; 3、這時就可以刪除剛插入的空白頁中的頁首頁尾了。