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();
}
}
}
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"));
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, 0, 0);
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", 440, 773, 0);
}
if(i == 2)...{
pcb.showTextAligned(PdfContentByte.ALIGN_LEFT, "13245678910", 270, 90, 0);
}
pcb.endText();
}
doc.close();
pdfw.close();
doc.open();
PdfContentByte pcb = pdfw.getDirectContent();
int i = 0;
while(i<n)...{
doc.newPage();
i++;
//读取当前页
PdfImportedPage pip = pdfw.getImportedPage(pdfr, i);
pcb.addTemplate(pip, 0, 0);
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", 440, 773, 0);
}
if(i == 2)...{
pcb.showTextAligned(PdfContentByte.ALIGN_LEFT, "13245678910", 270, 90, 0);
}
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();
}
}
}
//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();
}
}
}
- pdf文档的下载
- pdf文档转换器下载
- cnki下载pdf文档
- pdf文档下载地址
- 下载阅读pdf文档
- 下载百度文库里面的PDF文档(只能下PDF文档)
- spring-boot pdf文档下载
- advancED Flex 4 (一) 的 PDF格式文档下载地址
- jsp输出pdf文档(以下载的形式)
- pdf文档的下载与查看 jsp页面
- 批量下载网页中所有的PDF文档
- 下载spring网站的相关文档pdf版本
- lpc23XX的pdf文档
- PDF文档格式转换器绿色下载
- 大数据文档PDF开放下载
- 用Python 爬虫批量下载PDF文档
- spring官网下载pdf说明文档
- 30个值得收藏可免费搜索/下载PDF电子图书(文档)的搜索引擎
- 3DGS脚本教程翻译(4)-位图和面板
- IE浏览器中Flash stageWidth为零的BUG
- U-BOOT移植
- struts3.0 demo
- SWT Designer下载与安装
- pdf文档的下载
- Css 3级导航菜单,适用ie和firefox
- VC防止窗口和控件闪烁的方法
- ASP.NET的六大内置对象总结
- 绿茶
- IBM和SAP宣布将开源其JVM诊断工具
- .net 控件calendar 基础用法
- 3DGS脚本教程翻译(5)-数字和窗口
- 向2万名前进