如何使用iText为PDF添加水印

时间:2015-07-09 10:08:49

标签: itext

这是我的jsp代码,使用iText库。在添加水印之前我执行得很好。我想在这个pdf中添加一个水印图像。

try
 {
 Document document=new Document(PageSize.A4);
 if(str.equals("pdf"))
 {
    response.setContentType("application/pdf");
  PdfWriter.getInstance(document,response.getOutputStream());

  }
      document.open();
      Paragraph head=new Paragraph("Fixed Deposit Receipt                                              "+opendate);
      head.setSpacingAfter(35f);
      head.setAlignment(Element.ALIGN_RIGHT);
      document.add(head); 
     String filename = "C:/Documents and Settings/pooja/MyDocuments/NetBeansProjects/UpcidSociety/a.png";
      Image image = Image.getInstance(filename);
       image.setAbsolutePosition(235f, 735f);
      image.setSpacingBefore(20f);
      document.add(image);


      Paragraph head1=new Paragraph("UTTAR PRADESH CID CREDIT CO-OP SOCIETY LTD");
      head1.setAlignment(Element.ALIGN_CENTER);
      head1.setSpacingBefore(15f);
      document.add(head1);
      Paragraph head2=new Paragraph("Vikalp Khand-2, Gomti Nagar, Lucknow");
      head2.setAlignment(Element.ALIGN_CENTER);
      document.add(head2);
  PdfPTable table = new PdfPTable(2); // 2 columns.
   table.setSpacingBefore(45f);

      table.setWidthPercentage(100);
        PdfPCell cell11 = new PdfPCell(new Paragraph("FD Ledger No:   "+ledger));
         PdfPCell cell12 = new PdfPCell(new Paragraph("FD Certificate No:  "+certino));
       cell11.setBorder(Rectangle.NO_BORDER);
        cell12.setBorder(Rectangle.NO_BORDER);
      cell12.setHorizontalAlignment(Element.ALIGN_RIGHT);
      cell12.setRightIndent(25);
        table.addCell(cell11);
        table.addCell(cell12);

        PdfPCell cell21 = new PdfPCell(new Paragraph("Date of Issue:  "+opendate));
        PdfPCell cell22 = new PdfPCell(new Paragraph("Date of Maturity:  "+closingdate));
        cell21.setBorder(Rectangle.NO_BORDER);
        cell22.setBorder(Rectangle.NO_BORDER);
        cell22.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell22.setRightIndent(25);
        table.addCell(cell21);
        table.addCell(cell22);

        PdfPCell cell31 = new PdfPCell(new Paragraph("Deposit Amount:  Rs."+opendep+" /-"));
        PdfPCell cell32 = new PdfPCell(new Paragraph("Maturity Amount:  Rs."+maturityamt+" /-"));
        cell31.setBorder(Rectangle.NO_BORDER);
        cell32.setBorder(Rectangle.NO_BORDER);
        cell32.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell32.setRightIndent(25);
        table.addCell(cell31);
        table.addCell(cell32);
        table.setSpacingAfter(35f);
document.add(table);
 Font fontbold = FontFactory.getFont("Times-Roman", 14, Font.BOLD);

 Chunk ch11=new Chunk("We have received amount of Rs. ");
 Chunk ch12=new Chunk(" "+opendep+" ("+depamtwords+" ONLY)", fontbold);
  Chunk ch13=new Chunk(" from");
  Chunk ch14=new Chunk(" "+name+"  "+rank+"  "+padd, fontbold);
  Chunk ch15=new Chunk(" which is repayble ");
  Chunk ch16=new Chunk(" "+pfd, fontbold);
  Chunk ch17=new Chunk(" Year after date with interest at the rate of ");
  Chunk ch18=new Chunk(" "+intrate, fontbold);
  Chunk ch19=new Chunk(" percent per annum subjected to conditions overleaf. ");
 document.add(ch11);
 document.add(ch12);
 document.add(ch13);
 document.add(ch14);
 document.add(ch15);
 document.add(ch16);
 document.add(ch17);
 document.add(ch18);
 document.add(ch19);

   Paragraph paragraph1 = new Paragraph("Nomination Taken: "+nominee);
   paragraph1.setSpacingBefore(25);
   paragraph1.setIndentationLeft(50);
   document.add(paragraph1);

  Paragraph paragraph2 = new Paragraph("Authorized Officer                                Authorized Officer");
      paragraph2.setSpacingBefore(55);
       paragraph2.setAlignment(Element.ALIGN_RIGHT);
     document.add(paragraph2);
      document.newPage();
     Paragraph paragraph3 = new Paragraph("Renewal");
     paragraph3.setAlignment(Element.ALIGN_CENTER);
     document.add(paragraph3);
      PdfPTable table1 = new PdfPTable(7); // 3 columns.
   table1.setSpacingBefore(20f);
    table1.setWidthPercentage(100);

         PdfPCell cell1 = new PdfPCell(new Paragraph("Date"));
         PdfPCell cell2 = new PdfPCell(new Paragraph("Renew Amount"));
         PdfPCell cell3 = new PdfPCell(new Paragraph("From Date"));
         PdfPCell cell4 = new PdfPCell(new Paragraph("FD Period"));
         PdfPCell cell5 = new PdfPCell(new Paragraph("Interest Rate"));
         PdfPCell cell6 = new PdfPCell(new Paragraph("Due On"));
         PdfPCell cell7 = new PdfPCell(new Paragraph("Authorized Officer"));
        table1.addCell(cell1);
        table1.addCell(cell2);
        table1.addCell(cell3);
        table1.addCell(cell4);
        table1.addCell(cell5);
        table1.addCell(cell6);
        table1.addCell(cell7);

        PdfPCell cellt11 = new PdfPCell(new Paragraph(" "));
         PdfPCell cellt12 = new PdfPCell(new Paragraph(" "));
         PdfPCell cellt13= new PdfPCell(new Paragraph(" "));
         PdfPCell cellt14 = new PdfPCell(new Paragraph(" "));
         PdfPCell cellt15 = new PdfPCell(new Paragraph(" "));
         PdfPCell cellt16 = new PdfPCell(new Paragraph(" "));
         PdfPCell cellt17= new PdfPCell(new Paragraph(" "));
        table1.addCell(cellt11);
        table1.addCell(cellt12);
        table1.addCell(cellt13);
        table1.addCell(cellt14);
        table1.addCell(cellt15);
        table1.addCell(cellt16);
        table1.addCell(cellt17);

         PdfPCell cellt21 = new PdfPCell(new Paragraph(" "));
         PdfPCell cellt22 = new PdfPCell(new Paragraph(" "));
         PdfPCell cellt23= new PdfPCell(new Paragraph(" "));
         PdfPCell cellt24 = new PdfPCell(new Paragraph(" "));
         PdfPCell cellt25 = new PdfPCell(new Paragraph(" "));
         PdfPCell cellt26 = new PdfPCell(new Paragraph(" "));
         PdfPCell cellt27= new PdfPCell(new Paragraph(" "));
        table1.addCell(cellt21);
        table1.addCell(cellt22);
        table1.addCell(cellt23);
        table1.addCell(cellt24);
        table1.addCell(cellt25);
        table1.addCell(cellt26);
        table1.addCell(cellt27);
        PdfPCell cellt31 = new PdfPCell(new Paragraph(" "));
         PdfPCell cellt32 = new PdfPCell(new Paragraph(" "));
         PdfPCell cellt33= new PdfPCell(new Paragraph(" "));
         PdfPCell cellt34 = new PdfPCell(new Paragraph(" "));
         PdfPCell cellt35 = new PdfPCell(new Paragraph(" "));
         PdfPCell cellt36 = new PdfPCell(new Paragraph(" "));
         PdfPCell cellt37= new PdfPCell(new Paragraph(" "));
        table1.addCell(cellt31);
        table1.addCell(cellt32);
        table1.addCell(cellt33);
        table1.addCell(cellt34);
        table1.addCell(cellt35);
        table1.addCell(cellt36);
        table1.addCell(cellt37);
        table1.setSpacingAfter(25f);
        document.add(table1);

        Paragraph para4=new Paragraph("Memo: ");
        para4.setSpacingAfter(20);
        document.add(para4);
        Paragraph para5=new Paragraph("1. The deposit receipt duly exempt obligation, the deposit must be submitted at the time of payment or renewal. To prevent loss of interest, intended to upgrade the receipt must send the due date. The interest rates applicable on the date of renewal shall be in accordance with the committee's interest rate.");
        para5.setIndentationLeft(20);
        para5.setSpacingAfter(10);
        document.add(para5);

        Paragraph para6=new Paragraph("2. Deposit receipt is not transferable by endorsement deposit receipt in the absence of specific instructions to be given in the amount of the depositor himself. Authorizing payment to another party should be given by letter of the depositor and the deposit receipt must be duly obligation free.");
        para6.setIndentationLeft(20);
        para6.setSpacingAfter(10);
        document.add(para6);


        Chunk c1=new Chunk("Acknowledgment of Receipt of Payment");
        Paragraph para8=new Paragraph(c1);
        para8.setAlignment(Element.ALIGN_CENTER);
        para8.setSpacingBefore(25);
        c1.setUnderline(1.5f, -2f); 
        document.add(para8);

        Paragraph para9=new Paragraph("Please renew for ......... months/ Payment received.");
         para9.setAlignment(Element.ALIGN_CENTER);
         para9.setSpacingBefore(20);
         para9.setSpacingAfter(25);
        document.add(para9);

        Chunk c2=new Chunk("Date: ");
        document.add(c2);

        Chunk c3=new Chunk("                                                                                                     Depositor Signature");
        document.add(c3);

        BaseFont bf_normal
        = BaseFont.createFont("c://windows/fonts/arial.ttf",
            BaseFont.WINANSI, BaseFont.EMBEDDED);
        Font small=new Font(bf_normal, 8);
      PdfPTable table2 = new PdfPTable(1); // 3 columns.
       table2.setTotalWidth(40);
      table2.setLockedWidth(true);

    PdfPCell c21 = new PdfPCell(new Phrase("Stamp Ticket", small));
   PdfPCell c22 = new PdfPCell(new Phrase("")); 
   c21.setRowspan(2);
  c21.setPaddingLeft(5);
   c21.setPaddingRight(5);
  c21.setPaddingTop(10);
  c21.setPaddingBottom(10);
 table2.addCell(c21);
 table2.addCell(c22);

 table2.setHorizontalAlignment(Element.ALIGN_RIGHT);

   document.add(table2);

  ***PdfReader reader = new PdfReader("document.pdf");
  int n = reader.getNumberOfPages();
   PdfStamper stamp = new PdfStamper(reader, 
   new FileOutputStream("text1.pdf"));
   int i = 1;
   PdfContentByte under;
  PdfContentByte over;
   Image img = Image.getInstance("C:/Documents and Settings/pooja/My   Documents/NetBeansProjects/UpcidSociety/watermark.jsp");
   BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, 
   BaseFont.WINANSI, BaseFont.EMBEDDED);
    img.setAbsolutePosition(200, 400);
     while (i < n) 
    {
   under = stamp.getUnderContent(i);
   under.addImage(img);
  over = stamp.getOverContent(i);
  over.beginText();
   over.setFontAndSize(bf, 18);
  over.showText("page " + i);
  over.endText();
   i++;
  }
   stamp.close();***
   document.close(); 
   }
  catch(Exception de)
  {
out.println(de);
  }

以粗体显示的代码有一些错误但我无法识别它。任何人都可以帮助我....

0 个答案:

没有答案