Java iText操作PDF文档(六)

2014-11-24 03:29:11 · 作者: · 浏览: 2
opinion = opinion.replaceAll(" ", " ");
} else {
opinion = "无";
}
proinfoCell = new Cell(new Paragraph(opinion, ST));
proinfoCell.setUseAscender(true);
proinfoCell.setVerticalAlignment(Cell.LEFT);
proinfoCell.setHorizontalAlignment(Cell.LEFT);
proinfoTab.addCell(proinfoCell);
}
}

proinfoCell = new Cell(
new Paragraph(
"专 家 签 字:\n\n"
+ " 年 月 日\n",
ST));
proinfoCell.setUseAscender(true);
proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
proinfoCell.setHorizontalAlignment(Cell.ALIGN_LEFT);
proinfoCell.setColspan(8);
proinfoTab.addCell(proinfoCell);

document.add(proinfoTab);
} catch (DocumentException de) {
System.err.println(de.getMessage());
de.printStackTrace();
}
document.close();
return filename + ".pdf";
}
}

3.downLoad.jsp
[html]
<%@ page contentType="text/html;charset=UTF-8" import="com.jspsmart.upload.*" %>
<%
Object obj = request.getAttribute("filepath");
String filepath = "";
if(obj != null){
filepath = (String)obj;
}else{
filepath = request.getParameter("filepath");
}
System.out.println("**********"+filepath);
//filepathfilepath = filepath.replaceAll("/","\\");
// 新建一个SmartUpload对象
SmartUpload su = new SmartUpload();

// 初始化
su.initialize(pageContext);
su.setContentDisposition(null);
// 下载文件
su.downloadFile(filepath);
out.clear();
%>

下面的是在网上看到的一个例子,与大家分享
[java]
package com.thuram.test;

import java.awt.Color;
import java.io.FileOutputStream;

import com.lowagie.text.Cell;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.List;
import com.lowagie.text.ListItem;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;

public class ITextTest ...{
public static void main(String[] args) ...{
try ...{
/** *//**
* 首先,创建一个document。
* document是PDF文档中所有元素的容器。
* 第一个参数表示页的大小,其后的参数分别表示左、右、上、下的边距。
*/
Document document = new Document(PageSize.A4, 50, 50, 50, 50);

/** *//**
* 此处创建的write定义了上面创建的document的类型。
* 除了PdfWriter以为,还有HtmlWriter, RtfWriter, XmlWriter和一些别的类型。
* 第一个参数引用document对象,第二个参数指定了输出文件的绝对路径。
* 接着,我们open这个document往里边写入数据。
*/
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("c:/ITextTest.pdf"));
writer.setViewerPreferences(PdfWriter.HideMenubar
| PdfWriter.HideToolbar); // 隐藏菜单栏和工具栏