diff --git a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.AcroForms/PdfAcroField.cs b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.AcroForms/PdfAcroField.cs index f69b1cc8..f4c0a644 100644 --- a/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.AcroForms/PdfAcroField.cs +++ b/src/foundation/src/PDFsharp/src/PdfSharp/Pdf.AcroForms/PdfAcroField.cs @@ -2,6 +2,7 @@ // See the LICENSE file in the solution root for more information. using PdfSharp.Pdf.Advanced; +using System.Collections; namespace PdfSharp.Pdf.AcroForms { @@ -261,10 +262,10 @@ public PdfAcroFieldCollection Fields } PdfAcroFieldCollection? _fields; - /// - /// Holds a collection of interactive fields. - /// - public sealed class PdfAcroFieldCollection : PdfArray + /// + /// Holds a collection of interactive fields. + /// + public sealed class PdfAcroFieldCollection : PdfArray, IEnumerable { internal PdfAcroFieldCollection(PdfArray array) : base(array) @@ -366,12 +367,12 @@ public PdfAcroField this[int index] return null; } - /// - /// Create a derived type like PdfTextField or PdfCheckBox if possible. - /// If the actual cannot be guessed by PDFsharp the function returns an instance - /// of PdfGenericField. - /// - PdfAcroField CreateAcroField(PdfDictionary dict) + /// + /// Create a derived type like PdfTextField or PdfCheckBox if possible. + /// If the actual cannot be guessed by PDFsharp the function returns an instance + /// of PdfGenericField. + /// + PdfAcroField CreateAcroField(PdfDictionary dict) { string ft = dict.Elements.GetName(Keys.FT); PdfAcroFieldFlags flags = (PdfAcroFieldFlags)dict.Elements.GetInteger(Keys.Ff); @@ -402,7 +403,65 @@ PdfAcroField CreateAcroField(PdfDictionary dict) return new PdfGenericField(dict); } } - } + + public new PdfAcroFieldEnumerator GetEnumerator() + { + return new PdfAcroFieldEnumerator(this); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return new PdfAcroFieldEnumerator(this); + } + + /// + /// Enumerates the elements of a . + /// + public struct PdfAcroFieldEnumerator : IEnumerator, IEnumerator, IDisposable + { + private readonly PdfAcroFieldCollection _collection; + private int _currentIndex; + private PdfAcroField? _currentItem; + + internal PdfAcroFieldEnumerator(PdfAcroFieldCollection fieldCollection) + { + _collection = fieldCollection; + _currentIndex = -1; + _currentItem = default; + } + + public readonly PdfAcroField Current => _currentItem!; + + readonly Object IEnumerator.Current => Current; + + public readonly void Dispose() { } + + public Boolean MoveNext() + { + if (++_currentIndex >= _collection.Count) + { + return false; + } + else + { + // Gettig the current item using the index will convert + // the PdfItem to PdfAcroField for us + _currentItem = _collection[_currentIndex]; + } + return true; + } + + public void Reset() + { + _currentIndex = -1; + } + } + } /// /// Predefined keys of this dictionary.