- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
[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!