Java Code Examples for com.itextpdf.text.Document#setMargins()

The following examples show how to use com.itextpdf.text.Document#setMargins() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChangeMargins.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/38057241/itextpdf-different-margin-on-specific-page">
 * itextpdf different margin on specific page
 * </a>
 * <p>
 * This test shows how to set different margins to separate pages.
 * </p> 
 */
@Test
public void testChangingMargins() throws IOException, DocumentException
{
    StringBuilder builder = new StringBuilder("test");
    for (int i = 0; i < 100; i++)
        builder.append(" test");
    String test = builder.toString();
    
    try (   OutputStream pdfStream = new FileOutputStream(new File(RESULT_FOLDER, "ChangingMargins.pdf")))
    {
        Document pdfDocument = new Document(PageSize.A4.rotate(), 0, 0, 0, 0);
        PdfWriter.getInstance(pdfDocument, pdfStream);
        pdfDocument.open();

        for (int m = 0; m < pdfDocument.getPageSize().getWidth() / 2 && m < pdfDocument.getPageSize().getHeight() / 2; m += 100)
        {
            // pdfDocument.setMargins(m, m, 100, 100);
            pdfDocument.setMargins(m, m, m, m);
            pdfDocument.newPage();
            pdfDocument.add(new Paragraph(test));
        }

        pdfDocument.close();
    }
}
 
Example 2
Source File: LandscapePdf.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/47209312/i-have-a-trouble-with-lowagie-pdf-and-report-making-i-cant-include-headerfooter">
 * I have a trouble with lowagie pdf and Report Making. I cant include headerfooter on the first page
 * </a>
 * <p>
 * This example shows how to generate a PDF with page level
 * settings (page size, page margins) customized already on
 * the first page.
 * </p>
 */
@Test
public void testCreateLandscapeDocument() throws FileNotFoundException, DocumentException {
    Document document = new Document();

    PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "landscape.pdf")));

    document.setPageSize(PageSize.A4.rotate());
    document.setMargins(60, 30, 30, 30);
    document.open();
    document.add(new Paragraph("Test string for a landscape PDF."));
    document.close();
}
 
Example 3
Source File: DynamicFooter.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/43610868/how-to-add-dynamic-variable-to-footer-without-calling-document-newpage-in-itex">
 * How to add dynamic variable to footer without calling document.newPage() in iText 5
 * </a>
 * <p>
 * generator method of the OP
 * </p>
 * @see #testDynamicFooterLikeAyZagen()
 */
public static void createPdf(ArrayList<String> htmlStrings, FooterTable footerEvt, String destinationPath)
        throws IOException, DocumentException {
    Document document = new Document(PageSize.A4);
    document.setMargins(68, 85, 75, 85);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(destinationPath));
    if (footerEvt != null)
        writer.setPageEvent(footerEvt);
    document.open();

    CSSResolver cssResolver = new StyleAttrCSSResolver();
    CssFile cssFile = XMLWorkerHelper
            .getCSS(new ByteArrayInputStream(/*readCSS("resources/content.min.css").getBytes()*/ "".getBytes()));
    cssResolver.addCss(cssFile);

    XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
    fontProvider.register(/*"resources/ARIAL.TTF"*/ "c:/Windows/Fonts/arial.ttf");

    CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

    PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

    XMLWorker worker = new XMLWorker(css, true);
    XMLParser p = new XMLParser(worker);
    int i = 0;
    for (String htmlfile : htmlStrings) {
        i++;
        footerEvt.setTitleIndex("" + i);//or FooterTable.setTitleIndex("" + i);
        ByteArrayInputStream stream = new ByteArrayInputStream(htmlfile.getBytes("UTF-8"));
        p.parse(stream, Charset.forName("UTF-8"));
    }
    document.close();
}