pdf文档的下载

来源:互联网 发布:pua倪网络课程 编辑:程序博客网 时间:2024/04/28 07:27

1.若pdf中需动态填入的内容为已定义的Filed,则可用如下方法: 

 field.setField("address_a""jlTest");中的address_a即是pdf模板中已定义的Field

 

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.PdfStamper;

public class WriteInPdf {
    
public static void main(String[] args){
        
try {
            PdfReader pdfr 
= new PdfReader("D:/jltestFile/contract-sp2ad.pdf");
            PdfStamper stamper 
= new PdfStamper(pdfr,new FileOutputStream("D:/jltestFile/contract-sp2adu.pdf"));
            AcroFields field 
= stamper.getAcroFields();
            field.setField(
"address_a""jlTest");
            
//stamper.setFormFlattening(true);
            stamper.close();
            
            
//web程序中直接发送到浏览器
//            PdfReader reader = new PdfReader(template);
//            PdfStamper stamp = new PdfStamper(reader, response
//                    .getOutputStream());
//            stamp.setFormFlattening(true);
//            stamp.close();
        }
 catch (IOException e) {
            e.printStackTrace();
        }
 catch (DocumentException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

        
    }

}

 

2.若pdf中没有定义Field,只是为需动态填入内容的地方预留了空格,则需计算其坐标,再插入

 

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;

public class WriteInPdfNoText {
    
public static void main(String[] args) {
     
try {
        PdfReader pdfr 
= new PdfReader("D:/jltestFile/compnotify.pdf");
        Rectangle rec 
= pdfr.getPageSizeWithRotation(1);
        Document doc 
= new Document(rec);
        
//读入的pdf的总页数
        int n = pdfr.getNumberOfPages();
        PdfWriter pdfw 
= PdfWriter.getInstance(doc, new FileOutputStream("D:/jltestFile/notify.pdf"));
//web中
//ByteArrayOutputStream baos = new ByteArrayOutputStream();
//PdfWriter pdfw = PdfWriter.getInstance(doc, baos );
        doc.open();
        PdfContentByte pcb 
= pdfw.getDirectContent();
        
int i = 0;
        
while(i<n){
            doc.newPage();
            i
++;
            
//读取当前页
            PdfImportedPage pip = pdfw.getImportedPage(pdfr, i);
            pcb.addTemplate(pip, 
00);
            BaseFont font 
= BaseFont.createFont("HeiseiMin-W3""UniJIS-UCS2-HW-H",
                    BaseFont.NOT_EMBEDDED);
            pcb.beginText();
            pcb.setFontAndSize(font, 
12);
            
//分别处理pdf中各页中需要填充的位置
            if(i == 1){
                pcb.showTextAligned(PdfContentByte.ALIGN_RIGHT, 
"20"4407730);
            }

            
if(i == 2){
                pcb.showTextAligned(PdfContentByte.ALIGN_LEFT, 
"13245678910"270900);
            }

            pcb.endText();
        }

        doc.close();
        pdfw.close();
 //web中
  //doc.close();
  //baos.writeTo(response.getOutputStream());
  //baos.close();
    }
 catch (IOException e) {
        
// TODO Auto-generated catch block
        e.printStackTrace();
    }
 catch (DocumentException e) {
        
// TODO Auto-generated catch block
        e.printStackTrace();
    }

    }
        
}