引用spire.pdf.jar到java程序中,然后使用下面的代码,页码的位置可以自己调整
import com.spire.pdf.automaticfields.PdfCompositeFieldimport com.spire.pdf.automaticfields.PdfPageCountField
import com.spire.pdf.automaticfields.PdfPageNumberField
import com.spire.pdf.graphics.*
import java.awt.*
import java.awt.geom.Dimension2D
import java.awt.geom.Rectangle2D
public class AddPageNumbers {
public static void main(String[] args) {
//加载PDF文档
PdfDocument doc = new PdfDocument()
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\系统测试.pdf")
//创建字体
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN, 12), true)
//获取页面尺寸
Dimension2D pageSize = doc.getPages().get(0).getSize()
//初始化y坐标
float y = (float) pageSize.getHeight() - 72
//遍历文档中的页
for (int i = 0 i < doc.getPages().getCount() i++) {
//初始化页码域
PdfPageNumberField number = new PdfPageNumberField()
//初始化总页数域
PdfPageCountField count = new PdfPageCountField()
//创建复合域
PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.getBlack(), "第{0}页 共{1}页", number, count)
//设置复合域内文字对齐方式
compositeField.setStringFormat(new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top))
//测量文字大小
Dimension2D textSize = font.measureString(compositeField.getText())
//设置复合域的在PDF页面上的位置及大小
compositeField.setBounds(new Rectangle2D.Float((float) pageSize.getWidth() - (float) textSize.getWidth() - 80, y, (float) textSize.getWidth(), (float) textSize.getHeight()))
//将复合域添加到PDF页面
compositeField.draw(doc.getPages().get(i).getCanvas())
}
//保存为另外一个文档
doc.saveToFile("output/AddPageNumber.pdf")
}
}
效果:
java itext5.0.6怎么设置页脚/**
* ITextTest
* iText生成PDF加入列表,注释等内容,同时设置页眉和页脚及页码等。
*/
package com.labci.itext.test
import <a href="http://lib.csdn.net/base/17" class='replace_word' title="Java EE知识库" target='_blank' style='color:#df3434font-weight:bold'>Java</a>.awt.Color
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import com.lowagie.text.Annotation
import com.lowagie.text.Document
import com.lowagie.text.DocumentException
import com.lowagie.text.Font
import com.lowagie.text.HeaderFooter
import com.lowagie.text.List
import com.lowagie.text.ListItem
import com.lowagie.text.Phrase
import com.lowagie.text.Rectangle
import com.lowagie.text.pdf.BaseFont
import com.lowagie.text.pdf.PdfWriter
/**
* @author Bill Tu(tujiyue/iwtxokhtd)
* Jun 6, 2011[4:10:35 PM]
*
*/
public class ITextList {
private final static String RESULT_FILE="itext_list.pdf"
public static void main(String []args){
Document doc=new Document()
try {
PdfWriter.getInstance(doc, new FileOutputStream(RESULT_FILE))
BaseFont fontChinese=null
try {
fontChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED)//设置中文字体
} catch (IOException e) {
e.printStackTrace()
}
Font chinese = new Font(fontChinese, 10, Font.NORMAL)
/**
* HeaderFooter的第2个参数为非false时代表打印页码
* 页眉页脚中也可以加入图片,并非只能是文字
*/
HeaderFooter header=new HeaderFooter(new Phrase("这仅仅是个页眉,页码在页脚处",chinese),false)
//设置是否有边框等
// header.setBorder(Rectangle.NO_BORDER)
header.setBorder(Rectangle.BOTTOM)
header.setAlignment(1)
header.setBorderColor(Color.red)
doc.setHeader(header)
HeaderFooter footer=new HeaderFooter(new Phrase("-",chinese),new Phrase("-",chinese))
/**
* 0是靠左
* 1是居中
* 2是居右
*/
footer.setAlignment(1)
footer.setBorderColor(Color.red)
footer.setBorder(Rectangle.BOX)
doc.setFooter(footer)
/**
* 页眉页脚的设置一定要在open前设置好
*/
doc.open()
/**
* true:代表要排序,10代表序号与文字之间的间距
* false:代表不排序,则文字前的符号为"-"
*/
List itextList=new List(true,10)
/**
* 也可以改变列表的符号[可选]
* $$$$$$$$$$$
* 要改变列表符号时,上面的List构造方法第一参数值必须为false
* $$$$$$$$$$$
* 可以使用字符串,Chunk,Image等作列表符号,如下
*/
//itextList.setListSymbol("*")
ListItem firstItem=new ListItem("first paragraph")
ListItem secondItem=new ListItem("second paragraph")
ListItem thirdItem=new ListItem("third paragraph")
itextList.add(firstItem)
itextList.add(secondItem)
itextList.add(thirdItem)
doc.add(itextList)
//添加注释,注释有标题和内容,注释可以是文本,内部链接,外部链接,图片等
Annotation annotation=new Annotation("what's this?","it's a tree and it is not a big")
doc.add(annotation)
doc.close()
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (DocumentException e) {
e.printStackTrace()
}
}
}
public class JavaToWords {
/**
* word运行程序对象
*/
private ActiveXComponent word
/**
* 选定内容
* @return Dispatch 选定的范围或插入点
*/
public Dispatch select() {
return word.getProperty("Selection").toDispatch()
}
public void toWord(String seekView) {
Dispatch selection = select()
//设置页眉
if(seekView != null &&!"".equals(seekView)){
//取得活动窗体对象
Dispatch ActiveWindow = word.getProperty("ActiveWindow").toDispatch()
//取得活动窗格对象
Dispatch ActivePane = Dispatch.get(ActiveWindow,"ActivePane").toDispatch()
//取得视窗对象
Dispatch View = Dispatch.get(ActivePane, "View").toDispatch()
try{
Dispatch.put(View,"SeekView", "9")//设置页眉
Dispatch.put(selection,"Text",seekView)
Dispatch.put(View, "SeekView", "10")// 10是设置页脚
Dispatch.put(selection, "Text", seekView)//
}finally{
if(ActiveWindow != null ) ActiveWindow.safeRelease()
if(ActivePane != null ) ActivePane.safeRelease()
if(View != null ) View.safeRelease()
}
}
}
}
希望对你有些帮助,不过好像要下一个外部资源包,叫jacob 的,我也记的不太清楚了,应该是这个,你找找看
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)