public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { PdfWriter writer = canvases[0].getPdfWriter(); float x = position.getLeft(); float y = position.getBottom(); Rectangle rect = new Rectangle(x-5, y-5, x+5, y+5); RadioCheckField checkbox = new RadioCheckField( writer, rect, name, "Yes"); checkbox.setCheckType(RadioCheckField.TYPE_CROSS); checkbox.setChecked(check); // change: set border color checkbox.setBorderColor(BaseColor.BLACK); try { pdfStamper.addAnnotation(checkbox.getCheckField(), page); } catch (Exception e) { throw new ExceptionConverter(e); } }
/** * <a href="http://stackoverflow.com/questions/29209553/itextsharp-pdftextextractor-gettextfrompage-throwing-nullreferenceexception"> * iTextSharp PdfTextExtractor GetTextFromPage Throwing NullReferenceException * </a> * * Test using an incomplete, invalid copy of stockQuotes_03232015.pdf from * http://www.pse.com.ph/stockMarket/marketInfo-marketActivity.html?tab=4 */ @Test public void testStockQuotes_03232015_Incomplete() throws Exception { InputStream resourceStream = getClass().getResourceAsStream("stockQuotes_03232015-incomplete.pdf"); try { PdfReader reader = new PdfReader(resourceStream); String content = extractAndStore(reader, new File(RESULT_FOLDER, "stockQuotes_03232015-incomplete.%s.txt").toString()); System.out.println("\nText stockQuotes_03232015-incomplete.pdf\n************************"); System.out.println(content); System.out.println("************************"); } catch (ExceptionConverter e) { System.err.println("\nException for stockQuotes_03232015-incomplete.pdf\n************************"); e.printStackTrace(); System.err.println("************************"); } finally { if (resourceStream != null) resourceStream.close(); } }
/** * Adds a header to every page * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage( * com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document) */ public void onEndPage(PdfWriter writer, Document document) { PdfPTable table = new PdfPTable(3); try { table.setWidths(new int[]{40,5,10}); table.setTotalWidth(100); table.getDefaultCell().setBorder(Rectangle.NO_BORDER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT); Font font=new Font(chineseFont,8); font.setColor(new BaseColor(55,55,55)); Paragraph paragraph=new Paragraph("第 "+writer.getPageNumber()+" 页 共",font); paragraph.setAlignment(Element.ALIGN_RIGHT); table.addCell(paragraph); Image img=Image.getInstance(total); img.scaleAbsolute(28, 28); PdfPCell cell = new PdfPCell(img); cell.setBorder(Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); PdfPCell c = new PdfPCell(new Paragraph("页",font)); c.setHorizontalAlignment(Element.ALIGN_LEFT); c.setBorder(Rectangle.NO_BORDER); table.addCell(c); float center=(document.getPageSize().getWidth())/2-120/2; table.writeSelectedRows(0, -1,center,30, writer.getDirectContent()); } catch(DocumentException de) { throw new ExceptionConverter(de); } }
public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) { final PdfWriter writer = canvases[0].getPdfWriter(); final TextField textField = new TextField(writer, rectangle, fieldname); // change: set border color textField.setBorderColor(BaseColor.BLACK); try { final PdfFormField field = textField.getTextField(); pdfStamper.addAnnotation(field, page); } catch (final IOException ioe) { throw new ExceptionConverter(ioe); } catch (final DocumentException de) { throw new ExceptionConverter(de); } }
/** * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document) */ public void onEndPage(PdfWriter writer, Document document) { PdfContentByte cb = writer.getDirectContent(); cb.saveState(); // write the headertable table.setTotalWidth(document.right() - document.left()); table.writeSelectedRows(0, -1, document.left(), document.getPageSize().getHeight() - 50, cb); // compose the footer String text = "Page " + writer.getPageNumber() + " of "; float textSize = helv.getWidthPoint(text, 12); float textBase = document.bottom() - 20; cb.beginText(); cb.setFontAndSize(helv, 12); // for odd pagenumbers, show the footer at the left if ((writer.getPageNumber() & 1) == 1) { cb.setTextMatrix(document.left(), textBase); cb.showText(text); cb.endText(); cb.addTemplate(tpl, document.left() + textSize, textBase); } // for even numbers, show the footer at the right else { float adjust = helv.getWidthPoint("0", 12); cb.setTextMatrix(document.right() - textSize - adjust, textBase); cb.showText(text); cb.endText(); cb.addTemplate(tpl, document.right() - adjust, textBase); } cb.saveState(); // draw a Rectangle around the page cb.setColorStroke(BaseColor.ORANGE); cb.setLineWidth(2); cb.rectangle(20, 20, document.getPageSize().getWidth() - 40, document.getPageSize().getHeight() - 40); cb.stroke(); cb.restoreState(); // starting on page 3, a watermark with an Image that is made transparent if (writer.getPageNumber() >= 3) { cb.setGState(gstate); cb.setColorFill(BaseColor.RED); cb.beginText(); cb.setFontAndSize(helv, 48); cb.showTextAligned(Element.ALIGN_CENTER, "Watermark Opacity " + writer.getPageNumber(), document.getPageSize().getWidth() / 2, document.getPageSize().getHeight() / 2, 45); cb.endText(); try { cb.addImage(headerImage, headerImage.getWidth(), 0, 0, headerImage.getHeight(), 440, 80); } catch(Exception e) { throw new ExceptionConverter(e); } cb.restoreState(); } cb.sanityCheck(); }
public void cellLayout(final PdfPCell cell, final Rectangle position, final PdfContentByte[] canvases) { try { final BaseFont bf = font.getCalculatedBaseFont(false); float availableWidth = position.getWidth(); float contentWidth = bf.getWidthPoint(content, font.getSize()); final String newContent; if (contentWidth > availableWidth) { if (LOGGER.isTraceEnabled()) { LOGGER.trace("Need to truncate '" + content + "'"); } final int contentLength = content.length(); int leftChar = 0; availableWidth -= bf.getWidthPoint("...", font.getSize()); while (leftChar < contentLength) { availableWidth -= bf.getWidthPoint(content.charAt(leftChar), font.getSize()); if (availableWidth > 0) { leftChar++; } else { break; } } newContent = content.substring(0, leftChar) + "..."; } else { if (LOGGER.isTraceEnabled()) { LOGGER.trace("Not truncating '" + content + "'"); } newContent = content; } final PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS]; final ColumnText ct = new ColumnText(canvas); ct.setSimpleColumn(position); ct.addElement(new Paragraph(newContent, font)); ct.go(); } catch (final DocumentException e) { throw new ExceptionConverter(e); } }