Java Code Examples for com.itextpdf.text.PageSize#A4

The following examples show how to use com.itextpdf.text.PageSize#A4 . 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: PDFExport.java    From MtgDesktopCompanion with GNU General Public License v3.0 8 votes vote down vote up
@Override
public void exportDeck(MagicDeck deck, File f) throws IOException {
	PdfPTable table = new PdfPTable(3);
	table.setHorizontalAlignment(Element.ALIGN_CENTER);

	try {
		document = new Document(PageSize.A4, 5, 5, 10, 5);
		document.addAuthor(getString("AUTHOR"));
		document.addCreationDate();
		document.addCreator(MTGConstants.MTG_APP_NAME);
		document.addTitle(deck.getName());

		PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(f));
		document.open();
		document.add(new Chunk(""));
		for (MagicCard card : deck.getMainAsList()) {
			table.addCell(getCells(card));
			notify(card);
		}
		document.add(table);
		document.close();
		writer.close();
	} catch (Exception e) {
		logger.error("Error in pdf creation " + f, e);
	}
}
 
Example 2
Source File: UseColumnText.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/32162759/columntext-showtextaligned-vs-columntext-setsimplecolumn-top-alignment">
 * ColumnText.ShowTextAligned vs ColumnText.SetSimpleColumn Top Alignment
 * </a>
 * <p>
 * Indeed, the coordinates do not line up. The y coordinate of 
 * {@link ColumnText#showTextAligned(PdfContentByte, int, Phrase, float, float, float)}
 * denotes the baseline while {@link ColumnText#setSimpleColumn(Rectangle)} surrounds
 * the text to come.
 * </p>
 */
@Test
public void testShowTextAlignedVsSimpleColumnTopAlignment() throws DocumentException, IOException
{
    Document document = new Document(PageSize.A4);

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "ColumnTextTopAligned.pdf")));
    document.open();

    Font fontQouteItems = new Font(BaseFont.createFont(), 12);
    PdfContentByte canvas = writer.getDirectContent();

    // Item Number
    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, new Phrase("36222-0", fontQouteItems), 60, 450, 0);

    // Estimated Qty
    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, new Phrase("47", fontQouteItems), 143, 450, 0);

    // Item Description
    ColumnText ct = new ColumnText(canvas); // Uses a simple column box to provide proper text wrapping
    ct.setSimpleColumn(new Rectangle(193, 070, 390, 450));
    ct.setText(new Phrase("In-Situ : Poly Cable - 100'\nPoly vented rugged black gable 100ft\nThis is an additional description. It can wrap an extra line if it needs to so this text is long.", fontQouteItems));
    ct.go();

    document.close();
}
 
Example 3
Source File: PDF2TextExample.java    From tutorials with MIT License 6 votes vote down vote up
private static void generatePDFFromTxt(String filename) throws IOException, DocumentException {
	Document pdfDoc = new Document(PageSize.A4);
	PdfWriter.getInstance(pdfDoc, new FileOutputStream("src/output/txt.pdf"))
			.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
	pdfDoc.open();
	
	Font myfont = new Font();
	myfont.setStyle(Font.NORMAL);
	myfont.setSize(11);
	pdfDoc.add(new Paragraph("\n"));
	
	BufferedReader br = new BufferedReader(new FileReader(filename));
	String strLine;
	while ((strLine = br.readLine()) != null) {
		Paragraph para = new Paragraph(strLine + "\n", myfont);
		para.setAlignment(Element.ALIGN_JUSTIFIED);
		pdfDoc.add(para);
	}
	
	pdfDoc.close();
	br.close();
}
 
