Spire.PDFViewer is a powerful PDF Viewer component for .NET. It allows developers to load PDF document from stream, file and byte array.

Tue May 12, 2015 10:52 am

Hi
I am testing free trial version of Spire.PdfViewer.Wpf. The question is how to get actual x,y coordinate regardless the zoom or the size (similar to pointF=page.FindText(“Text”)) at MouseUp event.
Thanks
Rami

RamiKahalon
 
Posts: 1
Joined: Sun May 10, 2015 9:51 am

Wed May 13, 2015 6:56 am

Hello,

Thanks for your inquiry.
Sorry that our product doesn't support this feature at this stage.

Best Regards,
Sweety
E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Thu Nov 19, 2015 5:36 pm

Could you suggest a work-around? I find myself in need of the same feature.

I attempted the following solution and the results were not optimal.

Convert the desired page of the PDF to a TIFF

Code: Select all
private void ConvertPDFtoImage(string FullFilePath)
{
    //pdf file
    String file = FullFilePath;
    strSourcefile = file;

    //open pdf document
    PdfDocument doc = new PdfDocument();
    doc.LoadFromFile(file);

    ////save to images
    for (int i = 0; i < doc.Pages.Count; i++)
    {
   String fileName = String.Format("C:\\Temp\\Converted-{0}.tif", i);
   using (Image image = doc.SaveAsImage(i,PdfImageType.Bitmap,300,300))
   {
       image.Save(fileName, System.Drawing.Imaging.ImageFormat.Tiff);
   }
    }
    doc.Close();
    doc.Dispose();
}


Load the TIFF into a pictureBox and place this code in the double-click event:

Code: Select all
        private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Console.WriteLine("X: " + e.Location.X.ToString() + " Y: " + e.Location.Y.ToString());
        }


The coordinates did not line up exactly with the PDF when I tried using the returned X,Y for placing drawn objects on the original PDF page - which is the desired end result.

Any guidance on this issue would be greatly appreciated.

Thank you.

MatthewPierce
 
Posts: 22
Joined: Thu Jul 16, 2015 4:45 pm

Fri Nov 20, 2015 4:02 am

Hi,

Thanks for your inquiry.
Is your issue that the coordinates data of the object in pictureBox1 is not equal to the coordinates data of the same object?
If so, please try to remove this.pictureBox1's location.
Code: Select all
private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int x = e.Location.X - this.pictureBox1.Location.X;
            int y = e.Location.Y - this.pictureBox1.Location.Y;
            Console.WriteLine("X: " + x.ToString() + " Y: " + y.ToString());
        }

If the issue still exists, please provide us with your pdf document and the coordinates data you got(includes in pictureBox1 and in PDF).

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Fri Nov 20, 2015 3:55 pm

Thank you for your prompt response.

I implemented the code that you sent, but it did not make an appreciable difference in the result. I'm not sure if it has any bearing, but the picturebox is situated inside a panel.

Code: Select all
// panel1
            //
            this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.panel1.AutoScroll = true;
            this.panel1.Controls.Add(this.pictureBox2);
            this.panel1.Location = new System.Drawing.Point(577, 68);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(413, 565);
            this.panel1.TabIndex = 5;
            //
            // pictureBox2
            //
            this.pictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pictureBox2.Cursor = System.Windows.Forms.Cursors.Cross;
            this.pictureBox2.Location = new System.Drawing.Point(3, 3);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new System.Drawing.Size(368, 523);
            this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.pictureBox2.TabIndex = 0;
            this.pictureBox2.TabStop = false;
            this.pictureBox2.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseDoubleClick);
            this.pictureBox2.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseMove);


I have attached two PDF files that show the result of the code below.

"Output_x12y12 - x9y9.pdf" reports a location of x12 y12 in the picturebox, and when your code is applied the line origin location is set to x9 y9 on the pdf. The same applies for Output_x100y100 - x97y97.pdf.

