Java Tutorial/PDF/Margin
Версия от 17:44, 31 мая 2010; (обсуждение)
Custom margins
The left margin of this document is 36pt (0.5 inch);
the right margin 72pt (1 inch);
the top margin 108pt (1.5 inch);
the bottom margin 180pt (2.5 inch).
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
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.A5, 36, 72, 108, 180);
PdfWriter.getInstance(document, new FileOutputStream("1.pdf"));
document.open();
document.add(new Paragraph("this is a test"));
Paragraph paragraph = new Paragraph();
paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
for (int i = 0; i < 20; i++) {
paragraph.add("Hello People. ");
}
document.add(paragraph);
document.close();
}
}
Mirrored margins
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
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.A5, 36, 72, 108, 180);
PdfWriter.getInstance(document, new FileOutputStream(
"MirroredMargins.pdf"));
document.setMarginMirroring(true);
document.open();
document.add(new Paragraph("this is a test"));
Paragraph paragraph = new Paragraph();
paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
for (int i = 0; i < 20; i++) {
paragraph.add("Hello World");
}
document.add(paragraph);
document.add(new Paragraph("this is a test"));
document.close();
}
}
Set page margin
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.rupress = false;
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("HelloWorldOpen.pdf"));
document.setPageSize(PageSize.LETTER.rotate());
document.setMargins(36, 72, 108, 144);
document.open();
document.add(new Paragraph("Hello World"));
document.close();
}
}