I found in most cases I was adding content to the document as two
tables:  a summary of a product and the transactions on that product.
That said, I extended Document like so:

 

public class MyDocument extends Document {

 

      private Logger log = Logger.getLogger(this.getClass());

      private PdfContentByte canvas;

 

      public MyDocument(Rectangle pageSize, int left, int right, int
top, int bottom) {

            super(pageSize, left, right, top, bottom);

      }

 

      public void setDirectContent(PdfContentByte canvas) {

            this.canvas = canvas;

      }

 

      /**

       * creates a bookmark

       * 

       * @param bookmarkText

       */

      protected void createBookmark(String bookmarkText) {

            PdfDestination d = new PdfDestination(PdfDestination.XYZ, 0,
canvas.getPdfWriter().getVerticalPosition(true), 0);

            new PdfOutline(canvas.getRootOutline(), d, bookmarkText,
false);

      }

 

      protected float getPageWidth() {

            return right() - left();

      }

 

      /**

       * used when adding a section to the document where a summary and
some

       * number of transaction must always appear together

       * 

       * @param summary

       *            - a summary table which will not span multiple pages

       * @param transactions

       *            - a transaction table which _may_ span multiple
pages

       * @param numStickyTransactions

       *            - the number of transactions that must appear
together. If

       *            there isn't room for these transactions to appear
together, a

       *            pagebreak is inserted before the summary is added

       * @param bookmarkText

       *            - the text used as a bookmark to this section. If
null, no

       *            bookmark is created

       */

      public void addSection(PdfPTable summary, PdfPTable transactions,
int numStickyTransactions, String bookmarkText)

                  throws Exception {

 

            for (PdfPTable t : new PdfPTable[] { summary, transactions
}) {

                  t.setTotalWidth(getPageWidth());

                  t.setLockedWidth(true);

            }

 

            float totalNeeded = summary.getTotalHeight();

            // two header rows are assumed. row 0 is the "(Continued)"
banner and

            // row 1 is the column headers

            for (int i = 2; i <= numStickyTransactions + 2; i++) {

                  totalNeeded += transactions.getRowHeight(i);

            }

            float remaining =
canvas.getPdfWriter().getVerticalPosition(true) - bottomMargin();

            if (log.isDebugEnabled()) {

                  log.debug("total height needed " + totalNeeded);

                  log.debug("total space remaining: " + remaining);

            }

 

            if (remaining < totalNeeded) {

                  if (log.isDebugEnabled()) {

                        log.debug("page break needed");

                  }

                  newPage();

            }

            if (null != bookmarkText) {

                  createBookmark(bookmarkText);

            }

            add(summary);

            add(transactions);

      }

}

 

This way the summary is always added to the page before the
transactions.  This means the skipFirstHeaders on the transaction table
will always be respected.  It will also work as expected regardless of
font sizes and document margins.  I'm _sure_ there is a more "slick"
solution, but I'm a big fan of easy to understand code, and this is what
I came up with.  I also really wanted to use document.add() and avoid
absolute positioning hell as much as I could.

 

Thanks for the tip about cell borders, I'll give it whirl right now and
let you know what the difference in file size is.

 

Jason

 

 

-----Original Message-----
From: 1T3XT info [mailto:[email protected]] 
Sent: Friday, July 09, 2010 2:14 AM
To: Post all your questions about iText here
Subject: Re: [iText-questions] adding borders bloats file size?

 

Jason Berk wrote:

> Is it normal for cell borders to bloat the size of the file this 

> significantly?

 

Nice report!

I like what you've done with the "Continued" header!

How did you eventually implement it?

 

As for the cell borders.

I fear it's inherent to the fact that the full line

used as bottom border for the table consists of a

series of small lines, one for each cell.

 

You can significantly reduce the file size by using

a table event and by drawing the bottom border of

each row using one single line.

 

Use this for inspiration:

http://itextpdf.com/examples/index.php?page=example&id=93

 

(Change the tableLayout method so that it draws a line

instead of a Rectangle.)

-- 

This answer is provided by 1T3XT BVBA

http://www.1t3xt.com/ - http://www.1t3xt.info

 

------------------------------------------------------------------------
------

This SF.net email is sponsored by Sprint

What will you do first with EVO, the first 4G phone?

Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first

_______________________________________________

iText-questions mailing list

[email protected]

https://lists.sourceforge.net/lists/listinfo/itext-questions

 

Buy the iText book: http://www.itextpdf.com/book/

Check the site with examples before you ask questions:
http://www.1t3xt.info/examples/

You can also search the keywords list:
http://1t3xt.info/tutorials/keywords/



***This is a transmission from Purdue Employees Federal Credit
Union (PEFCU) and is intended solely for its authorized
recipient(s), and may contain information that is confidential
and or legally privileged.  If you are not an addressee, or the
employee or agent responsible for delivering it to an addressee,
you are hereby notified that any use, dissemination,
distribution, publication or copying of the information 
contained
in this email is strictly prohibited. If you have received this
transmission in error, please notify us by telephoning (765)
497-3328 or returning the email. You are then instructed to
delete the information from your computer.  Thank you for your
cooperation.***

------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to