public class demo { public static void put(int pagecount,int index,int location) { Rectangle rectangle = new Rectangle(PageSize.A4); rectangle.setBackgroundColor(Color.LIGHT_GRAY); rectangle.setBorder(Rectangle.BOX); rectangle.setBorderColor(Color.DARK_GRAY); rectangle.setBorderWidth(5); Document doc =new Document(rectangle); try { PdfReader reader =new PdfReader("E://test.pdf"); PdfWriter write =PdfWriter.getInstance(doc,new FileOutputStream("E://test.pdf")); BaseFont bfChinese =BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); Font fontchinese = new Font(bfChinese, 16, Font.NORMAL); doc.open(); PdfContentByte cb = write.getDirectContent(); cb.beginText(); cb.setFontAndSize(bfChinese, 12); cb.showTextAligned(0, "text" + "This text is centered", 250, 700, 0); cb.endText(); doc.add(new Chunk("Chapter 1").setLocalDestination("1")); for(int i=2;i<=pagecount;i++){ doc.newPage(); doc.add(new Chunk("Chapter "+i).setLocalDestination(i+"")); if(i==index){ Paragraph p1= new Paragraph("在"+index+"页加入内容",fontchinese); p1.setAlignment(location); // center 1;left 0;right 2;bottom 6; doc.add(p1); Paragraph p2= new Paragraph("在"+index+"页加入内容",fontchinese); p2.setAlignment(location); // center 1;left 0;right 2;bottom 6; doc.add(p2); Paragraph p3= new Paragraph("在"+index+"页加入内容",fontchinese); p2.setAlignment(location); // center 1;left 0;right 2;bottom 6; doc.add(p3); } } }catch (Exception e){ e.printStackTrace(); }finally { doc.close(); } }
测试数据
public static void main(String[] args) { demo.put(5,2,1); //int pagecount,int index,int location } }