SpringBoot中用itext实现PDF导出时实现循环添加元素

场景

SpringBoot加itext实现PDF导出下载

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/89475877

Itext实现导出PDF常用方法说明

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/89476534

实现

在上面的成功实现导出PDF的基础上,现在要查询数据库循环添加段落元素。

for(WmsInOrderList w:inOrderLists) {
            table4 = new PdfPTable(6);
            lightGrey = new BaseColor(0xCC, 0xCC, 0xCC);
            table4.setWidths(width4);
            cell41 = new PdfPCell(new Paragraph(w.getId()==null?"":w.getId().toString(), FontChinese11Bold));
            cell42 = new PdfPCell(new Paragraph(w.getMaterielNumber(), FontChinese11Bold));
            cell43 = new PdfPCell(new Paragraph(w.getMaterielName(), FontChinese11Bold));
            cell44 = new PdfPCell(new Paragraph(w.getNum()==null?"":w.getNum().toString(), FontChinese11Bold));
            cell45 = new PdfPCell(new Paragraph(w.getUntiy(), FontChinese11Bold));
            cell46 = new PdfPCell(new Paragraph(w.getBatch(), FontChinese11Bold));
            //表格高度
            cell41.setFixedHeight(25);
            cell42.setFixedHeight(25);
            cell43.setFixedHeight(25);
            cell44.setFixedHeight(25);
            cell45.setFixedHeight(25);
            cell46.setFixedHeight(25);
            cell41.setBorderColor(lightGrey);
            cell42.setBorderColor(lightGrey);
            cell43.setBorderColor(lightGrey);
            cell44.setBorderColor(lightGrey);
            cell45.setBorderColor(lightGrey);
            cell46.setBorderColor(lightGrey);
            table4.addCell(cell41);
            table4.addCell(cell42);
            table4.addCell(cell43);
            table4.addCell(cell44);
            table4.addCell(cell45);
            table4.addCell(cell46);
            doc.add(table4);
        }

效果

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您好!使用iText进行PDF转图片需要用到iText的核心模块 `itextpdf` 和 `pdf-renderer` 模块。这两个模块都是免费的。以下是一个基于Spring Boot的示例,演示如何使用iTextPDF转换为图像: 首先,在您的pom.xml文件添加以下依赖: ```xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13.2</version> </dependency> <dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId>flying-saucer-pdf</artifactId> <version>9.1.22</version> </dependency> ``` 接下来,创建一个名为 `PdfToImageService` 的服务类,该类具有一个名为 `pdfToImage` 的方法,该方法将接受一个PDF文件路径作为输入,并返回一个图像列表: ```java import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.xhtmlrenderer.swing.Java2DRenderer; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; @Service public class PdfToImageService { private static final Logger LOGGER = LoggerFactory.getLogger(PdfToImageService.class); public List<byte[]> pdfToImage(String pdfFilePath) throws IOException, DocumentException { LOGGER.info("Converting PDF to image: {}", pdfFilePath); List<byte[]> imageBytesList = new ArrayList<>(); PdfReader pdfReader = new PdfReader(pdfFilePath); for (int page = 1; page <= pdfReader.getNumberOfPages(); page++) { LOGGER.info("Converting page {} of PDF: {}", page, pdfFilePath); // 使用Flying Saucer将PDF页面转换为BufferedImage Java2DRenderer renderer = new Java2DRenderer(pdfReader.getPageContent(page)); BufferedImage bufferedImage = renderer.getImage(); // 将BufferedImage转换为byte数组 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); javax.imageio.ImageIO.write(bufferedImage, "png", outputStream); imageBytesList.add(outputStream.toByteArray()); outputStream.close(); } pdfReader.close(); return imageBytesList; } } ``` 最后,在您的控制器注入 `PdfToImageService`,并将其用于将PDF转换为图像: ```java import com.itextpdf.text.DocumentException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ByteArrayResource; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import java.io.IOException; import java.util.List; @Controller @RequestMapping("/pdf-to-image") public class PdfToImageController { private static final Logger LOGGER = LoggerFactory.getLogger(PdfToImageController.class); @Autowired private PdfToImageService pdfToImageService; @GetMapping(value = "/{pdfFileName}", produces = MediaType.IMAGE_PNG_VALUE) public ResponseEntity<ByteArrayResource> pdfToImage(@PathVariable String pdfFileName) throws IOException, DocumentException { LOGGER.info("Converting PDF to image: {}", pdfFileName); String pdfFilePath = "path/to/pdf/" + pdfFileName + ".pdf"; List<byte[]> imageBytesList = pdfToImageService.pdfToImage(pdfFilePath); if (imageBytesList.isEmpty()) { return ResponseEntity.notFound().build(); } else if (imageBytesList.size() == 1) { return ResponseEntity.ok(new ByteArrayResource(imageBytesList.get(0))); } else { // 将多个图像合并为一个图像 // ... byte[] mergedImageBytes = new byte[0]; // 合并后的图像的字节数组 return ResponseEntity.ok(new ByteArrayResource(mergedImageBytes)); } } } ``` 这个示例代码假设您已经将要转换的PDF文件放在 `path/to/pdf` 目录下。当您访问 `/pdf-to-image/{pdfFileName}` ,它将返回转换后的PNG图像。如果PDF文件具有多个页面,则可以将它们合并为一个图像,或者返回多个图像。希望这可以帮助到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值