Current location - Education and Training Encyclopedia - Resume - How does java output pdf files?
How does java output pdf files?
IText library is required for java to export pdf. IText is a project of sourceforge, a famous open source website, and a java class library for generating PDF documents. IText can not only generate PDF or rtf.

XML and Html files can be converted into PDF files.

The installation of iText is very convenient. After downloading the iText.jar file, you only need to add the path of iText.jar to the classpath of the system, and you can use it in the program.

IText class library

The code is as follows:

Public class createPdf {

//I made a simple example with pictures in the middle.

//Create a document object first: com.lowagie.text.Document is introduced into the corresponding version of jar.

Document Document = New document (page size. A4,36.0F,36.0F,36.0F,36.0 f);

PublicoVoid getpdfdemo () throws DocumentException, IOException{

//This export uses iTextAsian.jar and iText-2. 1.3.jar, both old methods. See address for details:

//First of all

//Definition of font: The font in jar is used here.

base font BF Chinese = base font . create font(" ST song-Light "," UniGB-UCS2-H ",false);

//Of course, you can also use the font library that comes with your computer.

//base font BF Chinese = base font . create font(" C:/WINDOWS/Fonts/SIMSUN。 TTC, 1 ",the basic font. IDENTITY_H,BaseFont。 Embedded);

//Define the font. Note that the color is encapsulated in the latest package.

Font Font Chinese 8 = new Font(BF Chinese, 10.0F,0,new Color(59,54,54));

//The first step in generating a pdf:

//Save the local specified path

save local();

document . open();

ByteArrayOutputStream ba = new ByteArrayOutputStream();

//pdf writer writer = pdf writer . getinstance(document,ba);

document . open();

//Get the file path of this compilation.

String path = this.getClass()。 getClassLoader()。 getResource(" ")。 getPath();

//Get the root path

string file path = path . substring( 1,path . length()- 15);

//Get the image path and find the image to be generated on the pdf.

//Write according to your own path here, just find the location of the picture.

string picPath = file path+" \ \ web content "+" \ \ images \ \ ";

//Add a paragraph to the PDF

Paragraph p header = new Paragraph();

PHeader.add(new Paragraph ("You want to generate text to write here", new font (bfChinese, 8.0F,1));

//p header.add (new paragraph ("text", the font can be written by itself or defined before fontChinese8);

document . add(pHeader); //Add what you wrote to the document.

//Get the picture

image img 2 = image . getinstance(picPath+" CCF-stamp-new . png));

//Defines the absolute position of the picture in the document.

img 2 . scale absolute( 137.0 f, 140.0 f);

img 2 . setabsoluteposition(330.0 f,37.0 f);

//Add a picture to the document

document . add(img 2);

//Close the document

document.close()。

/*//Set the file name of the saved document.

response.setHeader("Content-

Disposal "and" attachment "; Filename = \ "+ new string ("CCF member confirmation

Letter. pdf”)。 getbytes ("gbk ")," ISO-8859- 1 ")+" \ ");

//Set the type

response . setcontenttype(" application/pdf ");

response . setcontentlength(ba . size());

ServletOutputStream out = response . get output stream();

ba . write to(out);

out . flush(); */

}

Public static void main(String[]args) throws DocumentException, IOException{

create pdf pdf = new create pdf();

pdf . getpdfdemo();

}

//Specify the file to save. Here, save the file to text.pdf's D drive.

Public void saveLocal () throws IOException, DocumentException{

//Directly generate PDF, and formulate it to disk D test.pdf.

File File = new File(" D:\ \ text 2 . pdf ");

file . create new file();

PdfWriter.getInstance(document,new file output stream(file));

}

}