/* * DynamicReports - Free Java reporting library for creating reports dynamically * * Copyright (C) 2010 - 2018 Ricardo Mariaca and the Dynamic Reports Contributors * * This file is part of DynamicReports. * * DynamicReports is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * DynamicReports is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with DynamicReports. If not, see <http://www.gnu.org/licenses/>. */ package net.sf.dynamicreports.examples.sunny.templates; import com.lowagie.text.DocumentException; import com.lowagie.text.Image; import com.lowagie.text.pdf.*; import net.sf.dynamicreports.examples.Templates; import net.sf.dynamicreports.jasper.builder.export.JasperPdfExporterBuilder; import net.sf.dynamicreports.jasper.constant.PdfPermission; import net.sf.dynamicreports.report.builder.component.ComponentBuilder; import net.sf.dynamicreports.report.builder.component.HorizontalListBuilder; import net.sf.dynamicreports.report.builder.component.VerticalListBuilder; import net.sf.dynamicreports.report.constant.ComponentPositionType; import net.sf.dynamicreports.report.constant.HorizontalTextAlignment; import net.sf.dynamicreports.report.constant.ImageScale; import net.sf.dynamicreports.report.constant.PageType; import net.sf.dynamicreports.report.datasource.DRDataSource; import net.sf.dynamicreports.report.exception.DRException; import net.sf.jasperreports.engine.JRDataSource; import net.sf.jasperreports.export.SimplePdfExporterConfiguration; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import java.util.stream.IntStream; import static net.sf.dynamicreports.report.builder.DynamicReports.*; /** * <p>EncryptedPdfReport class.</p> * * Please ensure you have write-access to local disc C:\ before running this example * * @author Ricardo Mariaca * */ public class EncryptedPdfReport { /** * <p>Constructor for EncryptedPdfReport.</p> */ public EncryptedPdfReport() { build(); } /** * <p>main.</p> * * @param args an array of {@link String} objects. */ public static void main(String[] args) { new EncryptedPdfReport(); } private void build() { try { JasperPdfExporterBuilder pdfExporter = export.pdfExporter("D:/report1.pdf") // .setEncrypted(true).setUserPassword("1234") ; pdfExporter.addPermission(PdfPermission.PRINTING).setEncrypted(true).setOwnerPassword("1234"); report().setTemplate(Templates.reportTemplate) .columns(col.column("Item", "item", type.stringType()), col.column("Quantity", "quantity", type.integerType()), col.column("Unit price", "unitprice", type.bigDecimalType())) .title(Templates.createTitleComponent("EncryptedPdfReport")) .pageFooter(Templates.footerComponent) .setDataSource(createDataSource()) .summary(totalInfo()).setSummaryStyle(stl.style().setTopPadding(10)) .setPageFormat(PageType.A3) .toPdf(pdfExporter); } catch (DRException e) { e.printStackTrace(); } } private ComponentBuilder<?,?>[] totalInfo() { List<ComponentBuilder<?,?>> list=new ArrayList<ComponentBuilder<?,?>>(); VerticalListBuilder summaryRowInfo=cmp.verticalList(); initSign(summaryRowInfo); list.add(summaryRowInfo); return list.toArray(new ComponentBuilder<?, ?>[list.size()]); } private void initSign(VerticalListBuilder moduleList) { ComponentBuilder<?, ?> sign = cmp.image(Templates.class.getResource("images/ls_sign.png")) .setStyle(stl.style() // .setVerticalImageAlignment(VerticalImageAlignment.BOTTOM) .setImageScale(ImageScale.RETAIN_SHAPE) ) .setFixedWidth(180).setFixedHeight(100).setPositionType(ComponentPositionType.FIX_RELATIVE_TO_TOP) ; VerticalListBuilder list = cmp.verticalList(); // list.add(cmp.text("姓名: 赵斌"),sign); // list.add(coulmn); list.add(cmp.xyList().add(590, 10, sign).add(660, 20, cmp.text("日期: 2023-06-30")).add(660, 40, cmp.text("姓名: 赵斌"))); moduleList.add(list); } private JRDataSource createDataSource() { DRDataSource dataSource = new DRDataSource("item", "quantity", "unitprice"); IntStream.range(0, 20).forEach(i -> dataSource.add("Book", (int) (Math.random() * 10) + 1, BigDecimal.valueOf(Math.random() * 100 + 1))); return dataSource; } }