importjava.io.*;
importjava.text.*;
importjava.util.*;
/**AsimpletexttestofSPDFpackage
*/
publicclassPDFDemo{
publicstaticvoidmain(String[]argv)throwsIOException{
PrintWriterpout;
if(argv.length==0){
pout=newPrintWriter(System.out);
}else{
if(newFile(argv[0]).exists()){
thrownewIOException(
"Outputfile"+argv[0]+"alreadyexists");
}
pout=newPrintWriter(newFileWriter(argv[0]));
}
PDFp=newPDF(pout);
Pagep1=newPage(p);
p1.add(newMoveTo(p,100,600));
p1.add(newText(p,"Helloworld,liveontheweb."));
p1.add(newText(p,"Helloworld,line2ontheweb."));
p.add(p1);
p.setAuthor("IanF.Darwin");
p.writePDF();
}
}
/**ThemainclassfortheDarwinOpenSystems
*{Simple,Stupid,Simplistic}PDFAPI.
*PDFisAdobe'sPortableDocumentFormat,andisprobablyatrademark
*ofAdobeSystemsInc,MountainView,California.
*TheAdobePDFSpecificationwhichtheypublishgrantseveryone
*permissiontowritecodetogenerateand/orprocessPDFfiles.
*APDFObjectrepresentsonePDFfile.
*@authorIanF.Darwin,http://www.darwinsys.com/
*@version$Id:PDF.java,v1.62004/02/0903:34:02ianExp$
*/
classPDF{
/**Theoutputwriter*/
protectedPrintWriterout;
/**Thelistofpages*/
protectedArrayListpages;
/**Thelistofobjectxrefs*/
protectedArrayListxrefs;
/**Therootobject*/
PDFObjectrootObj=newRootObject(this);
/**TheInfoobject*/
InfoObjectinfoObj=newInfoObject(this);
/**Theoutlines(outlinefont)object*/
OutlinesObjectoutlinesObj=newOutlinesObject(this);
/**ThePagesobject*/
PagesObjectpagesObj=newPagesObject(this);
/**TheFontDictionary*/
FontDictfontDict=newFontDict(this);
/**Theobjectnumberofthecurrentobject*/
protectedintcurrObj=1;
/**Aflagtoavoidwritingtwice*/
protectedbooleanstartedWriting=false;
/**AmagicnumberthatidentifiestheoutputasaPDFfile*/
protectedfinalstaticStringPDF_MAGIC="%PDF-1.0";
/**Constructor*/
publicPDF(PrintWritero){
out=o;
pages=newArrayList();
xrefs=newArrayList();
}
publicvoidadd(Pagep){
pages.add(p);
}
publicvoidinsertPage(intwhere,Pagep){
pages.add(where,p);
}
//OUTPUTMETHODS--weprovideourownprint/println,sowe
//cankeeptrackoffileoffsets(itwaseitherthat,orkludgily
//useaRandomAccessFileandthegetFilePointer()method).
longoffset=0;
/**PrintaString*/
protectedvoidprint(Strings){
out.print(s);
offset+=s.length();
}
/**PrintlnaString*/
protectedvoidprintln(Strings){
print(s);
print("\n");
}
/**PrintanObject*/
protectedvoidprint(Objecto){
print(o.toString());
}
/**PrintlnanObject*/
protectedvoidprintln(Objecto){
println(o.toString());
}
/**Printanint*/
protectedvoidprint(inti){
Strings=Integer.toString(i);
print(s);
}
/**Printlnanint*/
protectedvoidprintln(inti){
Strings=Integer.toString(i);
print(s);
}
/**Printlnwithnoargs*/
protectedvoidprintln(){
print("\n");
}
//ENDOFOUTPUTMETHODS
/**Addanentryintotheoffsettable*/
protectedvoidaddXref(){
xrefs.add(newLong(offset));
}
/**Writetheentireoutput*/
publicvoidwritePDF(){
if(startedWriting){
thrownewIllegalStateException(
"writePDF()canonlybecalledonce.");
}
startedWriting=true;
writePDFHeader();
writePDFbody();
writeXrefs();
writePDFTrailer();
out.flush();
out.close();
}
protectedvoidwritePDFHeader(){
println(PDF_MAGIC);
rootObj.print();//1
infoObj.print();//2
outlinesObj.print();//3
pagesObj.print();//4
}
protectedvoidwritePDFbody(){
for(inti=0;i<pages.size();i++){
((Page)pages.get(i)).print();//5,6
}
addXref();
print(currObj++);println("0obj");
println("[/PDF/Text]");
println("endobj");
fontDict.print();//8
}
DecimalFormatnf10=newDecimalFormat("0000000000");
DecimalFormatnf5=newDecimalFormat("00000");
/**WriteoneXref,intheformat000000000065535f*/
protectedvoidprintXref(longn,intwhere,charinUse){
println(nf10.format(n)+""+nf5.format(where)+""+inUse);
}
longxrefStart;
/**Writeallthexrefs,usingtheformatabove*/
protectedvoidwriteXrefs(){
xrefStart=offset;
println("xref");
print(0);
print("");
print(xrefs.size()+1);
println();
//"fake"entryat0,always0,-1,f(free).
printXref(0,65535,'f');
//Remainingxrefentriesareforrealobjects.
for(inti=0;i<xrefs.size();i++){
Longlo=(Long)xrefs.get(i);
longl=lo.longValue();
printXref(l,0,'n');
}
}
protectedvoidwritePDFTrailer(){
println("trailer");
println("<<");
println("/Size"+(xrefs.size()+1));
println("/Root10R");
println("/Info20R");
println(">>");
println("%startxref");
println("%"+xrefStart);
println("%%EOF");
}
classRootObjectextendsPDFDict{
protectedRootObject(PDFm){
super(m);
dict.put("Type","/Catalog");
dict.put("Outlines","30R");
dict.put("Pages","40R");
}
}
classInfoObjectextendsPDFDict{
protectedInfoObject(PDFm){
super(m);
dict.put("Title","(SamplePDFbySPDF)");
dict.put("Creator","(DarwinOpenSystemsSPDFSoftware)");
dict.put("Created","(D:20000516010203)");
}
}
publicvoidsetAuthor(Stringau){
infoObj.dict.put("Author","("+au+")");
}
classOutlinesObjectextendsPDFDict{
protectedOutlinesObject(PDFm){
super(m);
dict.put("Type","/Outlines");
dict.put("Count","0");
}
}
classPagesObjectextendsPDFDict{
protectedPagesObject(PDFm){
super(m);
dict.put("Type","/Pages");
dict.put("Count","1");
dict.put("Kids","[50R]");
}
}
classFontDictextendsPDFDict{
protectedFontDict(PDFm){
super(m);
dict.put("Type","/Font");
dict.put("Subtype","/Type1");
dict.put("Name","/F1");
dict.put("BaseFont","/Helvetica");
dict.put("Encoding","/MacRomanEncoding");
}
}
}
/**APDFDictiasaPDFObjectthatisall,ormostly,aDictionary.
*@authorIanDarwin,http://www.darwinsys.com/
*/
abstractclassPDFDictextendsPDFObject{
/**TheDictionaryisaHashTable.Putthekeyswithouta
*leadingslash,sincetheyalwayshaveit.Valuescan
*be/names,(strings),orwhatever.
*/
protectedHashtabledict;
PDFDict(PDFm){
super(m);
dict=newHashtable();
}
/**WritetheobjecttotheOutputWriter.Thedefaultimplementation
*ofthismethodinPDFDictjustcallsstartObj,printDict,andendObj.
*/
protectedvoidprint(){
startObj();
printDict();
endObj();
}
protectedvoidstartObj(){
//RecordthestartingpositionofthisObjinthexreftable
master.addXref();
//Printoute.g.,"420obj"
master.print(master.currObj++);
master.print("0obj");
master.println();
}
protectedvoidendObj(){
master.println("endobj");
}
protectedvoidprintDict(){
master.println("<<");
Enumeratione=dict.keys();
while(e.hasMoreElements()){
master.print("\t/");
Stringkey=(String)e.nextElement();
master.print(key);
master.print("");
master.print(dict.get(key));
master.println();
}
master.println(">>");
}
}
/**RepresentoneTextobjectinaPDFfile.*/
classTextextendsPDFObject{
protectedintx,y;
protectedStringtext;
publicText(PDFm,Strings){
super(m);
text=s;
}
publicvoidprint(){
thrownewIllegalStateException("print()calledonaTextobj");
}
publicvoidprint(StringBuffersb){
sb.append("0-18Td(");
sb.append(text);//TODOmustsubstituteescapedcharacters
sb.append(")Tj\n");
}
}
/**APDFObjectrepresentsonenodeinthetreeofaPDFfile.
*@authorIanDarwin,http://www.darwinsys.com/
*/
abstractclassPDFObjectextendsjava.lang.Object{
/**ThecontainingPDFfile*/
protectedPDFmaster;
PDFObject(PDFm){
master=m;
}
/**WritetheobjecttotheOutputWriter*/
protectedabstractvoidprint();
protectedvoidstartObj(){
//RecordthestartingpositionofthisObjinthexreftable
master.addXref();
//Printoute.g.,"420obj"
master.print(master.currObj++);
master.print("0obj");
master.println();
}
protectedvoidendObj(){
master.println("endobj");
}
}
/**RepresentoneMoveobject("moveto")inaPDFfile.*/
classMoveToextendsPDFObject{
protectedintx,y;
publicMoveTo(PDFm,intx,inty){
super(
Java 打印到PDF文件
最新推荐文章于 2024-09-21 11:08:38 发布