Table of Contents

Class Document

Namespace
iTextSharp.text
Assembly
iTextSharp.LGPLv2.Core.dll

A generic Document class.

public class Document : IDocListener, IElementListener, IDisposable
Inheritance
Document
Implements
Derived
Inherited Members

Examples

// 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();

Remarks

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.

Constructors

Document()

membervariables concerning the layout

public Document()

Document(Rectangle)

Constructs a new Document-object.

public Document(Rectangle pageSize)

Parameters

pageSize Rectangle

the pageSize

Document(Rectangle, float, float, float, float)

Constructs a new Document-object.

public Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom)

Parameters

pageSize Rectangle

the pageSize

marginLeft float

the margin on the left

marginRight float

the margin on the right

marginTop float

the margin on the top

marginBottom float

the margin on the bottom

Fields

Chapternumber

This is a chapter number in case ChapterAutoNumber is used.

protected int Chapternumber

Field Value

int

Compress

Allows the pdf documents to be produced without compression for debugging purposes.

public static readonly bool Compress

Field Value

bool

IsDocumentClose

Has the document allready been closed?

protected bool IsDocumentClose

Field Value

bool

IsDocumentOpen

Is the document open or not?

protected bool IsDocumentOpen

Field Value

bool

MarginMirroring

protected bool MarginMirroring

Field Value

bool

MarginMirroringTopBottom

mirroring of the top/bottom margins @since 2.1.6

protected bool MarginMirroringTopBottom

Field Value

bool

WmfFontCorrection

Scales the WMF font size. The default value is 0.86.

public static readonly float WmfFontCorrection

Field Value

float

This is the textual part of the footer

protected HeaderFooter footer

Field Value

HeaderFooter

header

This is the textual part of a Page; it can contain a header

protected HeaderFooter header

Field Value

HeaderFooter

Properties

Bottom

Returns the lower left y-coordinate.

public float Bottom { get; }

Property Value

float

the lower left y-coordinate.

BottomMargin

Returns the bottom margin.

public float BottomMargin { get; protected set; }

Property Value

float

the bottom margin

Changes the footer of this document.

public virtual HeaderFooter Footer { set; }

Property Value

HeaderFooter

a HeaderFooter

Header

Changes the header of this document.

public virtual HeaderFooter Header { set; }

Property Value

HeaderFooter

a HeaderFooter

HtmlStyleClass

Gets the style class of the HTML body tag

public string HtmlStyleClass { get; set; }

Property Value

string

the style class of the HTML body tag

JavaScriptOnLoad

Gets the JavaScript onLoad command.

public string JavaScriptOnLoad { get; set; }

Property Value

string

the JavaScript onLoad command.

JavaScriptOnUnLoad

Gets the JavaScript onUnLoad command.

public string JavaScriptOnUnLoad { get; set; }

Property Value

string

the JavaScript onUnLoad command

Left

Returns the lower left x-coordinate.

public float Left { get; }

Property Value

float

the lower left x-coordinate

LeftMargin

Returns the left margin.

public float LeftMargin { get; protected set; }

Property Value

float

the left margin

PageCount

Sets the page number.

public virtual int PageCount { set; }

Property Value

int

an int

PageNumber

Returns the current page number.

public int PageNumber { get; protected set; }

Property Value

int

an int

PageSize

Gets the pagesize.

public Rectangle PageSize { get; protected set; }

Property Value

Rectangle

the page size

Product

listener methods

public static string Product { get; }

Property Value

string

Release

Gets the release number.

public static string Release { get; }

Property Value

string

Right

Returns the upper right x-coordinate.

public float Right { get; }

Property Value

float

the upper right x-coordinate.

RightMargin

methods to get the layout of the document.

public float RightMargin { get; protected set; }

Property Value

float

the right margin

Top

Returns the upper right y-coordinate.

public float Top { get; }

Property Value

float

the upper right y-coordinate.

TopMargin

Returns the top margin.

public float TopMargin { get; protected set; }

Property Value

float

the top margin

Version

Gets the iText version.

public static string Version { get; }

Property Value

string

iText version

Methods

Add(IElement)

Adds an Element to the Document.

public virtual bool Add(IElement element)

Parameters

element IElement

the Element to add

Returns

bool