Example 4
Source File: JustCopy.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
     * <a href="https://stackoverflow.com/questions/48586750/itext-messes-up-pdf-while-joining-multiple-pdfs">
     * iText messes up PDF while joining multiple PDFs
     * </a>
     * <br/>
     * <a href="https://drive.google.com/open?id=1nH_21_f1bmnxeOn5ul8db3eC2CggZiRN">
     * page_444.pdf
     * </a>
     * <p>
     * I cannot reproduce the issue.
     * </p>
     */
    @Test
    public void testPage444() throws IOException, DocumentException {
        Rectangle pageSize = PageSize.A4;
        File outPut = new File(RESULT_FOLDER, "page_444-copied.pdf");

        final Document document = new Document(pageSize );
        final FileOutputStream fos = FileUtils.openOutputStream(outPut);
        final PdfWriter pdfWriter = new PdfSmartCopy(document, fos);

        pdfWriter.setViewerPreferences(PdfWriter.PageLayoutTwoColumnRight);
        pdfWriter.setFullCompression();
        pdfWriter.setPdfVersion(PdfWriter.VERSION_1_6);
//        pdfWriter.setXmpMetadata(getPdfMetaData());

        document.open();
        document.addAuthor("Author");

        final PdfReader reader = new PdfReader(getClass().getResourceAsStream("page_444.pdf"));
        PdfSmartCopy pdfSmartCopy = (PdfSmartCopy) pdfWriter;
        pdfSmartCopy.addPage(pdfSmartCopy.getImportedPage(reader, 1));
        pdfSmartCopy.freeReader(reader);

        // After all files are merged
        document.close();
    }
 
Example 5
Source File: VeryDenseMerging.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/28991291/how-to-remove-whitespace-on-merge">
 * How To Remove Whitespace on Merge
 * </a>
 * <p>
 * Testing {@link PdfVeryDenseMergeTool} using the OP's files and a gap of 10. This was the
 * OP's gap value of choice resulting in lost lines. Cannot reproduce...
 * </p>
 */
@Test
public void testMergeGrandizerFilesGap10() throws DocumentException, IOException
{
    try (   InputStream docA = getClass().getResourceAsStream("Header.pdf");
            InputStream docB = getClass().getResourceAsStream("Body.pdf");
            InputStream docC = getClass().getResourceAsStream("Footer.pdf");    )
    {
        PdfVeryDenseMergeTool tool = new PdfVeryDenseMergeTool(PageSize.A4, 18, 18, 10);
        PdfReader readerA = new PdfReader(docA);
        PdfReader readerB = new PdfReader(docB);
        PdfReader readerC = new PdfReader(docC);
        try (FileOutputStream fos = new FileOutputStream(new File(RESULT_FOLDER, "GrandizerMerge-veryDense-gap10.pdf")))
        {
            List<PdfReader> inputs = Arrays.asList(readerA, readerB, readerC);
            tool.merge(fos, inputs);
        }
        finally
        {
            readerA.close();
            readerB.close();
            readerC.close();
        }
    }
}
 
Example 6
Source File: VeryDenseMerging.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/28991291/how-to-remove-whitespace-on-merge">
 * How To Remove Whitespace on Merge
 * </a>
 * <p>
 * Testing {@link PdfVeryDenseMergeTool} using the OP's files.
 * </p>
 */
@Test
public void testMergeGrandizerFiles() throws DocumentException, IOException
{
    try (   InputStream docA = getClass().getResourceAsStream("Header.pdf");
            InputStream docB = getClass().getResourceAsStream("Body.pdf");
            InputStream docC = getClass().getResourceAsStream("Footer.pdf");    )
    {
        PdfVeryDenseMergeTool tool = new PdfVeryDenseMergeTool(PageSize.A4, 18, 18, 5);
        PdfReader readerA = new PdfReader(docA);
        PdfReader readerB = new PdfReader(docB);
        PdfReader readerC = new PdfReader(docC);
        try (FileOutputStream fos = new FileOutputStream(new File(RESULT_FOLDER, "GrandizerMerge-veryDense.pdf")))
        {
            List<PdfReader> inputs = Arrays.asList(readerA, readerB, readerC);
            tool.merge(fos, inputs);
        }
        finally
        {
            readerA.close();
            readerB.close();
            readerC.close();
        }
    }
}
 
Example 7
Source File: DenseMerging.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/28991291/how-to-remove-whitespace-on-merge">
 * How To Remove Whitespace on Merge
 * </a>
 * <p>
 * Testing {@link PdfDenseMergeTool} using the OP's files.
 * </p>
 */
@Test
public void testMergeGrandizerFiles() throws DocumentException, IOException
{
    try (   InputStream docA = getClass().getResourceAsStream("Header.pdf");
            InputStream docB = getClass().getResourceAsStream("Body.pdf");
            InputStream docC = getClass().getResourceAsStream("Footer.pdf");    )
    {
        PdfDenseMergeTool tool = new PdfDenseMergeTool(PageSize.A4, 18, 18, 5);
        PdfReader readerA = new PdfReader(docA);
        PdfReader readerB = new PdfReader(docB);
        PdfReader readerC = new PdfReader(docC);
        try (FileOutputStream fos = new FileOutputStream(new File(RESULT_FOLDER, "GrandizerMerge.pdf")))
        {
            List<PdfReader> inputs = Arrays.asList(readerA, readerB, readerC);
            tool.merge(fos, inputs);
        }
        finally
        {
            readerA.close();
            readerB.close();
            readerC.close();
        }
    }

}
 
