This project has moved and is read-only. For the latest updates, please go here.


InvalidCastException in PdfAnnotations


PDFSharp 1.32
(Bug introduced in 1.31)
Previously reported at:

Attempting to iterate a PdfAnnotations instance with a foreach loop results in an InvalidCastException:

Unable to cast object of type 'AnnotationsIterator' to type 'System.Collections.Generic.IEnumerator`1[PdfSharp.Pdf.PdfItem]'
at PdfSharp.Pdf.Annotations.PdfAnnotations.GetEnumerator()

The private class AnnotationsIterator implements IEnumerator<PdfAnnotation>, but the GetEnumerator method attempts to cast it to IEnumerator<PdfItem>.

This would work if you were targeting 4.0, but in 2.0 generic covariance is not available.

To fix this, the AnnotationsIterator class needs to implement IEnumerator<PdfItem> instead of IEnumerator<PdfAnnotation>. The PdfAnnotations.GetEnumerator method will then work without the cast.


ThomasHoevel wrote Oct 15, 2015 at 5:03 PM

Should be fixed with PDFsharp 1.50 beta 3.