Java/PDF RTF/Page Size

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

Creates a PDF document with a certain pagesize: A9, A10, ARCH_A

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Creates a PDF document with a certain pagesize
 */
public class SettingPageSize11X17A9A10ARCHA {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SettingPageSize11X17A9A10ARCHA.pdf"));
      document.open();
      document.add(new Paragraph("First Page."));
      
      document.setPageSize(PageSize._11X17);
      document.newPage();
      document.add(new Paragraph("This PageSize is _11X17."));
      
      document.setPageSize(PageSize.A9);
      document.newPage();
      document.add(new Paragraph("This PageSize is A9."));
      document.setPageSize(PageSize.A10);
      document.newPage();
      document.add(new Paragraph("This PageSize is A10."));
      
      document.setPageSize(PageSize.ARCH_A);
      document.newPage();
      document.add(new Paragraph("This PageSize is ARCH_A."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Creates a PDF document with a certain pagesize: ARCH_B, ARCH_C, ARCH_D, ARCH_E

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Creates a PDF document with a certain pagesize
 */
public class SettingPageSizeARCHBCDE {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SettingPageSizeARCHBCDE.pdf"));
      document.open();
      document.add(new Paragraph("First Page."));
      
      document.setPageSize(PageSize.ARCH_B);
      document.newPage();
      document.add(new Paragraph("This PageSize is ARCH_B."));
      
      document.setPageSize(PageSize.ARCH_C);
      document.newPage();
      document.add(new Paragraph("This PageSize is ARCH_B."));
      document.setPageSize(PageSize.ARCH_D);
      document.newPage();
      document.add(new Paragraph("This PageSize is ARCH_B."));
      
      document.setPageSize(PageSize.ARCH_E);
      document.newPage();
      document.add(new Paragraph("This PageSize is ARCH_E."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Creates a PDF document with a certain pagesize: B3, B4

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Creates a PDF document with a certain pagesize
 */
public class SettingPageSizeB3B4B5 {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SettingPageSizeB3B4B5.pdf"));
      document.open();
      document.add(new Paragraph("First Page."));
      document.setPageSize(PageSize.B3);
      document.newPage();
      document.add(new Paragraph("This PageSize is B3."));
      
      document.setPageSize(PageSize.B4);
      document.newPage();
      document.add(new Paragraph("This PageSize is B4."));
      document.setPageSize(PageSize.B5);
      document.newPage();
      document.add(new Paragraph("This PageSize is B5."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Creates a PDF document with a certain pagesize: FLSA, FLSE, HALFLETTER, LEDGER

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Creates a PDF document with a certain pagesize
 */
public class SettingPageSizeFLSAFLSEHALFLETTERLEDGER {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SettingPageSizeFLSAFLSEHALFLETTERLEDGER.pdf"));
      document.open();
      document.add(new Paragraph("First Page."));
      document.setPageSize(PageSize.FLSA);
      document.newPage();
      document.add(new Paragraph("This PageSize is FLSA."));
      
      document.setPageSize(PageSize.FLSE);
      document.newPage();
      document.add(new Paragraph("This PageSize is FLSE."));
      document.setPageSize(PageSize.HALFLETTER);
      document.newPage();
      document.add(new Paragraph("This PageSize is HALFLETTER."));
      
      document.setPageSize(PageSize.LEDGER);
      document.newPage();
      document.add(new Paragraph("This PageSize is LEDGER."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Creates a PDF document with a certain pagesize: LETTER

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Creates a PDF document with a certain pagesize
 */
public class SettingPageSizeLETTER {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SettingPageSizeLETTER.pdf"));
      document.open();
      document.add(new Paragraph("First Page."));
      document.setPageSize(PageSize.LETTER);
      document.newPage();
      document.add(new Paragraph("This PageSize is DIN LETTER."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Custom PDF Document: Page Size

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.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
public class CustomPDFDocumentPageSizePDF {
  public static void main(String[] args) {
    Rectangle pageSize = new Rectangle(216, 720);
    Document document = new Document(pageSize);
    try {
      PdfWriter.getInstance(document, new FileOutputStream("CustomPDFDocumentPageSizePDF.pdf"));
      document.open();
      document.add(new Paragraph("The size of this page is 216x720 points."));
      document.add(new Paragraph("216pt / 72 points per inch = 3 inch"));
      document.add(new Paragraph("720pt / 72 points per inch = 10 inch"));
      document.add(new Paragraph("The size of this page is 3x10 inch."));
      document.add(new Paragraph("3 inch x 2.54 = 7.62 cm"));
      document.add(new Paragraph("10 inch x 2.54 = 25.4 cm"));
      document.add(new Paragraph("The size of this page is 7.62x25.4 cm."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Default Page Size A4

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 DefaultPageSizeA4PDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("DefaultPageSizeA4PDF.pdf"));
      document.open();
      document.add(new Paragraph("The default PageSize is DIN A4."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Setting Page size: A0

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Creates a PDF document with a certain pagesize
 */
public class SettingPageSizeA0 {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SettingPageSizeA0.pdf"));
      document.open();
      document.add(new Paragraph("First Page."));
      document.setPageSize(PageSize.A0);
      document.newPage();
      document.add(new Paragraph("This PageSize is DIN A0."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Setting Page Size: A1

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Creates a PDF document with a certain pagesize
 */
public class SettingPageSizeA1 {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SettingPageSizeA1.pdf"));
      document.open();
      document.add(new Paragraph("First Page."));
      document.setPageSize(PageSize.A1);
      document.newPage();
      document.add(new Paragraph("This PageSize is DIN A1."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Setting Page size: A2

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Creates a PDF document with a certain pagesize
 */
public class SettingPageSizeA2 {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SettingPageSizeA2.pdf"));
      document.open();
      document.setPageSize(PageSize.A2);
      document.add(new Paragraph("This PageSize is DIN A2."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Setting Page Size: a3

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Creates a PDF document with a certain pagesize
 */
public class SettingPageSizeA3 {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SettingPageSizeA3.pdf"));
      document.open();
      document.add(new Paragraph("First Page."));
      document.setPageSize(PageSize.A3);
      document.newPage();
      document.add(new Paragraph("This PageSize is A3."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Setting Page Size: A5

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Creates a PDF document with a certain pagesize
 */
public class SettingPageSizeA5 {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SettingPageSizeA5.pdf"));
      document.open();
      document.add(new Paragraph("First Page."));
      document.setPageSize(PageSize.A5);
      document.newPage();
      document.add(new Paragraph("This PageSize is DIN A5."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Setting Page Size: A6

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Creates a PDF document with a certain pagesize
 */
public class SettingPageSizeA6 {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SettingPageSizeA6.pdf"));
      document.open();
      document.add(new Paragraph("First Page."));
      document.setPageSize(PageSize.A6);
      document.newPage();
      document.add(new Paragraph("This PageSize is DIN A6."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Setting Page Size: A7

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Creates a PDF document with a certain pagesize
 */
public class SettingPageSizeA7 {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SettingPageSizeA7.pdf"));
      document.open();
      document.add(new Paragraph("First Page."));
      document.setPageSize(PageSize.A7);
      document.newPage();
      document.add(new Paragraph("This PageSize is DIN A7."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Setting Page Size: A8

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Creates a PDF document with a certain pagesize
 */
public class SettingPageSizeA8 {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SettingPageSizeA8.pdf"));
      document.open();
      document.add(new Paragraph("First Page."));
      document.setPageSize(PageSize.A8);
      document.newPage();
      document.add(new Paragraph("This PageSize is DIN A8."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Setting Page Size: B0

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Creates a PDF document with a certain pagesize
 */
public class SettingPageSizeB0 {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SettingPageSizeB0.pdf"));
      document.open();
      document.add(new Paragraph("First Page."));
      document.setPageSize(PageSize.B0);
      document.newPage();
      document.add(new Paragraph("This PageSize is DIN B0."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Setting Page Size: B1, B2

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Creates a PDF document with a certain pagesize
 */
public class SettingPageSizeB1B2 {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SettingPageSizeB1B2.pdf"));
      document.open();
      document.add(new Paragraph("First Page."));
      document.setPageSize(PageSize.B1);
      document.newPage();
      document.add(new Paragraph("This PageSize is B1."));
      document.setPageSize(PageSize.B2);
      document.newPage();
      document.add(new Paragraph("This PageSize is B2."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Setting Page Size: LEGAL, NOTE

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Creates a PDF document with a certain pagesize
 */
public class SettingPageSizeLEGALNOTE {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SettingPageSizeLEGALNOTE.pdf"));
      document.open();
      document.add(new Paragraph("First Page."));
      document.setPageSize(PageSize.LEGAL);
      document.newPage();
      document.add(new Paragraph("This PageSize is LEGAL."));
      
      document.setPageSize(PageSize.NOTE);
      document.newPage();
      document.add(new Paragraph("This PageSize is NOTE."));
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Width, Height

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class WidthHeightPDF {
  public static void main(String[] args) {
    Document document = new Document();
    try {
      PdfWriter.getInstance(document, new FileOutputStream("WidthHeightPDF.pdf"));
      document.open();
      BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
      document.add(new Paragraph("width: " + bfComic.getWidthPoint("a", 12)));
      document.add(new Paragraph("ascent: " + bfComic.getAscentPoint("A", 12)));
      document.add(new Paragraph("descent: " + bfComic.getDescentPoint("aaa", 12)));
      document.add(new Paragraph("height: " + (bfComic.getAscentPoint("AAA", 12) - bfComic.getDescentPoint("AAA", 12))));
    } catch (Exception e) {
      System.err.println(e.getMessage());
    }
    document.close();
  }
}