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

首先先引入包:加水印和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();
			}
		}
    }
}

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

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【Numpy】深入解析numpy.diag()函数

numpy.diag()&#xff1a;深入探索NumPy库中的对角矩阵操作 &#x1f308; 欢迎莅临我的个人主页&#x1f448;这里是我深耕Python编程、机器学习和自然语言处理&#xff08;NLP&#xff09;领域&#xff0c;并乐于分享知识与经验的小天地&#xff01;&#x1f387; &#x1f3…

统计绘图:基于matplotlib包绘制双轴图

本文介绍通过 Python的matplotlib包 绘制 双轴图&#xff08;Dual Y-Axis Plot&#xff09;&#xff0c;即双 y 轴图&#xff0c;又称双纵坐标图。 这类图表的主要用途是显示两个具有不同数值范围的变量在同一 x 轴上的 变化趋势&#xff0c;从而便于比较和分析。&#xff08;…

docker中安装jenkins,并在node和cloud上跑通基于源码控制SCM的pipeline

目录 一、摘要 二、部署和使用 1. docker部署jenkins 1.1 准备数据目录 1.2 拉取jenkins镜像并启动 1.3 初始化配置 1.3.1 登录容器查看初始化密码 1.3.2 访问jenkins并输入初始化密码 1.3.3 创建管理员账户 1.3.4 初始化完成 2. jenkins使用之多分支流水线 2.1 准…

大模型落地竞逐,云计算大厂“百舸争流”

作者 | 辰纹 来源 | 洞见新研社 从ChatGPT到Sora&#xff0c;从图文到视频&#xff0c;从通用大模型到垂直大模型……经过了1年多时间的探索&#xff0c;大模型进入到以落地为先的第二阶段。 行业的躁动与资本的狂热相交汇&#xff0c;既造就了信仰派的脚踏实地&#xff0c;也…

适用于当下的红色系统可视化大屏,大量图。

特定场合下使用红色系可视化大屏是可以的&#xff0c;但是千万要注意时间和场合&#xff0c;平时最好别用。

获取支持Windows7的最新Edge离线版本

从110版本开始&#xff0c;微软Edge和谷歌停止了对Win7、Win8/8.1的支持&#xff0c;后续又发布了几版安全更新&#xff0c;截止目前为止&#xff0c;能支持Win7的版本是 109.0.1518.140。 如果你想用最新版本谷歌浏览器&#xff0c;可以考虑下Supermium&#xff0c;这个浏览器…

内存马实战(持续更新中)

注&#xff1a;这篇文章记录在我的语雀里面&#xff0c;语雀格式好看一点&#xff0c;地址&#xff1a; https://ganmaocai.yuque.com/ghgp8x/zoy1yn/faet35ae9gpxzn61 计划 复现以下框架的内存马注入&#xff1a; shiro&#xff1a; 普通内存马 冰蝎马 WebSocket马 xxl-job…

C++ 计时器

文章目录 一、简介二、实现代码2.1 windows平台2.2 C标准库 三、实现效果 一、简介 有时候总是会用到一些计时的操作&#xff0c;这里也整理了一些代码&#xff0c;包括C标准库以及window自带的时间计算函数。 二、实现代码 2.1 windows平台 StopWatch.h #ifndef STOP_WATCH_H…

JWT的详解

一.什么是JWT JWT&#xff08;JSON Web Token&#xff09;是一种开放标准&#xff08;RFC 7519&#xff09;&#xff0c;用于在网络应用间安全地传递信息。它是一种紧凑的、自包含的方式&#xff0c;用于在用户和服务之间以 JSON 对象的形式安全地传输信息。 JWT 主要由三部分…

计算机系统基础 7 分支程序的实现

简单条件转移指令 根据单个标志位的值&#xff08;CF&#xff0c; SF&#xff0c;OF&#xff0c;PF&#xff0c;ZF&#xff09;来确定是否转移&#xff0c; 如果条件成立&#xff0c;则&#xff08;EIP&#xff09; 位移量 ➡ EIP&#xff0c;否则什么也不做。 注意&#xff0…

c# 将数据库连接字符串写到配置文件中,及获取

考虑到代码的安全性&#xff0c;已经修改起来的方便性&#xff0c;我们常常不会将数据库连接字符串直接放在代码中&#xff0c;而是将这个字符串放到一个App.config配置文件中&#xff0c;赋值给一个变量&#xff0c;然后再在代码中引用这个变量。 具体做法如下: ①展开项目名称…

微星笔记本618爆款推荐清单,好评有礼活动火热进行中

微星笔记本618爆款推荐清单&#xff0c;好评有礼活动火热进行中 又是一年一度的618大促&#xff0c;作为电子数码产品的主场&#xff0c;准备选购笔记本的消费者早已翘首以盼有更实惠的价格~ 不负期待&#xff0c;微星笔记本携多款性价比爆款笔记本、Claw掌上游戏机&#xff0…

Google Find My Device:科技守护,安心无忧

在数字化的时代&#xff0c;我们的生活与各种智能设备紧密相连。而 Google Find My Device 便是一款为我们提供安心保障的实用工具。 一、Find My Decice Netword的定义 谷歌的Find My Device Netword旨在通过利用Android设备的众包网络的力量&#xff0c;帮助用户安全的定位所…

记录一个更新adobe软件导致加载PDF文件异常的问题

最近由于项目需要,没有办法把原有的adobe正版软件进行了卸载,换了个盗版软件,结果导致我的pdf文件加载的时候出现异常。 报错的语句是这个 string str = System.Environment.CurrentDirectory; // string fileName = MyOpenFileDialog(); axAcroPDF1.LoadFile(…

abs(-2147483648) == 2147483648?

从数学意义上&#xff0c;这是对的。但是&#xff0c;就怕但是。 #include int main() {long long v;v abs(-2147483648);printf("%lld\n", v);return 0; } 输出: -2147483648 我们从source code中一一解开. /* Return the absolute value of I. */ int abs (…

uniapp星空效果

uniapp星空效果 背景实现思路代码实现尾巴 背景 之前在网上看到过一个视频&#xff0c;使用纯css实现过一个星空效果。具体出处找不到了&#xff0c;我们按照他那个思路来实现一个类似的效果&#xff0c;还是先上一张图镇楼&#xff1a; 实现思路 首先我们这个效果使用的是…

Php composer 基础教程

一、什么是Composer&#xff1f; Composer 是 PHP 中的依赖管理工具。它允许声明项目所依赖的库&#xff0c;并且它将为您管理&#xff08;安装/更新&#xff09;它们。 二、如何安装&#xff1f; Linux 系统和 MacOS 系统 直接下载最新稳定版&#xff1a; 然后执行下列命令&…

去中心化的 S3,CESS 首创去中心化对象存储 DeOSS

Web3 在各个领域的应用和发展已成为讨论的焦点&#xff0c;尽管行业对 Web3 的定义各不相同&#xff0c;但一个普遍的共识是 Web3 赋予了用户对其数据的所有权和自主权。这一转变在我们的生活和工作与数字化越来越深入地融合之际至关重要&#xff0c;这意味着所有人类活动很快将…

【Linux】Centos7安装JDK

【Linux】Centos7安装JDK 下载 Oracle 官网下载 JDK17 https://www.oracle.com/cn/java/technologies/downloads/#java17 安装 使用rz命令上传 jdk tar 包&#xff0c;上传失败直接用 xftp 上传 在安装图形界面时&#xff0c;有勾选开发工具&#xff0c;会自动安装 JDK 需要先…

D435相机内参标定(无法直接应用在相机上)

打开roscore和相机 输入 rostopic echo /camera/color/camera_info 从而得到相机的内参 cameraInfo包含D、K、R、P四个矩阵。 矩阵D是失真系数&#xff0c;包括(k1, k2, t1, t2, k3) 矩阵K是相机内参&#xff0c;即 矩阵R是一个3✖3的旋转矩阵&#xff0c;仅对双目相机有效&…