public class PdfC01 { /** * @author Helen * @date 2015年7月17日 上午9:35:54 */ public static void main(String[] args) { com.lowagie.text.Document document = new com.lowagie.text.Document(); try { PdfWriter.getInstance(document, new FileOutputStream("C:\\first.pdf")); document.open(); document.add(new Paragraph("PDF Document!")); document.add(new Paragraph("Author:yan.")); document.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } } }
public class PdfC02 { /** * @date 2015年7月17日 上午10:04:25 */ public static void main(String[] args) { Document document=new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("C:\\002.pdf")); document.addTitle("Title Info."); document.addSubject("For learning"); document.addKeywords("iText"); document.addAuthor("yan"); document.addCreator("Samgung SDS"); document.open(); document.add(new Paragraph("Content.")); document.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } } }
public class Pdf03C { /** * @date 2015年7月17日 上午10:52:12 */ public static void main(String[] args) { Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("C:\\003.pdf")); Rectangle pageSize=new Rectangle(200, 100); pageSize=pageSize.rotate(); document.setPageSize(pageSize); document.open(); document.add(new Paragraph("PageSize:200*100")); document.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } } }
public class Pdf04C { /** * @date 2015年7月17日 上午11:19:46 */ public static void main(String[] args) { try { PdfReader reader = new PdfReader("C:\\first.pdf"); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("C:\\first_pre.pdf")); PdfContentByte head = stamp.getOverContent(1); head.setTextRise(800); head.beginText(); BaseFont chinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); head.setFontAndSize(chinese, 16); head.showText("页眉"); head.endText(); stamp.insertPage(2, PageSize.A4); PdfContentByte bottom = stamp.getUnderContent(2); bottom.setTextRise(20); bottom.beginText(); bottom.setFontAndSize(chinese, 16); bottom.showText("页脚"); bottom.endText(); stamp.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
-
public PdfContentByte getOverContent(int pageNum),获得指定页面的内容。
-
public void setTextRise(float rise),使当前文本位置上移,如果将参数设置为小数,则必须使用标准的单精度表示(如100.5f)
-
使用beginText()标记文本开始,使用showText(String text)设置页眉页脚的显示内容,使用endText()标记文本结束。