当前位置:编程学习 > JAVA >>

Java iText操作PDF文档

1.Action
[java]
//在线打印PDF 
String path = servlet.getServletContext().getRealPath("");  //得到项目根目录 
CreatePdf checkPdf = new CreatePdf(); 
String filepath = checkPdf.printImportAndPtExpert(path,"expertBase",importList,putongtList); 
request.setAttribute("filepath", path+File.separator+"community"+File.separator+"pdfile"+File.separator+filepath); 
return mapping.findForward("downLoad"); 
 
2.公用类CreatPDF
[java]
import java.io.File; 
import java.io.FileOutputStream; 
import java.util.List; 
 
import com.lowagie.text.Cell; 
import com.lowagie.text.Document; 
import com.lowagie.text.DocumentException; 
import com.lowagie.text.Element; 
import com.lowagie.text.Font; 
import com.lowagie.text.FontFactory; 
import com.lowagie.text.HeaderFooter; 
import com.lowagie.text.Image; 
import com.lowagie.text.PageSize; 
import com.lowagie.text.Paragraph; 
import com.lowagie.text.Phrase; 
import com.lowagie.text.Rectangle; 
import com.lowagie.text.Table; 
import com.lowagie.text.Watermark; 
import com.lowagie.text.pdf.BaseFont; 
import com.lowagie.text.pdf.PdfPageEventHelper; 
import com.lowagie.text.pdf.PdfWriter; 
 
public class CopyOfCreatePdf extends PdfPageEventHelper { 
 
    /**
     * 打印专家评审基地(重点基地和普通基地)
     * 
     * @param path
     *            获得项目根目录 String path =
     *            servlet.getServletContext().getRealPath("");
     * @param filename
     *            文件名
     * @param importList
     *            重点基地集合
     * @param putongList
     *            普通基地集合
     * @return
     * @throws Exception
     */ 
    @SuppressWarnings("unchecked") 
    public String printImportAndPtExpert(String path, String filename, 
            List importList, List putongList) throws Exception { 
        // 创建报表大小,不能够指定页边距。 
        // Document document = new Document(PageSize.A4, 85, 71, 72, 72); 
        Document document = new Document(PageSize.A4, 30, 30, 72, 72); 
        try { 
            BaseFont bfFS = BaseFont.createFont(path + File.separator 
                    + "WEB-INF" + File.separator + "classes" + File.separator 
                    + "fonts" + File.separator + "SIMFANG.TTF", 
                    BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
            // 华文细黑 
            BaseFont bfHWXH = BaseFont.createFont(path + File.separator 
                    + "WEB-INF" + File.separator + "classes" + File.separator 
                    + "fonts" + File.separator + "STXIHEI.TTF", 
                    BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
            // 黑体 
            BaseFont bfHT = BaseFont.createFont(path + File.separator 
                    + "WEB-INF" + File.separator + "classes" + File.separator 
                    + "fonts" + File.separator + "SIMHEI.TTF", 
                    BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
 
            Font htFontB = new Font(bfHT, 14, Font.BOLD); 
 
            PdfWriter writer = PdfWriter.getInstance(document, 
                    new FileOutputStream(path + File.separator + "community" 
                            + File.separator + "pdfile" + File.separator 
                            + filename + ".pdf")); 
 
            // 插入水印 
            if (new File(path + File.separator + "image006.gif") != null) { 
                Watermark watermark = new Watermark(Image.getInstance(path 
                        + File.separator + "image006.gif"), 0f, 0f); 
                document.add(watermark); 
            } else { 
                System.out.println("添加水印失败"); 
            } 
            com.lowagie.text.Font ST3 = FontFactory.getFont("STSong-Light", 
                    "UniGB-UCS2-H"); 
  &nbs

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,