Example 8
Source File: EnlargePagePart.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/35374110/how-do-i-use-itext-to-have-a-landscaped-pdf-on-half-of-a-a4-back-to-portrait-and">
 * How do i use iText to have a landscaped PDF on half of a A4 back to portrait and full size on A4
 * </a>
 * <p>
 * This sample shows how to rotate and enlarge the upper half of an A4 page to fit into a new A4 page.
 * </p>
 */
@Test
public void testRotateAndZoomUpperHalfPage() throws IOException, DocumentException
{
    try (   InputStream resource = getClass().getResourceAsStream("/mkl/testarea/itext5/extract/test.pdf");
            OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "test-upperHalf.pdf"))   )
    {
        PdfReader reader = new PdfReader(resource);
        Document document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.getInstance(document, result);
        document.open();

        double sqrt2 = Math.sqrt(2);
        Rectangle pageSize = reader.getPageSize(1);
        PdfImportedPage importedPage = writer.getImportedPage(reader, 1);
        writer.getDirectContent().addTemplate(importedPage, 0, sqrt2, -sqrt2, 0, pageSize.getTop() * sqrt2, -pageSize.getLeft() * sqrt2);
        
        document.close();
    }
}
 
Example 9
Source File: DenseMerging.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/27988503/how-can-i-combine-multiple-pdf-files-excluding-page-breaks-using-itextsharp">
 * How can I combine multiple PDF files excluding page breaks using iTextSharp?
 * </a>
 * <p>
 * Testing {@link PdfDenseMergeTool}.
 * </p>
 */
@Test
public void testMergeOnlyTextLong() throws DocumentException, IOException
{
    byte[] docA = createSimpleTextPdf("First document, paragraph %s.", 20);
    Files.write(new File(RESULT_FOLDER, "textOnlyLongA.pdf").toPath(), docA);
    byte[] docB = createSimpleTextPdf("Second document, paragraph %s.", 20);
    Files.write(new File(RESULT_FOLDER, "textOnlyLongB.pdf").toPath(), docB);
    byte[] docC = createSimpleTextPdf("Third document, paragraph %s, a bit longer lines.", 20);
    Files.write(new File(RESULT_FOLDER, "textOnlyLongC.pdf").toPath(), docC);
    byte[] docD = createSimpleTextPdf("Fourth document, paragraph %s, let us make this a much longer paragraph spanning more than one line.", 20);
    Files.write(new File(RESULT_FOLDER, "textOnlyLongD.pdf").toPath(), docD);

    PdfDenseMergeTool tool = new PdfDenseMergeTool(PageSize.A4, 18, 18, 5);
    PdfReader readerA = new PdfReader(docA);
    PdfReader readerB = new PdfReader(docB);
    PdfReader readerC = new PdfReader(docC);
    PdfReader readerD = new PdfReader(docD);
    try (FileOutputStream fos = new FileOutputStream(new File(RESULT_FOLDER, "longTextOnlyMerge.pdf")))
    {
        List<PdfReader> inputs = Arrays.asList(readerA, readerB, readerC, readerD);
        tool.merge(fos, inputs);
    }
    finally
    {
        readerA.close();
        readerB.close();
        readerC.close();
        readerD.close();
    }
}
 
Example 10
Source File: DenseMerging.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/27988503/how-can-i-combine-multiple-pdf-files-excluding-page-breaks-using-itextsharp">
 * How can I combine multiple PDF files excluding page breaks using iTextSharp?
 * </a>
 * <p>
 * Testing {@link PdfDenseMergeTool}.
 * </p>
 */
