ITextRenderer render = new ITextRenderer(); ITextFontResolver font = rendergetFontResolver(); try { fontaddFont(thisgetClass()getResource(PDF_FONT)toString() substring(5), BaseFontIDENTITY_H, BaseFontNOT_EMBEDDED); } catch (DocumentException e) { eprintStackTrace(); } catch (IOException e) { eprintStackTrace(); } StringBuffer xhtml = new StringBuffer(XHTML_HEAD); xhtmlappend(html); xhtmlappend(XHTML_FOOT); rendersetDocumentFromString(xhtmltoString()); renderlayout(); ByteArrayOutputStream os = new ByteArrayOutputStream(); rendercreatePDF(os); osclose(); 其中xhtml是一个包含了多个表单的HTML代码,现在要实现导出的PDF文件中一个表单占一页
import javaioIOException;
import javautilArrayList;
import javautilArrays;
import javautilList;
import comitextpdfawtgeomRectangle2DFloat;
import comitextpdftextpdfPdfReader;
import comitextpdftextpdfparserImageRenderInfo;
import comitextpdftextpdfparserPdfReaderContentParser;
import comitextpdftextpdfparserRenderListener;
import comitextpdftextpdfparserTextRenderInfo;
public class Demo {
private static String KEY_WORD = "TIME SUMMARY";
private static int i = 0;
static List<float[]> arrays = new ArrayList<float[]>();
static String sb ;
private static List<float[]> getKeyWords(String filePath) {
try {
PdfReader pdfReader = new PdfReader(filePath);
int pageNum = pdfReadergetNumberOfPages();
PdfReaderContentParser pdfReaderContentParser = new PdfReaderContentParser(
pdfReader);
for (i = 1; i < 2; i++) {
pdfReaderContentParserprocessContent(i, new RenderListener() {
@Override
public void renderText(TextRenderInfo textRenderInfo) {
String text = textRenderInfogetText(); // 整页内容
if (null != text && textcontains(KEY_WORD)) {
Float boundingRectange = textRenderInfo
getBaseline()getBoundingRectange();
sb = boundingRectangex+"--"+boundingRectangey+"---";
float[] resu = new float[3];
resu[0] = boundingRectangex;
resu[1] = boundingRectangey;
resu[2] = i;
arraysadd(resu);
}
}
@Override
public void renderImage(ImageRenderInfo arg0) {
// TODO Auto-generated method stub
}
@Override
public void endTextBlock() {
// TODO Auto-generated method stub
}
@Override
public void beginTextBlock() {
// TODO Auto-generated method stub
}
});
}
} catch (IOException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
return arrays;
}
public static void main(String[] args) {
List<float[]> ff = getKeyWords("C:XXXXpdf");
for(float[] f : ff){
Systemoutprintln(f[0]+"----"+f[1]+"-----"+f[2]);
}
Systemoutprintln(sb);
}
}
我有完整代码
下面是方法,直接调用
public static void sign(String beforePdfpath,String afterPdfpath,String pngpath) throws Exception {
// KeyStore ks = KeyStoregetInstance("pkcs12");
// ksload(new FileInputStream("demop12"),"123456"toCharArray()); //123456为私钥密码
// String alias = (String) ksaliases()nextElement();
// PrivateKey key = (PrivateKey) ksgetKey(alias, "123456"toCharArray());
// Certificate[] chain = ksgetCertificateChain(alias);
Logd("pdfd", "do-1");
PdfReader reader = new PdfReader(beforePdfpath); //源文件
Logd("pdfd", "do-1-01");
FileOutputStream fout = new FileOutputStream(afterPdfpath);
Logd("pdfd", "do-1-1");
PdfStamper stp = PdfStampercreateSignature(reader, fout, '\0');
PdfSignatureAppearance sap = stpgetSignatureAppearance();
// sapsetCrypto(key, chain, null, PdfSignatureAppearanceVERISIGN_SIGNED);
Logd("pdfd", "do-2");
sapsetReason("");
sapsetLocation("");
sapsetContact(">
插入文本时样式可以用Font来定义:
Document doc = new Document();
docopen();
Paragraph text = new Paragraph("your text", new Font(FontgetFamily("TIMES_ROMAN"), 18, FontBOLD));
textsetAlignment(ElementALIGN_CENTER);
docadd(text);
以前插入过PDF文档,是用PdfReader读取然后用PdfWriter插入。插入应该是一样的原理:
PdfWritergetImportedPage(new PdfReader(bufferdInputStream), pageNumber);
/ 新建一个字体,iText的方法
STSongStd-Light 是字体,在iTextAsianjar 中以property为后缀
UniGB-UCS2-H 是编码,在iTextAsianjar 中以cmap为后缀
H 代表文字版式是横版,相应的 V 代表竖版
/
以上就是关于ITextRenderer生成PDF,如何强制分页全部的内容,包括:ITextRenderer生成PDF,如何强制分页、如何使用Itext获取pdf文字的坐标、如何使用itext对PDF生成叠加签名等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)