首页>代码>itext报表的小demo>/itext报表的小demo/iTextdemo/src/com/jll/demo/PDFBuild.java
package com.jll.demo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
/**
 * 
 * @author jiang.lili
 *
 */
public class PDFBuild {
	/**
	 * ��������������ʽ�ʹ�С
	 * @return
	 */
	 private static Font setChineseFont() {
         BaseFont base = null;
         Font fontChinese = null;
         // ����������
        
             try {
				base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
				         BaseFont.EMBEDDED);
				 fontChinese = new Font(base, 12, Font.NORMAL);
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            
      
         return fontChinese;
     }
	 // ����������
	// private static final String OWNERPASSWORD = "123456";
	
	  public static void buidPDF(String txtFilePath,String pdfFile, String imageFile,
			             String waterMarkName, int permission) {  
			          try {  
			             File file = File.createTempFile("tempFile", ".pdf"); // ������ʱ�ļ�  
			    
			              // ���PDF  
			              if (createPDFFile(txtFilePath,file)) {  
			                 waterMark(file.getPath(), imageFile, pdfFile,waterMarkName,  
			                          permission); // ���ˮӡ  
			              }  
			          } catch (Exception e) {  
			              e.printStackTrace();  
			          }  
			      }  
			    
			      /** 
			       * ����PDF�ļ� 
			       *  
			       * @param file 
			       *            ��ʱ�ļ� 
			       * @return �ɹ�/ʧ�� 
			       */  
			     public static boolean createPDFFile(String txtFilePath,File file) { 
			    	 // ����ֽ��
			         Rectangle rect = new Rectangle(PageSize.A4);  
			         Document doc = new Document(rect, 50.0F, 50.0F, 50.0F, 50.0F);  
			          try {  
			        	 
			        	  FileReader fileRead = new FileReader(txtFilePath);
			        	  BufferedReader read = new BufferedReader(fileRead);
			              PdfWriter.getInstance(doc, new FileOutputStream(file));  
			              doc.open();  
			           // ʵ��Paragraph ��ȡд��pdf�ļ������ݣ�����֧�����ĵķ���. step4
			              while (read.ready()) {
			                  // ������ݵ�pdf(���ォ�ᰴ��txt�ļ���ԭʼ��ʽ���)
			            	  System.out.println(read.readLine());
			            	  //����ʾ��û���
			                  doc.add(new Paragraph(read.readLine(), setChineseFont()));
			              }
			              BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",  
				                     "Identity-H", true);// ʹ��ϵͳ����  
				             Font font = new Font(bf, 14.0F, 0);  
				             font.setStyle(37); // ������ʽ  
				             font.setFamily("����"); // ��������  
			   
			              Paragraph p = new Paragraph("�� �� ͨ ֪ ��\r\n", font);  
			              p.setAlignment(1);  
			              doc.add(p);  
			              return true;  
			         } catch (Exception e) {  
			              e.printStackTrace();
			              return false; 
			          } finally{
			        	  doc.close();
			          } 
			      }  
			    /**
			     * ��ʼ��ԲȦ�����������ԲȦ���������λ��
			     * @param inputFile
			     * @param imageFile ͼƬ·��
			     * @param outputFile ���pdf·��
			     * @param waterMarkName Ҫ��ʾԲȦ�������
			     * @param permission
			     */
			     public static void waterMark(String inputFile, String imageFile,
			              String outputFile, String waterMarkName, int permission) {  
			         try {  
			              PdfReader reader = new PdfReader(inputFile);  
			              PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(  
			                      outputFile));  

			              // ��������(������֮ǰ��demoҲû��ʵ��Ŀǰû�ҵ�ԭ��)
			               //stamper.setEncryption(userPassWord.getBytes(), ownerPassWord.getBytes(), permission, false);
			              BaseFont base = BaseFont.createFont(  
			                     "C:/WINDOWS/Fonts/SIMSUN.TTC,1", "Identity-H", true);// ʹ��ϵͳ����  
			    
			              int total = reader.getNumberOfPages() + 1;  
			              Image image = Image.getInstance(imageFile);  
			    
			              // ͼƬλ��  (��߾࣬�߶�)
			              image.setAbsolutePosition(50, 100);  
			              image.setRotation(-20);//��ת ����
			              image.setRotationDegrees(-45);//��ת �Ƕ�
			              //image.scaleAbsolute(200,100);//�Զ����С
			              image.scalePercent(30);//���ձ�������
			             PdfContentByte under;  
			             int j = waterMarkName.length();  
			              char c = 0;  
			            int rise = 0;  
			              for (int i = 1; i < total; i++) {  
			                  rise = 420;  
			                under = stamper.getUnderContent(i); 
			                // ���ˮӡͼƬ  
			                under.addImage(image);  
			                PdfGState gs = new PdfGState();
			                gs.setFillOpacity(0.2f);// ����͸����Ϊ0.2(�о�ûЧ��)
			                under.setGState(gs);
			                
			                // ����ˮӡ����������б ��ʼ
			                 under.beginText(); 
			                 under.setColorFill(BaseColor.DARK_GRAY);
			                 //under.setGrayFill(0.1f);//���ñ�����ɫ��(���������õ�Ч����)
			                 under.setFontAndSize(base, 15);  
			    
			                  if (j >= 15) {  
			                    under.setTextMatrix(200, 120);  
			                      for (int k = 0; k < j; k++) {  
			                          under.setTextRise(rise);  
			                          c = waterMarkName.charAt(k);  
			                          under.showText(c + "");  
			                      }  
			                  } else {  
			                	  //����������ұ߿���260�����
			                      under.setTextMatrix(265, 100);  
			                     for (int k = 0; k < j; k++) {  
			                        under.setTextRise(rise);  
			                         c = waterMarkName.charAt(k);  
			                         under.showText(c + "");  
			                        rise -= 18;  
			                     }  
			                  }  
			               
			                 // ���ˮӡ����  ����
			                 under.endText();  
			                 // ����Ȧ  
			                  under.ellipse(250, 450, 350, 550);  
			                  under.setLineWidth(2f);  //Ȧ�Ŀ��
			                  under.stroke();  
			             }  
			            stamper.close();  
			         } catch (Exception e) {  
			              e.printStackTrace();  
			         }  
			    }  
			    
			      public static void main(String[] args) {  
			         String imageFilePath = "WebContent/image/00.jpg"; // ˮӡͼƬ·��  
			         String pdfFilePath = "D:/itext.pdf"; // �ļ����·��  
			         String txtFilePath = "d:/01.txt";// txtԭʼ�ļ���·��
			         buidPDF(txtFilePath,pdfFilePath, imageFilePath, "�����Ȩ", 16);  
			   }  

}
最近下载更多
crosa_Don  LV18 2022年6月7日
yaosiming  LV8 2020年4月14日
157554513749  LV11 2020年4月11日
zmy001  LV11 2020年4月8日
刘芳雄  LV5 2019年7月10日
zb8858662  LV4 2019年5月13日
皮皮虾一月  LV8 2019年4月25日
哦大幅度  LV16 2018年10月23日
tyyeng  LV18 2018年9月15日
gaoxin222  LV14 2018年9月3日
最近浏览更多
1358849392  LV21 2022年11月23日
akbar2020  LV9 2022年9月4日
是你爸爸啊100  LV5 2022年8月29日
crosa_Don  LV18 2022年6月7日
heifenglei  LV7 2022年4月7日
nickshen111  LV8 2021年12月13日
来恬爸爸晋亚阳  LV3 2021年10月29日
疯狂的巨兔12138  LV4 2021年5月14日
阿玉之父  LV1 2021年4月22日
一字清华  LV8 2021年2月21日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友