解决"STSong-Light' with 'UniGB-UCS2-H' is not recognized"问题

  iText5.x版本以上中的font和encoding文件都是从String RESOURCE_PATH = “com/itextpdf/text/pdf/fonts/”加载的,而itextasian1.5.x.jar的包名是com.lowagie.text.pdf.fonts, 包名不一致,导致路径错误。解决方法如下:
  1. 将itextasian1.5.x.jar解压,找到里面itextasian1.5.x/com里的lowagie文件,将lowagie该名为:itextpdf,如图(1)所示:

解决"STSong-Light' with 'UniGB-UCS2-H' is not recognized"问题_第1张图片 图(1)将itextasin解压,文件夹lowagie更名为: itextpdf
  2. 点击桌面右下角的【开始】–》cmd –》进入到itextasian文件夹,输入:  

jar cvf iTextAsian_1.5.2.jar com/itextpdf/text/pdf/fonts/*

  即可将文件夹itextasin 打包成 iTextAsian_1.5.2.jar。
  如图(2)所示:  

解决"STSong-Light' with 'UniGB-UCS2-H' is not recognized"问题_第2张图片 图(2) 在cmd窗口,进入到itextasian文件夹,使用打包命令:jar cvf iTextAsian_1.5.2.jar com/itextpdf/text/pdf/fonts/*
  3.使用itext.jar和itextasian.jar进行pdf操作的代码如下:
  //shuiyin.java

package com.pdf;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;


public class Shuiying {
    public static void main(String[] args) {
        Document document = new Document();
        try {
            PdfWriter.getInstance(document, new FileOutputStream("src/pdfwen/shuiyinTmp.pdf"));
            document.open();
            document.add(new Paragraph(" "));
            document.close();
            PdfReader reader = new PdfReader("src/pdfwen/shuiyinTmp.pdf");
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("src/pdfwen/shuiyin.pdf"));
            Image img = Image.getInstance("src/img/watermark.jpg");
            img.setAbsolutePosition(50, 385);
            PdfContentByte under = stamper.getUnderContent(1); //获得第一页的内容
            under.addImage(img); //添加图片,完成水印功能
            BaseFont chinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", 
                    BaseFont.NOT_EMBEDDED);
            under.beginText(); //标记文本开始
            under.setFontAndSize(chinese, 42);
            under.setTextMatrix(70,550);
            under.showText("下面是添加的水印图片.");
            under.endText(); //标记文本结束
            stamper.close(); //水印结束
            File file = new File("src/pdfwen/shuiyinTmp.pdf");
            file.delete(); //删除临时文件 
            System.out.print("添加水印成功!");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (DocumentException e1) {
            e1.printStackTrace();
        }catch (IOException e2) {
            e2.printStackTrace();
        }

    }

}

  效果如下:

解决"STSong-Light' with 'UniGB-UCS2-H' is not recognized"问题_第3张图片 图(4) 使用itext.jar和itextasian.jar 在pdf里添加文字与水印的效果
  JFreeChart和pdf操作完整代码:
   http://download.csdn.net/detail/sanqima/9371155

  修正之后的itextasian.jar:  http://download.csdn.net/detail/sanqima/9371147

你可能感兴趣的:(java,编码,Path,stong,UniGB)