Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I make a page break using itext

Tags:

java

itext

I want to generate a pdf using itext. I would at some point while the content was added to make a page break. I need to insert several separate conenidos dependence origin so I ask the user to do so on separate pages. Any ideas???

like image 992
meyquel Avatar asked Jun 19 '13 18:06

meyquel


People also ask

Is iText free for commercial use?

This license is a commercial license. You have to pay for it. To answer your question: iText can be used for free in situations where you also distribute your software for free. As soon as you want to use iText in a closed source, proprietary environment, you have to pay for your use of iText.

What is chunk in iText?

itextpdf. text. Chunk is the smallest significant part that can be added to a document. A chunk is a string with Font information. A chunk doesn't add line breaks, a paragraph spacing or any other spacing.

Is iText same as iTextSharp?

PLEASE NOTE: iTextSharp is EOL, and has been replaced by iText 7. Only security fixes will be added. We HIGHLY recommend customers use iText 7 for new projects, and to consider moving existing projects from iTextSharp to iText 7 to benefit from the many improvements such as: HTML to PDF (PDF/UA) conversion.

What is iText jar used for?

iText is a library for creating and manipulating PDF files in Java and . NET. iText was written by Bruno Lowagie. The source code was initially distributed as open source under the Mozilla Public License or the GNU Library General Public License open source licenses.


2 Answers

Anyone looking for a solution in iText7, please use the solution from @BadLeo, which is to use document.add(new AreaBreak());

Below answer is applicable for versions prior to 7.

Calling document.newPage() tells iText to place subsequent objects on a new page. The new page will only actually get created when you place the next object. Also, newPage() only creates a new page if the current page is not blank; otherwise, it's ignored; you can use setPageBlank(false) to overcome that.

Refer below link for an example: http://itextpdf.com/examples/iia.php?id=99 (Edit: dead 404)

Since the above link is dead, adding example code for anyone who is till using version prior to iText7.

/**
 * Creates from the given Collection of images an pdf file.
 *
 * @param result
 *            the output stream from the pdf file where the images shell be written.
 * @param images
 *            the BufferedImage collection to be written in the pdf file.
 * @throws DocumentException
 *             is thrown if an error occurs when trying to get an instance of {@link PdfWriter}.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static void createPdf(final OutputStream result, final List<BufferedImage> images)
  throws DocumentException, IOException
{
  final Document document = new Document();
  PdfWriter.getInstance(document, result);
  for (final BufferedImage image : images)
  {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "png", baos);
    final Image img = Image.getInstance(baos.toByteArray());
    document.setPageSize(img);
    document.setPageBlank(false);
    document.newPage();
    img.setAbsolutePosition(0, 0);
    document.add(img);
  }
  document.close();
}
like image 128
Mubin Avatar answered Oct 19 '22 04:10

Mubin


For iText7 try:

document.add(new AreaBreak());

Source: http://itextsupport.com/apidocs/itext7/7.0.0/com/itextpdf/layout/element/AreaBreak.html

like image 39
BadLeo Avatar answered Oct 19 '22 05:10

BadLeo