首先還是看看文檔??赐炅?,你把我寫的例子跑一遍你就會了。
創(chuàng)建文檔
//創(chuàng)建一個文檔對象,并設(shè)置他的初始化大小
Rectangle pSize=new Rectangle(144,90);
//文檔的背景色
pSize.setBackgroundColor(Color.blue);
//常用頁面大小 PageSize.A4;
Document doc=new Document(pSize);
try {
//定義輸出位置并把文檔對象裝入輸出對象中
PdfWriter.getInstance(doc, new FileOutputStream("c:/hello.pdf"));
//打開文檔對象
doc.open();
// 加入文字“Hello World”
doc.add(new Paragraph("HelloWorld"));
// 關(guān)閉文檔對象,釋放資源
doc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
設(shè)置字體
亞洲國家的字體你可以從http://itext.sourceforge.net/downloads/iTextAsian.jar下載這個包。然后把它直接放到你的ClassPath中就可以了
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
表格
//定義一個表格
Table table = new Table(2);
//設(shè)置表格邊框
table.setBorderWidth(1);
Cell cell = new Cell("Matrix III");
cell.setHeader(true);
//分列
cell.setColspan(2);
cell.setBackgroundColor(Color.blue);
//分行
Cell.setRowspan(2);
table.addCell(cell);
創(chuàng)建新頁
// 創(chuàng)建第2的頁面
document.newPage();
document.add(new Paragraph("Hello Earth"));
document.resetHeader();
// 創(chuàng)建第3的頁面
document.newPage();
document.add(new Paragraph("Hello Sun"));
document.add(new Paragraph("Remark: the header has vanished!"));
document.resetPageCount();
設(shè)置布局
PdfWriter writerA = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0108a.pdf"));
// 設(shè)置布局
writerA.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft);
PdfWriter writerB = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0108b.pdf"));
// 設(shè)置布局
writerB.setViewerPreferences(PdfWriter.HideMenubar
| PdfWriter.HideToolbar);
PdfWriter writerC = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0108c.pdf"));
// 設(shè)置布局
writerC.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft
| PdfWriter.PageModeFullScreen
| PdfWriter.NonFullScreenPageModeUseThumbs);
加密
document = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0109.pdf"));
// setEncryption方法中可以設(shè)置如下內(nèi)容(這樣打開pdf時需要輸入口令!!!!!!!!!!!)
// PdfWriter.STRENGTH128BITS, "userpass", "ownerpass",
// PdfWriter.AllowCopy | PdfWriter.AllowPrinting
writer.setEncryption(PdfWriter.STRENGTH40BITS, "", "",
PdfWriter.AllowCopy);
http://itextsharp.sourceforge.net/index.html)中看到了專門制作PDF文件的控件的介紹,暗喜之余,立馬下載試驗,果然非常輕松地制作出了想要的PDF文件,因為網(wǎng)站為英文,內(nèi)容又多,讀起來非常費(fèi)力,在解決了自己的問題后,看到許多網(wǎng)友還在為PDF文件制作而郁悶,遂決定將該內(nèi)容翻譯為中文,由于本人英語水平一般,許多地方又晦澀難懂,故翻譯質(zhì)量不是很滿意,敬請斧正,但大部分能看懂。本文的目的一是解決部分網(wǎng)友的燃眉之急,二是拋磚引玉,如果哪位仁兄愿意將該網(wǎng)站中的內(nèi)容準(zhǔn)確翻譯出來,則是天下之大幸。
要用本文的方法生成PDF文件,需要兩個控件:itextsharp.dll和ICSharpCode.SharpZipLib.dll,由于示例代碼實(shí)在太多,我將代碼全部整理出來,放在另外一個文件“示例代碼.doc”中,所有這些資源,我均放在了本人的ftp站點(diǎn)(ftp://202.107.251.26)上的“Pdf文件制作全攻略”文件夾中(文件夾中另外兩個rar壓縮文件為兩個控件的源代碼,供大家學(xué)習(xí)研究使用),你可以到這里下載相應(yīng)的資源,或者直接到原網(wǎng)站下載。
為便于調(diào)試和敘述,所有例子均為DOS控制臺程序,windows程序使用方法完全一樣,按照下面的步驟創(chuàng)建一個可調(diào)試的項目:
1、 打開VS2003;
2、 單擊菜單“文件”→“新建”→“項目”,在項目類型中選擇“Visual C#項目”,在模板中選擇“控制臺應(yīng)用程序”,輸入文件名稱如“MakePdf”,指定好存放路徑,然后點(diǎn)確定按鈕;
3、 在“解決方案資源管理器”中右鍵單擊“引用”,從彈出的菜單中選擇“添加引用”,在“.NET”選項夾中選擇“瀏覽”,添加前面提到的兩個應(yīng)用,如下圖:
4、 在代碼窗口頂部添加兩個引用:
using iTextSharp.text;
using iTextSharp.text.pdf;
至此,準(zhǔn)備工作完畢。
ViewerPreferences 或 Encryption)。所以通過下面的辦法得到實(shí)例已經(jīng)足夠了: PdfWriter.getInstance(document, new FileStream("Chap01xx.pdf"));
在第一步中創(chuàng)建一個文檔時,第一個參數(shù)意義不大,第二個參數(shù)可以是任何一種流,到目前為止我們一直使用System.IO.FileStream將Document寫入文件中,示例代碼0105用到了System.IO.MemoryStream(這不是一個獨(dú)立的例子,你必須在Servlet Engine中測試這些代碼。
Chap0111a.pdf和Chap0111b.pdf的區(qū)別
http://www.lowagie.com/iText/faq.html#images)。
u 通過URL得到圖片實(shí)例
這是添加一個圖片最簡單的辦法,見示例代碼0601,我們添加了一個WMF、一個Gif、一個Jpeg和一個PNG圖片到文檔中,使用4個URL得到:
Image wmf = Image.getInstance(new URL("../examples/harbour.wmf"));
Image gif = Image.getInstance(new URL("../examples/vonnegut.gif"));
Image jpeg = Image.getInstance(new URL("../examples/myKids.jpg"));
Image png = Image.getInstance(new URL("../examples/hitchcock.png"));
備注:許多PDF庫在插入一個圖片前都將其解壓縮并轉(zhuǎn)換成位圖格式,下面是幾個我為什么不這樣做的原因:
這將導(dǎo)致PDF文件增大,這樣產(chǎn)生的PDF文件尺寸是不同圖片文件尺寸總和的數(shù)十倍。
面臨一個法律問題:LZW算法受專利保護(hù),所以不允許使用這種算法來解壓縮GIF等文件。
u 通過文件名得到圖片實(shí)例
通過簡單地改變圖片引用路徑將示例代碼0601改成示例代碼0602:
Image gif = Image.getInstance("vonnegut.gif");
Image jpeg = Image.getInstance("myKids.jpg");
Image png = Image.getInstance("hitchcock.png");
同示例代碼0601的區(qū)別只是該圖象從本地獲取而已,另外一個例子見示例代碼0603。
http://itextsharp.sourceforge.net/examples/h.gif下載得到。
http://itextsharp.sourceforge.net/examples/pngnow.png下載。
u 圖片在表格中
你可以將圖片添加到單元格中,但有兩個副作用:
l 表格的寬度是確定,當(dāng)圖片超出單元格的寬度時,將自動縮小。
l 你不能進(jìn)行文字繞排和為圖片添加下劃線。
參見示例代碼0615。
u 圖片鏈接注釋
如果你希望得到一個可點(diǎn)擊的圖片,或者想添加鏈接注釋到圖片上,你需要創(chuàng)建一個Annotation對象,并添加到圖片上,你不需要指定位置(你可以使用0,0,0,0),該位置會內(nèi)部更新以適合該圖片。
gif.Annotation = new Annotation(0, 0, 0, 0, "Chap1102b.pdf", 3);
jpeg.Annotation = new Annotation("picture", "These are my children", 0, 0, 0, 0);
參加示例代碼0616。
caesar_coin.jpg的圖片:
PdfTable
在第5章中,我們簡要地講述了PdfPTable對象,現(xiàn)在我們將討論該對象更多的的特性。
你可以用3種不同的方法創(chuàng)建PdfTable:
PdfPTable(float[] relativeWidths);
PdfPTable(int numColumns);
PdfPTable(PdfPTable table);
你可以給該表設(shè)置更多的參數(shù),如表寬度、列寬度、水平對齊方式等,你可以通過下面的辦法添加單元格:
public void addCell(PdfPCell cell);
public void addCell(PdfPTable table);
public void addCell(Phrase phrase);
public void addCell(String text);
除了單元格填距和和間距,這些方法同Table對象非常類似。這些參數(shù)對每個單元格個體進(jìn)行了設(shè)置,當(dāng)然,你可以設(shè)置單元格的默認(rèn)值,為改變單元格的默認(rèn)值,使用getDefaultCell()和調(diào)用一個或更多的類PdfPCell的方法(你可以設(shè)置對齊方式、間距、邊框、顏色甚至最低高度)。
注:通過PdfPTable,你能改變一個單元格的列跨度,但不能改變行跨度!在PdfPTable內(nèi)部是一些獨(dú)立的行,要讓它支持行跨度更改需要對PdfPTable對象進(jìn)行很大的調(diào)整,不要期望在近期內(nèi)實(shí)現(xiàn),你可以用嵌套表來解決這些問題。
你可以象第5章一樣將一個PdfPTable添加到當(dāng)前文檔中,但你也可以添加一個表在當(dāng)前頁中的絕對位置:
public float writeSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas);
參數(shù)rowStart是你想開始的行的數(shù)目,參數(shù)rowEnd是你想顯示的最后的行(如果你想顯示所有的行,用-1),xPos和yPos是表格的坐標(biāo),canvas是一個PdfContentByte對象。在示例代碼1009中,我們添加了一個表在(100,600)處:
table.writeSelectedRows(0, -1, 100, 600, writer.DirectContent);
使用PdfPTable,你不能設(shè)置行跨度和(或)來跨度(怎么和上面的有點(diǎn)矛盾?)你可以使用嵌套表來解決,見示例代碼1010。
最后,示例代碼1011和示例代碼1012展示了PdfTable可以和templates 和 columns一起使用,在示例代碼1012中將用到cover.png圖片如下:
顏色(SpotColors)和圖案(Patterns)
顏色(spotcolors)的使用見示例代碼1013,示例代碼1014和示例代碼1015演示了圖案(patterns)的使用方法。
第十一章 本地和異地轉(zhuǎn)向、目標(biāo)和概要
本地轉(zhuǎn)向
有時你需要一個允許讀者從文檔的一個地方跳轉(zhuǎn)到另外一個地方的鏈接,你可以通過類Chunk的setLocalGoto 和setLocalDestination兩個方法實(shí)現(xiàn),例:
Chunk localgoto = new Chunk("this word", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 255))).setLocalGoto("test");
Chunk destination = new Chunk("local destination", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 255, 0))).setLocalDestination("test");
見示例代碼1101。
異地轉(zhuǎn)向
在第3章中,我們演示了一個錨點(diǎn)如何轉(zhuǎn)向到其他URL,一個錨點(diǎn)通過不同的字體、風(fēng)格和顏色,可以包含不同的Chunks,在iText的高級應(yīng)用中,下面定義鏈接到URL的其他方法:
Chunk chunk = new Chunk("anchor", FontFactory.getFont(FontFactory.HELVETICA, 12)).setAnchor(new URL("http://www.lowagie.com/iText/"));
u 轉(zhuǎn)到PDF文檔中的指定位置
如果你在文檔中指定了一個目的地,你可以從另外一個文檔跳轉(zhuǎn)到這里,為實(shí)現(xiàn)該功能,你可以使用方法:
setRemoteGoto: Chunk chunk = new Chunk("jump", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC)).setRemoteGoto("test.pdf", "test"));
test.pdf是另外一個pdf文件,”test”是該文件的一個目的地。
跳轉(zhuǎn)到另一個PDF文件指定頁
使用方法setRemoteGoto,用頁碼參數(shù)代替名稱參數(shù),可以非常容易地跳轉(zhuǎn)定另外一個文檔的指定頁:
chunk = new Chunk("jump", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC)).setRemoteGoto("test.pdf", 3));
見示例代碼1102
u 啟動一個應(yīng)用程序
可以使用下面的方法啟動一個應(yīng)用程序:
public PdfAction(String application, String parameters, String operation, String defaultDir)
如果application為“c:/winnt/notepad.exe”(其余參數(shù)可以為null),你可以通過PDF文件中的鏈接來啟動記事本程序。
u 文件和URL
如果你想跳轉(zhuǎn)到其他文檔或URL,你需要通過下面的構(gòu)造函數(shù)之一創(chuàng)建一個:
PdfAction(String filename, String name);
PdfAction(String filename, int page);
PdfAction(URL url);
PdfAction(String url);
前面兩個構(gòu)造函數(shù)允許你跳轉(zhuǎn)到文件的指定位置或頁碼,后兩個構(gòu)造函數(shù)允許你跳轉(zhuǎn)到其他URL上。
其余部分略。
第十二章 頁面和表格事件
略。
----------------------------------------------------------------------------------------------------------------------
package com.test;
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
public class PDFMain {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO 自動生成方法存根
//test3();
test3();
}
public static void test3() throws Exception
{
//上、下、左、右頁邊距
Document document = new Document(PageSize.A4.rotate(),10,10,1,1);
PdfWriter writer=null;
try {
writer = PdfWriter.getInstance(document, new FileOutputStream("c://chapter5.pdf"));
}
catch(Exception e)
{
}
// 設(shè)置頁眉和頁腳
HeaderFooter header = new HeaderFooter(new Phrase("頁眉"),false);
HeaderFooter footer = new HeaderFooter(new Phrase("頁腳"),false);
document.setHeader(header);
document.setFooter(footer);
document.open();
chapter5(document,writer);
document.close();
}
private static void chapter5(Document document,PdfWriter writer) throws Exception
{
BaseFont bfChinese=null;
try {
bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
Font fontChinesed = new Font(bfChinese, 20, Font.NORMAL);
Font fontChinesed1 = new Font(bfChinese, 15, Font.NORMAL);
Font fontChinesed2 = new Font(bfChinese, 15, Font.BOLD);
Paragraph par = new Paragraph("第五章投資資料表",fontChinesed);
par.setAlignment("center");
Paragraph par1 = new Paragraph("標(biāo)段一",fontChinesed);
par1.setAlignment("center");
Paragraph p1 = new Paragraph("\t本資料表是根據(jù)本項目的具體情況對 “投標(biāo)人須知”中有關(guān)條款所作的補(bǔ)充和修改。\n兩者如有不一致,以本資料表為準(zhǔn)。",fontChinesed1);
p1.setAlignment(Element.ALIGN_CENTER);
float [] widths={0.2f,0.8f};//20%,80%
Table tab1=new Table(2);
tab1.setWidths(widths);//設(shè)置表格2列所分配比例
tab1.setPadding(5); //表格間高度
//tab1.setSpacing(5); //邊框高度
//第一行
Paragraph par2 = new Paragraph("條款號",fontChinesed2);
par2.setAlignment("left");
Cell cel1=new Cell(par2);
tab1.addCell(cel1);
Paragraph par3 = new Paragraph("內(nèi)容",fontChinesed2);
Cell cel2=new Cell(par3);
cel2.setVerticalAlignment(Element.ALIGN_CENTER); //水平方向居中
cel2.setHorizontalAlignment(Element.ALIGN_CENTER);//垂直方向居中
tab1.addCell(cel2);
//第二行,添加2列
Cell cel3=new Cell();
tab1.addCell(cel3);
Paragraph par5 = new Paragraph("說明",fontChinesed2);
Cell cel4=new Cell(par5);
cel4.setHorizontalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel4);
//第三行
Paragraph par6 = new Paragraph("1.1",fontChinesed2);
Cell cel6=new Cell(par6);
cel6.setVerticalAlignment(Element.ALIGN_CENTER);
cel6.setRowspan(2);
tab1.addCell(cel6);
//拆分
Paragraph par7 = new Paragraph("招標(biāo)人名稱:",fontChinesed2);
Cell tab2_cel1=new Cell(par7);
Paragraph par8 = new Paragraph("合同名稱:",fontChinesed2);
Cell tab2_cel2=new Cell(par8);
tab1.addCell(tab2_cel1);
tab1.addCell(tab2_cel2);
//第四行
Cell cel7=new Cell();
cel7.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel7);
Paragraph par9 = new Paragraph("投標(biāo)報價",fontChinesed2);
Cell cel8=new Cell(par9);
tab1.addCell(cel8);
//第五行
Paragraph par10 = new Paragraph("11",fontChinesed2);
Cell cel9=new Cell(par10);
cel9.setVerticalAlignment(Element.ALIGN_CENTER);
cel9.setRowspan(2);
tab1.addCell(cel9);
//拆分
Paragraph par11 = new Paragraph("投標(biāo)人應(yīng)單獨(dú)報出貨物由投標(biāo)人制造廠當(dāng)?shù)亟坏劫I方工廠現(xiàn)場所需的運(yùn)輸費(fèi)和\n保險費(fèi),作為招標(biāo)人評標(biāo)時的評標(biāo)因素。",fontChinesed2);
Cell tab3_cel1=new Cell(par11);
Paragraph par12 = new Paragraph("投標(biāo)報價為:",fontChinesed2);
Paragraph par13 = new Paragraph("相關(guān)費(fèi)用:包括貨物總價、包裝費(fèi)和伴隨服務(wù)費(fèi)等\n"+
"(說明:\n"+
"\t\ta. 投標(biāo)人在報價時,包括制造和組裝貨物使用的配套件和原材料費(fèi)、\n"+
"\t\t相關(guān)服務(wù)費(fèi)和已付的全部增值稅或其它稅(所有稅費(fèi)填寫時均計\n"+
"\t\t入貨價)。\n"+
"\t\tb. 列出要求的備件的費(fèi)用和清單。\n"+
"\t\tc. 列出隨供件、易損件和專用工具的費(fèi)用和清單。\n"+
"\t\td. 每一貨物的價格應(yīng)是唯一的,招標(biāo)機(jī)構(gòu)和招標(biāo)人不接受任何可選\n"+
"\t\t的報價。\n"+
"\t\te. 合同要求的設(shè)計費(fèi)含技術(shù)資料費(fèi)。\n"+
"\t\tf. 履行合同所需的所有項目,如沒有另外說明價款,有關(guān)費(fèi)用視作\n"+
"\t\t已包括在其他有價款的項目的單價和總價中。\n"+
"\t\tg. 所有項目應(yīng)列出數(shù)量、單價和生產(chǎn)廠商等的明細(xì)清單,投標(biāo)人如\n"+
"\t\t將數(shù)個項目以一個總額標(biāo)價,招標(biāo)機(jī)構(gòu)有權(quán)視情況要求投標(biāo)人將\n"+
"\t\t每項單價分列表示。)\n"+
"\t\th. 投標(biāo)人承諾投標(biāo)基本方案價已包含招標(biāo)文件所規(guī)定的所有貨物和\n"+
"\t\t服務(wù)范圍。此報價若有漏項,由投標(biāo)人自行承擔(dān)。履行合同所需\n"+
"\t\t的所有項目,如沒有另外單獨(dú)價款說明,有關(guān)費(fèi)用視作已包括在\n"+
"\t\t其他有價款的項目的單價和總價中。",fontChinesed2);
par12.add(par13);
Cell tab3_cel2=new Cell(par12);
tab1.addCell(tab3_cel1);
tab1.addCell(tab3_cel2);
//第六行
Cell cel10=new Cell();
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel10);
Paragraph par14 = new Paragraph("證明投標(biāo)人合格和資格的文件",fontChinesed2);
Cell cel11=new Cell(par14);
tab1.addCell(cel11);
//第七行
Paragraph par15 = new Paragraph("13",fontChinesed2);
Cell cel12=new Cell(par15);
cel12.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
Paragraph par16 = new Paragraph("資格條件和提供文件:",fontChinesed2);
Paragraph par17 = new Paragraph("\n任何對上述資格條件的偏離和沒有實(shí)質(zhì)性的響應(yīng)均將導(dǎo)致廢標(biāo)。",fontChinesed2);
par16.add(par17);
Cell cel13=new Cell(par16);
tab1.addCell(cel13);
//第八行
par15 = new Paragraph("14.3",fontChinesed2);
cel12=new Cell(par15);
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("提供貨物保證期滿后運(yùn)行1 年所需的備件清單,包括貨源和價格表。(不計入投標(biāo)總價)",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第九行
cel12=new Cell();
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("投標(biāo)文件的編制和遞交",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十行
par15 = new Paragraph("15.1",fontChinesed2);
cel12=new Cell(par15);
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("投標(biāo)保證金:",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十一行
par15 = new Paragraph("15.3",fontChinesed2);
cel12=new Cell(par15);
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("投標(biāo)保證金采用形式:投標(biāo)保證金應(yīng)注明招標(biāo)編號,應(yīng)使用人民幣,采用投標(biāo)人"+
"企業(yè)網(wǎng)銀、電匯、銀行匯票(不包括承兌匯票)、本票、支票(僅限于上海本地"+
"支票)、貸記憑證、現(xiàn)金交款單等形式(現(xiàn)金交款單原件應(yīng)交于寶華招標(biāo)財務(wù)),"+
"不接收轉(zhuǎn)帳支票、全國支票、人民幣現(xiàn)鈔、銀行保函、已背書的票據(jù)和以個人名"+
"義匯入的匯款或解現(xiàn)等。",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十二行
par15 = new Paragraph("16.1",fontChinesed2);
cel12=new Cell(par15);
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("投標(biāo)有效期: 90 天",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十三行
par15 = new Paragraph("17.1",fontChinesed2);
cel12=new Cell(par15);
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("同一招標(biāo)項目下的所有投標(biāo)文件應(yīng)采用投標(biāo)人企業(yè)同一電子CA 簽名。",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十四行
par15 = new Paragraph("18.2",fontChinesed2);
cel12=new Cell(par15);
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("第9.2 條2)、3)、4)、8)和9.3條1)內(nèi)容須經(jīng)投標(biāo)人企業(yè)同一電子CA簽名和加密。",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十五行
cel12=new Cell();
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("開標(biāo)",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十六行
par15 = new Paragraph("19.1",fontChinesed2);
cel12=new Cell(par15);
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("投標(biāo)截止期:(北京時間)\n",fontChinesed2);
par17 = new Paragraph("開標(biāo)地點(diǎn):",fontChinesed2);
par16.add(par17);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十七行
par15 = new Paragraph("22.1",fontChinesed2);
cel12=new Cell(par15);
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("第一步開標(biāo)時間:(北京時間)\n",fontChinesed2);
par17 = new Paragraph("第二步開標(biāo)時間:(北京時間)",fontChinesed2);
par16.add(par17);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十八行
cel12=new Cell();
cel10.setVerticalAlignment(Element.ALIGN_LEFT);
tab1.addCell(cel12);
par16 = new Paragraph("評標(biāo)",fontChinesed2);
cel13=new Cell(par16);
tab1.addCell(cel13);
//第十九行
par6 = new Paragraph("25.3",fontChinesed2);
cel6=new Cell(par6);
cel6.setVerticalAlignment(Element.ALIGN_CENTER);
cel6.setRowspan(2);
tab1.addCell(cel6);
//拆分
par7 = new Paragraph("交貨時間:以下選擇",fontChinesed2);
tab2_cel1=new Cell(par7);
par8 = new Paragraph("付款條件:按招標(biāo)文件第六章合同資料表要求付款。",fontChinesed2);
tab2_cel2=new Cell(par8);
tab1.addCell(tab2_cel1);
tab1.addCell(tab2_cel2);
try {
document.add(par);
document.add(par1);
document.add(p1);
document.add(tab1);
} catch (Exception e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
throw new Exception(e);
}
}
public static void test1() throws Exception
{
Document document = new Document(PageSize.A4.rotate(),10,10,10,10);
FileOutputStream fops = new FileOutputStream("c://test.pdf");
PdfWriter.getInstance(document, fops);
document.open();
BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinesed = new Font(bfChinese, 15, Font.NORMAL);
Paragraph par = new Paragraph("aaaaa",FontChinesed);
document.add(par);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
par = new Paragraph("時間: 2007-2-1 -- 2007-4-8", FontChinese);
document.add(par);
par = new Paragraph(" ", FontChinese);
Table table = new Table(3);
table.setBorderWidth(5);
table.setBorderColor(new Color(0, 0, 255));
table.setPadding(5);
table.setSpacing(5);
Cell cell = new Cell("header");
cell.setHeader(true);
cell.setColspan(3);
table.addCell(cell);
table.endHeaders();
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.setRowspan(2);
cell.setBorderColor(new Color(255, 0, 0));
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("cell test1");
cell = new Cell("big cell");
cell.setRowspan(2);
cell.setColspan(2);
table.addCell(cell);
table.addCell("cell test2");
document.add(table);
document.close();
}
public static void test2() throws Exception
{
/*打開已經(jīng)定義好字段以后的pdf模板*/
PdfReader reader = new PdfReader("c://chapter5InvestTemplate.pdf");
/*將要生成的目標(biāo)PDF文件名稱*/
// PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("c://iText報表結(jié)果.pdf"));
// PdfContentByte under = stamp.getUnderContent(1);
/*使用中文字體*/
BaseFont bf = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bf, 12, Font.NORMAL);
/*取出報表模板中的所有字段*/
//AcroFields form = stamp.getAcroFields();
/*為字段賦值,注意字段名稱是區(qū)分大小寫的*/
// form.setField("Name", "裴賀先");
// form.setField("Age","26");
// form.setField("xueli","高二輟學(xué)");
// form.setField("email","phx@x263.net");
// form.setField("beizhu","用iText做報表簡單嗎?");
// stamp.setFormFlattening(true);
//stamp.close();
/*必須要調(diào)用這個,否則文檔不會生成的*/
Document document = new Document(reader.getPageSize(1));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c://iText多行報表結(jié)果.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
int pageNumber=1;
PdfImportedPage page1 = writer.getImportedPage(reader,pageNumber);
//cb.addTemplate(page1,0,1f,1f,1f,0,0);
// cb.addTemplate(page1, 1f, 0, 0, 1f, 0, 0);
//cb.addTemplate(page1, 0, 1, -1, 0, 600, 500);
//cb.addTemplate(page1, 0, 1, 0, 1, 600, 500);
//float textBase = document.bottom() - 20;
//cb.addTemplate(page1,document.left(),textBase);
//cb.addTemplate(page1, 1, -1, 1, 5, 10, 700);
PdfTemplate template = cb.createTemplate(500, 200);
template.moveTo(0, 200);
template.lineTo(500, 0);
template.stroke();
template.beginText();
bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
template.setFontAndSize(bf, 12);
template.setTextMatrix(100, 100);
template.showText("Text at the position 100,100 (relative to the template!)");
template.endText();
//cb.addTemplate(template, 0, 400);
cb.addTemplate(page1,(float)Math.cos(180/180),(float)Math.sin(180/180),(float)Math.sin(180/180),(float)Math.cos(180/180), 0, 400);
// cb.beginText();
// cb.setFontAndSize(bf, 12);
// cb.setTextMatrix(200, 20); //這里就是用來定位的值
// cb.showText("sdfsdfsdf");
// cb.endText();
document.close();
}
}
提示:你把pdfWriter 改成 rtfWriter 就可以生成word文檔了。沒試過,理論是這樣的 ^_^