About us | News | Contact
iText ® Licenses Support   
You are here: Home > Forums > iText in Action — Second Edition > Part 1: Creating PDF documents from scratch > Chapter 3: Adding content at absolute positions
User login
  • Request new password

saveState and restoreState

Submitted by Jiujing Gu on Tue, 07/05/2011 - 17:34

The method drawBlock in MovieTimeBlocks contains under.saveState() and under.restoreState(). Why there aren't corresponding over.saveState() and over.restoreState()?

I tried to comment out under.saveState() and under.restoreState(). I didn't see any
difference in results. Did I overlook something?

Thanks in advance.

Area taken by a ColumnText object ›
  • Login to post comments

Changing the state

Submitted by Bruno Lowagie on Wed, 07/06/2011 - 10:15.

Good question!

The saveState() and restoreState() method are for the under content because we change the graphics state: under.setColorFill(color);.
Suppose that you change the color to orange, and you would not use saveState()/restoreState(), every shape you draw afterwards (including text) would be orange (unless you changed the state to another color).
You don't see any difference, because we don't draw anything else, exept for the movie blocks for which we always set the color. The text is added to another layer (which adds saveState()/restoreState() implicitly), so it's not affected by the color change.

Summarized: you're correct when you say that saveState()/restoreState() isn't needed in this case, but saveState()/restoreState() are there because it's good practice to use them when you change the graphics state.

They aren't needed for the over content, because we construct a path (a rectangle) and we immediately stroke it. This doesn't change the graphics state, so there's no need for using saveState()/restoreState().

  • Login to post comments
Content © 2010 1T3XT BVBA