excel转pdf并且加水印,利用ByteArrayOutputStream内存流不产生中间文件

news2024/11/18 9:37:22

首先先引入包:加水印和excel转PDF的

    <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version> <!-- 版本号根据您实际使用的版本进行调整 -->
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version> <!-- 根据实际需要选择版本号 -->
        </dependency>

然后两个工具类,第一个是加水印的

package org.springblade.common.tool;


import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;


import java.io.*;
import java.util.List;

public class PDFWatermarkExample {
	public static void addWatermark(String inputFile, String outputFile, String watermarkText) {
		try {
			PdfReader reader = new PdfReader(inputFile);
			PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
			int numberOfPages = reader.getNumberOfPages();

			for (int i = 1; i <= numberOfPages; i++) {
				PdfContentByte content = stamper.getUnderContent(i);
				PdfGState gs = new PdfGState();
				gs.setFillOpacity(0.5f); // 设置水印透明度
				content.setGState(gs);

				ColumnText.showTextAligned(
					content,
					Element.ALIGN_CENTER,
					new Phrase(watermarkText, new Font(Font.FontFamily.HELVETICA, 40)),
					reader.getPageSizeWithRotation(i).getWidth() / 2,
					reader.getPageSizeWithRotation(i).getHeight() / 2,
					45
				);
			}

			stamper.close();
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void addWatermarkMulti(String inputFile, String outputFile, String watermarkText) {
		try {
			PdfReader reader = new PdfReader(inputFile);
			PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
			int numberOfPages = reader.getNumberOfPages();

			for (int i = 1; i <= numberOfPages; i++) {
				PdfContentByte content = stamper.getOverContent(i);
				PdfGState gs = new PdfGState();
				gs.setFillOpacity(0.5f); // 设置水印透明度
				content.setGState(gs);

				Rectangle pageSize = reader.getPageSizeWithRotation(i);
				float pageWidth = pageSize.getWidth();
				float pageHeight = pageSize.getHeight();

				// 设置水印间隔
				float xInterval = 200; // X轴间隔
				float yInterval = 50; // Y轴间隔

				// 计算水印个数
				int xCount = (int) Math.ceil(pageWidth / xInterval);
				int yCount = (int) Math.ceil(pageHeight / yInterval);

				// 平铺水印
				for (int x = 0; x < xCount; x++) {
					for (int y = 0; y < yCount; y++) {
						float xPosition = x * xInterval;
						float yPosition = y * yInterval;

						ColumnText.showTextAligned(
							content,
							Element.ALIGN_CENTER,
							new Phrase(watermarkText, new Font(Font.FontFamily.HELVETICA, 40)),
							xPosition,
							yPosition,
							45
						);
					}
				}
			}

			stamper.close();
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public static void addWatermarkMultiCN(String inputFile, String outputFile, String watermarkText) {
		try {
			PdfReader reader = new PdfReader(inputFile);
			PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
			int numberOfPages = reader.getNumberOfPages();


			Resource resource = new ClassPathResource("simhei.ttf"); // 加载位于resources目录下的字体文件
			InputStream inputStream = resource.getInputStream();
			byte[] fontBytes = IOUtils.toByteArray(inputStream);
			BaseFont baseFont = BaseFont.createFont("simhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, fontBytes, null); // 创建BaseFont对象

			for (int i = 1; i <= numberOfPages; i++) {
				PdfContentByte content = stamper.getOverContent(i);
				PdfGState gs = new PdfGState();
				gs.setFillOpacity(0.5f); // 设置水印透明度
				content.setGState(gs);

				Rectangle pageSize = reader.getPageSizeWithRotation(i);
				float pageWidth = pageSize.getWidth();
				float pageHeight = pageSize.getHeight();

				// 设置水印间隔
				float xInterval = 200; // X轴间隔
				float yInterval = 50; // Y轴间隔

				// 计算水印个数
				int xCount = (int) Math.ceil(pageWidth / xInterval);
				int yCount = (int) Math.ceil(pageHeight / yInterval);

				// 平铺水印
				for (int x = 0; x < xCount; x++) {
					for (int y = 0; y < yCount; y++) {
						float xPosition = x * xInterval;
						float yPosition = y * yInterval;

						ColumnText.showTextAligned(
							content,
							Element.ALIGN_CENTER,
							new Phrase(watermarkText, new Font(baseFont, 40)), // 使用创建的中文字体
							xPosition,
							yPosition,
							45
						);
					}
				}
			}

			stamper.close();
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 *
	 * @param inputStream  文件输入流
	 * @param watermarkText  水印文字
	 * @return
	 */
	public static ByteArrayOutputStream addWatermarkToPdfStream(InputStream inputStream, String watermarkText) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		try {
			PdfReader reader = new PdfReader(inputStream);
			PdfStamper stamper = new PdfStamper(reader, outputStream);
			int numberOfPages = reader.getNumberOfPages();

			// ...(原有的水印添加逻辑)
			Resource resource = new ClassPathResource("simhei.ttf"); // 加载位于resources目录下的字体文件
			InputStream inputStreamTTF = resource.getInputStream();
			byte[] fontBytes = IOUtils.toByteArray(inputStreamTTF);
			BaseFont baseFont = BaseFont.createFont("simhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, fontBytes, null); // 创建BaseFont对象

			for (int i = 1; i <= numberOfPages; i++) {
				PdfContentByte content = stamper.getOverContent(i);
				PdfGState gs = new PdfGState();
				gs.setFillOpacity(0.3f); // 设置水印透明度
				content.setGState(gs);

				Rectangle pageSize = reader.getPageSizeWithRotation(i);
				float pageWidth = pageSize.getWidth();
				float pageHeight = pageSize.getHeight();

				// 设置水印间隔
				float xInterval = 400; // X轴间隔
				float yInterval = 150; // Y轴间隔

				// 计算水印个数
				int xCount = (int) Math.ceil(pageWidth / xInterval);
				int yCount = (int) Math.ceil(pageHeight / yInterval);

				// 平铺水印
				for (int x = 0; x < xCount; x++) {
					for (int y = 0; y < yCount; y++) {
						float xPosition = x * xInterval;
						float yPosition = y * yInterval;

						ColumnText.showTextAligned(
							content,
							Element.ALIGN_CENTER,
							new Phrase(watermarkText, new Font(baseFont, 20)), // 使用创建的中文字体
							xPosition,
							yPosition,
							45
						);
					}
				}
			}

			stamper.close();
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return outputStream;
	}

	public static void addWatermarkExample(List<File> files) {
		// 在这里编写添加水印的代码逻辑,使用上面提到的添加水印的示例代码
		for (File file : files) {
			addWatermark(file.getPath(), file.getPath(), "Watermark Text");
		}
	}

	public static void main(String[] args) {
		String inputFile = "D:\\Downloads\\123.pdf";
		String outputFile = "D:\\Downloads\\456.pdf";
		String watermarkText = "这是中文水印";

		addWatermarkMultiCN(inputFile, outputFile, watermarkText);


		String outputFileStream = "D:\\Downloads\\789.pdf";
		String watermarkText02 = "这是读取流返回流";

		try (InputStream input = new FileInputStream(inputFile)) {
			ByteArrayOutputStream output = addWatermarkToPdfStream(input, watermarkText02);
			try (OutputStream fileOutput = new FileOutputStream(outputFileStream)) {
				output.writeTo(fileOutput);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

别忘记字体在resource里面读取的

第二个是转PDF的

package org.springblade.common.tool;



import cn.hutool.core.collection.CollUtil;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import lombok.experimental.UtilityClass;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@UtilityClass
public class ExcelToPdfUtil {

    public static void excelToPdf(String excelPath, String pdfPath, String excelSuffix) {
        try (InputStream in = Files.newInputStream(Paths.get(excelPath));
             OutputStream out = Files.newOutputStream(Paths.get(pdfPath))) {
            ExcelToPdfUtil.excelToPdf(in, out, excelSuffix);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Excel转PDF并写入输出流
     *
     * @param inStream    Excel输入流
     * @param outStream   PDF输出流
     * @param excelSuffix Excel类型 .xls 和 .xlsx
     * @throws Exception 异常信息
     */
    public static void excelToPdf(InputStream inStream, OutputStream outStream, String excelSuffix) throws Exception {
        // 输入流转workbook,获取sheet
        Sheet sheet = getPoiSheetByFileStream(inStream, 0, excelSuffix);
        // 获取列宽度占比
        float[] widths = getColWidth(sheet);
        PdfPTable table = new PdfPTable(widths);
        table.setWidthPercentage(100);
        int colCount = widths.length;
        //设置基本字体
        BaseFont baseFont = BaseFont.createFont("C:\\Windows\\Fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        // 遍历行
        for (int rowIndex = sheet.getFirstRowNum(); rowIndex <= sheet.getLastRowNum(); rowIndex++) {
            Row row = sheet.getRow(rowIndex);
            if (Objects.isNull(row)) {
                // 插入空对象
                for (int i = 0; i < colCount; i++) {
                    table.addCell(createPdfPCell(null, 0, 13f, null));
                }
            } else {
                // 遍历单元格
                for (int columnIndex = 0; (columnIndex < row.getLastCellNum() || columnIndex < colCount) && columnIndex > -1; columnIndex++) {
                    PdfPCell pCell = excelCellToPdfCell(sheet, row.getCell(columnIndex), baseFont);
                    // 是否合并单元格
                    if (isMergedRegion(sheet, rowIndex, columnIndex)) {
                        int[] span = getMergedSpan(sheet, rowIndex, columnIndex);
                        //忽略合并过的单元格
                        boolean mergedCell = span[0] == 1 && span[1] == 1;
                        if (mergedCell) {
                            continue;
                        }
                        pCell.setRowspan(span[0]);
                        pCell.setColspan(span[1]);
                    }
                    table.addCell(pCell);
                }
            }
        }
        // 初始化PDF文档对象
        createPdfTableAndWriteDocument(outStream, table);
    }

    /**
     * 单元格转换,poi cell 转换为 itext cell
     *
     * @param sheet     poi sheet页
     * @param excelCell poi 单元格
     * @param baseFont  基础字体
     * @return com.itextpdf.text.pdf.PdfPCell
     */
    private static PdfPCell excelCellToPdfCell(Sheet sheet, Cell excelCell, BaseFont baseFont) throws Exception {
        if (Objects.isNull(excelCell)) {
            return createPdfPCell(null, 0, 13f, null);
        }
        int rowIndex = excelCell.getRowIndex();
        int columnIndex = excelCell.getColumnIndex();
        // 图片信息
        List<PicturesInfo> infos = getAllPictureInfos(sheet, rowIndex, rowIndex, columnIndex, columnIndex, false);
        PdfPCell pCell;
        if (CollUtil.isNotEmpty(infos)) {
            pCell = new PdfPCell(Image.getInstance(infos.get(0).getPictureData()));
        } else {
            Font excelFont = getExcelFont(sheet, excelCell);
            //设置单元格字体
            com.itextpdf.text.Font pdFont = new com.itextpdf.text.Font(baseFont, excelFont.getFontHeightInPoints(), excelFont.getBold() ? 1 : 0, BaseColor.BLACK);
            Integer border = hasBorder(excelCell) ? null : 0;
            String excelCellValue = getExcelCellValue(excelCell);
            pCell = createPdfPCell(excelCellValue, border, excelCell.getRow().getHeightInPoints(), pdFont);
        }
        // 水平居中
        pCell.setHorizontalAlignment(getHorAlign(excelCell.getCellStyle().getAlignment().getCode()));
        // 垂直对齐
        pCell.setVerticalAlignment(getVerAlign(excelCell.getCellStyle().getVerticalAlignment().getCode()));
        return pCell;
    }

    /**
     * 创建pdf文档,并添加表格
     *
     * @param outStream 输出流,目标文档
     * @param table     表格
     * @throws DocumentException 异常信息
     */
    private static void createPdfTableAndWriteDocument(OutputStream outStream, PdfPTable table) throws DocumentException {
        //设置pdf纸张大小 PageSize.A4 A4横向
        Document document = new Document(new RectangleReadOnly(842.0F, 595.0F));
        PdfWriter.getInstance(document, outStream);
        //设置页边距 宽
        document.setMargins(10, 10, 10, 10);
        document.open();
        document.add(table);
        document.close();
    }

    /**
     * Excel文档输入流转换为对应的workbook及获取对应的sheet
     *
     * @param inputStream Excel文档输入流
     * @param sheetNo     sheet编号,默认0 第一个sheet
     * @param excelSuffix 文件类型 .xls和.xlsx
     * @return poi sheet
     * @throws IOException 异常
     */
    public static Sheet getPoiSheetByFileStream(InputStream inputStream, int sheetNo, String excelSuffix) throws IOException {
        Workbook workbook;
        if (excelSuffix.endsWith(".xls")) {
			workbook = new HSSFWorkbook(inputStream);

        } else {
			workbook = new XSSFWorkbook(inputStream);
        }
        return workbook.getSheetAt(sheetNo);
    }

    /**
     * 创建itext pdf 单元格
     *
     * @param content       单元格内容
     * @param border        边框
     * @param minimumHeight 高度
     * @param pdFont        字体
     * @return pdf cell
     */
    private static PdfPCell createPdfPCell(String content, Integer border, Float minimumHeight, com.itextpdf.text.Font pdFont) {
        String contentValue = content == null ? "" : content;
        com.itextpdf.text.Font pdFontNew = pdFont == null ? new com.itextpdf.text.Font() : pdFont;
        PdfPCell pCell = new PdfPCell(new Phrase(contentValue, pdFontNew));
        if (Objects.nonNull(border)) {
            pCell.setBorder(border);
        }
        if (Objects.nonNull(minimumHeight)) {
            pCell.setMinimumHeight(minimumHeight);
        }

        return pCell;
    }

    /**
     * excel垂直对齐方式映射到pdf对齐方式
     */
    private static int getVerAlign(int align) {
        switch (align) {
            case 2:
                return com.itextpdf.text.Element.ALIGN_BOTTOM;
            case 3:
                return com.itextpdf.text.Element.ALIGN_TOP;
            default:
                return com.itextpdf.text.Element.ALIGN_MIDDLE;
        }
    }

    /**
     * excel水平对齐方式映射到pdf水平对齐方式
     */
    private static int getHorAlign(int align) {
        switch (align) {
            case 1:
                return com.itextpdf.text.Element.ALIGN_LEFT;
            case 3:
                return com.itextpdf.text.Element.ALIGN_RIGHT;
            default:
                return com.itextpdf.text.Element.ALIGN_CENTER;
        }
    }

    /*============================================== POI获取图片及文本内容工具方法 ==============================================*/

    /**
     * 获取字体
     *
     * @param sheet excel 转换的sheet页
     * @param cell  单元格
     * @return 字体
     */
    private static Font getExcelFont(Sheet sheet, Cell cell) {
        // xls
        if (sheet instanceof HSSFSheet) {
            Workbook workbook = sheet.getWorkbook();
            return ((HSSFCell) cell).getCellStyle().getFont(workbook);
        }
        // xlsx
        return ((XSSFCell) cell).getCellStyle().getFont();
    }

    /**
     * 判断excel单元格是否有边框
     */
    private static boolean hasBorder(Cell excelCell) {
        short top = excelCell.getCellStyle().getBorderTop().getCode();
        short bottom = excelCell.getCellStyle().getBorderBottom().getCode();
        short left = excelCell.getCellStyle().getBorderLeft().getCode();
        short right = excelCell.getCellStyle().getBorderRight().getCode();
        return top + bottom + left + right > 2;
    }

    /**
     * 判断单元格是否是合并单元格
     */
    private static boolean isMergedRegion(Sheet sheet, int row, int column) {
        int sheetMergeCount = sheet.getNumMergedRegions();
        for (int i = 0; i < sheetMergeCount; i++) {
            CellRangeAddress range = sheet.getMergedRegion(i);
            int firstColumn = range.getFirstColumn();
            int lastColumn = range.getLastColumn();
            int firstRow = range.getFirstRow();
            int lastRow = range.getLastRow();
            if (row >= firstRow && row <= lastRow) {
                if (column >= firstColumn && column <= lastColumn) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 计算合并单元格合并的跨行跨列数
     */
    private static int[] getMergedSpan(Sheet sheet, int row, int column) {
        int sheetMergeCount = sheet.getNumMergedRegions();
        int[] span = {1, 1};
        for (int i = 0; i < sheetMergeCount; i++) {
            CellRangeAddress range = sheet.getMergedRegion(i);
            int firstColumn = range.getFirstColumn();
            int lastColumn = range.getLastColumn();
            int firstRow = range.getFirstRow();
            int lastRow = range.getLastRow();
            if (firstColumn == column && firstRow == row) {
                span[0] = lastRow - firstRow + 1;
                span[1] = lastColumn - firstColumn + 1;
                break;
            }
        }
        return span;
    }

    /**
     * 获取excel中每列宽度的占比
     */
    private static float[] getColWidth(Sheet sheet) {
        int rowNum = getMaxColRowNum(sheet);
        Row row = sheet.getRow(rowNum);
        int cellCount = row.getPhysicalNumberOfCells();
        int[] colWidths = new int[cellCount];
        int sum = 0;

        for (int i = row.getFirstCellNum(); i < cellCount; i++) {
            Cell cell = row.getCell(i);
            if (cell != null) {
                colWidths[i] = sheet.getColumnWidth(i);
                sum += sheet.getColumnWidth(i);
            }
        }

        float[] colWidthPer = new float[cellCount];
        for (int i = row.getFirstCellNum(); i < cellCount; i++) {
            colWidthPer[i] = (float) colWidths[i] / sum * 100;
        }
        return colWidthPer;
    }

    /**
     * 获取excel中列数最多的行号
     */
    private static int getMaxColRowNum(Sheet sheet) {
        int rowNum = 0;
        int maxCol = 0;
        for (int r = sheet.getFirstRowNum(); r < sheet.getPhysicalNumberOfRows(); r++) {
            Row row = sheet.getRow(r);
            if (row != null && maxCol < row.getPhysicalNumberOfCells()) {
                maxCol = row.getPhysicalNumberOfCells();
                rowNum = r;
            }
        }
        return rowNum;
    }

    /**
     * poi 根据单元格类型获取单元格内容
     *
     * @param excelCell poi单元格
     * @return 单元格内容文本
     */
    public static String getExcelCellValue(Cell excelCell) {
        if (excelCell == null) {
            return "";
        }
        // 判断数据的类型
        CellType cellType = excelCell.getCellType();

        if (cellType == CellType.STRING) {
            return excelCell.getStringCellValue();
        }
        if (cellType == CellType.BOOLEAN) {
            return String.valueOf(excelCell.getBooleanCellValue());
        }
        if (cellType == CellType.FORMULA) {
            return excelCell.getCellFormula();
        }
        if (cellType == CellType.NUMERIC) {
            //short s = excelCell.getCellStyle().getDataFormat();
            if (DateUtil.isCellDateFormatted(excelCell)) {// 处理日期格式、时间格式
                SimpleDateFormat sdf;
                // 验证short值
                if (excelCell.getCellStyle().getDataFormat() == 14) {
                    sdf = new SimpleDateFormat("yyyy/MM/dd");
                } else if (excelCell.getCellStyle().getDataFormat() == 21) {
                    sdf = new SimpleDateFormat("HH:mm:ss");
                } else if (excelCell.getCellStyle().getDataFormat() == 22) {
                    sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                } else {
                    throw new RuntimeException("日期格式错误!!!");
                }
                Date date = excelCell.getDateCellValue();
                return sdf.format(date);
            } else if (excelCell.getCellStyle().getDataFormat() == 0) {
                //处理数值格式
                DataFormatter formatter = new DataFormatter();
                return formatter.formatCellValue(excelCell);
            }
        }
        if (cellType == CellType.ERROR) {
            return "非法字符";
        }
        return "";
    }

    /**
     * 获取sheet内的所有图片信息
     *
     * @param sheet        sheet表
     * @param onlyInternal 单元格内部
     * @return 照片集合
     * @throws Exception 异常
     */
    public static List<PicturesInfo> getAllPictureInfos(Sheet sheet, boolean onlyInternal) throws Exception {
        return getAllPictureInfos(sheet, null, null, null, null, onlyInternal);
    }

    /**
     * 根据sheet和单元格信息获取图片
     *
     * @param sheet        sheet表
     * @param minRow       最小行
     * @param maxRow       最大行
     * @param minCol       最小列
     * @param maxCol       最大列
     * @param onlyInternal 是否内部
     * @return 图片集合
     * @throws Exception 异常
     */
    public static List<PicturesInfo> getAllPictureInfos(Sheet sheet, Integer minRow, Integer maxRow, Integer minCol,
                                                        Integer maxCol, boolean onlyInternal) throws Exception {
        if (sheet instanceof HSSFSheet) {
            return getXLSAllPictureInfos((HSSFSheet) sheet, minRow, maxRow, minCol, maxCol, onlyInternal);
        } else if (sheet instanceof XSSFSheet) {
            return getXLSXAllPictureInfos((XSSFSheet) sheet, minRow, maxRow, minCol, maxCol, onlyInternal);
        } else {
            throw new Exception("未处理类型,没有为该类型添加:GetAllPicturesInfos()扩展方法!");
        }
    }

    private static List<PicturesInfo> getXLSAllPictureInfos(HSSFSheet sheet, Integer minRow, Integer maxRow,
                                                            Integer minCol, Integer maxCol, Boolean onlyInternal) {
        List<PicturesInfo> picturesInfoList = new ArrayList<>();
        HSSFShapeContainer shapeContainer = sheet.getDrawingPatriarch();
        if (shapeContainer == null) {
            return picturesInfoList;
        }
        List<HSSFShape> shapeList = shapeContainer.getChildren();
        for (HSSFShape shape : shapeList) {
            if (shape instanceof HSSFPicture && shape.getAnchor() instanceof HSSFClientAnchor) {
                HSSFPicture picture = (HSSFPicture) shape;
                HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();

                if (isInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.getRow1(), anchor.getRow2(),
                        anchor.getCol1(), anchor.getCol2(), onlyInternal)) {
                    HSSFPictureData pictureData = picture.getPictureData();
                    picturesInfoList.add(new PicturesInfo()
                            .setMinRow(anchor.getRow1())
                            .setMaxRow(anchor.getRow2())
                            .setMinCol(anchor.getCol1())
                            .setMaxCol(anchor.getCol2())
                            .setPictureData(pictureData.getData())
                            .setExt(pictureData.getMimeType()));
                }
            }
        }
        return picturesInfoList;
    }

    private static List<PicturesInfo> getXLSXAllPictureInfos(XSSFSheet sheet, Integer minRow, Integer maxRow,
                                                             Integer minCol, Integer maxCol, Boolean onlyInternal) {
        List<PicturesInfo> picturesInfoList = new ArrayList<>();

        List<POIXMLDocumentPart> documentPartList = sheet.getRelations();
        for (POIXMLDocumentPart documentPart : documentPartList) {
            if (documentPart instanceof XSSFDrawing) {
                XSSFDrawing drawing = (XSSFDrawing) documentPart;
                List<XSSFShape> shapes = drawing.getShapes();
                for (XSSFShape shape : shapes) {
                    if (shape instanceof XSSFPicture) {
                        XSSFPicture picture = (XSSFPicture) shape;
                        XSSFClientAnchor anchor = picture.getPreferredSize();

                        if (isInternalOrIntersect(minRow, maxRow, minCol, maxCol, anchor.getRow1(), anchor.getRow2(),
                                anchor.getCol1(), anchor.getCol2(), onlyInternal)) {
                            XSSFPictureData pictureData = picture.getPictureData();
                            picturesInfoList.add(new PicturesInfo()
                                    .setMinRow(anchor.getRow1())
                                    .setMaxRow(anchor.getRow2())
                                    .setMinCol(anchor.getCol1())
                                    .setMaxCol(anchor.getCol2())
                                    .setPictureData(pictureData.getData())
                                    .setExt(pictureData.getMimeType()));
                        }
                    }
                }
            }
        }

        return picturesInfoList;
    }

    private static boolean isInternalOrIntersect(Integer rangeMinRow, Integer rangeMaxRow, Integer rangeMinCol,
                                                 Integer rangeMaxCol, int pictureMinRow, int pictureMaxRow, int pictureMinCol, int pictureMaxCol,
                                                 Boolean onlyInternal) {
        int _rangeMinRow = rangeMinRow == null ? pictureMinRow : rangeMinRow;
        int _rangeMaxRow = rangeMaxRow == null ? pictureMaxRow : rangeMaxRow;
        int _rangeMinCol = rangeMinCol == null ? pictureMinCol : rangeMinCol;
        int _rangeMaxCol = rangeMaxCol == null ? pictureMaxCol : rangeMaxCol;

        if (onlyInternal) {
            return (_rangeMinRow <= pictureMinRow && _rangeMaxRow >= pictureMaxRow && _rangeMinCol <= pictureMinCol
                    && _rangeMaxCol >= pictureMaxCol);
        } else {
            return ((Math.abs(_rangeMaxRow - _rangeMinRow) + Math.abs(pictureMaxRow - pictureMinRow) >= Math
                    .abs(_rangeMaxRow + _rangeMinRow - pictureMaxRow - pictureMinRow))
                    && (Math.abs(_rangeMaxCol - _rangeMinCol) + Math.abs(pictureMaxCol - pictureMinCol) >= Math
                    .abs(_rangeMaxCol + _rangeMinCol - pictureMaxCol - pictureMinCol)));
        }
    }

    /**
     * 图片基本信息
     */
    private class PicturesInfo {

        private int minRow;
        private int maxRow;
        private int minCol;
        private int maxCol;
        private String ext;
        private byte[] pictureData;

        public PicturesInfo() {
        }
        public byte[] getPictureData() {
            return pictureData;
        }
        public PicturesInfo setPictureData(byte[] pictureData) {
            this.pictureData = pictureData;
            return this;
        }
        public int getMinRow() {
            return minRow;
        }
        public PicturesInfo setMinRow(int minRow) {
            this.minRow = minRow;
            return this;
        }
        public int getMaxRow() {
            return maxRow;
        }
        public PicturesInfo setMaxRow(int maxRow) {
            this.maxRow = maxRow;
            return this;
        }
        public int getMinCol() {
            return minCol;
        }
        public PicturesInfo setMinCol(int minCol) {
            this.minCol = minCol;
            return this;
        }
        public int getMaxCol() {
            return maxCol;
        }
        public PicturesInfo setMaxCol(int maxCol) {
            this.maxCol = maxCol;
            return this;
        }
        public String getExt() {
            return ext;
        }
        public PicturesInfo setExt(String ext) {
            this.ext = ext;
            return this;
        }

		//有中间文件的 excel转pdf再加水印
//		public static void main(String[] args) throws FileNotFoundException {
//			String inputFile = "D:\\Downloads\\123.xlsx";
//			String outputFile = "D:\\Downloads\\999.pdf";
//			String watermarkedFile = "D:\\Downloads\\999_watermarked.pdf";
//
//			// 获取inputFile的文件输出流
//			FileInputStream fileInputStream = new FileInputStream(inputFile);
//
//			try {
//				// 创建输出流
//				OutputStream outputStream = new FileOutputStream(outputFile);
//
//				// 调用excelToPdf方法将Excel转换为PDF并写入输出流
//				excelToPdf(fileInputStream, outputStream, "xlsx");
//
//				//这个outputStream将里面的数据转成inputStream,后续要再做一次处理
//
//
//				// 关闭输入流和输出流
//				fileInputStream.close();
//
//				//==============================加水印=================================
//				// 创建输入流和输出流
//				FileInputStream pdfInputStream = new FileInputStream(outputFile);
//				ByteArrayOutputStream watermarkedOutputStream = PDFWatermarkExample.addWatermarkToPdfStream(pdfInputStream, "excel转PDF再加水印");
//
//				// 将加水印后的PDF写入文件
//				FileOutputStream watermarkedFileOutputStream = new FileOutputStream(watermarkedFile);
//				watermarkedOutputStream.writeTo(watermarkedFileOutputStream);
//
//				// 关闭输入流和输出流
//				pdfInputStream.close();
//				watermarkedOutputStream.close();
//				watermarkedFileOutputStream.close();
//
//
//				outputStream.close();
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}

		/**
		 * 通过使用内存流,可以直接从Excel文件转换为带水印的PDF文件,而不需要生成中间文件。
		 * @param args
		 * @throws FileNotFoundException
		 */
		public static void main(String[] args) throws FileNotFoundException {
			String inputFile = "D:\\Downloads\\123.xlsx";
			String watermarkedFile = "D:\\Downloads\\999_watermarked.pdf";

			try {
				// 创建输入流
				FileInputStream fileInputStream = new FileInputStream(inputFile);

				// 创建输出流
				ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();

				// 调用excelToPdf方法将Excel转换为PDF并写入输出流
				excelToPdf(fileInputStream, pdfOutputStream, "xlsx");

				// 关闭输入流
				fileInputStream.close();

				//==============================加水印=================================
				// 创建输入流
				ByteArrayInputStream pdfInputStream = new ByteArrayInputStream(pdfOutputStream.toByteArray());
				ByteArrayOutputStream watermarkedOutputStream = PDFWatermarkExample.addWatermarkToPdfStream(pdfInputStream, "excel转PDF再加水印");

				// 将加水印后的PDF写入文件
				FileOutputStream watermarkedFileOutputStream = new FileOutputStream(watermarkedFile);
				watermarkedOutputStream.writeTo(watermarkedFileOutputStream);

				// 关闭输入流和输出流
				pdfInputStream.close();
				watermarkedOutputStream.close();
				watermarkedFileOutputStream.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
    }
}

最后的代码就是通过内存流做的,避免中间文件。哎 写这个真是麻烦

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1683872.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

vue核心模块源码解析

响应式原理 Object.definePropertysetterProxy var count 1 var state {} Object.defineProperty(state , count,{get(){return count},set(val){count val} }) //弊端&#xff1a;不能主动监听到对象属性的新增或者删除&#xff0c;add/deleteref和reactive 声明响应式数…

Golang net/http标准库常用方法(二)

大家好&#xff0c;针对Go语言 net/http 标准库&#xff0c;将梳理的相关知识点分享给大家~~ 围绕 net/http 标准库相关知识点还有许多章节&#xff0c;请大家多多关注。 文章中代码案例只有关键片段&#xff0c;完整代码请查看github仓库&#xff1a;https://github.com/hltfa…

K8S认证|CKA题库+答案| 1. 权限控制RBAC

1、权限控制RBAC 您必须在以下Cluster/Node上完成此考题&#xff1a; Cluster Master node Worker node k8s master …

【云原生】Kubernetes 核心概念

什么是 Kubernetes Kubernetes&#xff0c;从官方网站上可以看到&#xff0c;它是一个工业级的容器编排平台。Kubernetes 这个单词是希腊语&#xff0c;它的中文翻译是“舵手”或者“飞行员”。在一些常见的资料中也会看到“ks”这个词&#xff0c;也就是“k8s”&#xff0c;它…

2024年信息素养大赛图形化编程、Python、算法创真题汇总

2024年信息素养大赛编程赛道初赛&#xff08;Scratch图形化编程、Python、C算法创意&#xff09;已经结束&#xff0c;根据Scratch实验室的了解全国青少年信息素养大赛初赛晋级及初赛成绩内容如下&#xff1a; 1.参赛选手将在 5 个工作日(节假日不计在内)内&#xff0c;通过信…

开源的数据标注工具--Label-Studio

最近在了解构建知识图谱的相关知识&#xff0c;收集了一些数据&#xff0c;对数据进行标注时尝试了下Label-Studio这个工具&#xff0c;它是开源的数据标注工具&#xff0c;个人觉得还是挺好用的。 Label-studio的安装 我是直接在服务器上用pip安装的&#xff0c;命令如下&am…

【刷题篇】位运算

文章目录 1、判定字符是否唯一2、丢失的数字3、两整数之和4、只出现一次的数字 II5、 消失的两个数字 1、判定字符是否唯一 class Solution { public:bool isUnique(string astr) {int nastr.size();if(n>26)//鸽巢原理return false;int bitMap0;for(auto& e : astr){in…

echart 折线图tooltip

运行结果 代码 import { truncate, merge } from lodash; import { getBasePieOptions, getTooltipFormatter } from "*/money/utils";const colorArray [#1F8BFF, #EDBE75, #26E3F0, #AF8FFF, #61DDAA, #FD996A, #8367E0, #1AAF87]export function getLineOptions…

大数据运维学习笔记之Ambari——筑梦之路

原则&#xff1a;分布式存储和分布式计算分开 今天就到这里啦。

以色列人Andi Gutmans开发的php zend

虽然目前php语言不行了【相关的文章前几年已经有人发过】&#xff0c;但这不是重点&#xff0c;重点是zend引擎的东西具有极大的技术价值&#xff0c;负责zend引擎实现的大佬都现在差不多都是40&#xff0c;50岁左右了&#xff0c;从1997&#xff0c;1998&#xff0c;2000到202…

记录centos中操作(查找、结束、批量)进程以及crontab定时写法的知识

环境&#xff1a;vps&#xff0c;centos7&#xff0c;python3。 近期写了个python程序&#xff0c;用青龙面板在centos上运行。程序中有while无限循环&#xff0c;但是我在青龙中设置了定时任务&#xff08;每隔半小时运行一次&#xff09;&#xff0c;于是造成了进程中有多个…

一个用Java编写的屏幕测距工具,包括游戏地图测量功能

该程序提供了一个简单便捷的方式&#xff0c;在屏幕上测量距离&#xff0c;包括游戏地图分析在内。它允许用户准确确定屏幕上两点之间的距离&#xff0c;帮助游戏过程中的战略规划、资源管理和决策制定。 特点&#xff1a; 简单易用的界面&#xff1a;直观的控制使测量距离变得…

C++的红黑树

目录 基本概念 插入结点的颜色 判断性质是否破坏 调整方式 u为g的右孩子 u存在且为红 u存在且为黑 u不存在 结论 红黑树结点定义 代码实现 基本概念 1、红黑树是一种特殊的二叉搜索树&#xff0c;每个结点会增加一个存储位表示结点的颜色&#xff08;红或黑&#x…

如何将老板的游戏机接入阿里云自建K8S跑大模型(下)- 安装nvidia/gpu-operator支持GPU在容器中共享

文章目录 安装nvidia/gpu-operator支持GPU在容器中共享 安装nvidia/gpu-operator支持GPU在容器中共享 安装 nvidia/gpu-operator遇到两个问题&#xff1a; 由于我们都懂的某个原因&#xff0c;导致某些镜像一直现在不成功。 解决办法&#xff0c;准备一个&#x1fa9c;&#…

如何理解kmp的套娃式算法啊?

概念 KMP算法&#xff0c;全称Knuth Morris Pratt算法 。文章大部分内容出自《数据结构与算法之美》 核心思想 假设主串是a&#xff0c;模式串是b 在模式串与主串匹配的过程中&#xff0c;当遇到不可匹配的字符的时候&#xff0c;对已经对比过的字符&#xff0c;是否能找到…

FME学习之旅---day27

我们付出一些成本&#xff0c;时间的或者其他&#xff0c;最终总能收获一些什么。 教程&#xff1a;Excel 入门 查看和检查 Excel 数据 1.读模块读取EXCEL文件 2.对源数据进行预览 Excel Reader 参数 |将 Excel 转换为 CSV 阅读 2020 年和平均值工作表&#xff0c;然后计算降…

运行Android项目时,提示错误: 程序包javax.annotation.processing不存在

今天在运行项目时提示错误: 错误: 程序包javax.annotation.processing不存在 import javax.annotation.processing.Generated; 最后是修改了Android Studio的JDK的路径修改为你安装的JDK路径&#xff0c;完成的修复&#xff1a;

thinkphp 多条件查询 不起作用 = like

不起作用的代码&#xff1a; &#xff08; where([category_id > $item[id]]) 没起作用 &#xff09; 传递参数 $model->where(product_name, like, "%$productName%") 不起作用 public function cateProductPage() {$builder new Builder(new CategoryMod…

基于transformers框架实践Bert系列4-文本相似度

本系列用于Bert模型实践实际场景&#xff0c;分别包括分类器、命名实体识别、选择题、文本摘要等等。&#xff08;关于Bert的结构和详细这里就不做讲解&#xff0c;但了解Bert的基本结构是做实践的基础&#xff0c;因此看本系列之前&#xff0c;最好了解一下transformers和Bert…

利用神经网络学习语言(一)——自然语言处理的基本要素

相关说明 这篇文章的大部分内容参考自我的新书《解构大语言模型&#xff1a;从线性回归到通用人工智能》&#xff0c;欢迎有兴趣的读者多多支持。 本文涉及到的代码链接如下&#xff1a;regression2chatgpt/ch10_rnn/tokenizer.ipynb 本系列文章将深入探讨一种应用广泛的神经…