Controller层(param为数据)
byte[]bytes=PdfUtils.createPdf(param);
ByteArrayInputStreaminStream=newByteArrayInputStream(bytes);
//设置输出的格式
response.setContentType("bin");
response.setHeader("content-disposition","attachment;filename="+URLEncoder.encode(itemName+"(评审意见).pdf","UTF-8"));
//循环取出流中的数据
byte[]b=newbyte[2048];
intlen;
while((len=inStream.read(b))>0)
response.getOutputStream().write(b,0,len);
inStream.close();
Service层(param为数据)
public static byte[] createPdf(Map<String,Object> param) {
byte[] result= null;
ByteArrayOutputStream baos = null;
//支流程评审信息汇总
List<CmplncBranchRvwInfoDTO> branchRvwInfos = (List<CmplncBranchRvwInfoDTO>) param.get("branchRvwInfos");
//主流程评审信息汇总
List<CmplncMainRvwInfoDTO> mainRvwInfos = (List<CmplncMainRvwInfoDTO>) param.get("mainRvwInfos");
//主评审信息
CmplncRvwFormDTO rvwFormDTO = (CmplncRvwFormDTO) param.get("rvwFormDTO");
//附件列表
List<FileInfoDTO> fileList = (List<FileInfoDTO>) param.get("fileList");
//专业公司
String legalEntityDeptDesc = (String) param.get("legalEntityDeptDesc");
String legalEntitySonDeptDesc = (String) param.get("legalEntitySonDeptDesc");
//设置页边距
Document doc = new Document(PageSize.A4, 20, 20, 60, 20);
try {
baos = new ByteArrayOutputStream();//构建字节输出流
PdfWriter writer = PdfWriter.getInstance(doc,baos);
//页眉页脚字体
BaseFont bf = null;
BaseFont bFont = null;
try {
bFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
Font footerFont = new Font(bFont, 10, Font.NORMAL);
Font title = new Font(bf,15,Font.BOLD);
Font content = new Font(bf,9,Font.NORMAL);
Font chinese = new Font(bf, 10, Font.BOLD);
/**
* HeaderFooter的第2个参数为非false时代表打印页码
* 页眉页脚中也可以加入图片,并非只能是文字
*/
HeaderFooter header=new HeaderFooter(new Phrase("法律合规评审系统",title),false);
//设置是否有边框等
header.setBorder(Rectangle.NO_BORDER);
header.setAlignment(1);
doc.setHeader(header);
HeaderFooter footer=new HeaderFooter(new Phrase("-",footerFont),new Phrase("-",footerFont));
/**
* 0左 1中 2右
*/
footer.setAlignment(1);
footer.setBorder(Rectangle.NO_BORDER);
doc.setFooter(footer);
doc.open();
//doc.add(new Paragraph("评审意见:",chinese));
//7列
PdfPTable table = new PdfPTable(7);
PdfPCell cell;
table.addCell(new Paragraph("评审项目编号",chinese));
table.addCell(new Paragraph(rvwFormDTO.getRvwItemCode(),content));
cell = new PdfPCell(new Paragraph("评审项目类型",chinese));
cell.setColspan(2);
table.addCell(cell);
String rvwItemParentType = (String) param.get("rvwItemParentType");
String rvwItemType = (String) param.get("rvwItemType");
String rvwItemTwoType = (String) param.get("rvwItemTwoType");
cell = new PdfPCell(new Paragraph(rvwItemParentType+"/"+rvwItemType+"/"+rvwItemTwoType,content));
cell.setColspan(3);
table.addCell(cell);
table.addCell(new Paragraph("申请人姓名",chinese));
table.addCell(new Paragraph(rvwFormDTO.getApplicantName(),content));
cell = new PdfPCell(new Paragraph("申请人所在部门",chinese));
cell.setColspan(2);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(rvwFormDTO.getAplcntDeptName(),content));
cell.setColspan(3);
table.addCell(cell);
table.addCell(new Paragraph("录入人姓名",chinese));
table.addCell(new Paragraph(rvwFormDTO.getRecordName(),content));
cell = new PdfPCell(new Paragraph("项目涉及金额",chinese));
cell.setColspan(2);
table.addCell(cell);
table.addCell(new Paragraph(String.valueOf(rvwFormDTO.getInvolveAmount()==null?0:rvwFormDTO.getInvolveAmount()),content));
table.addCell(new Paragraph("币种",chinese));
String involveAmountType = (String) param.get("involveAmountType");
table.addCell(new Paragraph(involveAmountType,content));
table.addCell(new Paragraph("专业公司",chinese));
cell = new PdfPCell(new Paragraph(legalEntityDeptDesc+"->"+legalEntitySonDeptDesc,content));
cell.setColspan(6);
table.addCell(cell);
table.addCell(new Paragraph("项目名称",chinese));
cell = new PdfPCell(new Paragraph(rvwFormDTO.getItemName(),content));
cell.setColspan(6);
table.addCell(cell);
table.addCell(new Paragraph("项目概述",chinese));
cell = new PdfPCell(new Paragraph(rvwFormDTO.getItemOverview(),content));
cell.setColspan(6);
table.addCell(cell);
table.addCell(new Paragraph("评审需求",chinese));
cell = new PdfPCell(new Paragraph(rvwFormDTO.getRvwDemand(),content));
cell.setColspan(6);
table.addCell(cell);
table.addCell(new Paragraph("申请人自我评估",chinese));
cell = new PdfPCell(new Paragraph(rvwFormDTO.getApplicantSelfAssmnt(),content));
cell.setColspan(6);
table.addCell(cell);
/* table.addCell(new Paragraph("同步抄送",chinese));
cell = new PdfPCell(new Paragraph(rvwFormDTO.getSyncsenderNames(),content));
cell.setColspan(6);
table.addCell(cell);*/
int infoNum = 0;
if(fileList.size() > 0){
//附件信息
cell = new PdfPCell(new Paragraph("附件信息",chinese));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
cell.setRowspan(fileList.size()+1);
table.addCell(cell);
//序号
cell = new PdfPCell(new Paragraph("序号",chinese));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//附件名称
cell = new PdfPCell(new Paragraph("附件名称",chinese));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//上传时间
cell = new PdfPCell(new Paragraph("上传时间",chinese));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//上传人
cell = new PdfPCell(new Paragraph("上传人",chinese));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//附件类型
cell = new PdfPCell(new Paragraph("附件类型",chinese));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//法律合规评审
cell = new PdfPCell(new Paragraph("法律合规评审",chinese));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
for (FileInfoDTO file : fileList) {
infoNum++;
//序号
cell = new PdfPCell(new Paragraph(infoNum+"",content));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//附件名称
cell = new PdfPCell(new Paragraph(file.getFileName(),content));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//上传时间
cell = new PdfPCell(new Paragraph(file.getUploadTimeFormat(),content));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//上传人
cell = new PdfPCell(new Paragraph(file.getUploadName(),content));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//附件类型
String fileType;
if("1".equals(file.getFileType())){
fileType = "合同文件";
}else if("99".equals(file.getFileType())){
fileType = "支持文档";
}else{
fileType = "";
}
cell = new PdfPCell(new Paragraph(fileType,content));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//法律合规评审
String typeRvwStatus;
if("1".equals(file.getTypeRvwStatus())){
typeRvwStatus = "经审查附件无重大法律合规问题";
}else if("2".equals(file.getTypeRvwStatus())){
typeRvwStatus = "退回修改";
}else{
typeRvwStatus = "";
}
cell = new PdfPCell(new Paragraph(typeRvwStatus,content));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
}
}else{
//附件信息
cell = new PdfPCell(new Paragraph("附件信息",chinese));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("没有附件",content));
cell.setColspan(6);
table.addCell(cell);
}
cell = new PdfPCell(new Paragraph("评审意见",chinese));
cell.setRowspan(mainRvwInfos.size()+branchRvwInfos.size());
//居中
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
if(mainRvwInfos.size()>0){
cell = new PdfPCell(new Paragraph("主评审意见",chinese));
cell.setRowspan(mainRvwInfos.size());
//居中
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
infoNum = 0;
for (CmplncMainRvwInfoDTO dto : mainRvwInfos) {
infoNum++;
//序号
cell = new PdfPCell(new Paragraph(infoNum+"",content));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//评审意见
PdfPTable branchRvwInfosTable = new PdfPTable(1);
cell = new PdfPCell(new Paragraph(delHTMLTag(dto.getRvwOpinion()),content));
cell.disableBorderSide(2);
branchRvwInfosTable.addCell(cell);
//评审人和评审时间
cell = new PdfPCell(new Paragraph(dto.getRvwUmName()+"/"+getStringDate(dto.getRvwTime()),content));
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.disableBorderSide(1);
cell.setPaddingTop(5);
branchRvwInfosTable.addCell(cell);
PdfPCell branchRvwInfosCell = new PdfPCell(branchRvwInfosTable);
branchRvwInfosCell.setColspan(4);
table.addCell(branchRvwInfosCell);
}
doc.add(table);
}
if(branchRvwInfos.size()>0){
cell = new PdfPCell(new Paragraph("支评审意见",chinese));
cell.setRowspan(branchRvwInfos.size());
//居中
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
infoNum = 0;
for (CmplncBranchRvwInfoDTO dto : branchRvwInfos) {
infoNum++;
//序号
cell = new PdfPCell(new Paragraph(infoNum+"",content));
cell.setUseAscender(true);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
//评审意见
PdfPTable branchRvwInfosTable = new PdfPTable(1);
cell = new PdfPCell(new Paragraph(delHTMLTag(dto.getRvwOpinion()),content));
cell.disableBorderSide(2);
branchRvwInfosTable.addCell(cell);
//评审人和评审时间
cell = new PdfPCell(new Paragraph(dto.getRvwUmName()+"/"+getStringDate(dto.getRvwTime()),content));
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.disableBorderSide(1);//隐藏上边框
cell.setPaddingTop(5);
branchRvwInfosTable.addCell(cell);
PdfPCell branchRvwInfosCell = new PdfPCell(branchRvwInfosTable);
branchRvwInfosCell.setColspan(4);
table.addCell(branchRvwInfosCell);
}
doc.add(table);
}
if(doc != null){
doc.close();
}
result =baos.toByteArray();
} catch (DocumentException e) {
e.printStackTrace();
}finally{
if(baos != null){
try {
baos.close();
} catch (IOException e) {
log.error("PDF异常", e);
}
}
}
return result;
}
工具
/**
* 去掉HTML标签
* @param htmlStr
* @return
*/
public static String delHTMLTag(String htmlStr){
if (htmlStr!=null){
String regEx_script="<script[^>]*?>[\s\S]*?<\/script>"; //定义script的正则表达式
String regEx_style="<style[^>]*?>[\s\S]*?<\/style>"; //定义style的正则表达式
String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式
Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
Matcher m_script=p_script.matcher(htmlStr);
htmlStr=m_script.replaceAll(""); //过滤script标签
Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
Matcher m_style=p_style.matcher(htmlStr);
htmlStr=m_style.replaceAll(""); //过滤style标签
Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
Matcher m_html=p_html.matcher(htmlStr);
htmlStr=m_html.replaceAll(""); //过滤html标签
Pattern p_enter = Pattern.compile("\s*| | | ");
Matcher m_enter = p_enter.matcher(htmlStr);
htmlStr = m_enter.replaceAll("");
}
return htmlStr.trim().replaceAll(" ", ""); //返回文本字符串
}
/**
* @return返回字符串格式 yyyy-MM-dd HH:mm:ss
*/
public static String getStringDate(Date date) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(date);
return dateString;
}
Ⅱ java创建pdf文件写入不进去
可以用生成PDF报表的Java组件--iText。
具体实现方法如下:1、导入itext-2。1。5。jar跟itextasian-1。5。2。jar两个包到项目里,2、建立一个pdf文件。
一般情况下,iText使用在有以下一个要求的项目中:1。内容无法提前利用:取决于用户的输入或实时的数据库信息。2。由于内容,页面过多,PDF文档不能手动生成。3。文档需在无人参与,批处理模式下自动创建。4。内容被定制或个性化。
Ⅲ Java如何使用Java向PDF页面中添加文本
试试这个教程,需要依赖免费版的Spire.Pdf.jar包
importjava.awt.*;
importjava.awt.geom.Point2D;
importjava.awt.geom.Rectangle2D;
importjava.io.*;
importcom.spire.pdf.PdfPageBase;
importcom.spire.pdf.graphics.*;
{
publicstaticvoidmain(String[]args)throwsFileNotFoundException,IOException{
//创建PdfDocument对象
PdfDocumentdoc=newPdfDocument();
//添加一页
PdfPageBasepage=doc.getPages().add();
//标题文字
Stringtitle="标题";
//创建单色画刷对象
PdfSolidBrushbrush1=newPdfSolidBrush(newPdfRGBColor(Color.BLUE));
PdfSolidBrushbrush2=newPdfSolidBrush(newPdfRGBColor(Color.BLACK));
//创建TrueType字体对象
PdfTrueTypeFontfont1=newPdfTrueTypeFont(newFont("ArialUnicodeMS",Font.PLAIN,14),true);
PdfTrueTypeFontfont2=newPdfTrueTypeFont(newFont("ArialUnicodeMS",Font.PLAIN,10),true);
//创建PdfStringFormat对象
PdfStringFormatformat1=newPdfStringFormat();
format1.setAlignment(PdfTextAlignment.Center);//设置文字居中
//使用drawString方法绘制标题文字
page.getCanvas().drawString(title,font1,brush1,newPoint2D.Float(page.getActualBounds(true).width/2,0),format1);
//从txt文件读取内容到字符串
Stringbody=readFileToString("C:\Users\Administrator\Desktop\bodyText.txt");
//创建PdfStringFormat对象
PdfStringFormatformat2=newPdfStringFormat();
format2.setParagraphIndent(20);//设置段首缩进
//创建Rectangle2D对象
Rectangle2D.Floatrect=newRectangle2D.Float(0,30,page.getActualBounds(true).width,page.getActualBounds(true).height);
//使用drawString方法在矩形区域绘制主体文字
page.getCanvas().drawString(body,font2,brush2,rect,format2);
//保存到PDF文档
doc.saveToFile("ouput.pdf");
}
//自定义方法读取txt文件内容到字符串
(Stringfilepath)throwsFileNotFoundException,IOException{
StringBuildersb=newStringBuilder();
Strings="";
BufferedReaderbr=newBufferedReader(newFileReader(filepath));
while((s=br.readLine())!=null){
sb.append(s+" ");
}
br.close();
Stringstr=sb.toString();
returnstr;
}
}
Ⅳ java实现将图片写到pdf文件尾部
要写pdf文件的话,可以使用itext,这个可以写文字或者图片,输出到pdf文件
Ⅳ java中怎么利用poi和itext生成pdf文档
生成PDF文档代码如下:
packagepoi.itext;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.awt.Color;
importcom.lowagie.text.*;
importcom.lowagie.text.pdf.*;
importcom.lowagie.text.pdf.BaseFont;
/**
*创建Pdf文档
*@authorAdministrator
*
*/
publicclassHelloPdf
{
publicstaticvoidmain(String[]args)throwsException
{
BaseFontbfChinese=BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
FontFontChinese=newFont(bfChinese,12,Font.NORMAL);
//第一步,创建document对象
RectanglerectPageSize=newRectangle(PageSize.A4);
//下面代码设置页面横置
//rectPageSize=rectPageSize.rotate();
//创建document对象并指定边距
Documentdoc=newDocument(rectPageSize,50,50,50,50);
Documentdocument=newDocument();
try
{
//第二步,将Document实例和文件输出流用PdfWriter类绑定在一起
//从而完成向Document写,即写入PDF文档
PdfWriter.getInstance(document,newFileOutputStream("src/poi/itext/HelloWorld.pdf"));
//第3步,打开文档
document.open();
//第3步,向文档添加文字.文档由段组成
document.add(newParagraph("HelloWorld"));
Paragraphpar=newParagraph("世界你好",FontChinese);
document.add(par);
PdfPTabletable=newPdfPTable(3);
for(inti=0;i<12;i++)
{
if(i==0)
{
PdfPCellcell=newPdfPCell();
cell.setColspan(3);
cell.setBackgroundColor(newColor(180,180,180));
cell.addElement(newParagraph("表格头",FontChinese));
table.addCell(cell);
}
else
{
PdfPCellcell=newPdfPCell();
cell.addElement(newParagraph("表格内容",FontChinese));
table.addCell(cell);
}
}
document.add(table);
}
catch(DocumentExceptionde)
{
System.err.println(de.getMessage());
}
catch(IOExceptionioe)
{
System.err.println(ioe.getMessage());
}
//关闭document
document.close();
System.out.println("生成HelloPdf成功!");
}
}
希望对你有帮助。
Ⅵ java中如何实现向已有的PDF文件插入附件
可以用Spire.Pdf for Java类库给PDF文档添加附件,下面的代码是插入Excel和Word附件给你参考:
import com.spire.pdf.annotations.*;
import com.spire.pdf.attachments.PdfAttachment;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class AttachFiles {
public static void main(String[] args) throws IOException {
//创建PdfDocument对象
PdfDocument doc = new PdfDocument();
//加载PDF文档
doc.loadFromFile("C:\Users\Administrator\Desktop\sample.pdf");
//添加附件到PDF
PdfAttachment attachment = new PdfAttachment("C:\Users\Administrator\Desktop\使用说明书.docx");
doc.getAttachments().add(attachment);
//绘制标签
String label = "财务报表.xlsx";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,12),true);
double x = 35;
double y = doc.getPages().get(0).getActualSize().getHeight() - 200;
doc.getPages().get(0).getCanvas().drawString(label, font, PdfBrushes.getOrange(), x, y);
//添加注释附件到PDF
String filePath = "C:\Users\Administrator\Desktop\财务报表.xlsx";
byte[] data = toByteArray(filePath);
Dimension2D size = font.measureString(label);
Rectangle2D bound = new Rectangle2D.Float((float) (x + size.getWidth() + 2), (float) y, 10, 15);
PdfAttachmentAnnotation annotation = new PdfAttachmentAnnotation(bound, filePath, data);
annotation.setColor(new PdfRGBColor(new Color(0, 128, 128)));
annotation.setFlags(PdfAnnotationFlags.Default);
annotation.setIcon(PdfAttachmentIcon.Graph);
annotation.setText("点击打开财务报表.xlsx");
doc.getPages().get(0).getAnnotationsWidget().add(annotation);
//保存文档
doc.saveToFile("Attachments.pdf");
}
//读取文件到byte数组
public static byte[] toByteArray(String filePath) throws IOException {
File file = new File(filePath);
long fileSize = file.length();
if (fileSize > Integer.MAX_VALUE) {
System.out.println("file too big...");
return null;
}
FileInputStream fi = new FileInputStream(file);
byte[] buffer = new byte[(int) fileSize];
int offset = 0;
int numRead = 0;
while (offset < buffer.length && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
}
if (offset != buffer.length) {
throw new IOException("Could not completely read file "
+ file.getName());
}
fi.close();
return buffer;
}
}
效果:
Ⅶ java如何创建pdf文件,并将数据库数据写入pdf
写入PDF? 可以,不过需要下载操作 PDF 的JAR包!操作起来不是太麻烦,不过就是生成的时间比较长。。。
flex 是 flash的一种延伸,flash 对于网站加载来说本来就比较慢,比较的消耗资源,生成PDF 可想而知!
其次 flex 发布到jobss tomcat 等服务器中时,你的 flex 是编译成flash,还是直接将xml放入容器中让其自动生成flash呢? 如果是的话,那反应就更慢了。。。
再后者,flex 虽然说是 flash 只要客户端支持flash就能看见 你的网站,但flex 需要在jobss tomcat 等容器中配置的,有免费的有收费的,它们的配置方法都不一样的。。。
好了就说到这里,虽然有点废话,并且与主题无关,但希望对LZ有帮助!
Ⅷ 怎么用java代码生成pdf文档
package com.qhdstar.java.pdf;
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter;
/**
* 描述:TODO 【JAVA生成PDF】
* <p>
*
* @title GeneratePDF
* @author SYJ
* @email [email protected]
* @date 2013-4-6
* @version V1.0
*/
public class GeneratePDF {
public static void main(String[] args) {
//调用第一个方法,向C盘生成一个名字为ITextTest.pdf 的文件
try {
writeSimplePdf();
}
catch (Exception e) { e.printStackTrace(); }
//调用第二个方法,向C盘名字为ITextTest.pdf的文件,添加章节。
try {
writeCharpter();
}
catch (Exception e) { e.printStackTrace(); }
}
public static void writeSimplePdf() throws Exception {
// 1.新建document对象
// 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
// 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
// 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\ITextTest.pdf"));
// 3.打开文档
document.open();
// 4.向文档中添加内容
// 通过 com.lowagie.text.Paragraph 来添加文本。可以用文本及其默认的字体、颜色、大小等等设置来创建一个默认段落
document.add(new Paragraph("First page of the document."));
document.add(new
Paragraph("Some more text on the first page with different color and
font type.", FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new
Color(255, 150, 200))));
// 5.关闭文档
document.close();
}
/**
* 添加含有章节的pdf文件
*
* @throws Exception
*/
public static void writeCharpter() throws Exception {
// 新建document对象 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:\\ITextTest.pdf"));
// 打开文件
document.open();
// 标题
document.addTitle("Hello mingri example");
// 作者
document.addAuthor("wolf");
// 主题
document.addSubject("This example explains how to add metadata.");
document.addKeywords("iText, Hello mingri");
document.addCreator("My program using iText");
// document.newPage();
// 向文档中添加内容
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new
Paragraph("Some more text on the first page with different color and
font type.", FontFactory.getFont(FontFactory.defaultEncoding, 10,
Font.BOLD, new Color(0, 0, 0))));
Paragraph title1 = new
Paragraph("Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 18,
Font.BOLDITALIC, new Color(0, 0, 255)));
// 新建章节
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
Paragraph
title11 = new Paragraph("This is Section 1 in Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255,
0, 0)));
Section section1 = chapter1.addSection(title11);
Paragraph someSectionText = new Paragraph("This text comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText = new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
document.add(chapter1);
// 关闭文档
document.close();
}
}