@Test
public void testMergeOnlyText() throws DocumentException, IOException
{
    byte[] docA = createSimpleTextPdf("First document, paragraph %s.", 3);
    Files.write(new File(RESULT_FOLDER, "textOnlyA.pdf").toPath(), docA);
    byte[] docB = createSimpleTextPdf("Second document, paragraph %s.", 3);
    Files.write(new File(RESULT_FOLDER, "textOnlyB.pdf").toPath(), docB);
    byte[] docC = createSimpleTextPdf("Third document, paragraph %s, a bit longer lines.", 3);
    Files.write(new File(RESULT_FOLDER, "textOnlyC.pdf").toPath(), docC);
    byte[] docD = createSimpleTextPdf("Fourth document, paragraph %s, let us make this a much longer paragraph spanning more than one line.", 3);
    Files.write(new File(RESULT_FOLDER, "textOnlyD.pdf").toPath(), docD);

    PdfDenseMergeTool tool = new PdfDenseMergeTool(PageSize.A4, 18, 18, 5);
    PdfReader readerA = new PdfReader(docA);
    PdfReader readerB = new PdfReader(docB);
    PdfReader readerC = new PdfReader(docC);
    PdfReader readerD = new PdfReader(docD);
    try (FileOutputStream fos = new FileOutputStream(new File(RESULT_FOLDER, "textOnlyMerge.pdf")))
    {
        List<PdfReader> inputs = Arrays.asList(readerA, readerB, readerC, readerD);
        tool.merge(fos, inputs);
    }
    finally
    {
        readerA.close();
        readerB.close();
        readerC.close();
        readerD.close();
    }
}
 
Example 11
Source File: VeryDenseMerging.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/28991291/how-to-remove-whitespace-on-merge">
 * How To Remove Whitespace on Merge
 * </a>
 * <p>
 * Testing {@link PdfVeryDenseMergeTool}.
 * </p>
 */
@Test
public void testMergeOnlyText() throws DocumentException, IOException
{
    byte[] docA = createSimpleTextPdf("First document, paragraph %s.", 3);
    Files.write(new File(RESULT_FOLDER, "textOnlyA.pdf").toPath(), docA);
    byte[] docB = createSimpleTextPdf("Second document, paragraph %s.", 3);
    Files.write(new File(RESULT_FOLDER, "textOnlyB.pdf").toPath(), docB);
    byte[] docC = createSimpleTextPdf("Third document, paragraph %s, a bit longer lines.", 3);
    Files.write(new File(RESULT_FOLDER, "textOnlyC.pdf").toPath(), docC);
    byte[] docD = createSimpleTextPdf("Fourth document, paragraph %s, let us make this a much longer paragraph spanning more than one line.", 3);
    Files.write(new File(RESULT_FOLDER, "textOnlyD.pdf").toPath(), docD);

    PdfVeryDenseMergeTool tool = new PdfVeryDenseMergeTool(PageSize.A4, 18, 18, 5);
    PdfReader readerA = new PdfReader(docA);
    PdfReader readerB = new PdfReader(docB);
    PdfReader readerC = new PdfReader(docC);
    PdfReader readerD = new PdfReader(docD);
    try (FileOutputStream fos = new FileOutputStream(new File(RESULT_FOLDER, "textOnlyMerge-veryDense.pdf")))
    {
        List<PdfReader> inputs = Arrays.asList(readerA, readerB, readerC, readerD);
        tool.merge(fos, inputs);
    }
    finally
    {
        readerA.close();
        readerB.close();
        readerC.close();
        readerD.close();
    }
}
 
Example 12
Source File: VeryDenseMerging.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/28991291/how-to-remove-whitespace-on-merge">
 * How To Remove Whitespace on Merge
 * </a>
 * <p>
 * Testing {@link PdfVeryDenseMergeTool}.
 * </p>
 */
