Java/PDF RTF/PDF Read

Материал из Java эксперт
Версия от 05:58, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add Page Numbers to Existing PDF document

import java.io.FileOutputStream;
import com.lowagie.text.Element;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class AddPageNumbersToExistingPageNumberPDF {
  public static void main(String[] args) {
    try {
      PdfReader reader = new PdfReader("YourOwnPDF.pdf");
      int n = reader.getNumberOfPages();
      PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("AddPageNumbersToExistingPageNumberPDF.pdf"));
      int i = 0;
      PdfContentByte over;
      BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
      while (i < n) {
        i++;
        over = stamp.getOverContent(i);
        over.beginText();
        over.setFontAndSize(bf, 18);
        over.setTextMatrix(30, 30);
        over.showText("page " + i);
        over.setFontAndSize(bf, 32);
        over.showTextAligned(Element.ALIGN_LEFT, "DUPLICATE", 230, 430, 45);
        over.endText();
      }
      stamp.close();
    } catch (Exception de) {
      de.printStackTrace();
    }
  }
}





Reading Your Own PDF File

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
public class ReadYourOwnPDFFile {
    public static void main(String[] args) {
        try {
            PdfReader reader = new PdfReader("YourOwnPDF.pdf");
            int n = reader.getNumberOfPages();
            // we retrieve the size of the first page
            Rectangle psize = reader.getPageSize(1);
            System.out.println(reader.getPdfVersion());
            System.out.println(reader.getFileLength());
            System.out.println(psize.height());
            System.out.println(psize.width());
        }
        catch (Exception de) {
            de.printStackTrace();
        }
    }
}





Reads the pages of an existing PDF file and puts 2 pages from the existing doc into one of the new doc

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
public class TwoOnOnePDF {
  public static void main(String[] args) {
    System.out.println("");
    try {
      PdfReader reader = new PdfReader("YourOwnPDF.pdf");
      int n = reader.getNumberOfPages();
      Rectangle psize = reader.getPageSize(1);
      float width = psize.height();
      float height = psize.width();
      Document document = new Document(new Rectangle(width, height));
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TwoOnOnePDF.pdf"));
      document.open();
      PdfContentByte cb = writer.getDirectContent();
      document.newPage();
      PdfImportedPage page1 = writer.getImportedPage(reader, 1);
      cb.addTemplate(page1, .5f, 0, 0, .5f, 60, 120);
      
      document.close();
    } catch (Exception de) {
      de.printStackTrace();
    }
  }
}