🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.lang.java
Article #19440

Re: ��JAVA��ʹ��PDF

#19440
From: ellipsis
Date: Sun, 04 Jan 2004 11:24
355 lines
20849 bytes
������������岻��

��ֱ�Ӹ��ߴ�ң��õ��Ǹ�pacakge,���������أ���url������ͺ�����

˵�˰��죬��Ҫ���Լ���googleȥ�ѣ�ft


�� �� flyingsky (���) �Ĵ������ᵽ: ��

��һ���֡�����һ���򵥵�PDF�ĵ�
1�� ����һ��com.lowagie.text.Documen����
Document document = new Document();
2�� ����һ��Write���󣬰�Document����д��������С�
PdfWriter.getInstance(document,new��FileOutputStream("Chap0101.pdf"));
3�� ���ļ�
document.open();
4�� ��������ӵ�document
document.add(new Paragraph("Hello World"));

5�� �ر��ĵ�
document.close();
���ĵ�����
com.lowagie.text.Documen��������캯��
public Document();
public Document(Rectangle pageSize);
public Document(Rectangle pageSize,
   int marginLeft,
   int marginRight,
   int marginTop,
   int marginBottom);
��һ�����캯����õڶ������캯����PageSize.A4Ϊ����ڶ������캯����õ����
���캯��36��Ϊÿ�������ֵ��
��1��   �ĵ��Ĵ�С��pagesize��
����Դ����Լ����ĵ���ͼ���Ͷ���Rectangle����������С����ɫ��
�����ǿ��Դ���һ���Ի�ɫΪ�����ij�խ���ĵ�����
Rectangle pageSize = new Rectangle(144, 720);
pageSize.setBackgroundColor(new java.awt.Color(0xFF, 0xFF, 0xDE));
Document document = new Document(pageSize);
������Զ����Լ�ϲ�������ĵ���С��PageSize���ṩ�ܶ�����ĵ���С�ľ�̬����A0-A
10, LEGAL, LETTER, HALFLETTER, _11x17, LEDGER, NOTE, B0-B5, ARCH_A-ARCH_E, FLS
A �� FLSE
ͨ��PageSize��������ĵ�������PORTRAIT���ķ�ʽ��ʾ�����������Ҫ��ת����
��LANDSCAPE����Ҫ��rotate()����ת��
Document document = new Document(PageSize.A4.rotate());
��2��   �ĵ��߾�
������Լ��趨�ĵ��ı߾ࣺ
Document document = new Document(PageSize.A5, 36, 72, 108, 180);
ע�⣺����ȷ���ĵ��ı߾�ʱҪע�������λ
72���㣽1Ӣ��
Ĭ��Ϊ36����Ϊ��Ӣ�硣


�������(Write)
�ĵ����󴴽��ú�����Դ���һ�������������������ĵ������е�����Ա���ʵ�ֳ���
��com.lowagie.text.DocWriter��
��ʱ�������������ĵ���һ������com.lowagie.text.pdf.PdfWriter���PDF�ĵ���һ��
����com.lowagie.text.html.HtmlWriter���HTML�ĵ���
�����㻹�������TeX-documents�����������com.lowagie.text.TeX.TeXWriter.��

