void applyRotation(PdfDictionary pageN, ByteBuffer out) { if (!cstp.rotateContents) return; Rectangle page = reader.getPageSizeWithRotation(pageN); int rotation = page.getRotation(); switch (rotation) { case 90: out.append(PdfContents.ROTATE90); out.append(page.getTop()); out.append(' ').append('0').append(PdfContents.ROTATEFINAL); break; case 180: out.append(PdfContents.ROTATE180); out.append(page.getRight()); out.append(' '); out.append(page.getTop()); out.append(PdfContents.ROTATEFINAL); break; case 270: out.append(PdfContents.ROTATE270); out.append('0').append(' '); out.append(page.getRight()); out.append(PdfContents.ROTATEFINAL); break; } }
private void applyPadding(Rectangle r, boolean isLeft) { if (isLeft) { r.setRight(r.getRight() - hPadding); } else { r.setLeft(r.getLeft() + hPadding); } }
// FIXME: Manage Exceptions private OutputStream marcarReporteProgramaRechazado(final Programa programa, InputStream src) throws IOException, DocumentException { PdfReader reader = null; reader = new PdfReader(src); int n = reader.getNumberOfPages(); ByteArrayOutputStream os = new ByteArrayOutputStream(); PdfStamper stamper = null; stamper = new PdfStamper(reader, os); // text watermark Font f = new Font(Font.FontFamily.HELVETICA, 65); Phrase p = new Phrase("RECHAZADO", f); // transparency PdfGState gs1 = new PdfGState(); gs1.setFillOpacity(0.3f); // properties PdfContentByte over; Rectangle pagesize; float x, y; // loop over every page for (int i = 1; i <= n; i++) { pagesize = reader.getPageSizeWithRotation(i); x = (pagesize.getLeft() + pagesize.getRight()) / 2; y = (pagesize.getTop() + pagesize.getBottom()) / 2; over = stamper.getOverContent(i); over.saveState(); over.setGState(gs1); ColumnText.showTextAligned(over, Element.ALIGN_CENTER, p, x, y, 45); } stamper.close(); reader.close(); return os; }
/** * Draws the info about the movie. * * @throws DocumentException */ protected void drawMovieInfo(Screening screening, PdfContentByte directcontent) throws DocumentException { if (screening.isPress()) { Rectangle rect = getPosition(screening); ColumnText.showTextAligned( directcontent, Element.ALIGN_CENTER, press, (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() + rect.getHeight() / 4, 0); } }
@Override public void onEndPage(PdfWriter writer, Document document) { Rectangle rect = writer.getBoxSize("art"); Image imghead = null; PdfContentByte cbhead = null; try { imghead = Image.getInstance("LogoSapito5.png"); imghead.setAbsolutePosition(0, 0); imghead.setAlignment(Image.ALIGN_CENTER); imghead.scalePercent(10f); cbhead = writer.getDirectContent(); PdfTemplate tp = cbhead.createTemplate(100, 150); tp.addImage(imghead); cbhead.addTemplate(tp, 100, 715); } catch (BadElementException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException ex) { Logger.getLogger(Cuentaspagar.class.getName()).log(Level.SEVERE, null, ex); } Phrase headPhraseImg = new Phrase(cbhead + "", FontFactory.getFont(FontFactory.TIMES_ROMAN, 7, Font.NORMAL)); Calendar c1 = Calendar.getInstance(); Calendar c2 = new GregorianCalendar(); String dia, mes, annio; dia = Integer.toString(c1.get(Calendar.DATE)); mes = Integer.toString(c1.get(Calendar.MONTH)); annio = Integer.toString(c1.get(Calendar.YEAR)); java.util.Date fecha = new Date(); String fechis = dia + "/" + mes + "/" + annio; Paragraph parrafo5 = new Paragraph( fechis, FontFactory.getFont(FontFactory.TIMES_ROMAN, 11, Font.NORMAL, BaseColor.BLACK)); ColumnText.showTextAligned( writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(parrafo5), rect.getRight(450), rect.getTop(-80), 0); Paragraph parrafo7 = new Paragraph( "Empresa Sapito S.A. de C.V.", FontFactory.getFont(FontFactory.TIMES_ROMAN, 16, Font.BOLD, BaseColor.BLACK)); ColumnText.showTextAligned( writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(parrafo7), rect.getBottom(250), rect.getTop(-60), 0); Paragraph parrafo8 = new Paragraph( "Cuentas por cobrar", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD, BaseColor.BLACK)); ColumnText.showTextAligned( writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(parrafo8), rect.getBottom(250), rect.getTop(-40), 0); ColumnText.showTextAligned( writer.getDirectContent(), Element.ALIGN_BOTTOM, new Phrase( " _________________________________________________________________________________ "), rect.getBorder(), rect.getTop(-24), 0); ColumnText.showTextAligned( writer.getDirectContent(), Element.ALIGN_BOTTOM, new Phrase( " _________________________________________________________________________________ "), rect.getBorder(), rect.getTop(-20), 0); Paragraph parrafo6 = new Paragraph( String.format("Pág %d", writer.getPageNumber()), FontFactory.getFont(FontFactory.TIMES_ROMAN, 11, Font.NORMAL, BaseColor.BLACK)); ColumnText.showTextAligned( writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(parrafo6), rect.getRight(-35), rect.getTop(-80), 0); }
private static PdfReader createPdf( String globalTitle, Date creationDate, ScrambleRequest scrambleRequest) throws DocumentException, IOException { azzert(scrambleRequest.scrambles.length > 0); ByteArrayOutputStream pdfOut = new ByteArrayOutputStream(); Rectangle pageSize = PageSize.LETTER; Document doc = new Document(pageSize, 0, 0, 75, 75); PdfWriter docWriter = PdfWriter.getInstance(doc, pdfOut); docWriter.setBoxSize( "art", new Rectangle(36, 54, pageSize.getWidth() - 36, pageSize.getHeight() - 54)); doc.addCreationDate(); doc.addProducer(); if (globalTitle != null) { doc.addTitle(globalTitle); } doc.open(); // Note that we ignore scrambleRequest.copies here. addScrambles(docWriter, doc, scrambleRequest, globalTitle); doc.close(); // TODO - is there a better way to convert from a PdfWriter to a PdfReader? PdfReader pr = new PdfReader(pdfOut.toByteArray()); if (scrambleRequest.fmc) { // We don't watermark the FMC sheets because they already have // the competition name on them. return pr; } pdfOut = new ByteArrayOutputStream(); doc = new Document(pageSize, 0, 0, 75, 75); docWriter = PdfWriter.getInstance(doc, pdfOut); doc.open(); PdfContentByte cb = docWriter.getDirectContent(); for (int pageN = 1; pageN <= pr.getNumberOfPages(); pageN++) { PdfImportedPage page = docWriter.getImportedPage(pr, pageN); doc.newPage(); cb.addTemplate(page, 0, 0); Rectangle rect = pr.getBoxSize(pageN, "art"); // Header ColumnText.showTextAligned( cb, Element.ALIGN_LEFT, new Phrase(Utils.SDF.format(creationDate)), rect.getLeft(), rect.getTop(), 0); ColumnText.showTextAligned( cb, Element.ALIGN_CENTER, new Phrase(globalTitle), (pageSize.getLeft() + pageSize.getRight()) / 2, pageSize.getTop() - 60, 0); ColumnText.showTextAligned( cb, Element.ALIGN_CENTER, new Phrase(scrambleRequest.title), (pageSize.getLeft() + pageSize.getRight()) / 2, pageSize.getTop() - 45, 0); if (pr.getNumberOfPages() > 1) { ColumnText.showTextAligned( cb, Element.ALIGN_RIGHT, new Phrase(pageN + "/" + pr.getNumberOfPages()), rect.getRight(), rect.getTop(), 0); } // Footer String generatedBy = "Generated by " + Utils.getProjectName() + "-" + Utils.getVersion(); ColumnText.showTextAligned( cb, Element.ALIGN_CENTER, new Phrase(generatedBy), (pageSize.getLeft() + pageSize.getRight()) / 2, pageSize.getBottom() + 40, 0); } doc.close(); // TODO - is there a better way to convert from a PdfWriter to a PdfReader? pr = new PdfReader(pdfOut.toByteArray()); return pr; // The PdfStamper class doesn't seem to be working. // pdfOut = new ByteArrayOutputStream(); // PdfStamper ps = new PdfStamper(pr, pdfOut); // // for(int pageN = 1; pageN <= pr.getNumberOfPages(); pageN++) { // PdfContentByte pb = ps.getUnderContent(pageN); // Rectangle rect = pr.getBoxSize(pageN, "art"); // System.out.println(rect.getLeft()); // System.out.println(rect.getWidth()); // ColumnText.showTextAligned(pb, // Element.ALIGN_LEFT, new Phrase("Hello people!"), 36, 540, 0); //// ColumnText.showTextAligned(pb, //// Element.ALIGN_CENTER, new Phrase("HELLO WORLD"), //// (rect.getLeft() + rect.getRight()) / 2, rect.getTop(), 0); // } // ps.close(); // return ps.getReader(); }
private float horCenter() { return (rectangle.getLeft() + rectangle.getRight()) / 2; }
public void addAnnotation(PdfAnnotation annot) { try { ArrayList<PdfAnnotation> allAnnots = new ArrayList<PdfAnnotation>(); if (annot.isForm()) { PdfFormField field = (PdfFormField) annot; if (field.getParent() != null) return; expandFields(field, allAnnots); if (cstp.fieldTemplates == null) cstp.fieldTemplates = new HashSet<PdfTemplate>(); } else allAnnots.add(annot); for (int k = 0; k < allAnnots.size(); ++k) { annot = allAnnots.get(k); if (annot.isForm()) { if (!annot.isUsed()) { HashSet<PdfTemplate> templates = annot.getTemplates(); if (templates != null) cstp.fieldTemplates.addAll(templates); } PdfFormField field = (PdfFormField) annot; if (field.getParent() == null) addDocumentField(field.getIndirectReference()); } if (annot.isAnnotation()) { PdfObject pdfobj = PdfReader.getPdfObject(pageN.get(PdfName.ANNOTS), pageN); PdfArray annots = null; if (pdfobj == null || !pdfobj.isArray()) { annots = new PdfArray(); pageN.put(PdfName.ANNOTS, annots); } else annots = (PdfArray) pdfobj; annots.add(annot.getIndirectReference()); if (!annot.isUsed()) { PdfRectangle rect = (PdfRectangle) annot.get(PdfName.RECT); if (rect != null && (rect.left() != 0 || rect.right() != 0 || rect.top() != 0 || rect.bottom() != 0)) { int rotation = reader.getPageRotation(pageN); Rectangle pageSize = reader.getPageSizeWithRotation(pageN); switch (rotation) { case 90: annot.put( PdfName.RECT, new PdfRectangle( pageSize.getTop() - rect.bottom(), rect.left(), pageSize.getTop() - rect.top(), rect.right())); break; case 180: annot.put( PdfName.RECT, new PdfRectangle( pageSize.getRight() - rect.left(), pageSize.getTop() - rect.bottom(), pageSize.getRight() - rect.right(), pageSize.getTop() - rect.top())); break; case 270: annot.put( PdfName.RECT, new PdfRectangle( rect.bottom(), pageSize.getRight() - rect.left(), rect.top(), pageSize.getRight() - rect.right())); break; } } } } if (!annot.isUsed()) { annot.setUsed(); cstp.addToBody(annot, annot.getIndirectReference()); } } } catch (IOException e) { throw new ExceptionConverter(e); } }