/* * (non-Javadoc) * * @see com.afunms.report.ExportInterface#insertChart(java.lang.String) */ public void insertChart(String path) throws Exception { if (!document.isOpen()) { document.open(); } Image png = Image.getInstance(path); // png.scaleAbsolute(560, 320); png.scalePercent(90); Table pngtable = new Table(1); pngtable.setAutoFillEmptyCells(true); pngtable.setAlignment(Element.ALIGN_CENTER); pngtable.setCellsFitPage(true); pngtable.setWidth(100); pngtable.setBorder(0); RtfCell cell = new RtfCell(png); cell.setBorder(0); pngtable.addCell(cell); document.add(pngtable); }
/* * (non-Javadoc) * * @see com.afunms.report.ExportInterface#insertTitle(java.lang.String, int, * java.lang.String) */ public void insertTitle(String title, int colspan, String timefromto) throws Exception { if (!document.isOpen()) { document.open(); } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Paragraph par1 = new Paragraph(title, new Font(bfChinese, 20, Font.BOLD)); Paragraph par2 = new Paragraph("中国气象局卫星中心", new Font(bfChinese, 14, Font.BOLD)); Paragraph time1 = new Paragraph("报表生成时间:" + sdf.format(new Date()), new Font(bfChinese, 14, Font.BOLD)); Paragraph time2 = new Paragraph("报表统计时间:" + sdf.format(new Date()), new Font(bfChinese, 14, Font.BOLD)); par1.setAlignment(Element.ALIGN_TOP); par2.setAlignment(Element.ALIGN_CENTER); time1.setAlignment(Element.ALIGN_UNDEFINED); time2.setAlignment(Element.ALIGN_CENTER); time1.spacingBefore(); document.add(par1); document.add(par2); document.add(time1); document.add(time2); }
/* * (non-Javadoc) * * @see com.afunms.report.ExportInterface#insertTable(java.util.ArrayList) */ public void insertTable(ArrayList<String[]> tableal) throws Exception { // step 3: we open the document if (!document.isOpen()) { document.open(); } Table pdfTable = new Table(tableal.get(0).length); for (int k = 0; k < tableal.size(); k++) { String[] row = tableal.get(k); for (int j = 0; j < row.length; j++) { Cell pdfcell = new Cell(); if (k == 0) { pdfcell.addElement(new Paragraph(row[j], FontChineseTitle)); pdfcell.setBackgroundColor(Color.gray); pdfTable.endHeaders(); } else { pdfcell.addElement(new Paragraph(row[j], FontChineseRow)); if (k % 2 == 0) { pdfcell.setBackgroundColor(Color.LIGHT_GRAY); } } // 合并单元格 // pdfcell.setColspan(1); // pdfcell.setRowspan(1); // 对齐方式 pdfcell.setHorizontalAlignment(Element.ALIGN_CENTER); pdfcell.setVerticalAlignment(Element.ALIGN_MIDDLE); pdfTable.addCell(pdfcell); } } pdfTable.setWidth(100); // 设置表格填距 pdfTable.setPadding(5); pdfTable.setAlignment(Element.ALIGN_CENTER); // pdfTable.setTableFitsPage(true); document.add(pdfTable); // step 5: we close the document }
public static void concatPDFs( List<InputStream> streamOfPDFFiles, InputStream fs, OutputStream outputStream, boolean paginate) throws IOException { Document document = new Document(new PdfReader(fs).getPageSize(1)); try { List<InputStream> pdfs = streamOfPDFFiles; List<PdfReader> readers = new ArrayList<PdfReader>(); int totalPages = 0; Iterator<InputStream> iteratorPDFs = pdfs.iterator(); // Create Readers for the pdfs. while (iteratorPDFs.hasNext()) { InputStream pdf = iteratorPDFs.next(); PdfReader pdfReader = new PdfReader(pdf); readers.add(pdfReader); totalPages += pdfReader.getNumberOfPages(); } // Create a writer for the outputstream PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); PdfContentByte cb = writer.getDirectContent(); // Holds the PDF // data PdfImportedPage page; int currentPageNumber = 0; int pageOfCurrentReaderPDF = 0; Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; currentPageNumber++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); // Code for pagination. if (paginate) { cb.beginText(); cb.setFontAndSize(bf, 9); cb.showTextAligned( PdfContentByte.ALIGN_CENTER, "" + currentPageNumber + " of " + totalPages, 520, 5, 0); cb.endText(); } } pageOfCurrentReaderPDF = 0; } outputStream.flush(); document.close(); outputStream.close(); } catch (Exception e) { log.error(e.getLocalizedMessage(), e); } finally { if (document.isOpen()) document.close(); try { if (outputStream != null) outputStream.close(); } catch (IOException ioe) { log.error(ioe.getLocalizedMessage(), ioe); } } }
/** * Closes the document if it is open. * * @param document The document to close. */ public static void closeDocument(Document document) { if (document.isOpen()) { document.close(); } }