Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: Document.java 4942 2011-08-15 07:29:15Z blowagie $
   *
   * This file is part of the iText (R) project.
   * Copyright (c) 1998-2011 1T3XT BVBA
   * Authors: Bruno Lowagie, Paulo Soares, et al.
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU Affero General Public License version 3
  * as published by the Free Software Foundation with the addition of the
  * following permission added to Section 15 as permitted in Section 7(a):
  * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
  * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  * or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU Affero General Public License for more details.
  * You should have received a copy of the GNU Affero General Public License
  * along with this program; if not, see http://www.gnu.org/licenses or write to
  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  * Boston, MA, 02110-1301 USA, or download the license from the following URL:
  * http://itextpdf.com/terms-of-use/
  *
  * The interactive user interfaces in modified source and object code versions
  * of this program must display Appropriate Legal Notices, as required under
  * Section 5 of the GNU Affero General Public License.
  *
  * In accordance with Section 7(b) of the GNU Affero General Public License,
  * a covered work must retain the producer line in every PDF that is created
  * or manipulated using iText.
  *
  * You can be released from the requirements of the license by purchasing
  * a commercial license. Buying such a license is mandatory as soon as you
  * develop commercial activities involving the iText software without
  * disclosing the source code of your own applications.
  * These activities include: offering paid services to customers as an ASP,
  * serving PDFs on the fly in a web application, shipping iText with a closed
  * source product.
  *
  * For more information, please contact iText Software Corp. at this
  * address: sales@itextpdf.com
  */
 package com.itextpdf.text;
 
 import java.util.Date;
 
A generic Document class.

All kinds of Text-elements can be added to a HTMLDocument. The Document signals all the listeners when an element has been added.

Remark:

  1. Once a document is created you can add some meta information.
  2. You can also set the headers/footers.
  3. You have to open the document before you can write content.
  4. You can only write content (no more meta-formation!) once a document is opened.
  5. When you change the header/footer on a certain page, this will be effective starting on the next page.
  6. After closing the document, every listener (as well as its OutputStream) is closed too.
