Java encrypted PDF Set Password and add watermark

Source: Internet
Author: User

/**
* Project name:xxx
* File Name:EncryptLogFile.java
* Date:2016-6-12 11:56:38
* Copyright (c), all rights Reserved.
*
*/

Package com.xxx. Xxx.utils;

Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.FileReader;
Import java.io.IOException;

Import com.lowagie.text.Document;
Import com.lowagie.text.DocumentException;
Import Com.lowagie.text.Font;
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.pdf.BaseFont;
Import Com.lowagie.text.pdf.PdfReader;
Import Com.lowagie.text.pdf.PdfStamper;
Import Com.lowagie.text.pdf.PdfWriter;

/**
* Classname:encryptlogfile
* Function: Log document to PDF and add password and watermark. Date:2016-6-12
* 11:56:38
*
* @author Yang
* @version
*/
public class Encryptlogfile {
Generate temporary file prefixes
Private static final String prefix = "Tempfile";

Owner password
private static final String OwnerPassword = "12345678";

/**
* TXT file conversion to PDF file
*
* @param txtfile
* TXT file path
* @param pdffile
* PDF file path
* @param userpassword
* User Password
* @param watermarkname
* Watermark Content
* @param permission
* Operation permissions
*/
public static void Generatepdfwithtxt (String txtfile, String pdffile,
String UserPassword, string watermarkname, int permission) {
try {
Generate Temporary files
File File = file.createtempfile (prefix, ". pdf");
Create a PDF file to a temporary file
if (Createpdffile (txtfile, file)) {
Add watermarks and encryption
WaterMark (File.getpath (), Pdffile, UserPassword, OwnerPassword,
Watermarkname, permission);
}
} catch (Exception e) {
E.printstacktrace ();
}
}

/**
* Create PDF document
*
* @param Txtfilepath
* TXT file path (source file)
* @param Pdffilepath
* PDF file path (new file)
*/
private static Boolean Createpdffile (String txtfilepath, file file) {
Set up paper
Rectangle rect = new Rectangle (PAGESIZE.A4);
Set Page numbers
HeaderFooter footer = new HeaderFooter (New Phrase ("page number:",
Setchinesefont ()), true);
Footer.setborder (Rectangle.no_border);

Step1
Document doc = new document (Rect, 50, 50, 50, 50);
Doc.setfooter (footer);
try {
FileReader fileRead = new FileReader (Txtfilepath);
BufferedReader read = new BufferedReader (fileRead);
Set PDF file Generation path Step2
Pdfwriter.getinstance (Doc, new FileOutputStream (file));
Open PDF File Step3
Doc.open ();
Instantiate paragraph gets the content written to the PDF file, calling a method that supports Chinese. Step4
while (Read.ready ()) {
Add content to PDF (this will be output according to the original style of the TXT file)
Doc.add (New Paragraph (Read.readline (), Encryptlogfile.setchinesefont ()));
}
Close PDF File Step5
Doc.close ();
return true;
} catch (Exception e) {
E.printstacktrace ();
return false;
}
}

/**
* in PDF encryption
*
* @param inputfile
* Original File
* @param outputFile
* Watermark Output file
* @param watermarkname
* Watermark Name
*/
private static void WaterMark (String inputfile, String outputFile,
String UserPassword, String OwnerPassword, String watermarkname,
int permission) {
try {
Pdfreader reader = new Pdfreader (inputfile);
Pdfstamper stamper = new Pdfstamper (reader, New FileOutputStream (
OutputFile));
Set Password
Stamper.setencryption (Userpassword.getbytes (),
Ownerpassword.getbytes (), permission, false);
Stamper.close ();
} catch (Exception e) {
E.printstacktrace ();
}
}

/**
* Set Chinese
*
* @return Font
*/
private static Font Setchinesefont () {
Basefont base = null;
Font Fontchinese = null;
try {
Base = Basefont.createfont ("Stsong-light", "Unigb-ucs2-h",
basefont.embedded);
Fontchinese = new Font (base, Font.normal);
} catch (Documentexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
return Fontchinese;
}
}
Portal: Encrypt the required jar package

Java encrypted PDF Set Password and add watermark

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.