Java/PDF RTF/Margin

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

Changing Document Margin

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 ChangingDocumentMarginPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A5, 36, 72, 108, 180);
    try {
      PdfWriter.getInstance(document, new FileOutputStream("ChangingDocumentMarginPDF.pdf"));
      document.open();
       
      Paragraph paragraph = new Paragraph();
      paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
      for (int i = 0; i < 60; i++) {
        paragraph.add("www.jexp.ru has demo for Java, JavaScript, PDF.");
      }
      document.add(paragraph);
      document.setMargins(180, 108, 72, 36);
      document.add(new Paragraph("Now we change the margins. You will see the effect on the next page."));
      
    } catch (Exception e) {
      System.err.println(e.getMessage());
    } 
    document.close();
  }
}





Demonstrates the use of margins

import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
/**
 * Demonstrates the use of margins.
 */
public class SettingPageSizeAndMargins {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A5, 36, 72, 108, 180);
    try {
      PdfWriter.getInstance(document, new FileOutputStream("SettingPageSizeAndMargins.pdf"));
      document.open();
       
      document.add(new Paragraph("The left margin of this document is 36pt (0.5 inch);"));
      document.add(new Paragraph("the right margin 72pt (1 inch);"));
      document.add(new Paragraph("the top margin 108pt (1.5 inch);"));
      document.add(new Paragraph("the bottom margin 180pt (2.5 inch)."));
      
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}





Mirroring Margins in Pdf document

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;
public class MirroringMarginsPDF {
  public static void main(String[] args) {
    Document document = new Document(PageSize.A5, 36, 72, 108, 180);
    try {
      PdfWriter.getInstance(document, new FileOutputStream("MirroringMarginsPDF.pdf"));
      document.open();
      document.add(new Paragraph("paragraph"));
      document.setMarginMirroring(true);
      document.add(new Paragraph("Starting on the next page, the margins will be mirrored."));
      for (int i = 0; i < 60; i++) {
        document.add(new Paragraph("paragraph"));
      }
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }
    document.close();
  }
}