NEWS

[DEVEXPRESS] Hướng dẫn cách chèn watermask Image vào file PDF C# bởi vị trí click chuột

[DEVEXPRESS] Hướng dẫn cách chèn watermask Image vào file PDF C# bởi vị trí click chuột
Đăng bởi: Thảo Meo - Lượt xem: 5570 21:24:49, 21/05/2020EBOOK

Xin chào các bạn bài viết hôm nay, mình sẽ tiếp tục hướng dẫn các bạn cách chèn hình ảnh watermask vào File PDF trong lập trình C# winform.

[DEVEXPRESS] Insert image watermask into Pdf File C# by Position Mouse Click

Trong ứng dụng demo này, khi mình mở một file pdf lên, và hover con trỏ chuột vào vị trí nào của văn bản, và chúng ta click chuột thì sẽ add Image watermask vào vị trí đó.

Các bạn có thể sử dụng ứng dụng này, để chèn vào file pdf mẫu của các bạn một cách dễ dàng.

Dưới đây là video demo ứng dụng chèn WaterMask vào file Pdf C#:

Full source code C#:

using DevExpress.Pdf;
using DevExpress.Pdf.Interop;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecognizeClickPositionOnPdf
{
	public partial class PdfViewer : Form
	{
		private string _documentPath;

		public PdfViewer()
		{
			InitializeComponent();
		}

		public void Init(string documentPath)
		{
			_documentPath = documentPath;
			pdfViewer1.LoadDocument(_documentPath);

            var bitmap = (Bitmap)GetImageWaterMask();
           // bitmap.SetResolution(72, 72);
            this.Cursor = CreateCursor(bitmap, new Size(bitmap.Width, bitmap.Height));
            pdfViewer1.CursorMode = DevExpress.XtraPdfViewer.PdfCursorMode.Custom;
        }

      
          

        private static int _counter = 0;

        public Image GetImageWaterMask()
        {
            return SetImageOpacity(Image.FromFile(@"con-dau-doc-hai.png"), 0.3f);
        }

		private void pdfViewer1_MouseClick(object sender, MouseEventArgs e)
		{
			if (!pdfViewer1.IsDocumentOpened)
			{
                Console.WriteLine("---------- No document loaded ----------");
				return;
			}
			var hitPoint = pdfViewer1.GetDocumentPosition(e.Location, false);
			if (hitPoint != null)
			{
				var currentPageNumber = hitPoint.PageNumber - 1;

				using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
				{
					processor.LoadDocument(_documentPath);

					var page = processor.Document.Pages[currentPageNumber];

                    Console.WriteLine("---------- {0} - {1} ----------", hitPoint.Point.X, hitPoint.Point.Y);

                    Console.WriteLine(string.Empty);

                    var image = GetImageWaterMask();

                    var graph = processor.CreateGraphics();
                     //var r = new RectangleF((float)hitPoint.Point.X - (image.Width/2) , (float)page.CropBox.Top - (float)hitPoint.Point.Y - (image.Height / 2), image.Width, image.Height);
var width = image.Width / image.HorizontalResolution * 62f;
                    var height = image.Height / image.VerticalResolution * 62f;
                   var r = new RectangleF((float)hitPoint.Point.X - (width/2) , (float)page.CropBox.Top - (float)hitPoint.Point.Y - (height / 2),width , height);
                    

                    graph.DrawImage(image, r);
					graph.AddToPageForeground(page, 72, 72);
					//graph.AddToPageForeground(page);
                    var filenmae = string.Format(@"test{0}.pdf", _counter);
					processor.SaveDocument(filenmae);
					pdfViewer1.LoadDocument(filenmae);
					++_counter;
				}
			}
			else
			{
				Console.WriteLine("---------- Outside document bounds ----------");
				return;
			}
		}

        public Cursor CreateCursor(Bitmap bitmap, Size size)
        {
            bitmap = new Bitmap(bitmap, size);
            return new Cursor(bitmap.GetHicon());
        }



        public Image SetImageOpacity(Image image, float opacity)
        {
            try
            {
                //create a Bitmap the size of the image provided  
                Bitmap bmp = new Bitmap(image.Width, image.Height);

                //create a graphics object from the image  
                using (Graphics gfx = Graphics.FromImage(bmp))
                {

                    //create a color matrix object  
                    ColorMatrix matrix = new ColorMatrix();

                    //set the opacity  
                    matrix.Matrix33 = opacity;

                    //create image attributes  
                    ImageAttributes attributes = new ImageAttributes();

                    //set the color(opacity) of the image  
                    attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

                    //now draw the image  
                    gfx.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
                }
                return bmp;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }
    }
}

Ở source code, mình đã cập nhật update, fixed lỗi hình lớn hơn khi đóng, vào đóng 1 lần đóng tất cả các trang.

Khi chuột move ra khỏi page pdf thì set cursor về default.

Thanks for watching!

DOWNLOAD SOURCE

 

Tags: watermask pdf c#insert watermask pdf c# winform

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Hướng dẫn cách chèn watermask Image vào file PDF C# bởi vị trí click chuột
Đăng bởi: Thảo Meo - Lượt xem: 5570 21:24:49, 21/05/2020EBOOK