Java Tutorial/PDF/Page Size
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
A custom pagesize
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Rectangle pageSize = new Rectangle(216f, 720f);
Document document = new Document(pageSize);
PdfWriter.getInstance(document,
new FileOutputStream("HelloWorldNarrow.pdf"));
document.open();
document.add(new Paragraph("Hello World"));
document.close();
}
}
PageSize.LETTER
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document(PageSize.LETTER);
PdfWriter.getInstance(document,
new FileOutputStream("HelloWorldLetter.pdf"));
document.open();
document.add(new Paragraph("Hello World"));
document.close();
}
}
PageSize.LETTER.rotate()
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document(PageSize.LETTER.rotate());
PdfWriter.getInstance(document, new FileOutputStream("1.pdf"));
document.open();
document.add(new Paragraph("Hello World"));
document.close();
}
}
Set page size to LETTER, the orientation to Landscape
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
public static void main(String[] args) throws Exception {
Document document = new Document(new Rectangle(792, 612));
PdfWriter.getInstance(document, new FileOutputStream("1.pdf"));
document.open();
document.add(new Paragraph("Hello World"));
document.close();
}
}
Some information on the page size
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("Page size p1: " + reader.getPageSize(1));
System.out.println("Rotation p1: " + reader.getPageRotation(1));
System.out.println("Page size p3: " + reader.getPageSize(3));
System.out.println("Rotation p3: " + reader.getPageRotation(3));
System.out.println("Size with rotation p3: " + reader.getPageSizeWithRotation(3));
}
}