如何把做的网站与域名连接不上,网站建设色彩搭配,网站小图片素材,哈尔滨手机网站建设价格低一、html转图片
引入依赖
implementation org.xhtmlrenderer:flying-saucer-core:9.1.22html页面转图片对于html标签格式有要求#xff0c;自己调整#xff0c;每个标签都要有结束标签#xff0c;标签也要#xff0c;标签也要#xff0c; doctype需要大写
try {String …一、html转图片
引入依赖
implementation org.xhtmlrenderer:flying-saucer-core:9.1.22html页面转图片对于html标签格式有要求自己调整每个标签都要有结束标签标签也要标签也要 doctype需要大写
try {String basePath D:\\;File source new File(basePath, report.html);File g2drDest new File(basePath, G2DR.png);BufferedImage g2drImage Graphics2DRenderer.renderToImageAutoSize(source.toURI().toURL().toExternalForm(),1024, BufferedImage.TYPE_INT_ARGB);ImageIO.write(g2drImage, png, g2drDest);}catch (Exception e){e.printStackTrace();}二、图片嵌入pdf
implementation com.itextpdf:itextpdf:5.5.5
implementation com.itextpdf:itext-asian:5.2.0try{BufferedImage originalImage ImageIO.read(new File(D:\\G2DR.png));float imgWidth originalImage.getWidth();float imgHeight originalImage.getHeight();float pageWidth PageSize.A4.getWidth();float pageHeight PageSize.A4.getHeight();float imgPerPage pageHeight; // 每页只显示图片的一段Document document new Document(PageSize.A4, 0, 0, 0, 0);PdfWriter.getInstance(document, new FileOutputStream(D:\\output.pdf));document.open();int totalImages (int) Math.ceil(imgHeight / imgPerPage);//图片长宽和A4大小不匹配可能会导致图片剪切嵌入观感不好看可自己微调页面和图片大小for (int i 0; i totalImages; i) {// 计算剪切起点和宽度int start (int) (imgPerPage * i);int end (int) Math.min(start imgPerPage, imgHeight);BufferedImage subImage originalImage.getSubimage(0, start, (int)imgWidth, end-start);// 将剪切后的图片转换为iText可用的Image对象Image image Image.getInstance(subImage, null);image.scaleToFit(pageWidth, pageHeight); // 调整图片大小以适应页面// 插入图片到PDF文档document.add(new Paragraph());document.add(image);// 添加页码如果是最后一段不需要换页if (i ! totalImages - 1) {document.newPage();}}document.close();}catch (Exception e){e.printStackTrace();}三、图片嵌入word
最简单的嵌入
implementation org.apache.poi:poi:4.1.2
implementation org.apache.poi:poi-ooxml:4.1.2try{BufferedImage originalImage ImageIO.read(new File(D:\\G2DR.png));XWPFDocument documentnew XWPFDocument();XWPFParagraph paragraphdocument.createParagraph();XWPFRun runparagraph.createRun();//自己设置页面参数run.addPicture(new FileInputStream(D:\\G2DR.png),XWPFDocument.PICTURE_TYPE_PNG,1.png,Units.toEMU(originalImage.getWidth()*(PageSize.A4.getHeight()/ originalImage.getHeight())),Units.toEMU(PageSize.A4.getHeight()));OutputStream outputStreamnew FileOutputStream(D:\\output.docx);document.write(outputStream);outputStream.close();}catch (Exception e){e.printStackTrace();}