Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9319379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:22:16+00:00 2026-06-19T03:22:16+00:00

I am generating a document, like in the following code, except ofcourse the contents

  • 0

I am generating a document, like in the following code, except ofcourse the contents of the table, which are varying. What I need to do is make sure that this table never exceeds one page in size, regardless of the amount of content in the cells. Is there a way to do it ?

import com.itextpdf.text.Phrase;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import java.awt.Desktop;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public void createTemplate() throws DocumentException, FileNotFoundException, IOException{ String TARGET = System.getProperty("user.home")+"\temp.pdf";
Document document = new Document(PageSize.A4); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(TARGET)); document.open();

PdfPTable table = new PdfPTable(7); for (int i = 0; i < 105; i++) { Phrase p = new Phrase("some text"); PdfPCell cell = new PdfPCell(); cell.addElement(p); table.addCell(cell); } table.setTotalWidth(PageSize.A4.getWidth()-10); table.setLockedWidth(true); PdfContentByte canvas = writer.getDirectContent(); PdfTemplate template = canvas.createTemplate(table.getTotalWidth(),table.getTotalHeight()); table.writeSelectedRows(0, -1, 0, PageSize.A4.getHeight(), template); Image img = Image.getInstance(template); img.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight()); img.setAbsolutePosition(0, PageSize.A4.getHeight()); document.add(img); document.close(); Desktop desktop = Desktop.getDesktop(); File file = new File(TARGET); desktop.open(file); }

Edit: @Bruno Lowagie. The hint with the template wrapped in an image sounds just right to me, and I changed the code according, but all I get now is an empty PDF. Am I doing something wrong, or is this the wrong approach alltogether?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T03:22:17+00:00Added an answer on June 19, 2026 at 3:22 am

    If you want a table to fit a page, you should create the table before even thinking about page size and ask the table for its height as is done in the TableHeight example. Note that the getTotalHeight() method returns 0 unless you define the width of the table. This can be done like this:

        table.setTotalWidth(width);
        table.setLockedWidth(true);
    

    Now you can create a Document with size Rectangle(0, 0, width + margin * 2, getTotalHeight() + margin * 2) and the table should fit the document exactly when you add it with the writeSelectedRows() method.

    If you don’t want a custom page size, then you need to create a PdfTemplate with the size of the table and add the table to this template object. Then wrap the template object in an Image and use scaleToFit() to size the table down.

    public static void main(String[] args) throws DocumentException, FileNotFoundException, IOException {
        String TARGET = "temp.pdf";
        Document document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(TARGET));
        document.open();
    
    
        PdfPTable table = new PdfPTable(7);         
    
        for (int i = 0; i < 700; i++) {
            Phrase p = new Phrase("some text");
            PdfPCell cell = new PdfPCell();
            cell.addElement(p);
            table.addCell(cell);            
        }
    
        table.setTotalWidth(PageSize.A4.getWidth());
        table.setLockedWidth(true);
        PdfContentByte canvas = writer.getDirectContent();
        PdfTemplate template = canvas.createTemplate(table.getTotalWidth(), table.getTotalHeight());
        table.writeSelectedRows(0, -1, 0, table.getTotalHeight(), template);
        Image img = Image.getInstance(template);
        img.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight());
        img.setAbsolutePosition(0, (PageSize.A4.getHeight() - table.getTotalHeight()) / 2);
        document.add(img);
        document.close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have something that looks like the following document structure: public class Document {
I have a document that I'm generating with headers and would like to generate
I am generating an XML document from a StringBuilder, basically something like: string.Format(<text><row>{0}</row><col>{1}</col><textHeight>{2}</textHeight><textWidth>{3}</textWidth><data>{4}</data><rotation>{5}</rotation></text> Later,
I am generating JavaScript pages through Java code like: FileOutputStream fs=new FileOutputStream(C:\\Documents and Settings\\prajakta\\Desktop\\searcheng.html);
I am generating PDF document using iTextSharp (winforms), now i need to print this
I'm generating a table in a pdf document using xml-fo. The number of columns
I am dynamically generating Like buttons for an app. I use FB.XFBML.parse(document.getElementById(fbook)) and can
I am generating a JSON document from an NSMutableDictionary that is composed of keys
I am generating a Word document with aspose and I would like to show
I am creating a pdf document using C# code in my process. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.