iText G (Android)PDF文本定位書寫兩種方式介紹

閒言少敘,直接進入話題(相關基礎知識不瞭解,請使用百度之類惡補),有不當之處,請高人批准指正!
方法一(絕對定位),參考形如:
PdfContentByte pdfContentByte = pdfWriter.getDirectContent();
BaseFont chineseFontNormal = BaseFont.createFont(“STSong-Light”, “UniGB-UCS2-H”, BaseFont.EMBEDDED);
pdfContentByte.beginText();
pdfContentByte.setFontAndSize(chineseFontNormal, 16);
//text absolute location
pdfContentByte.showTextAligned(PdfContentByte.ALIGN_CENTER, “你好嗎?”, 298, 730, 0);//X=298磅,Y=730磅,0爲旋轉角度
//font and fontsize
pdfContentByte.setFontAndSize(chineseFontNormal, 22);
…………………………
pdfContentByte.endText();
//只能使用BaseFont定義的字體
方法二(相對定位),參考形如:
Font fontNormal = new Font(chineseFontNormal, 16);
Font fontBold = new Font(chineseFontNormal, 22, Font.BOLD);
Paragraph paragraph = new Paragraph(“你好嗎?”, fontNormal);
//不可直接使用BaseFont直接定義的字體
//1、上下空間控制
paragraph.setLeading(112);//設置離頁頂或上一行(Paragraph書寫的)距離(112磅),距離可以設定爲負值,效果自明
paragraph.setAlignment(Element.ALIGN_CENTER);//除center外,還可使用left、right
//用Paragraph時,除第一行外,其他行還可以用setSpaceBefore/After,效果請自行測試
//2、左右空間控制
paragraph.paragraph.setIndentationLeft(20);//還可以使用Right,20磅,此值也可以取負值,效果自行測試
………………………………
document.add(paragraph);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章