Java Tutorial/PDF/PDF Reader — различия между версиями

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

Текущая версия на 05:22, 1 июня 2010

Create PdfReader

import com.lowagie.text.pdf.PdfReader;
public class MainClass {
  public static void main(String[] args) throws Exception {
    PdfReader reader = new PdfReader("HelloWorldRead.pdf", null);
  }
}





Get Document Information from PdfReader

import com.lowagie.text.pdf.PdfReader;
public class MainClass {
  public static void main(String[] args) throws Exception {
    PdfReader reader = new PdfReader("HelloWorldToRead.pdf");

    System.out.println("PDF Version: " + reader.getPdfVersion());
    System.out.println("Number of pages: " + reader.getNumberOfPages());
    System.out.println("File length: " + reader.getFileLength());
    System.out.println("Encrypted? " + reader.isEncrypted());
    System.out.println("Rebuilt? " + reader.isRebuilt());
  }
}





Get Pdf document information as a map

import java.util.Iterator;
import java.util.Map;
import com.lowagie.text.pdf.PdfReader;
public class MainClass {
  public static void main(String[] args) throws Exception {
    PdfReader reader = new PdfReader("HelloWorldToRead.pdf");
    Map info = reader.getInfo();
    for (Iterator i = info.keySet().iterator(); i.hasNext();) {
      String key = (String) i.next();
      String value = (String) info.get(key);
      System.out.println(key + ": " + value);
    }
  }
}





Get Pdf document size

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    PdfReader reader = new PdfReader("1.pdf");
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("my.pdf"),PdfWriter.VERSION_1_5);
    stamper.setFullCompression();
    stamper.close();
    reader = new PdfReader("1.pdf");
    stamper = new PdfStamper(reader, new FileOutputStream("myDecompressed.pdf"), "1");
    Document.rupress = false;
    int total = reader.getNumberOfPages() + 1;
    for (int i = 1; i < total; i++) {
      reader.setPageContent(i, reader.getPageContent(i));
    }
    stamper.close();
    showFileSize("1.pdf");
    showFileSize("my.pdf");
    showFileSize("myDecompressed.pdf");
  }
  private static void showFileSize(String filename) throws IOException {
    PdfReader reader = new PdfReader(filename);
    System.out.print("Size ");
    System.out.print(filename);
    System.out.print(": ");
    System.out.println(reader.getFileLength());
  }
}