PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.de/

Create pdf highlight annotation
http://forum.pdfsharp.de/viewtopic.php?f=2&t=4466
Page 1 of 1

Author:  logiarun [ Fri Aug 18, 2023 3:52 pm ]
Post subject:  Create pdf highlight annotation

How to create pdf highlight annotation using pdfsharp

I have using the below code to create highlight annotation

Annotation created in pdf, but not displayed in Pdf page.

using PdfSharp.Pdf.Annotations;
using PdfSharp.Pdf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using PdfSharp.Drawing;

namespace PDFAnnotationExtension
{

public class PdfHighlightAnnotation : PdfMarkupAnnotation
{
public PdfHighlightAnnotation()
{
Initialize();
}

public PdfHighlightAnnotation(PdfDocument document)
: base(document)
{
Initialize();
}

void Initialize()
{
Elements.SetName(Keys.Subtype, "/Highlight");
}

}

public class PdfStrikeOutAnnotation : PdfMarkupAnnotation
{
public PdfStrikeOutAnnotation()
{
Initialize();
}

public PdfStrikeOutAnnotation(PdfDocument document)
: base(document)
{
Initialize();
}

void Initialize()
{
Elements.SetName(Keys.Subtype, "/StrikeOut");
}
}

public abstract class PdfMarkupAnnotation : PdfAnnotation
{
protected PdfMarkupAnnotation()
{ }

protected PdfMarkupAnnotation(PdfDocument document)
: base(document)
{ }

public IEnumerable<PdfRectangle> Quadrilaterals
{
set
{
var points = new PdfArray();
foreach (var r in value)
{
points.Elements.AddRange(ToQuadPoints(r));
}
Elements.SetValue("/QuadPoints", points);
}
}

private IEnumerable<PdfItem> ToQuadPoints(PdfRectangle r)
{
// Conversion from PdfRectangle coordinates
//
// Y ^
// | (X2 Y2)
// | +-----------+
// | | |
// | | |
// | +-----------+
// | (X1 Y1)
// |
// +----------------------------->
// X
// to QuadPoints coordinates (x1 y1 x2 y2 x3 y3 x4 y4)
//
// Y ^
// | (x4 y4) (x3 y3)
// | +-----------+
// | | |
// | | |
// | +-----------+
// | (x1 y1) (x2 y2)
// |
// +----------------------------->
// X
//
return new List<PdfItem> { new PdfReal(r.X1), new PdfReal(r.Y1),
new PdfReal(r.X2), new PdfReal(r.Y1),
new PdfReal(r.X1), new PdfReal(r.Y2),
new PdfReal(r.X2), new PdfReal(r.Y2)};
}

}

public static class ArrayElementsExtensions
{
public static void AddRange(this PdfArray.ArrayElements elements, IEnumerable<PdfItem> values)
{
foreach (var v in values)
{
elements.Add(v);
}
}
}
}

Author:  coragi [ Thu Feb 15, 2024 11:36 am ]
Post subject:  Re: Create pdf highlight annotation

Hi,

Did you find a solution to your problem?
I would also like to use QuadPoints with LinkAnnotation.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/