VVL1295

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
public class Test {
    public static void main(String[] args) throws IOException, DocumentException { 
        //定义pdf文件的高和宽,以像素为单位
        float width = 595.27559f,height = 419.52755f;
        //定义pdf文件
        Document document = new Document(new Rectangle(width, height), 0, 0, 0, 0);
        //定义要写出的路径
        OutputStream output = new FileOutputStream("D:"+File.separator+"test8.pdf");
        //定义writer
        PdfWriter writer = PdfWriter.getInstance(document, output);
        //打开pdf文件(不可少)
        document.open();
        //新建一页
        document.newPage();
        //读入pdf
        PdfReader reader = new PdfReader("D:"+File.separator+"test7.pdf");
        //读入文件的一页
        PdfImportedPage page = writer.getImportedPage(reader, 1);
        //用image接收读入文件的一页
        Image image = Image.getInstance(page);
        //设置image的坐标
        image.setAbsolutePosition(0, 0);
        //把image加进文件,也就是把读入的pdf文件的一页复制粘贴到被编辑文件
        document.add(image);
        //获得PdfContentByte对象
        PdfContentByte contentByte = writer.getDirectContent();
        //定义PdfGraphics2D对象,用于对文件进行编辑(drawXXX)
        PdfGraphics2D pdfG = new PdfGraphics2D(contentByte, width, height);
        //在坐标(0,0)加上字符串"haha"
        pdfG.drawString("haha", 0, 0);
        //关闭文件,也就是文件被处理完毕,真正写到指定的写出路径
        document.close();
        
    }  
        
}

 

posted on 2016-09-17 20:51  bobo2018  阅读(738)  评论(0)    收藏  举报