@Test
public void testMergeOnlyTextLong() throws DocumentException, IOException
{
    byte[] docA = createSimpleTextPdf("First document, paragraph %s.", 20);
    Files.write(new File(RESULT_FOLDER, "textOnlyLongA.pdf").toPath(), docA);
    byte[] docB = createSimpleTextPdf("Second document, paragraph %s.", 20);
    Files.write(new File(RESULT_FOLDER, "textOnlyLongB.pdf").toPath(), docB);
    byte[] docC = createSimpleTextPdf("Third document, paragraph %s, a bit longer lines.", 20);
    Files.write(new File(RESULT_FOLDER, "textOnlyLongC.pdf").toPath(), docC);
    byte[] docD = createSimpleTextPdf("Fourth document, paragraph %s, let us make this a much longer paragraph spanning more than one line.", 20);
    Files.write(new File(RESULT_FOLDER, "textOnlyLongD.pdf").toPath(), docD);

    PdfVeryDenseMergeTool tool = new PdfVeryDenseMergeTool(PageSize.A4, 18, 18, 5);
    PdfReader readerA = new PdfReader(docA);
    PdfReader readerB = new PdfReader(docB);
    PdfReader readerC = new PdfReader(docC);
    PdfReader readerD = new PdfReader(docD);
    try (FileOutputStream fos = new FileOutputStream(new File(RESULT_FOLDER, "longTextOnlyMerge-veryDense.pdf")))
    {
        List<PdfReader> inputs = Arrays.asList(readerA, readerB, readerC, readerD);
        tool.merge(fos, inputs);
    }
    finally
    {
        readerA.close();
        readerB.close();
        readerC.close();
        readerD.close();
    }
}
 
Example 13
Source File: VeryDenseMerging.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/28991291/how-to-remove-whitespace-on-merge">
 * How To Remove Whitespace on Merge
 * </a>
 * <p>
 * Testing {@link PdfVeryDenseMergeTool}.
 * </p>
 */
@Test
public void testMergeOnlyGraphics() throws DocumentException, IOException
{
    byte[] docA = createSimpleCircleGraphicsPdf(20, 20, 20);
    Files.write(new File(RESULT_FOLDER, "circlesOnlyA.pdf").toPath(), docA);
    byte[] docB = createSimpleCircleGraphicsPdf(50, 10, 2);
    Files.write(new File(RESULT_FOLDER, "circlesOnlyB.pdf").toPath(), docB);
    byte[] docC = createSimpleCircleGraphicsPdf(100, -20, 3);
    Files.write(new File(RESULT_FOLDER, "circlesOnlyC.pdf").toPath(), docC);
    byte[] docD = createSimpleCircleGraphicsPdf(20, 20, 20);
    Files.write(new File(RESULT_FOLDER, "circlesOnlyD.pdf").toPath(), docD);

    PdfVeryDenseMergeTool tool = new PdfVeryDenseMergeTool(PageSize.A4, 18, 18, 5);
    PdfReader readerA = new PdfReader(docA);
    PdfReader readerB = new PdfReader(docB);
    PdfReader readerC = new PdfReader(docC);
    PdfReader readerD = new PdfReader(docD);
    try (FileOutputStream fos = new FileOutputStream(new File(RESULT_FOLDER, "circlesOnlyMerge-veryDense.pdf")))
    {
        List<PdfReader> inputs = Arrays.asList(readerA, readerB, readerC, readerD);
        tool.merge(fos, inputs);
    }
    finally
    {
        readerA.close();
        readerB.close();
        readerC.close();
        readerD.close();
    }
}
 
Example 14
Source File: CreateTableDirectContent.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/43807931/creating-table-in-pdf-on-last-page-bottom-wrong-official-solution">
 * Creating table in pdf on last page bottom (wrong official solution)
 * </a>
 * <p>
 * Indeed, there is an error in the official sample which effectively
 * applies the margins twice.
 * </p>
 */
@Test
public void testCreateTableLikeUser7968180() throws FileNotFoundException, DocumentException
{
    Document document = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(document,
            new FileOutputStream(new File(RESULT_FOLDER, "calendarUser7968180.pdf")));
    document.open();

    PdfPTable datatable = null;//createHeaderTable();
    //document.add(datatable);
    datatable = createFooterTable();

    drawTableAtTheEndOfPage(document, writer, datatable);

    // Marking the border
    PdfContentByte canvas = writer.getDirectContentUnder();
    canvas.setColorStroke(BaseColor.RED);
    canvas.setColorFill(BaseColor.PINK);
    canvas.rectangle(document.left(), document.bottom(), document.right() - document.left(), document.top() - document.bottom());
    Rectangle pageSize = document.getPageSize(); 
    canvas.rectangle(pageSize.getLeft(), pageSize.getBottom(), pageSize.getWidth(), pageSize.getHeight());
    canvas.eoFillStroke();

    document.close();
    System.out.println("done");
}
 
Example 15
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();
}
 
Example 16
Source File: HRPDFBuilder.java    From Spring-MVC-Blueprints with MIT License 4 votes vote down vote up
protected Document newDocument() {
    return new Document(PageSize.A4);
}
 
