图片合成pdf和word打印

it2025-02-19  5

图片合成pdf (easypoi导出)https://www.cnblogs.com/sunny1009/p/11437005.html

public class PdfTest { public static void main(String[] args) { List<String> as=new ArrayList<>(); as.add("C:\\Users\\Ivan\\Desktop\\01.jpg"); as.add("C:\\Users\\Ivan\\Desktop\\02.jpg"); as.add("C:\\Users\\Ivan\\Desktop\\03.jpg"); as.add("C:\\Users\\Ivan\\Desktop\\04.jpg"); try { image2pdf(as, "C:\\Users\\Ivan\\Desktop\\新建文件夹\\aa.pdf"); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /*** * @param picturePath * 图片地址 */ private static void createPic(Document document, String picturePath) { try { //image.scalePercent(40);//依照比例缩放 Image image = Image.getInstance(picturePath); Image.setAlignment(Image.MIDDLE);//居中 // float height = image.getHeight(); //float width = image.getWidth(); // int percent=getPercent(height, width); //float documentWidth = document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin(); // float documentHeight = documentWidth / 580 * 320;// 重新设置宽高 // image.scaleAbsolute(documentWidth, documentHeight);// 重新设置宽高 document.add(image); } catch (Exception ex) { } } public static void image2pdf(List<String> text, String pdf) throws DocumentException, IOException { Document document = new Document(); OutputStream os = new FileOutputStream(new File(pdf)); PdfWriter.getInstance(document, os); document.open(); for (String s : text) { createPic(document, s); } document.close(); } } public static int getPercent(float h,float w) { int p=0; float p2=0.0f; p2=530/w*100; p=Math.round(p2); return p; } <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency>

word打印

package com.example.pdfandword.entity; import com.deepoove.poi.data.PictureRenderData; /** * @author gssong * @description * @date 2020-10-22 */ public class Goods { private int count; private String name; private String desc; private int discount; private int tax; private int price; private int totalPrice; private PictureRenderData picture; public int getCount() { return count; } public void setCount(int count) { this.count = count; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public int getDiscount() { return discount; } public void setDiscount(int discount) { this.discount = discount; } public int getTax() { return tax; } public void setTax(int tax) { this.tax = tax; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public int getTotalPrice() { return totalPrice; } public void setTotalPrice(int totalPrice) { this.totalPrice = totalPrice; } public PictureRenderData getPicture() { return picture; } public void setPicture(PictureRenderData picture) { this.picture = picture; } } public class PaymentHackData { private List<Goods> goods; public List<Goods> getGoods() { return goods; } public void setGoods(List<Goods> goods) { this.goods = goods; } } package com.example.pdfandword.exoprtWord; import com.deepoove.poi.XWPFTemplate; import com.deepoove.poi.config.Configure; import com.deepoove.poi.data.PictureRenderData; import com.deepoove.poi.policy.HackLoopTableRenderPolicy; import com.deepoove.poi.util.BytePictureUtils; import com.example.pdfandword.entity.Goods; import com.example.pdfandword.entity.PaymentHackData; import org.junit.Test; import org.springframework.util.Assert; import java.io.File; import java.util.*; /** * @author gssong * @description * @date 2020-10-22 */ public class TestWord { //Poi-tl模板引擎官方文档:http://deepoove.com/poi-tl/ public static String createWord(String templatePath, String fileDir, String fileName, Map<String, Object> paramMap,Configure config) { Assert.notNull(templatePath, "word模板文件路径不能为空"); Assert.notNull(fileDir, "生成的文件存放地址不能为空"); Assert.notNull(fileName, "生成的文件名不能为空"); // 生成的word格式 String formatSuffix = ".docx"; // 拼接后的文件名 fileName = fileName + formatSuffix; // 生成的文件的存放路径 if (!fileDir.endsWith("/")) { fileDir = fileDir + File.separator; } File dir = new File(fileDir); if (!dir.exists()) { dir.mkdirs(); } String filePath = fileDir + fileName; // 读取模板templatePath并将paramMap的内容填充进模板,即编辑模板+渲染数据 XWPFTemplate template = XWPFTemplate.compile(templatePath,config).render(paramMap); try { // 将填充之后的模板写入filePath template.writeToFile(filePath); template.close(); } catch (Exception e) { e.printStackTrace(); return ""; } return filePath; } @Test public void test01(){ PaymentHackData table = new PaymentHackData(); List<Goods> goods = new ArrayList<>(); for (int i = 0; i <6 ; i++) { Goods good = new Goods(); good.setCount(i); good.setName("墙纸"); good.setDesc("书房卧室"); good.setDiscount(1500); good.setPrice(400); good.setTax(new Random().nextInt(10) + 20); good.setTotalPrice(1600); good.setPicture(new PictureRenderData(24, 24, ".png", BytePictureUtils.getUrlBufferedImage("http://deepoove.com/images/icecream.png"))); goods.add(good); table.setGoods(goods); } Map<String, Object> params = new HashMap<>(); params.put("datas", Arrays.asList(table)); params.put("myname", "张三"); String templatePath = "C:\\Users\\Ivan\\Desktop\\新建文件夹\\song.docx"; String fileDir = "C:\\Users\\Ivan\\Desktop"; String fileName = "zdd2"; HackLoopTableRenderPolicy hackLoopTableRenderPolicy = new HackLoopTableRenderPolicy(); Configure config = Configure.newBuilder().bind("goods", hackLoopTableRenderPolicy).build(); String wordPath = TestWord.createWord(templatePath, fileDir, fileName, params,config); System.out.println("生成文档路径:" + wordPath); } } <dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> <version>1.8.2</version> </dependency>

最新回复(0)