滴水穿石

编程入门 行业动态 更新时间:2024-10-25 12:28:07

滴水穿石

滴水穿石

iText默认是不支持中文的,处理中文需要用到itext-asian.jar包,关键代码:

处理中文:

BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);    
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
添加图片:
Image image = Image.getInstance("@sf/logo_itext.gif"); 
完成实例代码:
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
public class TestPdf { public static void main(String[] args) throws Exception{//step1  Document document = new Document();  //step2  PdfWriter.getInstance(document, new FileOutputStream("first.pdf"));  //step3  
        document.open();  //step4  BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);      Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);  Paragraph p = new Paragraph("Hello World,this is my first pdf!这是我的第一个pdf实例",fontChinese);document.add(p);  //添加一个图片  Image image = Image.getInstance("@sf/logo_itext.gif");  document.add(image);  //step5 
        document.close();   }
} 
运行效果图:

转载于:.html

更多推荐

滴水穿石

本文发布于:2024-02-25 15:50:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1699502.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:滴水穿石

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!