using System;
using System.IO;
using System.Collections;
using System.util;
using iTextSharp.text.pdf;
/*
* $Id: DocWriter.cs,v 1.7 2008/05/13 11:25:09 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 {
///
/// An abstract Writer class for documents.
///
///
/// DocWriter is the abstract class of several writers such
/// as PdfWriter and HtmlWriter.
/// A DocWriter can be added as a DocListener
/// to a certain Document by getting an instance (see method
/// GetInstance() in the specific writer-classes).
/// Every Element added to the original Document
/// will be written to the stream of the listening
/// DocWriter.
///
///
///
public abstract class DocWriter : IDocListener {
/// This is some byte that is often used.
public const byte NEWLINE = (byte)'\n';
/// This is some byte that is often used.
public const byte TAB = (byte)'\t';
/// This is some byte that is often used.
public const byte LT = (byte)'<';
/// This is some byte that is often used.
public const byte SPACE = (byte)' ';
/// This is some byte that is often used.
public const byte EQUALS = (byte)'=';
/// This is some byte that is often used.
public const byte QUOTE = (byte)'\"';
/// This is some byte that is often used.
public const byte GT = (byte)'>';
/// This is some byte that is often used.
public const byte FORWARD = (byte)'/';
// membervariables
/// The pageSize.
protected Rectangle pageSize;
/// This is the document that has to be written.
protected Document document;
/// The stream of this writer.
protected OutputStreamCounter os;
/// Is the writer open for writing?
protected bool open = false;
/// Do we have to pause all writing actions?
protected bool pause = false;
/** Closes the stream on document close */
protected bool closeStream = true;
// constructor
protected DocWriter() {
}
///
/// Constructs a DocWriter.
///
/// The Document that has to be written
/// The Stream the writer has to write to.
protected DocWriter(Document document, Stream os)
{
this.document = document;
this.os = new OutputStreamCounter(os);
}
// implementation of the DocListener methods
///
/// Signals that an Element was added to the Document.
///
///
/// This method should be overriden in the specific DocWriter classes
/// derived from this abstract class.
///
///
/// false
public virtual bool Add(IElement element) {
return false;
}
///
/// Signals that the Document was opened.
///
public virtual void Open() {
open = true;
}
///
/// Sets the pagesize.
///
/// the new pagesize
/// a boolean
public virtual bool SetPageSize(Rectangle pageSize) {
this.pageSize = pageSize;
return true;
}
///
/// Sets the margins.
///
///
/// This does nothing. Has to be overridden if needed.
///
/// 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) {
return false;
}
///
/// Signals that an new page has to be started.
///
///
/// This does nothing. Has to be overridden if needed.
///
/// true if the page was added, false if not.
public virtual bool NewPage() {
if (!open) {
return false;
}
return true;
}
///
/// Changes the header of this document.
///
///
/// This method should be overriden in the specific DocWriter classes
/// derived from this abstract class if they actually support the use of
/// headers.
///
/// the new header
public virtual HeaderFooter Header {
set {}
}
///
/// Resets the header of this document.
///
///
/// This method should be overriden in the specific DocWriter classes
/// derived from this abstract class if they actually support the use of
/// headers.
///
public virtual void ResetHeader() {
}
///
/// Changes the footer of this document.
///
///
/// This method should be overriden in the specific DocWriter classes
/// derived from this abstract class if they actually support the use of
/// footers.
///
/// the new footer
public virtual HeaderFooter Footer {
set {}
}
///
/// Resets the footer of this document.
///
///
/// This method should be overriden in the specific DocWriter classes
/// derived from this abstract class if they actually support the use of
/// footers.
///
public virtual void ResetFooter() {
}
///
/// Sets the page number to 0.
///
///
/// This method should be overriden in the specific DocWriter classes
/// derived from this abstract class if they actually support the use of
/// pagenumbers.
///
public virtual void ResetPageCount() {
}
///
/// Sets the page number.
///
///
/// This method should be overriden in the specific DocWriter classes
/// derived from this abstract class if they actually support the use of
/// pagenumbers.
///
public virtual int PageCount {
set {}
}
///
/// Signals that the Document was closed and that no other
/// Elements will be added.
///
public virtual void Close() {
open = false;
os.Flush();
if (closeStream)
os.Close();
}
// methods
///
/// Converts a string into a Byte array
/// according to the ISO-8859-1 codepage.
///
/// the text to be converted
/// the conversion result
public static byte[] GetISOBytes(string text) {
if (text == null)
return null;
int len = text.Length;
byte[] b = new byte[len];
for (int k = 0; k < len; ++k)
b[k] = (byte)text[k];
return b;
}
///
/// Let the writer know that all writing has to be paused.
///
public virtual void Pause() {
pause = true;
}
/**
* Checks if writing is paused.
*
* @return true
if writing temporarely has to be paused, false
otherwise.
*/
public bool IsPaused() {
return pause;
}
///
/// Let the writer know that writing may be resumed.
///
public virtual void Resume() {
pause = false;
}
///
/// Flushes the Stream.
///
public virtual void Flush() {
os.Flush();
}
///
/// Writes a string to the stream.
///
/// the string to write
protected void Write(string str) {
byte[] tmp = GetISOBytes(str);
os.Write(tmp, 0, tmp.Length);
}
///
/// Writes a number of tabs.
///
/// the number of tabs to add
protected void AddTabs(int indent) {
os.WriteByte(NEWLINE);
for (int i = 0; i < indent; i++) {
os.WriteByte(TAB);
}
}
///
/// Writes a key-value pair to the stream.
///
/// the name of an attribute
/// the value of an attribute
protected void Write(string key, string value) {
os.WriteByte(SPACE);
Write(key);
os.WriteByte(EQUALS);
os.WriteByte(QUOTE);
Write(value);
os.WriteByte(QUOTE);
}
///
/// Writes a starttag to the stream.
///
/// the name of the tag
protected void WriteStart(string tag) {
os.WriteByte(LT);
Write(tag);
}
///
/// Writes an endtag to the stream.
///
/// the name of the tag
protected void WriteEnd(string tag) {
os.WriteByte(LT);
os.WriteByte(FORWARD);
Write(tag);
os.WriteByte(GT);
}
///
/// Writes an endtag to the stream.
///
protected void WriteEnd() {
os.WriteByte(SPACE);
os.WriteByte(FORWARD);
os.WriteByte(GT);
}
///
/// Writes the markup attributes of the specified MarkupAttributes
/// object to the stream.
///
/// the MarkupAttributes to write.
///
protected bool WriteMarkupAttributes(Properties markup) {
if (markup == null) return false;
foreach (String name in markup.Keys) {
Write(name, markup[name]);
}
markup.Clear();
return true;
}
public virtual bool CloseStream {
get {
return closeStream;
}
set {
closeStream = value;
}
}
public virtual bool SetMarginMirroring(bool marginMirroring) {
return false;
}
}
}