Java/PDF RTF/Encrypted PDF

Материал из Java эксперт
Перейти к: навигация, поиск

Encrypted PDF: Allow Assembly, FillIn, ScreenReaders, ModifyContents

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class EncryptedPDFAllowAssemblyFillInScreenReadersModifyContents {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("EncryptedPDFAllowAssemblyFillInScreenReadersModifyContents.pdf"));
      // setEncryption(boolean strength, String userPassword, String
      // ownerPassword, int permissions)
      writer.setEncryption(PdfWriter.STRENGTH40BITS, "jexp.ru", "World", 
          PdfWriter.AllowAssembly | PdfWriter.AllowFillIn | PdfWriter.AllowScreenReaders| PdfWriter.AllowModifyContents);
      document.open();
      document.add(new Paragraph("Hello World"));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }}





Encrypted PDF: AllowCopy, Printing

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class EncryptedPDFWithAllowCopyPrinting {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("EncryptedPDFWithAllowCopyPrinting.pdf"));
      //setEncryption(boolean strength, String userPassword, String ownerPassword, int permissions) 
      writer.setEncryption(PdfWriter.STRENGTH128BITS, "jexp.ru", "World", PdfWriter.AllowCopy | PdfWriter.AllowPrinting);
      document.open();
      document.add(new Paragraph("Hello World"));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Encrypted PDF with: User Password and Owner Password

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class EncryptedPDFWithUserPasswordOwnerPassword {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("EncryptedPDFWithUserPasswordOwnerPassword.pdf"));
      //setEncryption(boolean strength, String userPassword, String ownerPassword, int permissions) 
      writer.setEncryption(PdfWriter.STRENGTH128BITS, "jexp.ru", "World", PdfWriter.AllowCopy | PdfWriter.AllowPrinting);
      document.open();
      document.add(new Paragraph("Hello World"));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Encryptor Example

import java.io.FileOutputStream;
import com.lowagie.text.pdf.PdfEncryptor;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
public class EncryptorExamplePDF {
  public static void main(String[] args) {
    try {
      PdfReader reader = new PdfReader("YourOwnPDF.pdf");
      PdfEncryptor.encrypt(reader, new FileOutputStream("EncryptorExamplePDF.pdf"), "Hello".getBytes(),
          "World".getBytes(), PdfWriter.AllowPrinting | PdfWriter.AllowCopy, false);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}