itext 导出pdf

Document document = new Document(PageSize.A4, 20, 20, 20, 20); 

PdfWriter.getInstance(document, out);

document.open();

document.addCreationDate();

// 标题

Paragraph title = new Paragraph(project.getProjectName(), fontFirstTitle);

title.setAlignment(Element.ALIGN_CENTER);    // 对其方式

title.setSpacingAfter(20);    // 段落后的距离

title.setIndentationLeft(15);    // 左边距

title.setIndentationRight(15);    // 右边距

document.add(title);

Chunk chunk = new Chunk("1. 简介", fontSecTitle);

document.add(chunk);

document.add(paragraphFactory(project.getSimpleIntro(), fontContent));

chunk = new Chunk("2. 项目亮点", fontSecTitle);

document.add(chunk);

document.add(paragraphFactory(project.getProjectExcellent(), fontContent));

chunk = new Chunk("3. 产品介绍", fontSecTitle);

document.add(chunk);

Paragraph productInfo = paragraphFactory(project.getProductState(), fontContent);

productInfo.setSpacingAfter(0);

document.add(productInfo);

productInfo = paragraphFactory(project.getProductConsumer(), fontContent);

productInfo.setSpacingAfter(0);

document.add(productInfo);

document.add(paragraphFactory(project.getProductSolveProblem(), fontContent));

chunk = new Chunk("4. 团队介绍", fontSecTitle);

document.add(chunk);

// 添加负责人

PdfPTable masterTable = new PdfPTable(new float[]{10f, 90f});

masterTable.setSpacingBefore(10f);  

masterTable.setWidthPercentage(95);

masterTable.getDefaultCell().setBorder(0);

//负责人头像

Image img = null;

try {

img = Image.getInstance(new URL("http:" + project.getMasterImage()));

} catch (Exception e) {

}

PdfPCell imageCell = pdfCellFactory(null, fontContent);

imageCell.setRowspan(2);

if (img != null) {

img.setAlignment(Image.LEFT);

imageCell.setImage(img);

}

masterTable.addCell(imageCell);

//负责人姓名

PdfPCell other = pdfCellFactory(project.getMasterName(), fontSecTitle);

masterTable.addCell(other);

//负责人职位

other.setPhrase(new Phrase("创始人",fontContent));

other.setVerticalAlignment(Element.ALIGN_BOTTOM);

other.setPaddingBottom(5f);

masterTable.addCell(other);

document.add(masterTable);

//负责人简介

document.add(paragraphFactory("简介:" + project.getMasterIntro(), fontContent));

// 团队成员

if (teamDOList != null && !teamDOList.isEmpty()) {

// 表格列数,每一列的宽度比例

PdfPTable teamTable = new PdfPTable(new float[]{10f, 20f, 10f, 20f, 10f, 30f});

teamTable.setSpacingBefore(20f);  // 段落前距离

teamTable.setWidthPercentage(95); // 表格宽度,百分比

teamTable.getDefaultCell().setBorder(0);

// 成员头像

Image teamImg = null;

PdfPCell teamImageCell = pdfCellFactory(null, fontContent);

teamImageCell.setRowspan(2);

// 成员信息

PdfPCell teamInfoCell = pdfCellFactory(null, fontSecTitle);

teamInfoCell.setVerticalAlignment(Element.ALIGN_BOTTOM);

teamInfoCell.setPaddingBottom(5f);

for (ContestProjectTeamDO team : teamDOList) {

try {

teamImg = Image.getInstance(new URL("http:" + team.getMemberImage()));

} catch (Exception e) {

}

if (teamImg != null) {

teamImg.setAlignment(Image.LEFT);

teamImageCell.setImage(teamImg);

}

teamTable.addCell(teamImageCell);

teamInfoCell.setPhrase(new Phrase(team.getMemberName() + "\n\n" + team.getMemberIntro(), fontContent));

teamTable.addCell(teamInfoCell);

}

int size = teamDOList.size();

if ((size = 3 - size % 3) > 0 ) {

for (int i=0; i <= size; i++) {

teamTable.addCell("");

teamTable.addCell("");

}

}

document.add(teamTable);

}

        // 团队亮点

Paragraph team = paragraphFactory("团队亮点:" + project.getProjectExcellent(), fontContent);

document.add(team);

        chunk = new Chunk("5. 运营状况", fontSecTitle);

document.add(chunk);

document.add(paragraphFactory(project.getOperationStatus(), fontContent));

chunk = new Chunk("6. 市场分析", fontSecTitle);

document.add(chunk);

Paragraph f = paragraphFactory(project.getProjectFuture(), fontContent);

f.add("\n");

f.add(project.getBusinessPattern());

document.add(f);

chunk = new Chunk("7. 竞争分析", fontSecTitle);

document.add(chunk);

Paragraph o = paragraphFactory(project.getOpponentAnalyze(), fontContent);

o.add("\n");

o.add(project.getProductMerit());

document.add(o);

chunk = new Chunk("8. 融资信息", fontSecTitle);

document.add(chunk);

String cf = "\n", ef = "", st = "";

if (StringUtil.isGreatOne(project.getCurrentFinance())

&& StringUtil.isGreatOne(project.getStock()) ) {

cf = project.getCurrentFinance() + "万元\n";

ef = "融资额   :" + project.getExpectFinance() + "万元\n";

st = "出让股份  :" + project.getStock() + "%\n";

} else {

ef = "融资额   :\n";

st = "出让股份  :\n";

}

Paragraph finance = paragraphFactory("融资前估值 :" + cf, fontContent);

finance.add(ef);

finance.add(st);

finance.add(project.getFinanceSpend());

document.add(finance);

chunk = new Chunk("9. 产品动态", fontSecTitle);

document.add(chunk);

if (doList != null && !doList.isEmpty()) {

float[] widths = {18f, 18f, 85f};

       PdfPTable ptable = new PdfPTable(widths);

       ptable.setSpacingBefore(20f);  

       ptable.setWidthPercentage(95);

       ptable.getDefaultCell().setBorder(0);

       

       String time;

       PdfPCell pcell = null;

for (ContestProjectRoadDO road : doList) {

time = GeneralUtils.monthFormat.format(road.getTime());

pcell = pdfCellFactory(time, fontContent);

ptable.addCell(pcell);

pcell.setPhrase(new Phrase(road.getType(), fontContent));

//pcell = pdfCellFactory(road.getType(), fontContent);

ptable.addCell(pcell);

//pcell = pdfCellFactory(road.getContent(), fontContent);

pcell.setPhrase(new Phrase(road.getContent(), fontContent));

ptable.addCell(pcell);

}

document.add(ptable);

}

        

document.close();

}

private static Paragraph paragraphFactory(String content, Font font) {

Paragraph paragraph = new Paragraph(content, font);

paragraph.setIndentationLeft(16);

paragraph.setSpacingBefore(10);

paragraph.setSpacingAfter(20);

return paragraph;

}

private static PdfPCell pdfCellFactory(String content, Font font) {

//pcell = new PdfPCell(new Paragraph("姓名", fontContent));

PdfPCell pcell = new PdfPCell(new Phrase(content, font));

pcell.setHorizontalAlignment(Element.ALIGN_LEFT);

pcell.setBorder(0);

return pcell;

}


你可能感兴趣的:(itext 导出pdf)