1.绝对位置添加文本
a.第一种方式
cb.SetTextMatrix(x, y);
Phrase C_EDD = new Phrase(item, baseFont);
ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, C_EDD, x, y, 0);
b.绝对位置添加文本
cb.SetTextMatrix(x, y);
cb.ShowText(text);
2.相对位置添加文本
//第一行内容
doc.Add(new Chunk("This is first line"));
//换行
doc.Add(Chunk.NEWLINE);
//第二行内容
doc.Add(new Chunk("This is second line"));
3.表格操作
List multiLinePosition = acroFields.getFieldPositions("multiLine");
int page = multiLinePosition.get(0).page;
Rectangle rectangle = multiLinePosition.get(0).position;
float left = rectangle.getLeft();
float right = rectangle.getRight();
float top = rectangle.getTop();
float bottom = rectangle.getBottom();
//BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
Font font = new Font(baseFont, 12, Font.BOLD, BaseColor.BLACK);
PdfContentByte pdfContentByte = ps.getOverContent(page);
ColumnText columnText = new ColumnText(pdfContentByte);
Rectangle r = new Rectangle(left, bottom, right, top);
columnText.setSimpleColumn(r);
//FontFactory.getFont(FontFactory.COURIER, 20, Font.BOLD, BaseColor.RED)
Chunk chunk = new Chunk("梅雪争春未肯降,骚人搁笔费评章。梅须逊雪三分白,雪却输梅一段香。");
Paragraph paragraph = new Paragraph(12, chunk);
paragraph.setSpacingBefore(16);
columnText.addText(paragraph);
// 设置字体,如果不设置添加的中文将无法显示
paragraph.setFont(font);
columnText.addElement(paragraph);
columnText.go();
4.插入图片
PdfPTable hiddenTable = new PdfPTable(new float[]{65f,5f,30f});//隐藏边框
hiddenTable.getDefaultCell().disableBorderSide(1);
hiddenTable.getDefaultCell().disableBorderSide(2);
hiddenTable.getDefaultCell().disableBorderSide(4);
hiddenTable.getDefaultCell().disableBorderSide(8);//插入空单元
hiddenTable.addCell(blank);//插入图片
Image mouse = Image.getInstance(this.getClass().getClassLoader().getResource("static/mouse.png"));
mouse.scalePercent(15);
cell= newPdfPCell();
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.setImage(mouse);//去掉边框
cell.setBorder(Rectangle.NO_BORDER);
hiddenTable.addCell(cell);
cell.setPhrase(new Phrase("Right click to save!", boldFont));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
hiddenTable.addCell(cell);
// 加入pdfdocument.add(hiddenTable);