Showing posts with label IText. Show all posts
Showing posts with label IText. Show all posts

Thursday, June 21, 2018

Protect PDF using password in iText

When you need to protect PDF files using password, you can use following iText codes to encrypt PDF using passwords.

String ownerPasswd="owner";
String userPasswd="user";

PdfReader reader = new PdfReader("/apps/abc.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("/apps/abc_pw.pdf"));
stamper.setEncryption(userPasswd.getBytes(), ownerPasswd.getBytes(),
PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
stamper.close();
reader.close();

Library versions used,
itextpdf-5.5.10.jar
bcprov-ext-jdk14-1.47.jar

You can use preferred encryption algorithms. Refer documentations.

Sunday, January 17, 2010

Embed fonts for pdf usng itext

Usually, "consola" font family is not used for pdfs, because it is not available in all systems. Using following code sample, you can embed your interested fonts for pdfs.

Registering regular True type "consola" font.
FontFactory.register("D:\\consola.ttf", "Manning");
FontFactory.getFont("Manning", BaseFont.CP1252, BaseFont.EMBEDDED);
Registering bold True type "consola" font.
FontFactory.register("D:\\consolab.ttf","Manning");
FontFactory.getFont("Manning", BaseFont.CP1252, BaseFont.EMBEDDED);

Sunday, April 19, 2009

Combine two pdf files using itext

Here is a simple way to combine two pdf files into one.
I have used iText-2.1.5.jar

This is the sample code

import java.io.FileOutputStream;

import com.lowagie.text.pdf.PdfCopyFields;

import com.lowagie.text.pdf.PdfReader;

public class PDFTest {

public static void main(String[] args) throws Exception {

System.out.println("Concatenate Two PDF");

PdfReader reader1 = new PdfReader("D:/upload/first.pdf");

PdfReader reader2 = new PdfReader("D:/upload/second.pdf");

PdfCopyFields copy = new PdfCopyFields(new FileOutputStream("D:/upload/concatenatedPDF.pdf"));

copy.addDocument(reader1);

copy.addDocument(reader2);

copy.close();

}

}