Code: Select all
        private void pictureBox2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int x = e.Location.X - this.pictureBox2.Location.X;
            int y = e.Location.Y - this.pictureBox2.Location.Y;

            string outFilename = @"C:\\Workspace\\Output" + ".pdf";

            DrawLines(strSourcefile, outFilename, x, y);
        }
       
        private void DrawLines(string strFileIn, string strOutFile, int LineX, int LineY)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(strFileIn);
            PdfPageBase page0 = doc.Pages[0];
            PdfMargins margin = doc.PageSettings.Margins;

            page0.Canvas.DrawLine(new PdfPen(PdfBrushes.Red, 1), new PointF(LineX, LineY), new PointF(LineX, LineY + 30));
            page0.Canvas.DrawLine(new PdfPen(PdfBrushes.Red, 1), new PointF(LineX, LineY), new PointF(LineX + 30, LineY));
           
            doc.SaveToFile(strOutFile, FileFormat.PDF);
            doc.Dispose();
            System.Diagnostics.Process.Start(strOutFile);
        }


As an aside, once the placement of the drawn lines are accurate with respect to the double-click in the picturebox, I will need to implement methods that allow the user to zoom in and out of the image and still make an accurate placement of the drawn lines.

MatthewPierce
 
Posts: 22
Joined: Thu Jul 16, 2015 4:45 pm

Mon Nov 23, 2015 7:47 am

Hi,

Thanks for your feedback.
Please do corresponding proportion conversion for the location depending on the page size and image size.
Sample code:
Code: Select all
  private float m_ImageHeight = 0;
        private float m_ImageWidth = 0;
        private float m_PageHeight = 0;
        private float m_PageWidth = 0;

        PdfDocument pdf = new PdfDocument();
        pdf.LoadFromFile(strSourcefile);
        this.m_PageHeight = pdf.Pages[0].ActualSize.Height;
        this.m_PageWidth = pdf.Pages[0].ActualSize.Width;

        pdf.SaveAsImage(0, PdfImageType.Bitmap,300,300).Save(imagefile, ImageFormat.Tiff);
        Image loImage = Image.FromFile(imagefile);
        this.m_ImageHeight = loImage.Height;
        this.m_ImageWidth = loImage.Width;

      private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int x = e.Location.X ;
            int y = e.Location.Y;
            this.textBox1.Text = "X: " + x.ToString() + " Y: " + y.ToString();
           
           
            float lfX = x * m_PageWidth / m_ImageWidth;
            float lfY = y * m_PageHeight / m_ImageHeight;
            DrawLines(strSourcefile, outFilename, lfX, lfY);
        }


Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Mon Nov 23, 2015 8:31 am

Amy,

You have absolutely made my entire week and it is just 3:30am on a Monday. This works perfectly.

Thank you very, very much.

~Matthew

MatthewPierce
 
Posts: 22
Joined: Thu Jul 16, 2015 4:45 pm

Mon Nov 23, 2015 8:47 am

Hi Matthew,

Thanks for your reply.
Welcome to write to us again for further problems.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Tue Nov 24, 2015 9:54 pm

Just to round this out, if anyone is following this and needs to resolve for zooming the preview image, this is the code that I am using and it works quite well.
Code: Select all
        public  Image ScaleImage(Image imageSource, decimal decPercentage)
        {
            int intNewWidth = Convert.ToInt32(imageSource.Width * decPercentage);
            int intNewHeight = Convert.ToInt32(imageSource.Height * decPercentage);

            Image newImage = new Bitmap(intNewWidth, intNewHeight);

            using (Graphics graphics = Graphics.FromImage(newImage))
                graphics.DrawImage(imageSource, 0, 0, intNewWidth, intNewHeight);

            m_ImageHeight = intNewHeight;
            m_ImageWidth = intNewWidth;
           
            return newImage;
        }


After resizing the double-click event is still dead-on accurate.

MatthewPierce
 
Posts: 22
Joined: Thu Jul 16, 2015 4:45 pm

Wed Nov 25, 2015 1:28 am

Hi Matthew,

Thanks for sharing. We believe that your code can help others.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.PDFViewer