true if the element was added, false if not

AddAuthor(string)

Adds the author to a Document.

public bool AddAuthor(string author)

Parameters

author string

the name of the author

Returns

bool

true if successful, false otherwise

AddCreationDate()

Adds the current date and time to a Document.

public bool AddCreationDate()

Returns

bool

true if successful, false otherwise

AddCreator(string)

Adds the creator to a Document.

public bool AddCreator(string creator)

Parameters

creator string

the name of the creator

Returns

bool

true if successful, false otherwise

AddDocListener(IDocListener)

Adds a IDocListener to the Document.

public void AddDocListener(IDocListener listener)

Parameters

listener IDocListener

the new IDocListener

AddHeader(string, string)

Adds a user defined header to the document.

public bool AddHeader(string name, string content)

Parameters

name string

the name of the header

content string

the content of the header

Returns

bool

true if successful, false otherwise

AddKeywords(string)

Adds the keywords to a Document.

public bool AddKeywords(string keywords)

Parameters

keywords string

keywords to add

Returns

bool

true if successful, false otherwise

AddProducer()

Adds the producer to a Document.

public bool AddProducer()

Returns

bool

true if successful, false otherwise

AddSubject(string)

Adds the subject to a Document.

public bool AddSubject(string subject)

Parameters

subject string

the subject

Returns

bool

true if successful, false otherwise

AddTitle(string)

methods concerning the header or some meta information

public bool AddTitle(string title)

Parameters

title string

the title

Returns

bool

true if successful, false otherwise

Close()

Closes the document.

public virtual void Close()

Remarks

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.

Dispose()

public void Dispose()

GetBottom(float)

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

public float GetBottom(float margin)

Parameters

margin float

a margin

Returns

float

the lower left y-coordinate

GetLeft(float)

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

public float GetLeft(float margin)

Parameters

margin float

a margin

Returns

float

the lower left x-coordinate

GetRight(float)

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

public float GetRight(float margin)

Parameters

margin float

a margin

Returns

float

the upper right x-coordinate

GetTop(float)

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

public float GetTop(float margin)

Parameters

margin float

a margin

Returns

float

the upper right y-coordinate

IsMarginMirroring()

Gets the margin mirroring flag.

public bool IsMarginMirroring()

Returns

bool

the margin mirroring flag

IsOpen()

Checks if the document is open.

public bool IsOpen()

Returns

bool

true if the document is open

NewPage()

Signals that an new page has to be started.

public virtual bool NewPage()

Returns

bool

true if the page was added, false if not.

Open()

methods implementing the IDocListener interface

public virtual void Open()

Remarks

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.

RemoveIDocListener(IDocListener)

Removes a IDocListener from the Document.

public void RemoveIDocListener(IDocListener listener)

Parameters

listener IDocListener

the IDocListener that has to be removed.

ResetFooter()

Resets the footer of this document.

public virtual void ResetFooter()

ResetHeader()

Resets the header of this document.

public virtual void ResetHeader()

ResetPageCount()

Sets the page number to 0.

public virtual void ResetPageCount()

SetMarginMirroring(bool)

Set the margin mirroring. It will mirror right/left margins for odd/even pages. Note: it will not work with {@link Table}. true to mirror the margins

public virtual bool SetMarginMirroring(bool marginMirroring)

Parameters

marginMirroring bool

Returns

bool

always true

SetMarginMirroringTopBottom(bool)

Set the margin mirroring. It will mirror top/bottom margins for odd/even pages. Note: it will not work with {@link Table}. true to mirror the margins @since 2.1.6

public virtual bool SetMarginMirroringTopBottom(bool marginMirroringTopBottom)

Parameters

marginMirroringTopBottom bool

Returns

bool

always true

SetMargins(float, float, float, float)

Sets the margins.

public virtual bool SetMargins(float marginLeft, float marginRight, float marginTop, float marginBottom)

Parameters

marginLeft float

the margin on the left

marginRight float

the margin on the right

marginTop float

the margin on the top

marginBottom float

the margin on the bottom

Returns

bool

SetPageSize(Rectangle)

Sets the pagesize.

public virtual bool SetPageSize(Rectangle pageSize)

Parameters

pageSize Rectangle

the new pagesize

Returns

bool

a bool