程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> iText生成PDF在PdfTemplate中設置文字加粗,斜體

iText生成PDF在PdfTemplate中設置文字加粗,斜體

編輯:關於JAVA
 

由於PdfTemplate繼承於PdfContentByte類,可以使用PdfContentByte方法來進行操作

Document doc = new Document();PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(filepath));
doc.open();PdfContentByte cb = writer.getDirectContent();
創建template並加到PdfContentByte裡,template的狀態位置是采用matrix方式來設置

PdfTemplate template = cb.createTemplate(width, height);
cb.addTemplate(template, 1f, 0f, 0f, 1f, 0f, 0f);

關於矩形變換,詳細可以到wiki上學習 https://en.wikipedia.org/wiki/Transformation_matrix

添加文字

template.saveState();
template.beginText();
template.moveText(5f, 5f); //坐標位置是相對template的左下角計算的//template.setColorFill(color);
template.showText("Test Text");
template.endText();
template.stroke();
template.restoreState();
設置文字粗體和斜體,由於沒有招到設置的函數,采用其他方式來實現這個效果
換個想法可以根據matrix的思路用setTextMatrix方法來設置

// Text Matrix Paramfloat ta = 1f, tb = 0f, tc = 0f, td = 1f, tx = 0f, ty = 0f;
設置粗體,具體要多大多粗可以根據需要調整

ta = ta + 0.15f;
td = td + 0.05f;
ty = ty - 0.15f;
設置斜體,斜的角度也可以按需調整

tc = tc + 0.3f;
添加文字並設置加粗,斜體

template.saveState();
template.beginText();
template.moveText(5f, 5f);
template.setTextMatrix(ta, tb, tc, td, tx, ty);//這裡對文字進行變形以達到想要的效果
template.showText("Test Text");
template.endText();
template.stroke();
template.restoreState();

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved