package com.doumee.core.utils;//package com.doumee.core.utils; // //import java.awt.image.BufferedImage; //import java.io.ByteArrayOutputStream; //import java.io.File; //import java.io.FileOutputStream; //import java.io.IOException; //import java.net.URL; //import java.util.stream.IntStream; //import javax.imageio.ImageIO; //import org.apache.poi.ss.usermodel.Cell; //import org.apache.poi.ss.usermodel.Row; //import org.apache.poi.ss.usermodel.Sheet; //import org.apache.poi.ss.usermodel.Workbook; //import org.apache.poi.ss.usermodel.WorkbookFactory; //import org.apache.poi.xssf.usermodel.XSSFClientAnchor; //import org.apache.poi.xssf.usermodel.XSSFDrawing; //import org.apache.poi.xssf.usermodel.XSSFGraphicFrame; //import org.apache.poi.xssf.usermodel.XSSFShape; //import org.apache.poi.xssf.usermodel.XSSFSheet; //import org.apache.poi.xssf.usermodel.XSSFWorkbook; //import com.itextpdf.text.BaseColor; //import com.itextpdf.text.Document; //import com.itextpdf.text.DocumentException; //import com.itextpdf.text.Element; //import com.itextpdf.text.Font; //import com.itextpdf.text.Image; //import com.itextpdf.text.PageSize; //import com.itextpdf.text.Paragraph; //import com.itextpdf.text.pdf.PdfPCell; //import com.itextpdf.text.pdf.PdfPTable; //import com.itextpdf.text.pdf.PdfWriter; // //public class TestUtil { // public static void main(String[] args) { // try (Workbook workbook = WorkbookFactory.create(new File("example.xlsx")); // FileOutputStream fos = new FileOutputStream("example.pdf")) { // Document document = new Document(PageSize.A4, 50, 50, 50, 50); // PdfWriter writer = PdfWriter.getInstance(document, fos); // document.open(); // for (Sheet sheet : workbook) { // PdfPTable table = new PdfPTable(sheet.getRow(0).getLastCellNum()); // table.setHeaderRows(1); // table.setWidths(IntStream.range(0, sheet.getRow(0).getLastCellNum()).mapToDouble(i -> 1).toArray()); // table.setWidthPercentage(100); // Paragraph title = new Paragraph(sheet.getSheetName(), new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 16, Font.BOLD)); // title.setAlignment(Element.ALIGN_CENTER); // document.add(title); // sheet.forEach(row -> { // IntStream.range(0, row.getLastCellNum()).forEach(i -> { // Cell cell = row.getCell(i); // PdfPCell pdfCell = null; // try { // pdfCell = new PdfPCell(new Paragraph(cell.getStringCellValue(), new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 12))); // } catch (DocumentException e) { // throw new RuntimeException(e); // } catch (IOException e) { // throw new RuntimeException(e); // } // pdfCell.setBorderWidth(1f); // pdfCell.setBorderColor(BaseColor.BLACK); // pdfCell.setPadding(5f); // if (row.getRowNum() == 0) { // pdfCell.setBackgroundColor(BaseColor.LIGHT_GRAY); // } // table.addCell(pdfCell); // if (cell.getCellType() == CellType.BLANK || cell.getCellType() == CellType.FORMULA || cell.getCellType() == CellType.ERROR || cell.getCellType() == CellType.STRING && cell.getStringCellValue().trim().isEmpty() || cell.getCellType() == CellType.NUMERIC && cell.getNumericCellValue() == 0 || cell.getCellType() == CellType.BOOLEAN && !cell.getBooleanCellValue() || cell.getCellType() == CellType.STRING && cell.getStringCellValue().startsWith("#") || cell.getCellType() == CellType.FORMULA && cell.getCellFormula().startsWith("#") || cell.getCellType() == CellType.STRING && cell.getStringCellValue().startsWith("=") || cell.getCellType() == CellType.FORMULA && cell.getCellFormula().startsWith("=") || cell.getCellType() == CellType.STRING && cell.getStringCellValue().startsWith("@") || cell.getCellType() == CellType.FORMULA && cell.getCellFormula().startsWith("@") || cell.getCellType() == CellType.STRING && cell.getStringCellValue().startsWith("'") || cell.getCellType() == CellType.FORMULA && cell.getCellFormula().startsWith("'")) { // return; // } // if (cell.getCellType() == CellType.STRING && cell.getStringCellValue().startsWith("http")) { // try { // BufferedImage image = ImageIO.read(new URL(cell.getStringCellValue())); // if (image != null) { // addImageToTable(table, image); // } // } catch (Exception e) { // e.printStackTrace(); // } // } else { // if (sheet instanceof XSSFSheet) { // XSSFSheet xssfSheet = (XSSFSheet) sheet; // XSSFDrawing drawing = xssfSheet.createDrawingPatriarch(); // for (XSSFShape shape : drawing.getShapes()) { // if (shape instanceof XSSFClientAnchor) { // XSSFClientAnchor anchor = (XSSFClientAnchor) shape.getAnchor(); // if (anchor.getRow1() == cell.getRowIndex() && anchor.getCol1() == cell.getColumnIndex()) { // if (shape instanceof XSSFGraphicFrame) { // XSSFGraphicFrame frame = (XSSFGraphicFrame) shape; // BufferedImage image = frame.getGraphic().getBufferedImage(); // try { // addImageToTable(table, image); // } catch (Exception e) { // throw new RuntimeException(e); // } // } // } // } // } // } // } // }); // }); // table.setSpacingBefore(20f); // table.setSpacingAfter(20f); // table.setKeepTogether(true); // document.add(table); // } // document.close(); // } catch (Exception e) { // e.printStackTrace(); // } // } // // private static void addImageToTable(PdfPTable table, BufferedImage image) throws Exception { // ByteArrayOutputStream baos = new ByteArrayOutputStream(); // ImageIO.write(image, "png", baos); // baos.flush(); // byte[] imageData = baos.toByteArray(); // baos.close(); // Image pdfImage = Image.getInstance(imageData); // pdfImage.scaleToFit(100f, 100f); // PdfPCell imageCell = new PdfPCell(pdfImage, true); // imageCell.setBorderWidth(1f); // imageCell.setBorderColor(BaseColor.BLACK); // imageCell.setPadding(5f); // table.addCell(imageCell); // } //}