Пример #1
0
 @Override
 public void onEndPage(PdfWriter writer, Document document) {
   // This scales the image to the page,
   // use the image's width & height if you don't want to scale.
   float width = document.getPageSize().getWidth();
   float height = document.getPageSize().getHeight();
   try {
     Image background = Image.getInstance("e:/upload/toys/makbuz.jpg");
     writer.getDirectContentUnder().addImage(background, width, 0, 0, height, 0, 0);
   } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
Пример #2
0
 /**
  * Main method.
  *
  * @param args no arguments needed
  * @throws DocumentException
  * @throws IOException
  */
 public static void main(String[] args) throws IOException, DocumentException {
   // step 1
   Document document = new Document(PageSize.POSTCARD, 30, 30, 30, 30);
   // step 2
   PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
   // step 3
   document.open();
   // step 4
   // Create and add a Paragraph
   Paragraph p = new Paragraph("Foobar Film Festival", new Font(FontFamily.HELVETICA, 22));
   p.setAlignment(Element.ALIGN_CENTER);
   document.add(p);
   // Create and add an Image
   Image img = Image.getInstance(RESOURCE);
   img.setAbsolutePosition(
       (PageSize.POSTCARD.getWidth() - img.getScaledWidth()) / 2,
       (PageSize.POSTCARD.getHeight() - img.getScaledHeight()) / 2);
   document.add(img);
   // Now we go to the next page
   document.newPage();
   document.add(p);
   document.add(img);
   // Add text on top of the image
   PdfContentByte over = writer.getDirectContent();
   over.saveState();
   float sinus = (float) Math.sin(Math.PI / 60);
   float cosinus = (float) Math.cos(Math.PI / 60);
   BaseFont bf = BaseFont.createFont();
   over.beginText();
   over.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
   over.setLineWidth(1.5f);
   over.setRGBColorStroke(0xFF, 0x00, 0x00);
   over.setRGBColorFill(0xFF, 0xFF, 0xFF);
   over.setFontAndSize(bf, 36);
   over.setTextMatrix(cosinus, sinus, -sinus, cosinus, 50, 324);
   over.showText("SOLD OUT");
   over.endText();
   over.restoreState();
   // Add a rectangle under the image
   PdfContentByte under = writer.getDirectContentUnder();
   under.saveState();
   under.setRGBColorFill(0xFF, 0xD7, 0x00);
   under.rectangle(5, 5, PageSize.POSTCARD.getWidth() - 10, PageSize.POSTCARD.getHeight() - 10);
   under.fill();
   under.restoreState();
   // step 4
   document.close();
 }
 /**
  * Creates a PDF document.
  *
  * @param filename the path to the new PDF document
  * @throws DocumentException
  * @throws IOException
  */
 public void createPdf(String filename) throws IOException, DocumentException {
   // step 1
   Document document = new Document(PageSize.A4.rotate());
   // step 2
   PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
   // step 3
   document.open();
   // step 4
   PdfContentByte over = writer.getDirectContent();
   PdfContentByte under = writer.getDirectContentUnder();
   try {
     DatabaseConnection connection = new HsqldbConnection("filmfestival");
     locations = PojoFactory.getLocations(connection);
     List<Date> days = PojoFactory.getDays(connection);
     List<Screening> screenings;
     int d = 1;
     for (Date day : days) {
       drawTimeTable(under);
       drawTimeSlots(over);
       drawInfo(over);
       drawDateInfo(day, d++, over);
       screenings = PojoFactory.getScreenings(connection, day);
       for (Screening screening : screenings) {
         drawBlock(screening, under, over);
         drawMovieInfo(screening, over);
       }
       document.newPage();
     }
     connection.close();
   } catch (SQLException sqle) {
     sqle.printStackTrace();
     document.add(new Paragraph("Database error: " + sqle.getMessage()));
   }
   // step 5
   document.close();
 }
Пример #4
0
 private static void splitLine(Document doc, PdfWriter writer) {
   PdfContentByte cb = writer.getDirectContentUnder();
   cb.moveTo(doc.right() / 2, doc.bottom());
   cb.lineTo(doc.right() / 2, doc.top());
   cb.stroke();
 }
  private void pageHeader(PdfWriter writer)
      throws MalformedURLException, IOException, DocumentException {
    Integer colspan = 15;
    PdfPTable headerTable = newTable().columns(colspan).totalWidth(510F).o();

    // logo & start
    Image logo =
        Image.getInstance("pdf" + File.separator + "img" + File.separator + "logo_top_final.png");
    logo.scaleAbsolute(171f, 45f);
    logo.setAbsolutePosition(44, 760);
    writer.getDirectContent().addImage(logo);

    Phrase t1 = new Phrase("Statement / Tax Invoice", ITextFont.arial_normal_14);
    Phrase t2 =
        new Phrase(
            "GST Registration Number: " + this.getCompanyDetail().getGst_registration_number(),
            ITextFont.lucida_sans_unicode_9);
    PdfContentByte canvas = writer.getDirectContentUnder();
    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, t1, 44, 744, 0);
    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, t2, 44, 730, 0);

    /*
     * header table begin
     */
    addEmptyCol(headerTable, 34F, colspan);
    addCol(headerTable, this.getCompanyDetail().getName())
        .colspan(colspan)
        .font(ITextFont.arial_normal_8)
        .alignH("r")
        .o();
    addCol(headerTable, this.getCompanyDetail().getBilling_address())
        .colspan(colspan)
        .font(ITextFont.arial_normal_8)
        .alignH("r")
        .o();
    addCol(headerTable, "Tel: " + this.getCompanyDetail().getTelephone())
        .colspan(colspan)
        .font(ITextFont.arial_normal_8)
        .alignH("r")
        .o();
    addCol(headerTable, this.getCompanyDetail().getDomain())
        .colspan(colspan)
        .font(ITextFont.arial_normal_8)
        .alignH("r")
        .o();
    addCol(headerTable, " ")
        .colspan(colspan)
        .borderColor("b", titleBGColor)
        .borderWidth("b", 3F)
        .o();
    /*
     * header table end
     */
    /*
     * invoice basics begin
     */
    addEmptyCol(headerTable, 14F, colspan);
    addEmptyCol(headerTable, 4F, colspan - 4);
    addCol(headerTable, "Customer Id: ").colspan(2).font(ITextFont.arial_bold_8).o();
    addCol(headerTable, this.getCo().getCustomer_id().toString())
        .colspan(2)
        .font(ITextFont.arial_bold_8)
        .alignH("r")
        .o();
    addEmptyCol(headerTable, 4F, colspan - 4);
    addCol(headerTable, "Invoice No: ").colspan(2).font(ITextFont.arial_bold_8).o();
    addCol(headerTable, this.getEtc().getId().toString())
        .colspan(2)
        .font(ITextFont.arial_bold_8)
        .alignH("r")
        .o();
    addEmptyCol(headerTable, 4F, colspan - 4);
    addCol(headerTable, "Date: ").colspan(2).font(ITextFont.arial_bold_8).o();
    addCol(headerTable, TMUtils.retrieveMonthAbbrWithDate(this.getEtc().getCreate_date()))
        .colspan(2)
        .font(ITextFont.arial_bold_8)
        .alignH("r")
        .o();
    addEmptyCol(headerTable, colspan);

    /*
     * invoice basics end
     */
    // complete the table
    headerTable.completeRow();
    // write the table to an absolute position
    PdfContentByte paymentSlipTableCanvas = writer.getDirectContent();
    headerTable.writeSelectedRows(0, -1, 41, 810, paymentSlipTableCanvas);
  }