Introduction to 1 and iText
IText is an open source Java class library, which can be used to generate PDF files conveniently. You can visit/itext/itext/tutorial/index.html.
There are more detailed tutorials. From the beginning, this course systematically introduces the methods and skills of putting words, pictures and tables into PDF files.
After reading this tutorial, you can change some PDF files from simple to complex. However, it is undoubtedly an extravagant hope to try to solve all the difficulties encountered in the process of generating PDF files through tutorials. Therefore, it is very important to read the api documentation of iText. Readers can download the documents of the class library at the same time.
Note: If the above two download links can't be downloaded, comrades who can't find this jar package through the network can leave their email, and I will send them an email within two working days.
The following part is the source code I debugged for your reference:
Import java.awt. *;
Import java.io. *;
Import com.lowagie.text. *;
Import com.lowagie.text.font;
Imported?
com . lowa gie . text . rectangle;
Import com.lowagie.text.pdf. *;
/**
* In the recent project, the txt file was converted into a PDF file by using Itext, which realized some rights control on the file.
Add reality to pdf file
* confidentiality, watermarking, etc.
*/
Public class PDFConvertBL
{
//
The path of the txt original file.
Private static final string txtfile path = "d:/11.txt";
//The path of the generated pdf file.
Private static final string pdfFilePath =
”d:/22 . pdf”;
//Add watermark image path
//Private static final string
imagefile path = " D:/33 . jpg ";
//Generate temporary file prefix
Private static end
String prefix = " tempFile
//Owner password
Private static final string
owner password = " 12345678 ";
/**
* txt file is converted into pdf file.
*
* @param txtFile
Txt file path
* @param pdfFile pdf file path
* @param user password
User password
* @param waterMarkName watermark content
* @param permission
Transport permit
*/
Public static void generated pfwithtxt (String Txtfile,
String pdf file, string user password, string
Watermark name,
Int permissions)
{
attempt
{
//Generate a temporary file
File file =
File.createTempFile (prefix. pdf”);
//
Create a pdf file as a temporary file
if (createPDFFile(txtFile,File))
{
//Add Watermarking and Encryption
Watermark (file.getPath (),
pdfFile、userPassWord、OWNERPASSWORD、waterMarkName、permission);
}
}
Capture (exception e)
{
e . printstacktrace();
}
}
/**
* create a PDF document
*
* @param txtFilePath
Txt file path (source file)
* @param pdfFilePath pdf file path (new file)
*/
private
Static Boolean createPDFFile (txtFilePath, file file)
{
//Set paper
Rectangle rect = new rectangle (PageSize. a4);
//
set the page number
Headerfooter footer = new headerfooter (new phrase ("page number:",
setChineseFont()),true);
Footer.setBorder (rectangle. NO _ BORDER);
//Step 1
document
Doc = new document (rect, 50, 50, 50, 50);
Doc.setFooter (footer);
attempt
{
File reader
fileRead = new file reader(txtFilePath);
BufferedReader read = new
buffered reader(fileRead);
//Set the pdf file generation path step2.
PdfWriter.getInstance(doc,new file output stream(file));
//
Open pdf file step 3.
doc . open();
//Instantiate a paragraph
Get the content written in the pdf file and call the method supporting Chinese. Fourth step
while (read.ready())
{
//Add the content to pdf (it will be output in the original style of txt file here)
Doc.add (new paragraph (read.readLine (), setchinesefont ()));
}
//Close the pdf file Step 5.
doc . close();
Return true
}
Capture (exception e)
{
e . printstacktrace();
Returns false
}
}
/**
* Add a watermark to the pdf file.
*
* @param input file
Original document
* @param outputFile watermark output file
* @param waterMarkName
Watermark name
*/
Private static void watermark (string input file, string
Output file, string user password, string owner password,
String waterMarkName, int permission)
{
attempt
{
pdf reader reader = new pdf reader(input file);
PdfStamper stamper = new pdf stamper (reader, new
FileOutputStream);
//Set the password
stamper . set encryption(user password . getbytes()、ownerPassWord.getBytes(),
Permission, false);
BaseFont base =
base font . create font(" ST song-Light "," UniGB-UCS2-H ",
Basic fonts. Not _ embedded);
int total = reader . getnumberofpages()+
1;
// Image image =
image . getinstance(image file path);
//
image.setAbsolutePosition(200,400);
PdfContentByte
Down;
int j = water mark name . length();
char c =
0;
int rise = 0;
for(int I = 1; I < total;
i++)
{
Rise = 500;
Below =
stamper . getundercontent(I);
//Add a picture
//
Under.addImage (picture);
under . begin text();
Under.setColorFill (color. Cyan);
under.setFontAndSize(base,
30);
//Set the watermark font tilt start.
if(j & gt; =
15)
{
under.setTextMatrix(200,
120);
for(int k = 0; k & ltj;
k++)
{
Under.setTextRise (rise);
c =
water mark name . charat(k);
under.showText(c +
"");
Rise-= 20;
}
}
other
{
under.setTextMatrix( 180, 100);
for(int k = 0; k & ltj; k++)
{
Under.setTextRise (rise);
c = water mark name . charat(k);
under.showText(c +
"");
rise-= 18;
}
}
//End of font setting
under . end text();
//Draw a circle
//
Under the ellipse (250, 450, 350, 550);
//
under . set line width( 1f);
//under . stroke();
}
stamper . close();
}
Catch (exception
e)
{
e . printstacktrace();
}
}
/**
* Set Chinese
*
* @ Returns the font
*/
Private static font setChineseFont ()
{
BaseFont base =
null
Font fontChinese = null
attempt
{
base = base font . create font(" ST song-Light "," UniGB-UCS2-H ",
Basic fonts. Embedded);
FontChinese = new font (base, 12,
Font. Normal);
}
Catch (document exception e)
{
e . printstacktrace();
}
catch (IOException
e)
{
e . printstacktrace();
}
Return to fontChinese
}
Public static void main(String[] args)
{
Generate epdfwithtxt (txtfile path, pdf file path, "123", "watermark text",16);
}
}
Referring to some online blogs, the article is slightly modified and debugged, and it is hereby declared.
/sx _ python/item/ 1508 153 1ad 7d 1bc 2 1b 96965 e