Itext in Action notes -- Hello World Express

Source: Internet
Author: User

Set the page size and margins (the parameters are float ):

Rectangle pagesize = new rectangle (200f, 800f); // set page 200*800 units to User display units. The default value is 1/72 inch.
Document document = new document (pagesize, 20f, 20f, 20f, 20f); // The page margin. The order is left and right.

Custom User display unit size:

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf")); 
Writer. setuserunit (72f); // This works with the above to change the actual page size and margin.

Use the standard page format:

Document document = new document (pagesize. letter); // In the pagesize class, includes A0-A10, B0-B10, leter, legal, ledger, tabloid.

Another method for setting attributes such as size and margin:

Document. setmargins (10f, 20f, 10f, 20f); // the upper and lower order is the same as above.
document.setPageSize(PageSize.A5);

The horizontal margin of the entity book is symmetric (that is, the odd margin is the same as the setting in setmargins, the left and right margins of even pages are reversed, and the top and bottom margins are unchanged), or the vertical margin is symmetric:

document.setMarginMirroring(true);
document.setMarginMirroringTopBottom(true);

Write it into the memory before outputting:

     public static void main(String args[]) throws DocumentException, IOException{
        Document document = new Document();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);
        document.open();
        document.add(new Paragraph("Hello World!"));
        document.close();
        FileOutputStream fos = new FileOutputStream("Hello.pdf");
        fos.write(baos.toByteArray());
        fos.close();
    }

Another method:

    public static void main(String args[]) throws DocumentException,
            IOException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream("HelloWorld1.pdf"));
        document.open();
        PdfContentByte canvas = writer.getDirectContentUnder();
Writer. setcompressionlevel (0); // sets the compression level. If 0 is not compressed, you can directly view the content in a text editor.
         canvas.saveState();
        canvas.beginText();
Canvas. movetext (30,180); // left and top offset of the initial text position
         canvas.setFontAndSize(BaseFont.createFont(), 12);
        canvas.showText("Hello world!");
        canvas.endText();
        canvas.restoreState();
        document.close();
    }

Set "Hello world !" Insert as a phrase (same effect as above ):

public static void main(String args[]) throws DocumentException,
        IOException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document,
            new FileOutputStream("HelloWorld1.pdf"));
    document.open();
    PdfContentByte canvas = writer.getDirectContentUnder();
Writer. setcompressionlevel (0); // set
     Phrase hello = new Phrase("Hello world!");
// The following sentence indicates adding the text in the short sentence Hello variable to the canvas variable in the left alignment mode,
// The coordinates are (30,180) and the rotation angle is 0.
    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, hello, 30, 180, 0);
    document.close();
}

Create n documents and save them to the package:

public static void main(String args[]) throws DocumentException,
        IOException {
    ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(
            "test.zip"));
For (INT I = 0; I
        ZipEntry entry = new ZipEntry("hello" + i + ".pdf");
        zip.putNextEntry(entry);
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, zip);
Writer. setclosestream (false); // if this is not done, the output stream of zip is automatically closed in the first loop.
        document.open();
        document.add(new Paragraph("Hello world " + i));
        document.close();
        zip.closeEntry();
    }
    zip.close();
 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.