- [C#] Hướng dẫn fix lỗi Visual Studio 2022 not Support Target Net Framework 4.5.2
- [C#] Giới thiệu thư viện Sunny UI thiết kế giao diện trên Winform
- [DATABASE] Hướng dẫn thêm và cập nhật Extended Property Column trong Table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng Vertical Gridview để hiển thị thông tin sản phẩm
- [C#] Hướng dẫn sử dụng Json Schema để Validate chuỗi string có phải json
- [C#] Hướng dẫn sử dụng công cụ Clean Code trên Visual Studio
- [C#] Hướng dẫn Drag and Drop File vào RichTextBox
- [C#] Hướng dẫn tạo hiệu ứng Karaoke Text Effect Winform
- [C#] Sử dụng thư viện ZedGraph vẽ biểu đồ Line, Bar, Pie trên Winform
- [DATABASE] Hướng dẫn sort sắp xếp địa chỉ IP trên sqlserver sử dụng hàm PARSENAME
- [C#] Theo dõi sử kiện process Start hay Stop trên Winform
- [ASP.NET] Chia sẻ source code chụp hình sử dụng camera trên website
- [C#] Chạy ứng dụng trên Virtual Desktop (màn hình ảo) Winform
- [C#] Mã hóa và giải mã Data Protection API trên winform
- [C#] Hướng dẫn tạo Gradient Background trên Winform
- [DATABASE] Hướng dẫn convert Epoch to DateTime trong sqlserver
- [DATABASE] Hướng dẫn sử dụng STRING_AGG và CONCAT_WS trong sqlserver 2017
- [C#] Hướng dẫn Copy With All Property in Class Object
- [DEVEXPRESS] Hướng dẫn load Json DataSource vào GridView
- [C#] Hướng dẫn tạo chữ ký trên winform Signature Pad
[DEVEXPRESS] Hướng dẫn cách chèn watermask Image vào file PDF C# bởi vị trí click chuột
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!