1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
|
#region PDFsharp - A .NET library for processing PDF
//
// Authors:
// Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
//
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
//
// http://www.pdfsharp.com
// http://sourceforge.net/projects/pdfsharp
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Diagnostics;
using System.Collections;
using System.Text;
using System.IO;
using PdfSharp.Internal;
using PdfSharp.Pdf.Advanced;
using PdfSharp.Pdf.IO;
namespace PdfSharp.Pdf
{
/// <summary>
/// Represents a PDF array object.
/// </summary>
[DebuggerDisplay("(elements={Elements.Count})")]
public class PdfArray : PdfObject, IEnumerable
{
ArrayElements elements;
/// <summary>
/// Initializes a new instance of the <see cref="PdfArray"/> class.
/// </summary>
public PdfArray()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PdfArray"/> class.
/// </summary>
/// <param name="document">The document.</param>
public PdfArray(PdfDocument document)
: base(document)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PdfArray"/> class.
/// </summary>
/// <param name="document">The document.</param>
/// <param name="items">The items.</param>
public PdfArray(PdfDocument document, params PdfItem[] items)
: base(document)
{
foreach (PdfItem item in items)
Elements.Add(item);
}
/// <summary>
/// Initializes a new instance from an existing dictionary. Used for object type transformation.
/// </summary>
/// <param name="array">The array.</param>
protected PdfArray(PdfArray array)
: base(array)
{
if (array.elements != null)
array.elements.SetOwner(this);
}
/// <summary>
/// Creates a copy of this array. Direct elements are deep copied. Indirect references are not
/// modified.
/// </summary>
public new PdfArray Clone()
{
return (PdfArray)Copy();
}
/// <summary>
/// Implements the copy mechanism.
/// </summary>
protected override object Copy()
{
PdfArray array = (PdfArray)base.Copy();
if (array.elements != null)
{
array.elements = array.elements.Clone();
int count = array.elements.Count;
for (int idx = 0; idx < count; idx++)
{
PdfItem item = array.elements[idx];
if (item is PdfObject)
array.elements[idx] = item.Clone();
}
}
return array;
}
/// <summary>
/// Gets the collection containing the elements of this object.
/// </summary>
public ArrayElements Elements
{
get
{
if (this.elements == null)
this.elements = new ArrayElements(this);
return this.elements;
}
}
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
public virtual IEnumerator GetEnumerator()
{
return Elements.GetEnumerator();
}
/// <summary>
/// Returns a string with the content of this object in a readable form. Useful for debugging purposes only.
/// </summary>
public override string ToString()
{
StringBuilder pdf = new StringBuilder();
pdf.Append("[ ");
int count = Elements.Count;
for (int idx = 0; idx < count; idx++)
pdf.Append(Elements[idx].ToString() + " ");
pdf.Append("]");
return pdf.ToString();
}
internal override void WriteObject(PdfWriter writer)
{
writer.WriteBeginObject(this);
int count = Elements.Count;
for (int idx = 0; idx < count; idx++)
{
PdfItem value = Elements[idx];
value.WriteObject(writer);
}
writer.WriteEndObject();
}
/// <summary>
/// Represents the elements of an PdfArray.
/// </summary>
public sealed class ArrayElements : IList, ICloneable
{
ArrayList elements;
PdfArray owner;
internal ArrayElements(PdfArray array)
{
this.elements = new ArrayList();
this.owner = array;
}
object ICloneable.Clone()
{
ArrayElements elements = (ArrayElements)MemberwiseClone();
elements.elements = (ArrayList)elements.elements.Clone();
elements.owner = null;
return elements;
}
/// <summary>
/// Creates a shallow copy of this object.
/// </summary>
public ArrayElements Clone()
{
return (ArrayElements)((ICloneable)this).Clone();
}
/// <summary>
/// Moves this instance to another dictionary during object type transformation.
/// </summary>
internal void SetOwner(PdfArray array)
{
this.owner = array;
array.elements = this;
}
/// <summary>
/// Converts the specified value to boolean.
/// If the value not exists, the function returns false.
/// If the value is not convertible, the function throws an InvalidCastException.
/// If the index is out ouf range, the function throws an ArgumentOutOfRangeException.
/// </summary>
public bool GetBoolean(int index)
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException("index", index, PSSR.IndexOutOfRange);
object obj = this[index];
if (obj == null)
return false;
if (obj is PdfBoolean)
return ((PdfBoolean)obj).Value;
else if (obj is PdfBooleanObject)
return ((PdfBooleanObject)obj).Value;
throw new InvalidCastException("GetBoolean: Object is not a boolean.");
}
/// <summary>
/// Converts the specified value to integer.
/// If the value not exists, the function returns 0.
/// If the value is not convertible, the function throws an InvalidCastException.
/// If the index is out ouf range, the function throws an ArgumentOutOfRangeException.
/// </summary>
public int GetInteger(int index)
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException("index", index, PSSR.IndexOutOfRange);
object obj = this[index];
if (obj == null)
return 0;
if (obj is PdfInteger)
return ((PdfInteger)obj).Value;
if (obj is PdfIntegerObject)
return ((PdfIntegerObject)obj).Value;
throw new InvalidCastException("GetInteger: Object is not an integer.");
}
/// <summary>
/// Converts the specified value to double.
/// If the value not exists, the function returns 0.
/// If the value is not convertible, the function throws an InvalidCastException.
/// If the index is out ouf range, the function throws an ArgumentOutOfRangeException.
/// </summary>
public double GetReal(int index)
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException("index", index, PSSR.IndexOutOfRange);
object obj = this[index];
if (obj == null)
return 0;
if (obj is PdfReal)
return ((PdfReal)obj).Value;
else if (obj is PdfRealObject)
return ((PdfRealObject)obj).Value;
else if (obj is PdfInteger)
return ((PdfInteger)obj).Value;
else if (obj is PdfIntegerObject)
return ((PdfIntegerObject)obj).Value;
throw new InvalidCastException("GetReal: Object is not a number.");
}
/// <summary>
/// Converts the specified value to string.
/// If the value not exists, the function returns the empty string.
/// If the value is not convertible, the function throws an InvalidCastException.
/// If the index is out ouf range, the function throws an ArgumentOutOfRangeException.
/// </summary>
public string GetString(int index)
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException("index", index, PSSR.IndexOutOfRange);
object obj = this[index];
if (obj == null)
return "";
if (obj is PdfString)
return ((PdfString)obj).Value;
if (obj is PdfStringObject)
return ((PdfStringObject)obj).Value;
throw new InvalidCastException("GetString: Object is not an integer.");
}
/// <summary>
/// Converts the specified value to a name.
/// If the value not exists, the function returns the empty string.
/// If the value is not convertible, the function throws an InvalidCastException.
/// If the index is out ouf range, the function throws an ArgumentOutOfRangeException.
/// </summary>
public string GetName(int index)
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException("index", index, PSSR.IndexOutOfRange);
object obj = this[index];
if (obj == null)
return "";
if (obj is PdfName)
return ((PdfName)obj).Value;
if (obj is PdfNameObject)
return ((PdfNameObject)obj).Value;
throw new InvalidCastException("GetName: Object is not an integer.");
}
/// <summary>
/// Returns the indirect object if the value at the specified index is a PdfReference.
/// </summary>
[Obsolete("Use GetObject, GetDictionary, GetArray, or GetReference")]
public PdfObject GetIndirectObject(int index)
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException("index", index, PSSR.IndexOutOfRange);
PdfItem item = this[index];
if (item is PdfReference)
return ((PdfReference)item).Value;
return null;
}
/// <summary>
/// Gets the PdfObject with the specified index, or null, if no such object exists. If the index refers to
/// a reference, the referenced PdfObject is returned.
/// </summary>
public PdfObject GetObject(int index)
{
if (index < 0 || index >= Count)
throw new ArgumentOutOfRangeException("index", index, PSSR.IndexOutOfRange);
PdfItem item = this[index];
if (item is PdfReference)
return ((PdfReference)item).Value;
return item as PdfObject;
}
/// <summary>
/// Gets the PdfArray with the specified index, or null, if no such object exists. If the index refers to
/// a reference, the referenced PdfArray is returned.
/// </summary>
public PdfDictionary GetDictionary(int index)
{
return GetObject(index) as PdfDictionary;
}
/// <summary>
/// Gets the PdfArray with the specified index, or null, if no such object exists. If the index refers to
/// a reference, the referenced PdfArray is returned.
/// </summary>
public PdfArray GetArray(int index)
{
return GetObject(index) as PdfArray;
}
/// <summary>
/// Gets the PdfReference with the specified index, or null, if no such object exists.
/// </summary>
public PdfReference GetReference(int index)
{
PdfItem item = this[index];
return item as PdfReference;
}
/// <summary>
/// Gets all items of this array.
/// </summary>
public PdfItem[] Items
{
get { return (PdfItem[])this.elements.ToArray(typeof(PdfItem)); }
}
/// <summary>
/// INTERNAL USE ONLY.
/// </summary>
public ArrayList GetArrayList()
{
// I use this hack to order the pages by ZIP code (MigraDoc ControlCode-Generator)
// TODO: implement a clean solution
return this.elements;
}
#region IList Members
/// <summary>
/// Returns false.
/// </summary>
public bool IsReadOnly
{
get { return false; }
}
object IList.this[int index]
{
get { return this.elements[index]; }
set { this.elements[index] = value; }
}
/// <summary>
/// Gets or sets an item at the specified index.
/// </summary>
/// <value></value>
public PdfItem this[int index]
{
get { return (PdfItem)this.elements[index]; }
set
{
if (value == null)
throw new ArgumentNullException("value");
this.elements[index] = value;
}
}
/// <summary>
/// Removes the item at the specified index.
/// </summary>
public void RemoveAt(int index)
{
this.elements.RemoveAt(index);
}
void IList.Insert(int index, object value)
{
this.elements.Insert(index, value);
}
/// <summary>
/// Inserts the item the specified index.
/// </summary>
public void Insert(int index, PdfItem value)
{
this.elements.Insert(index, value);
}
void IList.Remove(object value)
{
this.elements.Remove(value);
}
/// <summary>
/// Removes the specified value.
/// </summary>
public void Remove(PdfItem value)
{
this.elements.Remove(value);
}
bool IList.Contains(object value)
{
return this.elements.Contains(value);
}
/// <summary>
/// Determines whether the specified value is in the array.
/// </summary>
public bool Contains(PdfItem value)
{
return this.elements.Contains(value);
}
/// <summary>
/// Removes all items from the array.
/// </summary>
public void Clear()
{
this.elements.Clear();
}
int IList.IndexOf(object value)
{
return this.elements.IndexOf(value);
}
/// <summary>
/// Gets the index of the specified item.
/// </summary>
public int IndexOf(PdfItem value)
{
return this.elements.IndexOf(value);
}
int IList.Add(object value)
{
return this.elements.Add(value);
}
/// <summary>
/// Appends the specified object to the array.
/// </summary>
public int Add(PdfItem value)
{
// TODO: ???
//Debug.Assert((value is PdfObject && ((PdfObject)value).Reference == null) | !(value is PdfObject),
// "You try to set an indirect object directly into an array.");
PdfObject obj = value as PdfObject;
if (obj != null && obj.IsIndirect)
return this.elements.Add(obj.Reference);
return this.elements.Add(value);
}
/// <summary>
/// Returns false.
/// </summary>
public bool IsFixedSize
{
get { return false; }
}
#endregion
#region ICollection Members
/// <summary>
/// Returns false.
/// </summary>
public bool IsSynchronized
{
get { return false; }
}
/// <summary>
/// Gets the number of elements in the array.
/// </summary>
public int Count
{
get { return this.elements.Count; }
}
/// <summary>
/// Copies the elements of the array to the specified array.
/// </summary>
public void CopyTo(Array array, int index)
{
this.elements.CopyTo(array, index);
}
/// <summary>
/// The current implementation return null.
/// </summary>
public object SyncRoot
{
get { return null; }
}
#endregion
#region IEnumerable Members
/// <summary>
/// Returns an enumerator that iterates through the array.
/// </summary>
public IEnumerator GetEnumerator()
{
return this.elements.GetEnumerator();
}
#endregion
}
}
}
|