Some annotation features offered by AnnotationUI objects may be incompatible
with particular image formats. A translucent RectangleAnnotation embedded in a
PDF, for instance, renders as an opaque annotation in most PDF viewing
applications.
We can sometimes get around limitations like this by substituting
incompatible annotations with annotations that support more of the desired
features. The code below demonstrates how to convert all the translucent
RectangleAnnotations in a LayerCollection into a suitable PdfMarkupAnnotation
with similar properties.
public static AnnotationDataCollection
ConvertForPdfExport(LayerCollection
origLayers)
{
AnnotationDataCollection annotationData = new
AnnotationDataCollection();
foreach (LayerAnnotation layer in
origLayers)
{
LayerAnnotation convertedLayer = new
LayerAnnotation();
foreach (AnnotationUI anno in
layer.Items)
{
if (anno is
RectangleAnnotation)
{
RectangleAnnotation
rectangleAnno = anno as RectangleAnnotation;
if
(rectangleAnno != null &&
rectangleAnno.Translucent)
{
PdfMarkupAnnotation
replacementAnno = new PdfMarkupAnnotation(null,
PdfMarkupType.Highlight);
replacementAnno.Location = new
PointF(0,0);
replacementAnno.Size =
rectangleAnno.Size;
replacementAnno.Fill =
rectangleAnno.Fill;
replacementAnno.Quadrilaterals.Add(BoundingQuad(rectangleAnno));
convertedLayer.Items.Add(replacementAnno);
}
else
{
convertedLayer.Items.Add(anno);
}
}
}
annotationData.Add(convertedLayer.Data);
}
return
annotationData;
}
private static QuadrilateralF
BoundingQuad(AnnotationUI anno)
{
PointF ul = new
PointF(anno.Bounds.X, anno.Bounds.Y);
PointF ur = new
PointF(anno.Bounds.X + anno.Bounds.Width, anno.Bounds.Y);
PointF ll = new
PointF(anno.Bounds.X, anno.Bounds.Y - anno.Bounds.Height);
PointF lr =
new PointF(anno.Bounds.X + anno.Bounds.Width,
anno.Bounds.Y -
anno.Bounds.Height);
QuadrilateralF quad = new QuadrilateralF(ll, lr,
ul, ur);
return quad;
}
The AnnotationDataCollection returned by the ConvertForPdfExport method can
be used as an argument to a PdfAnnotationDataExporter's ExportOver method, which
will embed the annotations into an existing PDF. Now any annotations that
started out as RectangleAnnotations with transparency will render with
transparency in the PDF itself.
Take a look at this other helpful article on getting
transparent annotations to render with transparency. The approach described
there involves burning the annotation to the actual image, so the pixels to
render the annotation become an inseparable part of the image. When working with
formats that don't support embedded annotations burning may be a good
option.
Original Article:
Q10334 - HOWTO: Transparent Highlight Annotations in PDF