Example:
// creation of the document with a certain size and certain margins
 Document document = new Document(PageSize.A4, 50, 50, 50, 50);
  try {
   // creation of the different writers
   HtmlWriter.getInstance(document , System.out);
   PdfWriter.getInstance(document , new FileOutputStream("text.pdf"));
   // we add some meta information to the document
   document.addAuthor("Bruno Lowagie"); 
   document.addSubject("This is the result of a Test."); 
   // we open the document for writing
   document.open(); 
   document.add(new Paragraph("Hello world"));
  } catch(DocumentException de) {
   System.err.println(de.getMessage());
  }
  document.close();
 
 
 
 public class Document implements DocListener {
 
     // membervariables
     
This constant contains the name of the product. iText is a registered trademark. Please don't change this constant.

Since:
2.1.6
	private static final String ITEXT = "iText\u00ae";
    
This constant contains the version number of this iText release. For debugging purposes, we request you NOT to change this constant.

Since:
2.1.6
	private static final String RELEASE = "5.1.2";
This constant the iText version as shown in the producer line. iText is a product developed by 1T3XT BVBA. 1T3XT requests that you retain the iText producer line in every PDF that is created or manipulated using iText.
	private static final String ITEXT_VERSION =  + " " +  + " \u00a92000-2011 1T3XT BVBA";

Allows the pdf documents to be produced without compression for debugging purposes.
    public static boolean compress = true;

When true the file access is not done through a memory mapped file. Use it if the file is too big to be mapped in your address space.
    public static boolean plainRandomAccess = false;

    
Scales the WMF font size. The default value is 0.86.
    public static float wmfFontCorrection = 0.86f;

The DocListener.

Since:
iText 5.1.0 changed from private to protected
    protected ArrayList<DocListenerlisteners = new ArrayList<DocListener>();

Is the document open or not?
    protected boolean open;

Has the document already been closed?
    protected boolean close;
    // membervariables concerning the layout
The size of the page.
    protected Rectangle pageSize;

margin in x direction starting from the left
    protected float marginLeft = 0;

margin in x direction starting from the right
    protected float marginRight = 0;

margin in y direction starting from the top
    protected float marginTop = 0;

margin in y direction starting from the bottom
    protected float marginBottom = 0;

    
mirroring of the left/right margins
    protected boolean marginMirroring = false;

    
mirroring of the top/bottom margins

Since:
2.1.6
    protected boolean marginMirroringTopBottom = false;

Content of JavaScript onLoad function
    protected String javaScript_onLoad = null;

Content of JavaScript onUnLoad function
    protected String javaScript_onUnLoad = null;

Style class in HTML body tag
    protected String htmlStyleClass = null;
    // headers, footers
Current pagenumber
    protected int pageN = 0;

    
This is a chapter number in case ChapterAutoNumber is used.
    protected int chapternumber = 0;
    // constructor
Constructs a new Document -object.
    public Document() {
        this(.);
    }

Constructs a new Document -object.

Parameters:
pageSize the pageSize
    public Document(Rectangle pageSize) {
        this(pageSize, 36, 36, 36, 36);
    }

Constructs a new Document -object.

Parameters:
pageSize the pageSize
marginLeft the margin on the left
marginRight the margin on the right
marginTop the margin on the top
marginBottom the margin on the bottom
	public Document(Rectangle pageSizefloat marginLeftfloat marginRight,
			float marginTopfloat marginBottom) {
        this. = pageSize;
        this. = marginLeft;
        this. = marginRight;
        this. = marginTop;
        this. = marginBottom;
    }
    // listener methods
Adds a DocListener to the Document.

Parameters:
listener the new DocListener.
    public void addDocListener(DocListener listener) {
        .add(listener);
    }

Removes a DocListener from the Document.

Parameters:
listener the DocListener that has to be removed.
    public void removeDocListener(DocListener listener) {
        .remove(listener);
    }
    // methods implementing the DocListener interface
Adds an Element to the Document.

Parameters:
element the Element to add
Returns:
true if the element was added, false if not
Throws:
DocumentException when a document isn't open yet, or has been closed
    public boolean add(Element elementthrows DocumentException {
        if () {
			throw new DocumentException(MessageLocalization.getComposedMessage("the.document.has.been.closed.you.can.t.add.any.elements"));
        }
		if (! && element.isContent()) {
			throw new DocumentException(MessageLocalization.getComposedMessage("the.document.is.not.open.yet.you.can.only.add.meta.information"));
        }
        boolean success = false;
        if (element instanceof ChapterAutoNumber) {
        }
        for (DocListener listener : ) {
            success |= listener.add(element);
        }
		if (element instanceof LargeElement) {
			LargeElement e = (LargeElement)element;
			if (!e.isComplete())
		}
        return success;
    }

Opens the document.

Once the document is opened, you can't write any Header- or Meta-information anymore. You have to open the document before you can begin to add content to the body of the document.

    public void open() {
		if (!) {
             = true;
        }
		for (DocListener listener : ) {
            listener.setPageSize();
            listener.open();
        }
    }

Sets the pagesize.

Parameters:
pageSize the new pagesize
Returns:
a boolean
    public boolean setPageSize(Rectangle pageSize) {
        this. = pageSize;
		for (DocListener listener : ) {
            listener.setPageSize(pageSize);
        }
        return true;
    }

Sets the margins.

Parameters:
marginLeft the margin on the left
marginRight the margin on the right
marginTop the margin on the top
marginBottom the margin on the bottom
Returns:
a boolean
	public boolean setMargins(float marginLeftfloat marginRight,
			float marginTopfloat marginBottom) {
        this. = marginLeft;
        this. = marginRight;
        this. = marginTop;
        this. = marginBottom;
		for (DocListener listener : ) {
			listener.setMargins(marginLeftmarginRightmarginTop,
					marginBottom);
        }
        return true;
    }

Signals that an new page has to be started.

Returns:
true if the page was added, false if not.
    public boolean newPage() {
        if (! || ) {
            return false;
        }
        for (DocListener listener : ) {
            listener.newPage();
        }
        return true;
    }

Sets the page number to 0.
    public void resetPageCount() {
         = 0;
        for (DocListener listener) {
            listener.resetPageCount();
        }
    }

Sets the page number.

Parameters:
pageN the new page number
    public void setPageCount(int pageN) {
        this. = pageN;
        for (DocListener listener) {
            listener.setPageCount(pageN);
        }
    }

Returns the current page number.

Returns:
the current page number
    public int getPageNumber() {
        return this.;
    }

Closes the document.

Once all the content has been written in the body, you have to close the body. After that nothing can be written to the body anymore.

    public void close() {
		if (!) {
             = false;
             = true;
        }
		for (DocListener listener : ) {
            listener.close();
        }
    }
    // methods concerning the header or some meta information
Adds a user defined header to the document.

Parameters:
name the name of the header
content the content of the header
Returns:
true if successful, false otherwise
    public boolean addHeader(String nameString content) {
        try {
            return add(new Header(namecontent));
catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

Adds the title to a Document.

Parameters:
title the title
Returns:
true if successful, false otherwise
    public boolean addTitle(String title) {
        try {
            return add(new Meta(.title));
catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

Adds the subject to a Document.

Parameters:
subject the subject
Returns:
true if successful, false otherwise
    public boolean addSubject(String subject) {
        try {
            return add(new Meta(.subject));
catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

Adds the keywords to a Document.

Parameters:
keywords adds the keywords to the document
Returns:
true if successful, false otherwise
    public boolean addKeywords(String keywords) {
        try {
            return add(new Meta(.keywords));
catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

Adds the author to a Document.

Parameters:
author the name of the author
Returns:
true if successful, false otherwise
    public boolean addAuthor(String author) {
        try {
            return add(new Meta(.author));
catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

Adds the creator to a Document.

Parameters:
creator the name of the creator
Returns:
true if successful, false otherwise
    public boolean addCreator(String creator) {
        try {
            return add(new Meta(.creator));
catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

Adds the producer to a Document.

Returns:
true if successful, false otherwise
    public boolean addProducer() {
        try {
            return add(new Meta(.getVersion()));
catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }

Adds the current date and time to a Document.

Returns:
true if successful, false otherwise
    public boolean addCreationDate() {
        try {
			/* bugfix by 'taqua' (Thomas) */
			final SimpleDateFormat sdf = new SimpleDateFormat(
					"EEE MMM dd HH:mm:ss zzz yyyy");
			return add(new Meta(.sdf.format(new Date())));
catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }
    // methods to get the layout of the document.
Returns the left margin.

Returns:
the left margin
    public float leftMargin() {
        return ;
    }

Return the right margin.

Returns:
the right margin
    public float rightMargin() {
        return ;
    }

Returns the top margin.

Returns:
the top margin
    public float topMargin() {
        return ;
    }

Returns the bottom margin.

Returns:
the bottom margin
    public float bottomMargin() {
        return ;
    }

Returns the lower left x-coordinate.

Returns:
the lower left x-coordinate
    public float left() {
        return .getLeft();
    }

Returns the upper right x-coordinate.

Returns:
the upper right x-coordinate
    public float right() {
        return .getRight();
    }

Returns the upper right y-coordinate.

Returns:
the upper right y-coordinate
    public float top() {
        return .getTop();
    }

Returns the lower left y-coordinate.

Returns:
the lower left y-coordinate
    public float bottom() {
        return .getBottom();
    }

Returns the lower left x-coordinate considering a given margin.

Parameters:
margin a margin
Returns:
the lower left x-coordinate
    public float left(float margin) {
        return .getLeft( + margin);
    }

Returns the upper right x-coordinate, considering a given margin.

Parameters:
margin a margin
Returns:
the upper right x-coordinate
    public float right(float margin) {
        return .getRight( + margin);
    }

Returns the upper right y-coordinate, considering a given margin.

Parameters:
margin a margin
Returns:
the upper right y-coordinate
    public float top(float margin) {
        return .getTop( + margin);
    }

Returns the lower left y-coordinate, considering a given margin.

Parameters:
margin a margin
Returns:
the lower left y-coordinate
    public float bottom(float margin) {
        return .getBottom( + margin);
    }

Gets the pagesize.

Returns:
the page size
	public Rectangle getPageSize() {
        return this.;
    }

Checks if the document is open.

Returns:
true if the document is open
    public boolean isOpen() {
        return ;
    }

Gets the product name. This method may only be changed by Paulo Soares and/or Bruno Lowagie.

Returns:
the product name
Since:
2.1.6
    public static final String getProduct() {
        return ;
    }

Gets the release number. This method may only be changed by Paulo Soares and/or Bruno Lowagie.

Returns:
the product name
Since:
2.1.6
    public static final String getRelease() {
        return ;
    }

Returns the iText version as shown in the producer line. iText is a product developed by 1T3XT BVBA. 1T3XT requests that you retain the iText producer line in every PDF that is created or manipulated using iText.

Returns:
iText version
    public static final String getVersion() {
        return ;
    }

Adds a JavaScript onLoad function to the HTML body tag

Parameters:
code the JavaScript code to be executed on load of the HTML page
    public void setJavaScript_onLoad(String code) {
        this. = code;
    }

Gets the JavaScript onLoad command.

Returns:
the JavaScript onLoad command
    public String getJavaScript_onLoad() {
        return this.;
    }

Adds a JavaScript onUnLoad function to the HTML body tag

Parameters:
code the JavaScript code to be executed on unload of the HTML page
    public void setJavaScript_onUnLoad(String code) {
        this. = code;
    }

Gets the JavaScript onUnLoad command.

Returns:
the JavaScript onUnLoad command
    public String getJavaScript_onUnLoad() {
        return this.;
    }

Adds a style class to the HTML body tag

Parameters:
htmlStyleClass the style class for the HTML body tag
    public void setHtmlStyleClass(String htmlStyleClass) {
        this. = htmlStyleClass;
    }

Gets the style class of the HTML body tag

Returns:
the style class of the HTML body tag
    public String getHtmlStyleClass() {
        return this.;
    }

    
Set the margin mirroring. It will mirror right/left margins for odd/even pages.

Parameters:
marginMirroring true to mirror the margins
Returns:
always true
    public boolean setMarginMirroring(boolean marginMirroring) {
        this. = marginMirroring;
        DocListener listener;
		for (Object element : ) {
            listener = (DocListenerelement;
            listener.setMarginMirroring(marginMirroring);
        }
        return true;
    }

    
Set the margin mirroring. It will mirror top/bottom margins for odd/even pages.

Parameters:
marginMirroringTopBottom true to mirror the margins
Returns:
always true
Since:
2.1.6
    public boolean setMarginMirroringTopBottom(boolean marginMirroringTopBottom) {
        this. = marginMirroringTopBottom;
        DocListener listener;
		for (Object element : ) {
            listener = (DocListenerelement;
            listener.setMarginMirroringTopBottom(marginMirroringTopBottom);
        }
        return true;
    }

    
Gets the margin mirroring flag.

Returns:
the margin mirroring flag
    public boolean isMarginMirroring() {
        return ;
    }
New to GrepCode? Check out our FAQ X