I'm trying to incorporate drawn annotations to a page. To do that, I'm putting a canvas object, letting the user do the drawing on the canvas, and then transfering the drawing contents on to the PDF file. The action I describe above works fine. However, when I try to transfer the drawings to the PDF that is being viewed on-screen, I'm having trouble putting them in the visually correct location, especially if the PDF is zoomed in.
For instance, at regular zoom, I can compute an X-offset value, and then compute the correct Y-value and then put the annotation in the expected place. However, when the PDF viewer is zoomed in, the drawing annotation appears off to the side and to the bottom. I tried incorporating zoom factors into the calculations, and could not get it right under zooming conditions. It looks like if I could get a reference to the top/bottom/any location on the zoomed view, I'd be able to derive the offset from there but I could not figure it out.
On a separate note, I could not get ZoomTo() method to work programmatically for a PDF loaded into the viewer.
I have a function that does the drawing, and it is pasted below. Can you please let me know whether I'm on the correct path, and if there's something that I'm missing re: zoomed PDF object? If nothing obvious, I can package the contents in a separate project and send over.
private void DrawLineOnPDF()
{
//draw the line on the PDF loaded in the memory, based on the lines drawn on the canvas
PdfPen redPen = new PdfPen(PdfBrushes.Red, m_CurrentDrawingSize);
//get the scaling factor to convert position on page to position on the PDF page
double pageWidth = m_loadedDocument.Pages[currentPageIndex].Size.Width;
double pageHeight = m_loadedDocument.Pages[currentPageIndex].Size.Height;
float pixelPageHeight = m_converter.ConvertToPixels((float)pageHeight, PdfGraphicsUnit.Point);
float pixelPageWidth = m_converter.ConvertToPixels((float)pageWidth, PdfGraphicsUnit.Point);
//M_ZOOMFACTOR IS DERIVED FROM pdfViewer.Zoom value, an integer
float zoomConversionFactor = m_zoomFactor / 100.0f;
//trying the annotations approach
List annotInkPoints = new List();
//THE FOLLOWING CALCULATION FOR OFFSETPIXELSX WORKS WHEN ZOOM IS AT 100, FAILS IF ZOOM INCREASES
float OffsetPixelsX = (float)syncFusionPDFViewer.ActualWidth - (pixelPageWidth * zoomConversionFactor);
OffsetPixelsX = OffsetPixelsX / 2.0f;
//SIMILARLY, OFFSETPIXELSY WORKS WHEN ZOOM IS AT 100, FAILS AT HIGHER ZOOMS
float OffsetPixelsY = (pixelPageHeight * zoomConversionFactor) - pixelPageHeight;
//ViewerPointsList is a list of custom ViewerPoint structure which has 4 double values to represent coordinates
foreach (ViewerPoint curPoint in ViewerPointsList)
{
float startX, startY, finishX, finishY;
startX = m_converter.ConvertFromPixels((float)curPoint.X1 - OffsetPixelsX, PdfGraphicsUnit.Point);
startY = m_converter.ConvertFromPixels(pixelPageHeight - (float)curPoint.Y1 - OffsetPixelsY, PdfGraphicsUnit.Point);
finishX = m_converter.ConvertFromPixels((float)curPoint.X2 - OffsetPixelsX, PdfGraphicsUnit.Point);
finishY = m_converter.ConvertFromPixels(pixelPageHeight - (float)curPoint.Y2 - OffsetPixelsY, PdfGraphicsUnit.Point);
annotInkPoints.Add(startX);
annotInkPoints.Add(startY);
annotInkPoints.Add(finishX);
annotInkPoints.Add(finishY);
}
System.Drawing.RectangleF rect = new System.Drawing.RectangleF(0.0f, 0.0f, (float)pageWidth, (float)pageHeight);
Syncfusion.Pdf.Interactive.PdfInkAnnotation annot = new Syncfusion.Pdf.Interactive.PdfInkAnnotation(rect, annotInkPoints);
annot.Color = new PdfColor(redPen.Color.R, redPen.Color.G, redPen.Color.B);
annot.BorderWidth = 100;
annot.Size = new System.Drawing.SizeF(100f, 100f);
System.Drawing.PointF locationF = new System.Drawing.PointF(0.0f, 0.0f);
annot.Location = locationF;
m_loadedDocument.Pages[currentPageIndex].Annotations.Add(annot);
//clear the canvas object
inkCanvas.Children.Clear();
//finally, clear the points list
ViewerPointsList.Clear();
//load the document back into the viewer and refresh the view on screen
syncFusionPDFViewer.LoadDocument(m_loadedDocument);
//THE FOLLOWING IS THE ZOOMING FUNCTIONALITY THAT DOES NOT WORK VISUALLY UPON RE-LOADING THE DOCUMENT
syncFusionPDFViewer.ZoomTo(m_zoomFactor);
}