package org.jeecg.modules.demo.st.controller;
import java.io.IOException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
public class PdfFontUtils {
private static BaseFont baseFont = null;
static{
try {
baseFont = BaseFont.createFont("C:/Windows/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Paragraph getFont(int type, String text){
Font font = new Font(baseFont);
if(1 == type){
font.setSize(16f);
font.setStyle(Font.BOLD);
} else if(2 == type){
font.setSize(16f);
font.setStyle(Font.BOLD);
} else if(3 == type){
font.setSize(14f);
font.setStyle(Font.BOLD);
} else if(4 == type){
font.setSize(14f);
} else if(5 == type){
font.setSize(10.5f);
} else if(6 == type){
font.setSize(10.5f);
} else {
font.setSize(10.5f);
}
Paragraph paragraph = new Paragraph(text, font);
if(1 == type){
paragraph.setAlignment(Paragraph.ALIGN_CENTER);
paragraph.setSpacingBefore(10f);
paragraph.setSpacingAfter(10f);
} else if(2 == type){
paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
paragraph.setSpacingBefore(2f);
paragraph.setSpacingAfter(2f);
} else if(3 == type){
paragraph.setSpacingBefore(2f);
paragraph.setSpacingAfter(1f);
} else if(4 == type){
paragraph.setSpacingBefore(2f);
paragraph.setSpacingAfter(2f);
} else if(5 == type){
paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
paragraph.setFirstLineIndent(24);
paragraph.setSpacingBefore(1f);
paragraph.setSpacingAfter(1f);
} else if(6 == type){
paragraph.setAlignment(Element.ALIGN_LEFT);
paragraph.setSpacingBefore(1f);
paragraph.setSpacingAfter(1f);
}
return paragraph;
}
}
package org.jeecg.modules.demo.st.controller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.jeecg.modules.demo.st.controller.PdfFontUtils;
import org.jeecg.modules.demo.st.entity.*;
import org.jeecg.modules.demo.st.service.IStClassService;
import org.jeecg.modules.demo.st.service.IStStudentService;
import org.jeecg.modules.demo.st.vo.ClassVo;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
public class CreatePdfText {
public static void main(String[] args) {
System.out.println("===========start=============");
try {
Document doc = createPdf("F:\\test.pdf");
doc.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("===========end=============");
}
public static Document createPdf(String outpath) throws DocumentException, IOException{
Rectangle rect = new Rectangle(PageSize.A4);
File saveDir = new File(outpath);
File dir = saveDir.getParentFile();
if (!dir.exists()) {
dir.mkdirs();
}
Document doc = new Document(rect);
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(outpath));
writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);
doc.addTitle("Title@wpixel");
doc.addAuthor("Author@wpixel");
doc.addSubject("Subject@wpixel");
doc.addKeywords("Keywords@wpixel");
doc.addCreator("Creator@wpixel");
doc.setMargins(40, 40, 40, 40);
doc.open();
return doc;
}
public static void createFile(Document doc,
StStudent stStudent,
ISysDictService sysDictService,
IStClassService stClassService,
ISysDepartService sysDepartService,
List<StStateJilu> stStateJilu,
List<StHonor> stHonor,
List<StPunish> stPunish ) throws DocumentException{
doc.add(PdfFontUtils.getFont(1, "学生信息"));
doc.add(PdfFontUtils.getFont(2, "学生详情信息"));
PdfPTable table = new PdfPTable(2);
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
PdfPCell cell;
cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "姓名:"+stStudent.getStName())));
cell.setColspan(1);
cell.setBorder(0);
table.addCell(cell);
String text = sysDictService.queryDictTextByKey("sex", stStudent.getSex());
cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "性别:"+text)));
cell.setColspan(1);
cell.setBorder(0);
table.addCell(cell);
String text2 = sysDepartService.queryByIdCollges(stStudent.getCollege());
cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "学院:"+text2)));
cell.setColspan(1);
cell.setBorder(0);
table.addCell(cell);
String text1 = stClassService.queryByIdClas(stStudent.getStClass());
cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "班级:"+text1)));
cell.setColspan(1);
cell.setBorder(0);
table.addCell(cell);
String text3 = sysDictService.queryDictTextByKey("nation", stStudent.getNationality());
cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "民族:"+text3)));
cell.setColspan(1);
cell.setBorder(0);
table.addCell(cell);
cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "身份证:"+stStudent.getStCard())));
cell.setColspan(1);
cell.setBorder(0);
table.addCell(cell);
cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "联系方式:"+stStudent.getPhone())));
cell.setColspan(1);
cell.setBorder(0);
table.addCell(cell);
cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "详细地址:"+stStudent.getFamilyAddress())));
cell.setColspan(1);
cell.setBorder(0);
table.addCell(cell);
cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "家长电话:"+stStudent.getPhoneOne())));
cell.setColspan(1);
cell.setBorder(0);
table.addCell(cell);
String text4 = sysDictService.queryDictTextByKey("st_status", stStudent.getStatus());
cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "在校情况:"+text4)));
cell.setColspan(1);
cell.setBorder(0);
table.addCell(cell);
String text9 = sysDictService.queryDictTextByKey("face", stStudent.getFace());
cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "政治面貌:"+text9)));
cell.setColspan(1);
cell.setBorder(0);
table.addCell(cell);
String text5 = sysDictService.queryDictTextByKey("single", stStudent.getSingle());
cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "单亲孤儿:"+text5)));
cell.setColspan(1);
cell.setBorder(0);
table.addCell(cell);
String text6 = sysDictService.queryDictTextByKey("difficult", stStudent.getDifficult());
cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "家庭困难级别:"+text6)));
cell.setColspan(1);
cell.setBorder(0);
table.addCell(cell);
cell.setBorder(Rectangle.NO_BORDER);
String text7 = sysDictService.queryDictTextByKey("psychological", stStudent.getPsychological());
cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "心理情况:"+text7)));
cell.setColspan(1);
cell.setBorder(0);
table.addCell(cell);
String text11 = sysDictService.queryDictTextByKey("whether", stStudent.getAcademicWarning());
cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "学业预警:"+text11)));
cell.setColspan(1);
cell.setBorder(0);
table.addCell(cell);
String text8 = sysDictService.queryDictTextByKey("delayedGraduation", stStudent.getDelayedGraduation());
cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "学籍状态:"+text8)));
cell.setColspan(1);
cell.setBorder(0);
table.addCell(cell);
doc.add(table);
PdfPTable table1 = new PdfPTable(2);
table1.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
PdfPCell cell1;
doc.add(PdfFontUtils.getFont(2, "离校记录"));
for (StStateJilu stStateJilu1: stStateJilu) {
cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "离校时间:"+stStateJilu1.getCreateTime())));
cell1.setColspan(1);
cell1.setBorder(0);
table1.addCell(cell1);
String text10 = sysDictService.queryDictTextByKey("st_status", stStateJilu1.getTheReason());
cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, ""+text10)));
cell1.setColspan(1);
cell1.setBorder(0);
table1.addCell(cell1);
cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "审批人:"+stStateJilu1.getFieldTwo())));
cell1.setColspan(1);
cell1.setBorder(0);
table1.addCell(cell1);
cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "离校原因:"+stStateJilu1.getFieldOne())));
cell1.setColspan(1);
cell1.setBorder(0);
table1.addCell(cell1);
cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "归校时间:"+stStateJilu1.getDepartureTime())));
cell1.setColspan(1);
cell1.setBorder(0);
table1.addCell(cell1);
cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "登记人:"+stStateJilu1.getSort())));
cell1.setColspan(1);
cell1.setBorder(0);
table1.addCell(cell1);
}
doc.add(table1);
PdfPTable table2 = new PdfPTable(2);
table2.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
PdfPCell cell2;
doc.add(PdfFontUtils.getFont(2, "荣誉记录"));
for (StHonor stHonor1: stHonor ) {
cell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "荣誉名称:"+stHonor1.getName())));
cell2.setColspan(1);
cell2.setBorder(0);
table2.addCell(cell2);
String text12 = sysDictService.queryDictTextByKey("types", stHonor1.getTypes());
cell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, ""+text12)));
cell2.setColspan(1);
cell2.setBorder(0);
table1.addCell(cell2);
String text14 = sysDictService.queryDictTextByKey("reward",stHonor1.getReward());
cell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "类别:"+text14)));
cell2.setColspan(1);
cell2.setBorder(0);
table2.addCell(cell2);
String text15 = sysDictService.queryDictTextByKey("personal",stHonor1.getPersonal());
cell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, ""+text15)));
cell2.setColspan(1);
cell2.setBorder(0);
table2.addCell(cell2);
String text16 = sysDictService.queryDictTextByKey("experience",stHonor1.getExperience());
cell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, ""+text16)));
cell2.setColspan(1);
cell2.setBorder(0);
table2.addCell(cell2);
String text13 = sysDictService.queryDictTextByKey("review", stHonor1.getReview());
cell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, ""+text13)));
cell2.setColspan(1);
cell2.setBorder(0);
table1.addCell(cell2);
cell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "审批人:"+stHonor1.getYearTwo())));
cell2.setColspan(1);
cell2.setBorder(0);
table2.addCell(cell2);
cell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "时间:"+stHonor1.getYearOne())));
cell2.setColspan(1);
cell2.setBorder(0);
table2.addCell(cell2);
}
doc.add(table2);
PdfPTable table3 = new PdfPTable(2);
table3.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
PdfPCell cell3;
doc.add(PdfFontUtils.getFont(2, "处分记录"));
for (StPunish stPunish1: stPunish ) {
cell3 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "处分时间:"+stPunish1.getPunishmentTime())));
cell3.setColspan(1);
cell3.setBorder(0);
table3.addCell(cell3);
String text16 = sysDictService.queryDictTextByKey("punish", stPunish1.getPunish());
cell3 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, ""+text16)));
cell3.setColspan(1);
cell3.setBorder(0);
table3.addCell(cell3);
cell3 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "审批人:"+stPunish1.getApprover())));
cell3.setColspan(1);
cell3.setBorder(0);
table3.addCell(cell3);
cell3 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "处分原因:"+stPunish1.getTheReason())));
cell3.setColspan(1);
cell3.setBorder(0);
table3.addCell(cell3);
}
doc.add(table3);
}
}
@AutoLog(value = "生成学生的dpf")
@ApiOperation(value = "生成学生的dpf", notes = "生成学生的dpf")
@GetMapping(value = "/getPDF")
public Result<?> getPDF(@RequestParam(required = true) String stId) {
System.out.println("===========start=============");
try {
Document doc = createPdf("F:\\test.pdf");
StStudent student=stStudentService.getById(stId);
List<StStateJilu> stStateJilu = stStateJiluService.getRecording(stId);
List<StHonor> stHonor = stHonorService.getStudentHonors(stId);
List<StPunish> stPunish = stPunishService.getSchoolPunishs(stId);
createFile(doc,student,sysDictService,stClassService,sysDepartService,stStateJilu,stHonor,stPunish);
doc.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("===========end=============");
Result result = new Result();
return result;
}
public String queryByIdClas(String id);
@Override
@Cacheable(value = CacheConstant.SYS_DICT_CACHE,key = "#id")
public String queryByIdClas(String id) {
return stClassMapper.queryByIdClas(id);
}
public String queryByIdClas(@Param("id") String id);
<select id="queryByIdClas" parameterType="String" resultType="String">
SELECT
classes
FROM
st_class
WHERE
id=#{id}
</select>
转载请注明原文地址:https://ipadbbs.8miu.com/read-58603.html