//字体 BaseFont baseFont = null; //路径 File path = new File("G:\123.pdf"); //document对象 设置纸张 四周边距 Document document = new Document(PageSize.A4, 50, 50, 50, 50); try { //设置字体 baseFont = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); Font ht = new Font(baseFont); baseFont = BaseFont.createFont("G:\framdit.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); Font st = new Font(baseFont); //创建书写器 PdfWriter pdfw = PdfWriter.getInstance(document,new FileOutputStream(path)); //设置水印 //Watermark watermark =new Watermark(Image.getInstance("G:\2.jpg"), 0f, 0f); //document.add(watermark); //--------------------------页眉 Phrase phrase = new Phrase(new Chunk("页眉")); //phrase.add(new Chunk("页眉")); HeaderFooter header = new HeaderFooter(phrase, false);//false不显示页码 //设置居左 0 居中1 居右2 header.setAlignment(0); //设置是否有边框 header.setBorder(Rectangle.BOTTOM); //写进文档 document.setHeader(header); //-----------------------页脚 HeaderFooter footer = new HeaderFooter(new Phrase("--"), new Phrase("--")); footer.setAlignment(1); footer.setBorder(Rectangle.NO_BORDER);//没有边框 document.setFooter(footer); //---------------------加密 pdfw.setEncryption(PdfWriter.STRENGTH128BITS, null, "123", PdfWriter.AllowPrinting); //文档属性 与文档是否打开无关 document.addTitle("标题"); document.addAuthor("作者"); document.addSubject("主题"); document.addKeywords("关键字"); document.addCreator("创建者"); //打开文档 document.open(); Paragraph paragraph = new Paragraph("123pdf示例",ST); paragraph.setAlignment(1); document.add(paragraph); document.newPage(); Table table = new Table(4);//设置4列 //设置占页面宽度比例 table.setWidth(100); //设置每列的宽度 int [] width = {25, 25, 25, 25}; table.setWidths(width); Cell cell = new Cell(new Paragraph()); //水平居中 cell.setHorizontalAlignment(Element.ALIGN_CENTER); //垂直居中 cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setColspan(4); cell.addElement(new Phrase("示例",ht)); table.addCell(cell); cell = new Cell(new Paragraph()); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase("名称",st)); table.addCell(cell); cell = new Cell(new Paragraph()); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setColspan(3); cell.addElement(new Phrase("名称",ST)); table.addCell(cell); cell = new Cell(new Paragraph()); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase("1",ST)); table.addCell(cell); cell = new Cell(new Paragraph()); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase("2",ST)); table.addCell(cell); cell = new Cell(new Paragraph()); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase("3",ST)); table.addCell(cell); cell = new Cell(new Paragraph()); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.addElement(new Phrase("4",ST)); table.addCell(cell); document.add(table); document.newPage(); String str = "在研发科普教具的过程后期,企业与学校教师与学生可以共同研发出新的产品品种。因为老产品是一个平台,在此平台上将开发出很多种,最终需要筛选出机中最具使用价值的新产品或科普教具。这就是课题的主要研发内容。企业研发人员是要把科普教具研发的思路通过科普教具的分析给学生们讲解,学生的提问与建议将研发人员启发,这就是研究开发的意外收获。只要去做这项工作,研究开发就会有收获。如果效果特别好,这种形式可以在今后大规模推广成为常态"; Paragraph para = new Paragraph(str,ST); document.add(para); } catch (Exception e) { e.printStackTrace(); } document.close();