ITEXT-小图片并排显示,大图片按行显示

x33g5p2x  于2021-12-28 转载在 其他  
字(5.0k)|赞(0)|评价(0)|浏览(427)

问题场景

使用itext开发PDF过程中,有这样一个需求:有N个图片,每个图片大小不一,要求小图片每2个并排显示,大图片逐一按行显示。那么问题来了,图片多小算小?由API知image01.getWidth()与image01.getWeight()可以判断图片大小。

代码

需导入的jar包:itext-pdfa-5.5.6.jar、itext-xtra-5.5.6.jar、itext-5.5.6.jar、itext-asian.jar

/** * 添加图片(有并排需求) 图片高度必须一致 * 思路: * 1. 通过图片的宽度是否小于pos值决定是否并排显示,是-> half; 否->all * 2. 新建一个2列的PdfTable * 3. 根据得到的half与all集合判断显示图片 * @param imagesDNA 图片集合 * @param size 样品编号数量 * @param reportCompile 信息载体,应用的时候可根据情况忽略 * @param document * @param writer * @param baseFont1 * @param yahei09 * @param imageLocalAddr 本地图片前缀 * @param pos 图片宽度标准 * @throws DocumentException * @throws MalformedURLException * @throws IOException */
    private void addFulu(List<PdfImage> imagesDNA, int size, ReportCompile reportCompile, Document document, PdfWriter writer, BaseFont baseFont1,
            Font yahei09, String imageLocalAddr, Float pos) throws DocumentException, MalformedURLException, IOException {

        String bigTitle = "";
        String completeCheck = reportCompile.getCompleteCheck();
        if (completeCheck != null && completeCheck.equals("1") || completeCheck.equals("2")) {
            bigTitle = "a";
        } else if (completeCheck != null && completeCheck.equals("3")){
            bigTitle = "b";
        } else if (completeCheck != null && completeCheck.equals("4")){
            bigTitle = "c";
        }

        if (imagesDNA == null) {
            System.out.println("在图片库没有查到值.");

            PdfPTable table = new PdfPTable(1);
            PdfPCell cell0 = new PdfPCell();

            Paragraph p = new Paragraph(bigTitle, yahei09);
            p.setFirstLineIndent(-14f);
            p.setSpacingAfter(20f);
            cell0.addElement(p);

            String imagePath = imageLocalAddr + "error.png";
            Image image01 = Image.getInstance(imagePath);
            image01.scaleAbsoluteWidth(250); 
            image01.scaleAbsoluteHeight(80); 
            cell0.setBorder(0);
            cell0.setPaddingLeft(100f);
            cell0.addElement(image01);

            table.addCell(cell0);
            table.setSplitLate(false);
            table.setSplitRows(false);

            document.add(table);

        } else {
            PdfImage[] images = new PdfImage[imagesDNA.size() > size ? size : imagesDNA.size()];
            images = imagesDNA.toArray(images);

            List<String> flags = this.getFlagRowOrCol(imagesDNA, pos, imageLocalAddr);

            PdfPTable table = new PdfPTable(2);
            int halfFlagNum = 0;
            for (int i = 0; i < images.length; i++) {

                if (flags.get(i).equals("half")) {
                    halfFlagNum++;
                }

                PdfPCell cell = new PdfPCell();
                if (i == 0) {
                    Paragraph p = new Paragraph(bigTitle, yahei09);
                    p.setSpacingAfter(14f);
                    cell.addElement(p);
                }

                String imagePath = images[i].getImagePath();
                if (imagePath == null || imagePath == "") {
                    imagePath = imageLocalAddr + "error.png";
                } else {
                    imagePath = "http://file.xxx.com" + imagePath;
                }
                Image image01 = null;
                try {
                    image01 = Image.getInstance(imagePath);
                } catch (FileNotFoundException e) {
                    image01 = Image.getInstance(imageLocalAddr + "error.png");
                }
                if (flags.get(i).equals("half")) {
                    image01.scalePercent(75);
                    //设置对齐方式
                    if (halfFlagNum % 2 == 1) {
                        image01.setAlignment(Image.RIGHT);
                        cell.setPaddingLeft(-21f);
                        cell.setPaddingRight(15f);
                    } else {
                        cell.setPaddingLeft(35f);
                    }
                    cell.setRowspan(2);
                } else {
                    if (image01.getWidth() <= pos) {
                            image01.scalePercent(75);
                        } else {
                            image01.scalePercent(45);
                        }
                    image01.setAlignment(Image.MIDDLE);
                    cell.setColspan(2);
                    if (images.length == 1) {
                        cell.setPaddingLeft(-21f);
                    }
                }
                cell.addElement(image01);
                cell.setBorder(0);
                if (i > 0) {
                    cell.setPaddingTop(32f);
                }

                table.addCell(cell);
                table.setSplitLate(false);
                table.setSplitRows(false);
            }

            document.add(table);
        }
    }

    /** * 获取标志位 * 直接通过图片的宽度来判断是否并排显示 * @param imagesDNA * @param pos 图片宽度标准,2个小图片放在一行 * @param imageLocalAddr * @return * @throws IOException * @throws MalformedURLException * @throws BadElementException */
    private List<String> getFlagRowOrCol(List<PdfImage> imagesDNA, Float pos, String imageLocalAddr) throws BadElementException, MalformedURLException, IOException {
        List<String> res = new ArrayList<String>();
        List<Float> list = new ArrayList<Float>();
        if (imagesDNA != null && imagesDNA.size() > 0) {
            for (int i = 0; i < imagesDNA.size(); i++) {
                String imagePath = imagesDNA.get(i).getImagePath();
                if (imagePath == null || imagePath == "") {
                    imagePath = imageLocalAddr + "error.png";
                } else {
                    imagePath = "http://file.xxx.com" + imagePath;
                }
                Image image01 = null;
                try {
                    image01 = Image.getInstance(imagePath);
                } catch (FileNotFoundException e) {
                    image01 = Image.getInstance(imageLocalAddr + "error.png");
                }
                list.add(image01.getWidth());
            }
        }

        if (list.size() <= imagesDNA.size()) {
            res = this.judgeRowOrCol(list, pos);
        }

        return res;
    }

    /** * 此方法用于判断是合并列还是合并行 * @param list 判断依据 * @param pos 连续两个在一起的且小于等于pos值的标志位为half,否则为all * @return */
        private List<String> judgeRowOrCol(List<Float> list, Float pos) {
            List<String> res = new ArrayList<String>();
            String half = "half";
            String all = "all";
            int couple = 0;

            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    float current = list.get(i);
                    if (i < list.size() - 1) {
                        float next = list.get(i + 1);
                        if (current <= pos) {
                            couple++;
                            if (next <= pos && couple != 2) {
                                res.add(half);
                            } else {
                                if (couple == 2) {
                                    res.add(half);
                                    couple = 0;
                                } else {
                                    res.add(all);
                                }
                            }
                        } else {
                            res.add(all);
                            couple = 0;
                        }
                    } else {
                        float previous = list.size() == 1 ? current : list.get(i - 1);
                        if (previous <= pos && current <= pos) {
                            if (list.size() == 1) {
                                res.add(all);
                            } else {
                                if (couple == 0) {
                                    res.add(all);
                                } else {
                                    res.add(half);
                                }
                            }
                        } else {
                            res.add(all);
                        }
                    }
                }
            }

            return res;
        }

相关文章