Write��û�й��õĹ��캯�������ͨ��public static xxxWriter getInstance(Docum
ent document, OutputStream os) throws DocumentException��������Ķ����磺
PdfWriter��writer = PdfWriter.getInstance(document,new FileOutputStream("Chap0
1xx.pdf"));
�㼸������ҪWrite���󣬳������ҪһЩ�߼���Ӧ�ã�������ֻҪPdfWriter.getInstan
ce(document, new��FileOutputStream("Chap01xx.pdf"));
��һ������ֻҪ�ǵ�һ�����ĵ�����Ϳ����ˣ��ڶ�������������κ����͵��������ͨ
��������java.io.FileOutputStream���ĵ����󱣴浽һ���ļ�����ʱ�����ǻ���javax.
servlet.ServletOutputStream�������������������MSIS��ʱ��ῴ��һ���հ�ҳ�棬
�����MSIEһ��BUG�������ͨ�����ǵ�������������������

һЩ�������ݺ������ĵ�
1�� ��������
�ڼ����ĵ�����ǰ����Լ���һЩ��־�����ݣ�
public boolean addTitle(String title)
public boolean addSubject(String subject)
public boolean addKeywords(String keywords)
public boolean addAuthor(String author)
public boolean addCreator(String creator)
public boolean addProducer()
public boolean addCreationDate()
public boolean addHeader(String name, String content)
ע�⣺��PDF�в����á�addHeader(String name, String content)����
2�� ���ĵ�ǰ����������
������ĵ�ǰ�������һЩ�������ڿ��ڴ��ĵ���Ͳ������޸��ˡ���PDF�б�������
�ݱ�������PdfInfo�����У����ĵ��ر�ʱ���档
3�� ҳ���ʼ��
�ڴ��ĵ�ʱ������һЩ��ͬ����������ʼ��������������ڵ�һ��ҳ������һЩˮ
ӡ��Watermark����ҳüҳ�ţ�HeaderFooter����ͬ����ֻҪ���ĵ���ǰ���á�ͬ��Ҳ��
������һЩ��Ķ�����ҳ���С��
��ֻҪ��������ĺ���
public boolean setPageSize(Rectangle pageSize)
public boolean add(Watermark watermark)
public void removeWatermark()
public void setHeader(HeaderFooter header)
public void resetHeader()
public void setFooter(HeaderFooter footer)
public void resetFooter()
public void resetPageCount()
public void setPageCount(int pageN)
������������Щ���������ֻ������һ��ҳ�濴��Ч��example 7��.
��������
�����ͨ������ķ�������һ�Ķ�����
public void setViewerPreferences(int preferences)
��8��һЩ�����Ӧ��
writerA.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft);
������һЩ���ò���
��1��   ��ҳ���ʱʹ��ҳ�沼�֡�
PdfWriter.PageLayoutSinglePage��ÿ����ʾһ��ҳ��
PdfWriter.PageLayoutOneColumn����һ����ʾҳ�档
PdfWriter.PageLayoutTwoColumnLeft�����������ʽ��ʾҳ�棬����ҳ�������
PdfWriter.PageLayoutTwoColumnRight�����������ʽ��ʾҳ�棬����ҳ�����ұ�

��2��   ʲô���ҳ��ģʽ���ڴ�ʱ��ʾ
PdfWriter.PageModeUseNone���ĵ���ͼ������ݶ�����ʾ
PdfWriter.PageModeUseOutlines����ʾ�ĵ�����
PdfWriter.PageModeUseThumbs����ʾСͼ
PdfWriter.PageModeFullScreen���������ݺ�ͼ��֮��û����������ʾ��
��3��   PdfWriter.HideToolbar���Ƿ���ʾ��������
��4��   PdfWriter.HideMenubar �C���Ƿ���ʾ�˵���
��5��   PdfWriter.HideWindowUI���Ƿ���ʾ�û��ӿڣ����������
��6��   PdfWriter.FitWindow������ʾ�ĵ�����ʱ�ܷ�ı䴰�ڵĴ�С��
��7��   PdfWriter.CenterWindow �C�Ƿ�ָ���ĵ���������Ļ���м���ʾֻ��ҳ��ģʽ��P
ageModeFullScreenʱ����ʹ�á���������ģʽ����ѡ��һ��
PdfWriter.NonFullScreenPageModeUseNone �C ����ʾ�ĵ���ͼ��
PdfWriter.NonFullScreenPageModeUseOutlines �C��ʾ�ĵ�
PdfWriter.NonFullScreenPageModeUseThumbs �C ��ʾͼ��
ע�⣺��ֻ��������ΪPdfWriter�е���
��8��   ��ȫ
������ڴ��ĵ�ʱ�����ĵ������롣
setEncryption(boolean strength, String userPassword, String ownerPassword, int
 permissions);
���������͵����볤�ȣ�
��1��   PdfWriter.STRENGTH40BITS��40λ
��2��   PdfWriter.STRENGTH128BITS��128λ��ֻ��Acrobat Reader 5.0���߰汾֧�֣�

userPassword �� ownerPassword�����ǿգ�NULL�������㳤�ȣ���������ownerPassword
����һ���������档
���û����������¼�����ɡ�
PdfWriter.AllowPrinting 
PdfWriter.AllowModifyContents 
PdfWriter.AllowCopy 
PdfWriter.AllowModifyAnnotations 
PdfWriter.AllowFillIn 
PdfWriter.AllowScreenReaders 
PdfWriter.AllowAssembly 
PdfWriter.AllowDegradedPrinting
��Щ������ʾ��9��10��ʾ
writer.setEncryption(PdfWriter.STRENGTH40BITS, null, null, PdfWriter.AllowCopy
);
���ʾ���ʾ�û�����û��������ĵ������Dz����Զ��ĵ����в������ӡ���޸ġ�

writer.setEncryption(PdfWriter.STRENGTH128BITS, "userpass", "ownerpass", PdfWr
iter.AllowCopy | PdfWriter.AllowPrinting);
���ʾ���ʾ�û����ĵ�Ҫ���룬���ǿ����κβ�����



�������
�������е�Write������������е��ࡣ����HTML�в�֧��Graphic����ʱ����Ҫһ���ĵ�
�м�����ʽ���ʱҲ������������
PdfWriter writerA = PdfWriter.getInstance(document, new FileOutputStream("Chap
0111a.pdf"));
PdfWriter writerB = PdfWriter.getInstance(document, new FileOutputStream("Chap
0111b.pdf"));
�⽫����?
���ǿ���ͨ��һЩϸ΢�ı仯�����������⣺
writerA.pause();
document.add(new Paragraph("This paragraph will only be added to Chap0111b.pdf
, not to Chap0111a.pdf"));
writerA.resume();
�ر�























�ڶ����ݡ��ı����������
�ı���chunk��
�ı������ı�������ӵ��ĵ��е���С���ݣ����Ǵ�������ݵĻ�ʯ������һ������
�����͵��ַ��±����������Ǵ�����һ����hello word�����ı���
Chunk chunk = new Chunk("Hello world", FontFactory.getFont(FontFactory.COURIER
, 20, Font.ITALIC, new Color(255, 0, 0)));

Type 1����
����������ʹ��Type 1���壬�����ڽ�������½���ѧϰ���塣�����Dz�ͬ���͵�Type 1
����
Courier (���ǹ̶���������)
Helvetica 
Times Roman 
Symbol 
ZapfDingbats 

�»��ߺ�ɾ����
�������������ı�������»��߻��ɾ������ֻҪ����������������ٵĸĶ���
Chunk chunk1 = new Chunk("This text is underlined", FontFactory.getFont(FontFa
ctory.HELVETICA, 12, Font.UNDERLINE));
Chunk chunk2 = new Chunk("This font is of type ITALIC | STRIKETHRU", FontFacto
ry.getFont(FontFactory.HELVETICA, 12, Font.ITALIC | Font.STRIKETHRU));

��ע��βע
�����кܶ෽������������ӽ�ע��βע������ֻ�õ���setTextRise(float f)�������
��������½��н��⡣

�ı��ı�����ɫ
�������setBackground(Color color).�����ñ�����ɫ��
ck.setBackground(new Color(0xFF, 0xFF, 0x00));
������һ��ʹ�����������

����
һ��������һЩ�ı���ɣ���һЩ�����һ��������һ����Ҫ�����壬���ڶ�����
��һЩ�ı����Ժ���Ҫ�����岻�õ����塣������ò�ͬ�Ĺ����������õĶ��

��ϣ������
��Ϊ��Ϊϣ���ַ�ʹ�÷dz��࣬�����ṩһЩ���ر��Phrase���캯�����ַ���Ϊ��
������㲻��ʹ���ַ������ʹ���ַ�����Կ���3������������Զ��Ľ�913��
937��945��969��Χ�ڵ��ַ������ת��

ȥ��ǰ��
���൱��һ��bug�����ǣ�����������Դ���һ������ǰ���ո�Ĵ�����Ǿ��ӵľɸ�
ʽ����������д����һ���ǿ������õģ�������������ƶ�һЩ������
ע�⣺����û�м�����Ƿ�Խ�磬���Ҳ����ƶ�����һҳ��
�½�
�½�����һЩ�ַ��һЩ������ɡ��Ͷ���һ���½�Ҳ��һЩǰ���ո��û��ܶ���һ
Щ����������һЩǰ����󵼿ո���ɡ��½��ܶ�����뷽ʽ��ÿ���ŵ��ĵ��е��½�
�����Զ��Ĵ����п�ʼ�������в�ͬ�ķ�������һ���½ڣ����磺
Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph.", FontFact
ory.getFont(FontFactory.HELVETICA, 12)));
Paragraph p2 = new Paragraph(new Phrase("This is my second paragraph.", FontFa
ctory.getFont(FontFactory.HELVETICA, 12)));
Paragraph p3 = new Paragraph("This is my third paragraph.", FontFactory.getFon
t(FontFactory.HELVETICA, 12));

�кܶ��ֶ����ܷŵ��½ڶ���paragraph�����磺
p1.add("you can add strings, "); 
p1.add(new Chunk("you can add chunks ")); 
p1.add(new Phrase("or you can add phrases."));
ע�⣺һ���½�ֻ��һ��ǰ���ո����������˱���½ڻ����ַ�ԭ��ǰ���ո���
������Ըı�ǰ���ո���setLeading��������ݽ�ʹ�������ǰ����example 5��

�����½ڵ�������
�������setKeepTogether(true)��������ͬһҳ�е��½������ԡ�����Ҳ����һ����Ҫ��
�����磺����½ںܳ�����ͬһҳ�оͲ���Ҫ������6��

������ʽ
�����㽫�˽��������������ʽ��������������7��
���㴴����һ��������½ڣ��������а���һЩ���壬����Ĭ�ϵ����壬���������
��Щ������ʽ�ڶ����ʼ��ʱ��
Phrase myPhrase = new Phrase("Hello 2! ", new Font(Font.TIMES_NEW_ROMAN, 8, Fo
nt.BOLD));
myPhrase.add(new Phrase("some other font ", new Font(Font.HELVETICA, 8, Font.I
TALIC)));
myPhrase.add(new Phrase("This is the end of the sentence.\n", new Font(Font.TI
MES_NEW_ROMAN, 8, Font.ITALIC)));
document.add(myPhrase);
���ǿ�ʼʱʹ����Times New Roman���壬����һЩ�ı���Helveticaû��ʹ����ʽ������
������Щ��ʽ����˴��壬�����Ǽ���һЩ�ı�ʹ����ITALIC��Times New Roman�����
��BOLDITALIC���壬�������ʹ��FontFactoryȥ����Font����Щ���彫�����죬��Ϊʹ��
������ļ���ȥ����Font��
myPhrase = new Phrase("Hello 1bis! ", FontFactory.getFont(FontFactory.TIMES_NE
W_ROMAN, 8, Font.BOLD));
myPhrase.add(new Phrase("some other font ", FontFactory.getFont(FontFactory.HE
LVETICA, 8, Font.ITALIC)));
myPhrase.add(new Phrase("This is the end of the sentence.\n", FontFactory.getF
ont(FontFactory.TIMES_NEW_ROMAN, 8, Font.ITALIC)));
document.add(myPhrase);
������������У����������û�����ó�Helvetica���ı��������itlatic ��Times New
 Roman��

���ǿ������½�����ͬ��ķ���
Paragraph myParagraph = new Paragraph("Hello 1! ", new Font(Font.TIMES_NEW_ROM
AN, 8, Font.BOLD));
myParagraph.add(new Paragraph("This is the end of the sentence.",FontFactory.g
etFont(new Font.TIMES_NEW_ROMAN, 8)));
document.add(myParagraph);
����Բ�����Щ�����㽫ʧȥ������ʽ�������ԡ���ʼ�����½�ʱ����Բ�ָ�����塣��
��
myParagraph = new Paragraph(12);
myParagraph.add(new Paragraph("Hello 3! ", new Font(Font.TIMES_NEW_ROMAN, 8, F
ont.BOLD)));
myParagraph.add(new Paragraph("This is the end of the sentence.", new Font(Fon
t.TIMES_NEW_ROMAN, 8, Font.ITALIC)));
document.add(myParagraph);
�����п�����ͬ��ķ�����

ʹ�÷ָ���
Ĭ�ϵķָ����ǡ� ���͡�-�����������setSplitCharacter.������������8��
ʹ�����ַ�
�������Chunk���������ַ�
// Hyphenating a British text
Chunk ckEN = new Chunk(textEN);
HyphenationAuto autoEN = new HyphenationAuto("en", "GB", 2, 2);
ckEN.setHyphenation(autoEN);
// Hyphenating a Dutch text
Chunk ckNL = new Chunk(textNL);
HyphenationAuto autoNL = new HyphenationAuto("nl", null, 2, 2);
ckNL.setHyphenation(autoNL);
���ǹ����������ַ�
ע�⣺��Ҫ��������������ҪiTextHYPH.jar���㽫���������ļ���
en_GB.hyp 
en_US.hyp 
fr.hyp 
nl.hyp
����������е������й�������˺ܶ����ԡ�
����֡����ӡ��С�ע��
����
��֪��������HMTL�ó����Ӵ�һ��ҳ�浽��һ��ҳ������PDF��Ҳ���������������ڳ�����
��һЩ�߼���Ӧ�����ǵ������ٽ��⣬������һЩ�򵥵�Ӧ�á��������Anchor���ﵽ��
��Ŀ�ġ���ֻҪʹ��2�������Ϳ��ԴﵽĿ�ģ�setName ��setReference��
���ӵ��ⲿ���ļ�
Anchor anchor = new Anchor("website", FontFactory.getFont(FontFactory.HELVETIC
A, 12, Font.UNDERLINE, new Color(0, 0, 255)));
anchor.setReference("http://www.lowagie.com/iText/");
anchor.setName("website");
���ӵ��ڲ��ļ�
�������ʹ���ڲ����ӣ���Ҫ��֤ʹ�õ�������Ψһ�ģ�������Ҫ��Ŀ����ʹ�ã���
�磺��0301.java
Anchor anchor1 = new Anchor("This is an internal link");
anchor1.setName("link1");
Anchor anchor2 = new Anchor("Click here to jump to the internal link");
anchor.setReference("#link1");
��
ʹ��List �� ListItem��������Ŀ¼��PDF�ĵ��С����������Ҳ���Բ��š�
����
List list = new List(true, 20);
list.add(new ListItem("First line"));
list.add(new ListItem("The second line is longer to see what happens once the 
end of the line is reached. Will it start on a new line?"));
list.add(new ListItem("Third line"));
������
List overview = new List(false, 10);
overview.add(new ListItem("This is an item"));
overview.add("This is another item");

--
http://bbs.sjtu.edu.cn/showfile?name=1060053491008.gif 
--
�� ��Դ:����ˮ˼Դ bbs.sjtu.edu.cn��[FROM: 211.80.36.130]
Technical Details
Message-ID: <4A8DES$9CR@bbs.sjtu.edu.cn>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!c03.atl99!sjc70.webusenet.com!news.webusenet.com!maily.cic.tsinghua.edu.cn!news.ccie.net.cn!SJTU

Show Full Headers
Complete Article Headers
X-Google-Language: Chinese,GB
X-Google-Thread: f6cb0,dfc04afa855b5719,start
X-Google-Attributes: gidf6cb0,public
X-Google-ArrivalTime: 2004-01-04 03:24:34 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!c03.atl99!sjc70.webusenet.com!news.webusenet.com!maily.cic.tsinghua.edu.cn!news.ccie.net.cn!SJTU
From: ellipsis.bbs@bbs.sjtu.edu.cn (ellipsis)
Newsgroups: cn.bbs.comp.lang.java
Subject: Re: ��JAVA��ʹ��PDF
Date: 04 Jan 2004 11:24:33 GMT
Organization: ��ˮ˼Դ
Message-ID: <4A8DES$9CR@bbs.sjtu.edu.cn>
Mime-Version: 1.0
Content-Type: text/plain; charset="gb2312"
Content-Transfer-Encoding: 8bit
X-Filename: Java/M.1073215420.A