2011-11-26 25 views
1

我正在尋找最合適的方式來允許用戶將圖像導入到PDF並允許它們拖動PDF/winform圖片並指定放置圖像的位置。使用iTextSharp跟蹤cusor位置並將圖像從cusor位置添加到PDF中

我在想這樣做的最好方法是從位置拉出位置。

是這樣的:

Rectangle rect = new Rectangle(400, 772, 545, 792); 

代替預先定義的座標,具有輸出是用戶的選擇光標位置。

任何幫助將非常感激。

預先感謝您!

回答

0

用戶可能很難通過在表單上按下鼠標光標來選擇圖像位置。相反,我建議允許他們圍繞網格拖動相對大小的矩形。您必須適當地轉換座標,包括修復Y值,因爲iTextSharp從左下角開始,而不是左上角,但這不應太難。

下面是一個完整的C#2010 WinForms應用程序,它允許您在黑色方框周圍拖動紅色矩形。代碼中的註釋幾乎可以解釋所有事情。它有一個需要解決的重大問題,它存儲的X/Y座標是基於屏幕的,而不是基於表單的,所以如果拖動一次,移動整個表單並再次拖動它會變得「怪異」。這可以通過計算相對於窗體的x/y來解決,而不是我將留給你的。

希望這會讓你走上適合你的道路!

using System; 
using System.ComponentModel; 
using System.Drawing; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     //Basic dimensions of our "border" and our "image". Assumes 72 dots per inch which isn't technically correct but gives us something to work with 
     private int BORDER_WIDTH = (int)11 * 72; 
     private int BORDER_HEIGHT = (int)8.5 * 72; 
     private int IMAGE_WIDTH = (int)2 * 72; 
     private int IMAGE_HEIGHT = (int)3 * 72; 
     private int IMAGE_OFFSET = 5; 

     //These will store the x/y when we press our mouse down so that we can calculate the drag later 
     private int offsetX; 
     private int offsetY; 

     //Our main "image" that we'll move around 
     PictureBox pb; 
     //The "border" to move the image around in 
     PictureBox border; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 
     private void Form1_Load(object sender, EventArgs e) 
     { 
      //Resize the form and make it not user-resizable 
      this.Size = new Size(BORDER_WIDTH + 30, BORDER_HEIGHT + 50); 
      this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; 

      //Create our "image" 
      pb = new PictureBox(); 
      pb.Size = new Size(IMAGE_WIDTH, IMAGE_HEIGHT); 
      pb.Location = new Point(IMAGE_OFFSET, IMAGE_OFFSET); 
      pb.BackColor = Color.Red; 
      //Bind a handler to the mouse down event 
      pb.MouseDown += new MouseEventHandler(this.pbMouseDown); 
      this.Controls.Add(pb); 

      //Create our "border" 
      border = new PictureBox(); 
      border.Size = new Size(BORDER_WIDTH, BORDER_HEIGHT); 
      border.Location = new Point(IMAGE_OFFSET, IMAGE_OFFSET); 
      border.BackColor = Color.Black; 
      this.Controls.Add(border); 
     } 
     private void pbMouseDown(object sender, MouseEventArgs e) 
     { 
      //Store the current x/y so that we can use them in calculations later 
      offsetX = e.X; 
      offsetY = e.Y; 
      //When the mouse is down we want to remove the original mouse down handler 
      pb.MouseDown -= new MouseEventHandler(this.pbMouseDown); 
      //Add to more handler looking for mouse up and mouse movement 
      pb.MouseUp += new MouseEventHandler(this.pbMouseUp); 
      pb.MouseMove += new MouseEventHandler(this.pbMouseMove); 
     } 
     private void pbMouseUp(object sender, MouseEventArgs e) 
     { 
      //When the mouse button is released, remove old handlers and add back the down event 
      pb.MouseMove -= new MouseEventHandler(this.pbMouseMove); 
      pb.MouseUp -= new MouseEventHandler(this.pbMouseUp); 
      pb.MouseDown += new MouseEventHandler(this.pbMouseDown); 
      //Pop up a message, this is where something with iTextSharp would be done 
      MessageBox.Show(String.Format("The top left of the image is at {0}x{1}", pb.Top - IMAGE_OFFSET, pb.Left - IMAGE_OFFSET)); 
     } 
     private void pbMouseMove(object sender, MouseEventArgs e) 
     { 
      //Calculate where to draw the "image" at next 
      //First, calculate based on the current image's location, the offset that we stored ealier and the current mouse position 
      int newLeft = e.X + pb.Left - offsetX; 
      int newTop = e.Y + pb.Top - offsetY; 
      //Next, make sure that we haven't gone over one of the boundaries of the "border" 
      if (newLeft < border.Left) newLeft = border.Left; 
      if (newTop < border.Top) newTop = border.Top; 
      if (newLeft + pb.Width > border.Right) newLeft = border.Right - pb.Width; 
      if (newTop + pb.Height > border.Bottom) newTop = border.Bottom - pb.Height; 
      //Finally, assign the new value 
      pb.Left = newLeft; 
      pb.Top = newTop; 
     } 
    } 
} 
+0

非常感謝您的先生,您的幫助非常感謝! – Beak