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();
}
}