ColumnText ct = new ColumnText(cb); Phrase myText = new Phrase("TEST paragraph\nAfter Newline"); ct.setSimpleColumn(myText, 34, 750, 580, 317, 15, Element.ALIGN_LEFT); ct.go();
int efficientPDFPageCount(String path) { RandomAccessFileOrArray file = new RandomAccessFileOrArray(path, false, true ); PdfReader reader = new PdfReader(file); int ret = reader.getNumberOfPages(); reader.close(); return ret; }
PdfDictionary pageDict = myReader.getPageN(1); PdfArray annots = pageDict.getAsArray(PdfName.ANNOTS); ArrayList<String> dests = new ArrayList<String>(); if (annots != null) { for (int i = 0; i < annots.size(); ++i) { PdfDictionary annotDict = annots.getAsDict(i); PdfName subType = annotDict.getAsName(PdfName.SUBTYPE); if (subType != null && PdfName.LINK.equals(subType)) { PdfDictionary action = annotDict.getAsDict(PdfName.A); if (action != null && PdfName.URI.equals(action.getAsName(PdfName.S)) { dests.add(action.getAsString(PdfName.URI).toString()); } // else { its an internal link, meh } } } }
Document document=new Document(pageSize,leftMargin,rightMargin,topMargin,bottomMargin); try{ PdfWriter writer=PdfWriter.getInstance(document,outputStream); PageHeaderFooterEvent headerFooterEvent=new PageHeaderFooterEvent(report); writer.setPageEvent(headerFooterEvent); document.open(); List<Column> columns=report.getColumns(); Map<Row, Map<Column, Cell>> cellMap=report.getRowColCellMap(); for(List<Page> pages:list){ PdfPTable table=new PdfPTable(size); table.setLockedWidth(true); table.setTotalWidth(w); table.setWidths(widths); table.setHorizontalAlignment(Element.ALIGN_LEFT); int ps=pages.size(); for(int i=0;i<ps;i++){ if(i>0){ PdfPCell pdfMarginCell=new PdfPCell(); pdfMarginCell.setBorder(Rectangle.NO_BORDER); table.addCell(pdfMarginCell); PdfPTable childTable=new PdfPTable(colSize); childTable.setLockedWidth(true); childTable.setTotalWidth(totalWidth); childTable.setWidths(columnsWidth); childTable.setHorizontalAlignment(Element.ALIGN_LEFT); List<Row> rows=page.getRows(); for(Row row:rows){
PdfDocument pdf = new PdfDocument(); PdfWriter writer = PdfWriter.getInstance(pdf, outputStream); pdf.addWriter(writer); int w = (int) (pageSize.getWidth() * 0.9); int h = (int) (pageSize.getHeight() * 0.95); PdfContentByte cb = writer.getDirectContent(); PdfTemplate tp = cb.createTemplate(w, h); Graphics2D g2 = tp.createPrinterGraphics(w, h, null); tp.setWidth(w); tp.setHeight(h); cb.addTemplate(tp, offsetX, offsetY);
if(StringUtils.isNotEmpty(left)){ if(StringUtils.isNotEmpty(center) && StringUtils.isNotEmpty(right)){ table = new PdfPTable(3); table.setWidths(new int[]{1, 1, 1}); table.addCell(buildPdfPCell(hf,left,1)); table.addCell(buildPdfPCell(hf,center,2)); table.addCell(buildPdfPCell(hf,right,3)); }else if(StringUtils.isNotEmpty(center)){ table = new PdfPTable(3); table.setWidths(new int[]{1, 1, 1}); table.addCell(buildPdfPCell(hf,left,1)); table.addCell(buildPdfPCell(hf,center,2)); table.addCell(buildPdfPCell(hf,"",3)); }else if(StringUtils.isNotEmpty(right)){ table = new PdfPTable(3); table.setWidths(new int[]{1, 1, 1}); table.addCell(buildPdfPCell(hf,left,1)); table.addCell(buildPdfPCell(hf,"",2)); table.addCell(buildPdfPCell(hf,right,3)); }else{ table = new PdfPTable(1); table.setWidths(new int[]{1}); table.addCell(buildPdfPCell(hf,left,1)); table = new PdfPTable(3); table.setWidths(new int[]{1, 1, 1}); table.addCell(buildPdfPCell(hf,"",1)); table.addCell(buildPdfPCell(hf,center,2)); table.addCell(buildPdfPCell(hf,right,3));
CellStyle colStyle=cellInfo.getColumn().getCustomCellStyle(); PdfPCell cell=newPdfCell(cellInfo,cellHeight); cell.setPadding(0); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new CellBorderEvent(style,customStyle)); int rowSpan=cellInfo.getPageRowSpan(); if(rowSpan>0){ cell.setRowspan(rowSpan); cell.setColspan(colSpan); cell.setHorizontalAlignment(Element.ALIGN_LEFT); }else if(align.equals(Alignment.center)){ cell.setHorizontalAlignment(Element.ALIGN_CENTER); }else if(align.equals(Alignment.right)){ cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_TOP); }else if(valign.equals(Alignment.middle)){ cell.setVerticalAlignment(Element.ALIGN_MIDDLE); }else if(valign.equals(Alignment.bottom)){ cell.setVerticalAlignment(Element.ALIGN_BOTTOM); cell.setBackgroundColor(new BaseColor(Integer.valueOf(colors[0]),Integer.valueOf(colors[1]),Integer.valueOf(colors[2])));
private PdfPCell buildPdfPCell(HeaderFooter phf,String text,int type){ PdfPCell cell=new PdfPCell(); cell.setPadding(0); cell.setBorder(Rectangle.NO_BORDER); Font font=FontBuilder.getFont(phf.getFontFamily(), phf.getFontSize(), phf.isBold(), phf.isItalic(),phf.isUnderline()); String fontColor=phf.getForecolor(); if(StringUtils.isNotEmpty(fontColor)){ String[] color=fontColor.split(","); font.setColor(Integer.valueOf(color[0]), Integer.valueOf(color[1]), Integer.valueOf(color[2])); } Paragraph graph=new Paragraph(text,font); cell.setPhrase(graph); switch(type){ case 1: cell.setHorizontalAlignment(Element.ALIGN_LEFT); break; case 2: cell.setHorizontalAlignment(Element.ALIGN_CENTER); break; case 3: cell.setHorizontalAlignment(Element.ALIGN_RIGHT); break; } cell.setVerticalAlignment(Element.ALIGN_MIDDLE); return cell; } }
public static void render(Drawable drawable, Rectangle2D bounds, File file) { initialize(); Rectangle size = new Rectangle((float) bounds.getWidth(), (float) bounds.getHeight()); Document document = new Document(size); FileOutputStream fos; try { fos = new FileOutputStream(file); } catch (FileNotFoundException e) { throw new RuntimeException("The file " + file + "could not be created", e); } PdfWriter writer; try { writer = PdfWriter.getInstance(document, fos); } catch (DocumentException e) { throw new RuntimeException("An error occurred while creating a PdfWriter object.", e); } document.open(); PdfContentByte contentByte = writer.getDirectContent(); Graphics2D g = new PdfGraphics2D(contentByte, (float) bounds.getWidth(), (float) bounds.getHeight(), fontMapper); g.translate(-bounds.getX(), -bounds.getY()); drawable.draw(g); g.dispose(); document.close(); }
if(cellData instanceof Image){ Image img=(Image)cellData; cell=new PdfPCell(buildPdfImage(img.getBase64Data(), 0, 0)); }else if(cellData instanceof ChartData){ ChartData chartData=(ChartData)cellData; if(base64Data!=null){ Image img=new Image(base64Data,chartData.getWidth(),chartData.getHeight()); cell=new PdfPCell(buildPdfImage(img.getBase64Data(), 0, 0)); }else{ cell=new PdfPCell(); CellPhrase pargraph=new CellPhrase(cellInfo,""); cell.setPhrase(pargraph); cell.setFixedHeight(cellHeight); cell=new PdfPCell(); CellPhrase pargraph=new CellPhrase(cellInfo,cellData); cell.setPhrase(pargraph); cell.setFixedHeight(cellHeight); cell.setLeading(style.getLineHeight(), style.getLineHeight());
cb.moveTo(position.getLeft(), position.getTop()); cb.lineTo(position.getLeft(), position.getBottom()); cb.stroke(); if(leftBorder.getStyle().equals(BorderStyle.doublesolid)){ cb.moveTo(position.getLeft()+2, position.getTop()-2); cb.lineTo(position.getLeft()+2, position.getBottom()+2); cb.stroke(); cb.restoreState(); cb.moveTo(position.getLeft(), position.getTop()); cb.lineTo(position.getRight(), position.getTop()); cb.stroke(); if(topBorder.getStyle().equals(BorderStyle.doublesolid)){ cb.moveTo(position.getLeft()+2, position.getTop()-2); cb.lineTo(position.getRight()-2, position.getTop()-2); cb.stroke(); cb.restoreState(); cb.moveTo(position.getRight(), position.getTop()); cb.lineTo(position.getRight(), position.getBottom()); cb.stroke(); if(rightBorder.getStyle().equals(BorderStyle.doublesolid)){ cb.moveTo(position.getRight()-2, position.getTop()-2); cb.lineTo(position.getRight()-2, position.getBottom()+2); cb.stroke(); cb.restoreState();
private PdfContentByte bulidCellBorder(PdfContentByte[] canvases,Border border){ PdfContentByte cb=canvases[PdfPTable.LINECANVAS]; cb.saveState(); BigDecimal w=new BigDecimal(border.getWidth()); cb.setLineWidth(w.divide(new BigDecimal(2),10,RoundingMode.HALF_UP).floatValue()); if(border.getStyle().equals(BorderStyle.dashed)){ cb.setLineDash(new float[]{2f,3f,1f},2); } String borderColor[]=border.getColor().split(","); cb.setColorStroke(new BaseColor(Integer.valueOf(borderColor[0]),Integer.valueOf(borderColor[1]),Integer.valueOf(borderColor[2]))); return cb; } }
private BaseFont getIdentityFont(String fontFamily,String fontPath,ApplicationContext applicationContext) throws DocumentException,IOException { if(!fontPath.startsWith(ApplicationContext.CLASSPATH_URL_PREFIX)){ fontPath=ApplicationContext.CLASSPATH_URL_PREFIX+fontPath; } String fontName = fontPath; int lastSlashPos=fontPath.lastIndexOf("/"); if(lastSlashPos!=-1){ fontName = fontPath.substring(lastSlashPos+1,fontPath.length()); } if (fontName.toLowerCase().endsWith(".ttc")) { fontName = fontName + ",0"; } InputStream inputStream=null; try{ fontPathMap.put(fontFamily, fontPath); inputStream=applicationContext.getResource(fontPath).getInputStream(); byte[] bytes = IOUtils.toByteArray(inputStream); BaseFont baseFont = BaseFont.createFont(fontName, BaseFont.IDENTITY_H,BaseFont.EMBEDDED,true,bytes,null); baseFont.setSubset(true); return baseFont; }finally{ if(inputStream!=null)inputStream.close(); } } }
void sweepKids(PdfObject obj) { PdfObject oo = PdfReader.killIndirect(obj); if (oo == null || !oo.isDictionary()) return; PdfDictionary dic = (PdfDictionary) oo; PdfArray kids = (PdfArray) PdfReader.killIndirect(dic.get(PdfName.KIDS)); if (kids == null) return; for (int k = 0; k < kids.size(); ++k) { sweepKids(kids.getPdfObject(k)); } }
private void beginLayer2(final PdfOCG layer) { PdfName name = (PdfName)writer.addSimpleProperty(layer, layer.getRef())[0]; PageResources prs = getPageResources(); name = prs.addProperty(name, layer.getRef()); content.append("/OC ").append(name.getBytes()).append(" BDC").append_i(separator); }
String[] files = { MovieLinks1.RESULT, MovieHistory.RESULT }; // step 1 Document document = new Document(); // step 2 PdfCopy copy = new PdfCopy(document, new FileOutputStream(RESULT)); // step 3 document.open(); // step 4 PdfReader reader; int n; // loop over the documents you want to concatenate for (int i = 0; i < files.length; i++) { reader = new PdfReader(files[i]); // loop over the pages in that document n = reader.getNumberOfPages(); for (int page = 0; page < n; ) { copy.addPage(copy.getImportedPage(reader, ++page)); } copy.freeReader(reader); reader.close(); } // step 5 document.close();
PdfCopy writer = new PdfCopy(document, new FileOutputStream(outFile)); document.open(); PdfImportedPage page = writer.getImportedPage(reader, ++i); writer.addPage(page); document.close(); writer.close();
@Override public void onEndPage(PdfWriter writer, Document document) { List<Page> pages=report.getPages(); int pageNumber=writer.getPageNumber(); if(pageNumber>pages.size()){ return; } Page page=pages.get(pageNumber-1); HeaderFooter header=page.getHeader(); HeaderFooter footer=page.getFooter(); if(header!=null){ buildTable(writer,header,true,report); } if(footer!=null){ buildTable(writer,footer,false,report); } } private void buildTable(PdfWriter writer,HeaderFooter hf,boolean header,Report report) {