From 32119af862a2ab8b04fd7454aa7912119d3d9a1f Mon Sep 17 00:00:00 2001 From: richie Date: Wed, 25 Sep 2019 17:19:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=89=93=E5=8D=B0=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E7=9A=84API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/fanruan/api/report/PrintKit.java | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/main/java/com/fanruan/api/report/PrintKit.java diff --git a/src/main/java/com/fanruan/api/report/PrintKit.java b/src/main/java/com/fanruan/api/report/PrintKit.java new file mode 100644 index 0000000..de8476f --- /dev/null +++ b/src/main/java/com/fanruan/api/report/PrintKit.java @@ -0,0 +1,112 @@ +package com.fanruan.api.report; + +import com.fanruan.api.log.LogKit; +import com.fr.base.Margin; +import com.fr.main.FineBook; +import com.fr.page.ClippedPageProvider; +import com.fr.page.PaperSettingProvider; +import com.fr.page.ReportPageProvider; +import com.fr.page.ReportSettingsProvider; +import com.fr.report.cell.CellElement; +import com.fr.report.cell.cellattr.CellGUIAttr; +import com.fr.report.stable.ReportSettings; +import com.fr.stable.Constants; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019/9/25 + * 报表打印导出相关工具类 + */ +public class PrintKit { + + private PrintKit() { + + } + + /** + * 根据模板获取打印/导出的页面设置 + * + * @param book 模板 + * @return 多个待打印对象的页面设置集合 + */ + public static List getPaperSettings(FineBook book) { + return PrintKit.getPaperSettings(book, null); + } + + /** + * 根据模板获取打印/导出的页面设置 + * + * @param book 模板 + * @param offsetMargin 偏移量 + * @return 多个待打印对象的页面设置集合 + */ + public static @Nullable List getPaperSettings(FineBook book, Margin offsetMargin) { + if (book == null) { + return null; + } + List list = new ArrayList<>(); + ReportSettingsProvider reportSettings; + for (int i = 0; i < book.getReportCount(); i++) { + reportSettings = book.getReport(i).getReportSettings(); + if (reportSettings == null) { + reportSettings = ReportSettings.DEFAULTSETTINGS; + } + try { + PaperSettingProvider setting = (PaperSettingProvider) reportSettings.getPaperSetting().clone(); + Margin margin = setting.getMargin(); + /* + * 偏移量top、left为加,button 、right为减 + */ + if (offsetMargin != null) { + margin.setTop(margin.getTop().add(offsetMargin.getTop())); + margin.setLeft(margin.getLeft().add(offsetMargin.getLeft())); + margin.setBottom(margin.getBottom().subtract(offsetMargin.getBottom())); + margin.setRight(margin.getRight().subtract(offsetMargin.getRight())); + } + list.add(setting); + } catch (CloneNotSupportedException e) { + LogKit.error(e.getMessage(), e); + break; + } + } + return list; + } + + /** + * 选取分页结果对象中可导出的对象,如果没有可导出对象,会返回null。 + * + * @param reportPage 分页结果对象 + * @return 可导出对象 + */ + public static @Nullable ClippedPageProvider pick(ReportPageProvider reportPage) { + if (reportPage == null) { + return null; + } + ClippedPageProvider[] clippers = reportPage.getPages(); + if (clippers.length == 1 && clippers[0] != null) { + ClippedPageProvider cp = clippers[0]; + cp.deriveResolution(Constants.FR_PAINT_RESOLUTION); + return cp; + } + return null; + } + + /** + * 单元格是否可打印/导出,典型的如:以html格式显示的单元格,就是不可打印/导出的 + * + * @param cell 单元格 + * @return 单元格内容可打印/导出则返回true,否则返回false + */ + public static boolean isCellPrintable(CellElement cell) { + if (cell == null) { + return false; + } + CellGUIAttr gui = cell.getCellGUIAttr(); + return gui == null || gui.isPrintContent(); + } +}