Example 17
Source File: KpiReportPdfCommand.java    From bamboobsc with Apache License 2.0 4 votes vote down vote up
private String createPdf(Context context) throws Exception {
	BscReportPropertyUtils.loadData();
	BscReportSupportUtils.loadExpression(); // 2015-04-18 add
	String visionOid = (String)context.get("visionOid");
	VisionVO vision = null;
	BscStructTreeObj treeObj = (BscStructTreeObj)this.getResult(context);
	for (VisionVO visionObj : treeObj.getVisions()) {
		if (visionObj.getOid().equals(visionOid)) {
			vision = visionObj;
		}
	}		
	FontFactory.register(BscConstants.PDF_ITEXT_FONT);
	String fileName = SimpleUtils.getUUIDStr() + ".pdf";
	String fileFullPath = Constants.getWorkTmpDir() + "/" + fileName;
	OutputStream os = new FileOutputStream(fileFullPath);
	//Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
	Document document = new Document(PageSize.A4, 10, 10, 10, 10);		
	document.left(100f);
	document.top(150f);
	PdfWriter writer = PdfWriter.getInstance(document, os);
	document.open();  
	
	int dateRangeRows = 4 + vision.getPerspectives().get(0).getObjectives().get(0).getKpis().get(0).getDateRangeScores().size();
	PdfPTable table = new PdfPTable(MAX_COLSPAN);
	PdfPTable dateRangeTable = new PdfPTable( dateRangeRows );
	PdfPTable chartsTable = new PdfPTable( 2 );
	PdfPTable signTable = new PdfPTable( 1 );
	table.setWidthPercentage(100f);	
	dateRangeTable.setWidthPercentage(100f);
	chartsTable.setWidthPercentage(100f);
	signTable.setWidthPercentage(100f);
	
	this.createHead(table, vision);
	this.createBody(table, vision);
	this.createDateRange(dateRangeTable, vision, context, dateRangeRows);		
	this.putCharts(chartsTable, context);
	this.putSignature(signTable, context);
	
	document.add(chartsTable);
	document.add(table);  
	document.add(dateRangeTable);  
	document.add(signTable);
	document.close();
	writer.close();			
	
	os.flush();
	os.close();
	os = null;
	
	File file = new File(fileFullPath);
	String oid = UploadSupportUtils.create(
			Constants.getSystem(), UploadTypes.IS_TEMP, false, file, "kpi-report.pdf");
	file = null;
	return oid;
}
 
Example 18
Source File: Abstract2DPdfPageSplittingTool.java    From testarea-itext5 with GNU Affero General Public License v3.0 4 votes vote down vote up
void initDocument(OutputStream outputStream) throws DocumentException {
    final Document document = new Document(PageSize.A4);
    final PdfWriter writer = PdfWriter.getInstance(document, outputStream);
    this.document = document;
    this.writer = writer;
}
 
Example 19
Source File: UseMillimeters.java    From testarea-itext5 with GNU Affero General Public License v3.0 3 votes vote down vote up
public static boolean createPDF(String pathPDF) throws FileNotFoundException, DocumentException {

        Document document = new Document(PageSize.A4);

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pathPDF));
        writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
        document.open();

        createRectangle(writer, 30.75f, 11, 148.5f, 210, BaseColor.RED);
        document.close();

        return true;
    }
 
Example 20
Source File: DrawGradient.java    From testarea-itext5 with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/39072316/itext-gradient-issue-in-landscape">
 * iText gradient issue in landscape
 * </a>
 * <p>
 * The problem is that while itext content adding functionalities <b>do</b> take the page
 * rotation into account (they translate the given coordinates so that in the rotated page
 * <em>x</em> goes right and <em>y</em> goes up and the origin is in the lower left), the
 * shading pattern definitions (which are <em>not</em> part of the page content but
 * externally defined) <b>don't</b>.
 * </p>
 * <p>
 * Thus, you have to make the shading definition rotation aware, e.g. like this.
 * </p>
 */
@Test
public void testGradientOnRotatedPage() throws FileNotFoundException, DocumentException
{
    Document doc = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(new File(RESULT_FOLDER, "gradientProblem.pdf")));
    doc.open();
    drawSexyTriangle(writer, false);
    doc.setPageSize(PageSize.A4.rotate());
    doc.newPage();
    drawSexyTriangle(writer, true);
    doc.close();
}