0

I want to make a password protected pdf by java itext. This code I get from net .

public class GeneratePDF
 {
    private static String USER_PASS = "Hello123";

    private static String OWNER_PASS = "Owner123";


    public static void main(String[] args) {
        try {

            OutputStream file = new FileOutputStream(new File("D:\\Test.pdf"));

            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document, file);

            writer.setEncryption(USER_PASS.getBytes(), OWNER_PASS.getBytes(),
                    PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);

            document.open();
            document.add(new Paragraph("Hello World, iText"));
            document.add(new Paragraph(new Date().toString()));

            document.close();
            file.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}

They said to run the code I need these jar files-

• itextpdf-5.2.1.jar

• bcmail-jdk16-1.46.jar

• bcprov-jdk16-1.46.jar

• bctsp-jdk16-1.46.jar

I add this , but every time there is - Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1Primitive How can I successfully run my code ?

Vivin
  • 1,327
  • 2
  • 9
  • 28
Alimur Razi Rana
  • 87
  • 1
  • 2
  • 9
  • I add this.. where? NoClassDefFoundError are because these jars are not available at runtime (which is different from compile time). – kosa Aug 19 '16 at 03:23
  • I use NetBeans . And there is a option named add jar/folder I add there . I added itext.jar here before and successfully run programme about pdf . – Alimur Razi Rana Aug 19 '16 at 03:36
  • 3
    Hint: you want to step back and read about the Java **classpath**. What it is, what it contains; and how it affects compiling and running java classes. Then you want to understand how your NetBeans project setup relates to that. In other words: you want to stop blindly doing things, but start to care about the underlying concepts! – GhostCat Aug 19 '16 at 04:00
  • 1
    check http://stackoverflow.com/questions/14042693/itext-and-org-bouncycastle-asn1-asn1primitive-not-found – kuhajeyan Aug 19 '16 at 06:30
  • Please **throw away** iText 5.2.1! It has serious flaws and it was completely removed from the original releases: see the [official changelogs](http://developers.itextpdf.com/content/changelogs/itext-520-521-february-29-2012-march-31-2012) where it says **IMPORTANT: we've removed all 5.2.x versions from SourceForge because of a major issue with large PDF files. Please don't use iText 5.2.0 or 5.2.1!** This won't fix your CLASSPATH problem (that's a trivial problem), but it will avoid much bigger issues. – Bruno Lowagie Aug 19 '16 at 06:54
  • In addition to Bruno's comment: (1) You want to use iText 5.5.9. (2) You want to use Maven, which will automatically pull in the correct Bouncycastle dependency. (3) You want to configure Netbeans to use Maven. – Amedee Van Gasse Aug 19 '16 at 07:55

0 Answers0