2021-08-03

java设计pdf并实现下载

导入依赖

<dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext</artifactId>
    <version>2.1.7</version>
 </dependency>

导出服务接口

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfWriter;
import org.springframework.web.servlet.view.document.AbstractPdfView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class PdfView extends AbstractPdfView {

    private PdfExportService pdfExportService = null;
    /**
     * 创建对象时载入导出服务接口
     *
     * @param pdfExportService
     */
    public PdfView(PdfExportService pdfExportService) {
        this.pdfExportService = pdfExportService;
    }
    /**
     * 调用接口实现导出
     */
    @Override
    protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer,
                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 调用导出服务接口类
        pdfExportService.make(model, document, writer, request, response);
    }

}
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfWriter;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Service
public interface PdfExportService {
    public void make(Map<String, Object> model, Document document, PdfWriter writer,
                     HttpServletRequest request, HttpServletResponse response) throws IOException;
}

pdf设计、下载的service

@Service
public class DownloadService {
//pdf下载类
public ModelAndView exportPdf( HttpServletResponse response) {
        //下载
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition","attachment;filename"+"test.pdf");
        View view = new PdfView(exportService());
        ModelAndView mv = new ModelAndView();        // 设置视图
        mv.setView(view);        // 加入数据模型
        return mv;
    }
  //pdf设计类 
public PdfExportService exportService() {        // 使用Lambda表达式
        return(model, document, writer, request, response)-> {
            try {
                // A4纸张
                document.setPageSize(PageSize.A4);
                // title
                document.addTitle("用户信息");
                // 换行
                document.add(new Chunk("\n"));
                //中文显示
                BaseFont baseFont = BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
                Font f1 = new Font(baseFont,20, Font.BOLD ,Color.GREEN);    // 标题(字体12,加粗,绿色)
                Font f8 = new Font(baseFont,15,Font.BOLD, Color.BLACK);     // 表格中文字


                //段落paragraph 、文本块(Chunk)、短语(Phrase)
                Paragraph paragraph = null;
                paragraph =  new Paragraph("这是一个段落", f1);
                //1居中对齐、2为右对齐、3为左对齐,默认为左对齐
                paragraph.setAlignment(1);
                //将段落加入到pdf
                document.add(paragraph);
                

                //图片显示并插入到PDF,也可插入到表格cell
                Image jpg = Image.getInstance("C:/Users/Administrator/Desktop/1.jpg");
                jpg.setAlignment(1);
                document.add(jpg);
                document.add(new Chunk("\n"));


                /*设计不同宽度的3列表格
                float[] columnWidths = { 0.8f, 0.8f, 0.5f};
                PdfPTable table = new PdfPTable(3);
                table.setWidths(columnWidths);*/

				//插入3列表格
                PdfPTable table = new PdfPTable(3);
                // 单元格
                PdfPCell cell = null;

                cell = new PdfPCell(new Paragraph(“这是第1个单元格”, f8));
                // 居中对齐
                cell.setHorizontalAlignment(1);
                // 将单元格加入表格
                table.addCell(cell);

				 cell = new PdfPCell(new Paragraph(“这是合并后第2个单元格”, f8));
                // 居中对齐
                cell.setHorizontalAlignment(1);
				// 合并2列
				cell.setColspan(2);
				/**
				合并2行
				cell.setRowspan(2);
				*/
                // 将单元格加入表格
                table.addCell(cell);

                // 文档中加入表格
                document.add(table);

            } catch(DocumentException e) {
                e.printStackTrace();
            }
        };
    }
}
上一篇:JAVA读写Excel和word 代码


下一篇:使用 itext 生成简单 pdf 文件