using System;
using System.Collections;
/*
* $Id: Document.cs,v 1.45 2008/05/25 13:08:44 psoares33 Exp $
*
*
* Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library 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 Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text {
///
/// 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.
///
/// Once a document is created you can add some meta information.
/// You can also set the headers/footers.
/// You have to open the document before you can write content.
/// You can only write content (no more meta-formation!) once a document is opened.
/// When you change the header/footer on a certain page, this will be effective starting on the next page.
/// Ater closing the document, every listener (as well as its OutputStream) is closed too.
///
///
///
///
/// // 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 define a header and a footer
/// HeaderFooter header = new HeaderFooter(new Phrase("This is a header."), false);
/// HeaderFooter footer = new HeaderFooter(new Phrase("This is page "), new Phrase("."));
/// footer.SetAlignment(Element.ALIGN_CENTER);
/// document.SetHeader(header);
/// document.SetFooter(footer);
/// // we open the document for writing
/// document.Open();
/// document.Add(new Paragraph("Hello world"));
/// }
/// catch (DocumentException de) {
/// Console.Error.WriteLine(de.Message);
/// }
/// document.Close();
///
///
public class Document : IDocListener {
// membervariables
/// This constant may only be changed by Paulo Soares and/or Bruno Lowagie.
private const string ITEXT_VERSION = "iTextSharp 4.1.2 (based on iText 2.1.2u)";
/// Allows the pdf documents to be produced without compression for debugging purposes.
public static bool Compress = true;
/// Scales the WMF font size. The default value is 0.86.
public static float WmfFontCorrection = 0.86f;
/// The IDocListener.
private ArrayList listeners = new ArrayList();
/// Is the document open or not?
protected bool open;
/// Has the document allready been closed?
protected bool 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;
protected bool marginMirroring = 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 the textual part of a Page; it can contain a header
protected HeaderFooter header = null;
/// This is the textual part of the footer
protected HeaderFooter footer = null;
// constructor
///
/// Constructs a new Document-object.
///
///
/// Has three overloads.
///
public Document() : this(iTextSharp.text.PageSize.A4) {}
///
/// Constructs a new Document-object.
///
/// the pageSize
public Document(Rectangle pageSize) : this(pageSize, 36, 36, 36, 36) {}
///
/// Constructs a new Document-object.
///
/// the pageSize
/// the margin on the left
/// the margin on the right
/// the margin on the top
/// the margin on the bottom
public Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom) {
this.pageSize = pageSize;
this.marginLeft = marginLeft;
this.marginRight = marginRight;
this.marginTop = marginTop;
this.marginBottom = marginBottom;
}
// listener methods
///
/// Adds a IDocListener to the Document.
///
/// the new IDocListener
public void AddDocListener(IDocListener listener) {
listeners.Add(listener);
}
///
/// Removes a IDocListener from the Document.
///
/// the IDocListener that has to be removed.
public void RemoveIDocListener(IDocListener listener) {
listeners.Remove(listener);
}
// methods implementing the IDocListener interface
///
/// Adds an Element to the Document.
///
/// the Element to add
/// true if the element was added, false if not
public virtual bool Add(IElement element) {
if (close) {
throw new DocumentException("The document has been closed. You can't add any Elements.");
}
if (!open && element.IsContent()) {
throw new DocumentException("The document is not open yet; you can only add Meta information.");
}
bool success = false;
foreach (IDocListener listener in listeners) {
success |= listener.Add(element);
}
if (element is ILargeElement) {
ILargeElement e = (ILargeElement)element;
if (!e.ElementComplete)
e.FlushContent();
}
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 virtual void Open() {
if (! close) {
open = true;
}
foreach (IDocListener listener in listeners) {
listener.SetPageSize(pageSize);
listener.SetMargins(marginLeft, marginRight, marginTop, marginBottom);
listener.Open();
}
}
///
/// Sets the pagesize.
///
/// the new pagesize
/// a bool
public virtual bool SetPageSize(Rectangle pageSize) {
this.pageSize = pageSize;
foreach (IDocListener listener in listeners) {
listener.SetPageSize(pageSize);
}
return true;
}
///
/// Sets the margins.
///
/// the margin on the left
/// the margin on the right
/// the margin on the top
/// the margin on the bottom
///
public virtual bool SetMargins(float marginLeft,float marginRight,float marginTop,float marginBottom) {
this.marginLeft = marginLeft;
this.marginRight = marginRight;
this.marginTop = marginTop;
this.marginBottom = marginBottom;
foreach (IDocListener listener in listeners) {
listener.SetMargins(marginLeft, marginRight, marginTop, marginBottom);
}
return true;
}
///
/// Signals that an new page has to be started.
///
/// true if the page was added, false if not.
public virtual bool NewPage() {
if (!open || close) {
return false;
}
foreach (IDocListener listener in listeners) {
listener.NewPage();
}
return true;
}
///
/// Changes the header of this document.
///
/// a HeaderFooter
public virtual HeaderFooter Header {
set {
this.header = value;
foreach (IDocListener listener in listeners) {
listener.Header = value;
}
}
}
///
/// Resets the header of this document.
///
public virtual void ResetHeader() {
this.header = null;
foreach (IDocListener listener in listeners) {
listener.ResetHeader();
}
}
///
/// Changes the footer of this document.
///
/// a HeaderFooter
public virtual HeaderFooter Footer {
set {
this.footer = value;
foreach (IDocListener listener in listeners) {
listener.Footer = value;
}
}
}
///
/// Resets the footer of this document.
///
public virtual void ResetFooter() {
this.footer = null;
foreach (IDocListener listener in listeners) {
listener.ResetFooter();
}
}
///
/// Sets the page number to 0.
///
public virtual void ResetPageCount() {
pageN = 0;
foreach (IDocListener listener in listeners) {
listener.ResetPageCount();
}
}
///
/// Sets the page number.
///
/// an int
public virtual int PageCount {
set {
this.pageN = value;
foreach (IDocListener listener in listeners) {
listener.PageCount = value;
}
}
}
///
/// Returns the current page number.
///
/// an int
public int PageNumber {
get {
return this.pageN;
}
}
///
/// 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 virtual void Close() {
if (!close) {
open = false;
close = true;
}
foreach (IDocListener listener in listeners) {
listener.Close();
}
}
// methods concerning the header or some meta information
///
/// Adds a user defined header to the document.
///
/// the name of the header
/// the content of the header
/// true if successful, false otherwise
public bool AddHeader(string name, string content) {
return Add(new Header(name, content));
}
///
/// Adds the title to a Document.
///
/// the title
/// true if successful, false otherwise
public bool AddTitle(string title) {
return Add(new Meta(Element.TITLE, title));
}
///
/// Adds the subject to a Document.
///
/// the subject
/// true if successful, false otherwise
public bool AddSubject(string subject) {
return Add(new Meta(Element.SUBJECT, subject));
}
///
/// Adds the keywords to a Document.
///
/// keywords to add
/// true if successful, false otherwise
public bool AddKeywords(string keywords) {
return Add(new Meta(Element.KEYWORDS, keywords));
}
///
/// Adds the author to a Document.
///
/// the name of the author
/// true if successful, false otherwise
public bool AddAuthor(string author) {
return Add(new Meta(Element.AUTHOR, author));
}
///
/// Adds the creator to a Document.
///
/// the name of the creator
/// true if successful, false otherwise
public bool AddCreator(string creator) {
return Add(new Meta(Element.CREATOR, creator));
}
///
/// Adds the producer to a Document.
///
/// true if successful, false otherwise
public bool AddProducer() {
return Add(new Meta(Element.PRODUCER, "iText# by lowagie.com"));
}
///
/// Adds the current date and time to a Document.
///
/// true if successful, false otherwise
public bool AddCreationDate() {
return Add(new Meta(Element.CREATIONDATE, DateTime.Now.ToString("ddd MMM dd HH:mm:ss zzz yyyy")));
}
// methods to get the layout of the document.
///
/// Returns the left margin.
///
/// the left margin
public float LeftMargin {
get {
return marginLeft;
}
}
///
/// Return the right margin.
///
/// the right margin
public float RightMargin {
get {
return marginRight;
}
}
///
/// Returns the top margin.
///
/// the top margin
public float TopMargin {
get {
return marginTop;
}
}
///
/// Returns the bottom margin.
///
/// the bottom margin
public float BottomMargin {
get {
return marginBottom;
}
}
///
/// Returns the lower left x-coordinate.
///
/// the lower left x-coordinate
public float Left {
get {
return pageSize.GetLeft(marginLeft);
}
}
///
/// Returns the upper right x-coordinate.
///
/// the upper right x-coordinate.
public float Right {
get {
return pageSize.GetRight(marginRight);
}
}
///
/// Returns the upper right y-coordinate.
///
/// the upper right y-coordinate.
public float Top {
get {
return pageSize.GetTop(marginTop);
}
}
///
/// Returns the lower left y-coordinate.
///
/// the lower left y-coordinate.
public float Bottom {
get {
return pageSize.GetBottom(marginBottom);
}
}
///
/// Returns the lower left x-coordinate considering a given margin.
///
/// a margin
/// the lower left x-coordinate
public float GetLeft(float margin) {
return pageSize.GetLeft(marginLeft + margin);
}
///
/// Returns the upper right x-coordinate, considering a given margin.
///
/// a margin
/// the upper right x-coordinate
public float GetRight(float margin) {
return pageSize.GetRight(marginRight + margin);
}
///
/// Returns the upper right y-coordinate, considering a given margin.
///
/// a margin
/// the upper right y-coordinate
public float GetTop(float margin) {
return pageSize.GetTop(marginTop + margin);
}
///
/// Returns the lower left y-coordinate, considering a given margin.
///
/// a margin
/// the lower left y-coordinate
public float GetBottom(float margin) {
return pageSize.GetBottom(marginBottom + margin);
}
///
/// Gets the pagesize.
///
/// the page size
public Rectangle PageSize {
get {
return this.pageSize;
}
}
///
/// Checks if the document is open.
///
/// true if the document is open
public bool IsOpen() {
return open;
}
///
/// Gets the iText version.
///
/// iText version
public static string Version {
get {
return ITEXT_VERSION;
}
}
///
/// Gets the JavaScript onLoad command.
///
/// the JavaScript onLoad command.
public string JavaScript_onLoad {
get {
return this.javaScript_onLoad;
}
set {
this.javaScript_onLoad = value;
}
}
///
/// Gets the JavaScript onUnLoad command.
///
/// the JavaScript onUnLoad command
public string JavaScript_onUnLoad {
get {
return this.javaScript_onUnLoad;
}
set {
this.javaScript_onUnLoad = value;
}
}
///
/// Gets the style class of the HTML body tag
///
/// the style class of the HTML body tag
public string HtmlStyleClass {
get {
return this.htmlStyleClass;
}
set {
this.htmlStyleClass = value;
}
}
/**
* Set the margin mirroring. It will mirror margins for odd/even pages.
*
* Note: it will not work with {@link Table}.
*
* @param marginMirroring
* true
to mirror the margins
* @return always true
*/
public virtual bool SetMarginMirroring(bool marginMirroring) {
this.marginMirroring = marginMirroring;
foreach (IDocListener listener in listeners) {
listener.SetMarginMirroring(marginMirroring);
}
return true;
}
/**
* Gets the margin mirroring flag.
*
* @return the margin mirroring flag
*/
public bool IsMarginMirroring() {
return marginMirroring;
}
}
}