java写pdf_java写出PDF

1\包

com.itextpdf

itextpdf

5.2.0

com.itextpdf

itext-asian

5.2.0

2、画格子

@RequestMapping("capitalBill")

public ModelAndView capitalBill(

int type, //1日\2月\3季度\4年

String dateBegin,//yyyy-MM-dd

String dateEnd,//yyyy-MM-dd

HttpServletRequest request,

HttpServletResponse response, HttpSession session

) throws Exception{

ModelAndView mov = new ModelAndView("/platformCapitalBill");

//查询数据

if(type==1){

//日报表数据, 某天0:00到23:59:59

//查询日报表对象

}else if(type==2){//月

}else if(type==3){//季度

}else if(type==4){//年

}

//展示数据

response.setContentType("application/pdf");

response.setHeader("Content-disposition", "attachment;filename="

+ new String("报表".getBytes("gb2312"), "ISO8859-1")

+ ".pdf");

ByteArrayOutputStream stream = new ByteArrayOutputStream();

// 获取响应数据流

ServletOutputStream out = response.getOutputStream();

// 创建文档对象,A4纸大小,40边距

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

PdfWriter.getInstance(document, stream);

// 解决中文不显示,设定初始字体

BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);

BaseFont baseFont_V = BaseFont.createFont("STSong-Light","UniGB-UCS2-V", BaseFont.NOT_EMBEDDED);

Font fonttitle = new Font(baseFont, 16, Font.BOLD);

Font font12bold = new Font(baseFont, 12, Font.BOLD);

Font font8 = new Font(baseFont, 8);

Font font8bold = new Font(baseFont, 8, Font.BOLD);

// 换行

Paragraph enterParagraph = new Paragraph("\n");

// 打开文档

document.open();

//获取路径,用于获取图片等资源

ServletContext servletContext = request.getSession()

.getServletContext();

String basePath = servletContext.getRealPath("/");

// 创建标题

Paragraph fileTile = new Paragraph("环链平台资金日报表", fonttitle);

fileTile.setAlignment(Paragraph.ALIGN_CENTER);

fileTile.setSpacingAfter(5);// 设置与后边段落间距

document.add(fileTile);

// 下载日期

SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");

Paragraph ParaDate = new Paragraph("下载日期: " + sf.format(new Date()),font8);

ParaDate.setAlignment(Paragraph.ALIGN_LEFT);

ParaDate.setIndentationLeft(150);

document.add(ParaDate);

// 创建一个表格

PdfPTable table = new PdfPTable(6);

table.setTotalWidth(new float[] { 28, 56, 163, 28, 56, 163 });// 去掉左右各40,515

table.setLockedWidth(true);

table.setSpacingBefore(5);

// 第一行

PdfPCell tableCell = new PdfPCell(new Paragraph("报表日期:", new Font(baseFont, 8, Font.BOLD)));

tableCell.setColspan(2);

tableCell.setMinimumHeight(20);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

tableCell = new PdfPCell(new Paragraph("2039-12-12", new Font(baseFont, 8)));

tableCell.setColspan(4);

tableCell.setMinimumHeight(20);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

// 第二行

tableCell = new PdfPCell(new Paragraph("上期余额:", font8bold));

tableCell.setMinimumHeight(20);

tableCell.setColspan(2);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

tableCell = new PdfPCell(new Paragraph("财务环链支付在关联方之间形成新的债权债务", font8));

tableCell.setColspan(4);

tableCell.setMinimumHeight(20);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

// 第10行

tableCell = new PdfPCell(new Paragraph("其中:", font8bold));

tableCell.setMinimumHeight(20);

tableCell.setColspan(1);

//tableCell.setRowspan(1);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

tableCell = new PdfPCell(new Paragraph("服务费存入:", font8bold));

tableCell.setMinimumHeight(20);

tableCell.setColspan(1);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

tableCell = new PdfPCell(new Paragraph("100.00", font8bold));

tableCell.setMinimumHeight(20);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

tableCell = new PdfPCell(new Paragraph("", font8bold));

tableCell.setMinimumHeight(20);

tableCell.setColspan(2);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

tableCell = new PdfPCell(new Paragraph("", font8bold));

tableCell.setMinimumHeight(20);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

// 第11行

tableCell = new PdfPCell(new Paragraph("其他存入", font8bold));

tableCell.setMinimumHeight(20);

tableCell.setColspan(2);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

tableCell = new PdfPCell(new Paragraph("00", font8bold));

tableCell.setMinimumHeight(20);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

tableCell = new PdfPCell(new Paragraph("其它转出:", font8bold));

tableCell.setMinimumHeight(20);

tableCell.setColspan(2);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

tableCell = new PdfPCell(new Paragraph("00", font8bold));

tableCell.setMinimumHeight(20);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

// 第12行

tableCell = new PdfPCell(new Paragraph("合计:", font8bold));

tableCell.setMinimumHeight(20);

tableCell.setColspan(2);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

tableCell = new PdfPCell(new Paragraph("0.0", font8bold));

tableCell.setMinimumHeight(20);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

tableCell = new PdfPCell(new Paragraph("合计:", font8bold));

tableCell.setMinimumHeight(20);

tableCell.setColspan(2);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

tableCell = new PdfPCell(new Paragraph("0.0", font8bold));

tableCell.setMinimumHeight(20);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

tableCell = new PdfPCell(new Paragraph("本期全额", new Font(baseFont, 8, Font.BOLD)));

tableCell.setColspan(2);

tableCell.setMinimumHeight(20);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

tableCell = new PdfPCell(new Paragraph("2039-12-12", new Font(baseFont, 8)));

tableCell.setColspan(4);

tableCell.setMinimumHeight(20);

tableCell.setUseAscender(true);

tableCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);

tableCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);

table.addCell(tableCell);

//添加

document.add(table);

// 关闭文档

document.close();

// 文档输出

stream.writeTo(out);

out.flush();

out.close();

return mov;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要用 Java一个将 PDF 文件转换成 Word 文件的程序,可以使用 Apache PDFBox 和 Apache POI 这两个 Java 库。以下是一个简单的示例代码,可以将 PDF 文件转换成 Word 文件: ``` import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; public class PDFToWordConverter { public static void main(String[] args) { try { // 读取 PDF 文件 PDDocument pdfDoc = PDDocument.load(new File("input.pdf")); // 提取 PDF 文件中的文本 PDFTextStripper stripper = new PDFTextStripper(); String text = stripper.getText(pdfDoc); // 创建 Word 文档 XWPFDocument wordDoc = new XWPFDocument(); // 添加文本到 Word 文档中 XWPFParagraph para = wordDoc.createParagraph(); XWPFRun run = para.createRun(); run.setText(text); // 将 Word 文档保存为文件 FileOutputStream out = new FileOutputStream("output.docx"); wordDoc.write(out); out.close(); // 关闭 PDF 文件 pdfDoc.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 这个程序将一个名为 `input.pdf` 的 PDF 文件转换成一个名为 `output.docx` 的 Word 文件。您需要将程序中的文件名替换为您自己的文件名,并且可能需要添加更多的错误处理代码以确保程序的稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值