I am adding a PDF annotation with appearance stream via iText7 below, where it suppose to draw an ink within the canvas on top left rectangle, but it was shown and located at lower left bottom, apparently it is doesn’t fit correctly into the canvas rectangle frame.
How am I suppose to change it’s behavior to always start within the frame rather than lower-left bottom? Or did I do it in the wrong matter?
private PdfAnnotation CreateAnnotation(PdfPage page)
{
var rect = new Rectangle((float)1.63, (float)762.69, (float)28.25, (float)33.5);
var pdfAnnotation = PdfAnnotation.MakeAnnotation(new PdfDictionary());
pdfAnnotation.SetRectangle(new PdfArray(rect));
pdfAnnotation.Put(PdfName.Contents, new PdfString("Text"));
pdfAnnotation.Put(PdfName.Title, new PdfString("Title"));
pdfAnnotation.Put(PdfName.Type, PdfName.Annot);
pdfAnnotation.Put(PdfName.Subtype, new PdfName("Ink"));
pdfAnnotation.Put(new PdfName("Subj"), new PdfString("Pencil"));
pdfAnnotation.Put(PdfName.CreationDate, new PdfDate(DateTime.Now).GetPdfObject());
pdfAnnotation.Put(PdfName.M, new PdfDate(DateTime.Now).GetPdfObject());
pdfAnnotation.Put(PdfName.F, new PdfNumber(PdfAnnotation.PRINT));
pdfAnnotation.Put(PdfName.CA, new PdfNumber(100));
var dict = new PdfDictionary();
dict.Put(PdfName.S, new PdfName(""));
pdfAnnotation.Put(PdfName.BS, dict);
pdfAnnotation.Put(PdfName.RC, new PdfString("Rich text"));
var inkListArray = new PdfArray(new double[] { 20.25, 29.13, 20.38, 29.13, 21.13, 29.01, 23.63, 27.88, 30.13, 24.88, 35.38, 22.26, 40.25, 19.51, 44, 17.13, 46.38, 15.26, 47.75, 14.63, 48, 13.63, 48, 13.38 });
pdfAnnotation.SetColor(new DeviceRgb(255, 0, 0));
pdfAnnotation.Put(PdfName.InkList, inkListArray);
pdfAnnotation.Put(PdfName.BorderThickness, new PdfNumber(5));
var xObj = new PdfFormXObject(rect);
var canvas = new PdfCanvas(page);
canvas.Rectangle(rect);
var pdfAnnotationAppearance = new PdfAnnotationAppearance();
pdfAnnotationAppearance.SetState(PdfName.Normal, xObj);
canvas.AddXObjectFittedIntoRectangle(xObj, rect);
// set line width, stroke and fill colours
var black = DeviceRgb.BLACK;
canvas.SetLineWidth(2);
canvas.SetStrokeColor(black);
// stroke path
canvas.Stroke();
// release canvas
canvas.Release();
// set the normal appearance
pdfAnnotation.SetNormalAppearance(pdfAnnotationAppearance);
return pdfAnnotation;
}