File.cs :  » PDF » PDF-Clown » it » stefanochizzolini » clown » files » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » PDF Clown 
PDF Clown » it » stefanochizzolini » clown » files » File.cs
/*
  Copyright 2006,2007,2008 Stefano Chizzolini. http://clown.stefanochizzolini.it

  Contributors:
    * Stefano Chizzolini (original code developer, http://www.stefanochizzolini.it)

  This file should be part of the source code distribution of "PDF Clown library"
  (the Program): see the accompanying README files for more info.

  This Program is free software; you can redistribute it and/or modify it under
  the terms of the GNU General Public License as published by the Free Software
  Foundation; either version 2 of the License, or (at your option) any later version.

  This Program is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY, either expressed or implied; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.

  You should have received a copy of the GNU General Public License along with this
  Program (see README files); if not, go to the GNU website (http://www.gnu.org/).

  Redistribution and use, with or without modification, are permitted provided that such
  redistributions retain the above copyright notice, license and disclaimer, along with
  this list of conditions.
*/

using it.stefanochizzolini.clown.bytes;
using it.stefanochizzolini.clown.documents;
using it.stefanochizzolini.clown.documents.contents;
using it.stefanochizzolini.clown.objects;
using it.stefanochizzolini.clown.tokens;

using System;
using System.Collections;
using System.Text;
using System.IO;

namespace it.stefanochizzolini.clown.files{
  /**
    <summary>PDF file representation.</summary>
  */
  public class File : IDisposable
  {
    #region static
    #region fields
    private static Random hashCodeGenerator = new Random();
    #endregion

    #region interface
    #region public
    /**
      <summary>Forces a generic object to be expressed as its corresponding
      data object.</summary>
    */
    public static PdfDataObject Resolve(
      PdfObject obj
      )
    {
      if(obj is IPdfIndirectObject)
        return ((IPdfIndirectObject)obj).DataObject;
      else
        return (PdfDataObject)obj;
    }

    /**
      <summary>Forces a direct object to be updated (whether possible).</summary>
    */
    public static bool Update(
      PdfDirectObject obj
      )
    {
      /*
        NOTE: Only PDF references are able to be updated. Other direct types
        are dependent on their respective containers for update.
      */
      if(obj is PdfReference)
      {
        ((PdfReference)obj).IndirectObject.Update();
        return true;
      }

      return false;
    }
    #endregion
    #endregion
    #endregion

    #region dynamic
    #region fields
    private Document document;
    private int hashCode = File.hashCodeGenerator.Next();
    private IndirectObjects indirectObjects;
    private Reader reader;
    private PdfDictionary trailer;
    private string version;
    private XRefEntry[] xrefEntries;
    #endregion

    #region constructors
    public File(
      )
    {
      this.version = "1.6";
      this.trailer = new PdfDictionary();
      this.indirectObjects = new IndirectObjects(this,null);
      this.document = new Document(this);
    }

    public File(
      string path
      ) : this(
        new bytes.Stream(
          new FileStream(
            path,
            FileMode.Open,
            FileAccess.Read
            )
          )
        )
    {}

    public File(
      IInputStream stream
      )
    {
      this.reader = new Reader(stream,this);

      this.version = reader.ReadVersion();
      this.trailer = reader.ReadTrailer();

      // Is this file encrypted?
      if(trailer.ContainsKey(PdfName.Encrypt))
        throw new NotImplementedException("Encrypted files are currently not supported.");

      this.xrefEntries = reader.ReadXRefTable(trailer);
      this.indirectObjects = new IndirectObjects(this,xrefEntries);
      this.document = new Document(trailer[PdfName.Root]);
    }
    #endregion

    #region interface
    #region public
    public Document Document
    {get{return document;}}

    public override int GetHashCode(
      )
    {return hashCode;}

    public IndirectObjects IndirectObjects
    {get{return indirectObjects;}}

    public Reader Reader
    {get{return reader;}}

    /**
      <summary>Registers an <b>internal data object</b>.</summary>
    */
    public PdfReference Register(
      PdfDataObject obj
      )
    {return indirectObjects.Add(obj).Reference;}

    public PdfDictionary Trailer
    {get{return trailer;}}

    /**
      <summary>Unregisters an <b>internal object</b>.</summary>
    */
    public void Unregister(
      PdfReference reference
      )
    {indirectObjects.RemoveAt(reference.ObjectNumber);}

    public string Version
    {get{return version;}}

    /**
      <summary>Serializes the file to the specified file system path.</summary>
      <param name="path">Target path.</param>
      <param name="mode">Serialization mode.</param>
    */
    public void WriteTo(
      string path,
      SerializationModeEnum mode
      )
    {
      FileStream outputStream = new System.IO.FileStream(
        path,
        System.IO.FileMode.Create,
        System.IO.FileAccess.Write
        );

      WriteTo(
        new bytes.Stream(outputStream),
        mode
        );

      outputStream.Flush();
      outputStream.Close();
    }

    /**
      <summary>Serializes the file to the specified stream.</summary>
      <remarks>It's caller responsibility to close the stream after this method
      ends.</remarks>
      <param name="stream">Target stream.</param>
      <param name="mode">Serialization mode.</param>
    */
    public void WriteTo(
      IOutputStream stream,
      SerializationModeEnum mode
      )
    {
      Writer writer = new Writer(stream,this);

      switch(mode)
      {
        case SerializationModeEnum.Incremental:
          if(reader == null)
            goto case SerializationModeEnum.Standard;

          writer.WriteIncremental();
          break;
        case SerializationModeEnum.Standard:
          writer.WriteStandard();
          break;
        case SerializationModeEnum.Linearized:
          throw new NotImplementedException();
      }
    }

    //TODO:IMPL avoid direct exposure of array (corruptible!)!!!
    public XRefEntry[] XRefEntries
    {get{return xrefEntries;}}

    #region IDisposable
    public void Dispose(
      )
    {
      if(reader != null)
      {
        reader.Dispose();
        reader = null;
      }

      GC.SuppressFinalize(this);
    }
    #endregion
    #endregion
    #endregion
    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.