- Tạo bản quyền phần mềm C# và bảo mật code | The Enigma Protector
- [C#] Hướng dẫn giới hạn số cửa sổ ứng dụng khi chạy trên winform
- [C#] Lập trình ứng dụng lấy ngày giờ hệ thống mạng LAN sử dụng giao thức UDP
- [DATABASE] Sự khác nhau giữa hai câu lệnh TRUNCATE vs DELETE trong sqlserver
- [C#] Các cách chuyển đổi kiểu dữ liệu text String sang kiểu số Int
- [C#] Di chuyển và thay đổi kích thước Control Winform khi ứng dụng đang chạy
- [VB.NET] Chia sẻ source tạo sắp xếp đội hình bóng đá Line-ups đội bóng
- [C#] Hướng dẫn chỉnh sửa Text của label trực tiếp trên winform
- [C#] Hướng dẫn custom TextBox giống Ultraviewer trên Winform
- [C#] Show Modal Winform like Bootstrap
- [DATABASE] Thứ tự thực hiện mệnh đề truy vấn SELECT trong Sqlserver
- [C#] Hướng dẫn viết addin Excel Lấy hình ảnh từ URL internet vào Excel
- [DATABASE] TSQL view max length all column data trên table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng MailMerge kèm Hình ảnh trên Winform
- [DATABASE] Hướng dẫn truy vấn xem kích thước lưu trữ của từng bảng ghi Table trên sqlserver
- [C#] Hướng dẫn Fake Date Time sử dụng thư viện Harmony
- [DATABASE] Phân biệt câu lệnh DDL và DML trong sqlserver
- [C#] Hướng dẫn convert file mã HTML sang file Pdf trên winform
- [DEVEXPRESS] Tạo các loại mã vạch Barcode trực tiếp trên Devexpress Barcode API
- [DEVEXPRESS] Hướng dẫn custom Simple button thành Progressbar
[C#] Chuyển đổi nhiều file hình ảnh thành một file PDF - sử dụng PdfSharp library
Xin chào các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn cách sử dụng thư viện PDFSharp để convert nhiều hình ảnh (image) thành một file PDF trong lập trình C# winform.
Trong ứng dụng demo này, mình có tích hợp thêm chức năng tạo Watermask khi mình chuyển đổi danh sách hình ảnh sang file PDF.
Ứng dụng này các bạn có thể sử dụng để chuyển đổi nhiều hình ảnh của truyện tranh thành một file PDF.
Giao diện demo ứng dụng Nối nhiều hình ảnh thành một file PDF C# Winform:
và dưới đây là file PDF kết quả khi các bạn chuyển đổi convert xong:
Source code Convert Image to PDF C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
namespace ImageToPdf
{
public partial class MainForm : Form
{
bool success = false;
public MainForm()
{
InitializeComponent();
initCtrls();
}
#region form events
private void btnSelectSrc_Click(object sender, EventArgs e)
{
if (ofdSrcFile.ShowDialog() != DialogResult.OK)
return;
toolStripStatusLabel1.Text = "";
initCtrls();
listBox1.Items.Clear();
foreach (string fName in ofdSrcFile.FileNames) listBox1.Items.Add(fName);
listBox1.SelectedIndex = 0;
txbxDestFile.Text =
Path.GetDirectoryName((string)listBox1.Items[0]) + "\\" +
getCommonPart(listBox1.Items) + ".pdf";
}
private void btnSelectDest_Click(object sender, EventArgs e)
{
if (sfdDestFile.ShowDialog() != DialogResult.OK)
return;
toolStripStatusLabel1.Text = "";
txbxDestFile.Text = sfdDestFile.FileName;
}
private void buttonPreviewSource_Click(object sender, EventArgs e)
{
errProv.Clear();
if (listBox1.Items.Count == 0)
{
errProv.SetError(btnSelectSrc, toolStripStatusLabel1.Text = "Please point source file(s).");
return;
}
else if (listBox1.SelectedIndex < 0)
{
errProv.SetError(listBox1, toolStripStatusLabel1.Text = "Please select source file to preview.");
return;
}
try
{
foreach (string s in listBox1.SelectedItems)
Process.Start(s);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonPreviewDest_Click(object sender, EventArgs e)
{
errProv.Clear();
if (txbxDestFile.Text.Length == 0)
{
errProv.SetError(btnSelectDest, "Please point destination file.");
return;
}
try
{
Process.Start(txbxDestFile.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void buttonUp_Click(object sender, EventArgs e)
{
MoveItem(-1);
checkDestination();
}
private void buttonDown_Click(object sender, EventArgs e)
{
MoveItem(1);
checkDestination();
}
private void btnConvert_Click(object sender, EventArgs e)
{
errProv.Clear();
if (listBox1.Items.Count == 0)
{
errProv.SetError(listBox1, toolStripStatusLabel1.Text = "Please point source file(s).");
return;
}
if (!checkBoxSeparate.Checked)
{
if (txbxDestFile.Text.Length == 0)
{
errProv.SetError(txbxDestFile, toolStripStatusLabel1.Text = "Please point destination file.");
return;
}
else if (File.Exists(txbxDestFile.Text))
{
if (MessageBox.Show("Overwrite file " + txbxDestFile.Text,
toolStripStatusLabel1.Text = "Destination file exists",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Warning) != System.Windows.Forms.DialogResult.OK)
{
return;
}
}
}
success = false;
List<string> l = new List<string>();
// 1st argument = destination file
l.Add(txbxDestFile.Text);
// Rest arguments = source files in required order
foreach (string f in listBox1.Items)
l.Add(f);
bw.RunWorkerAsync(l.ToArray());
toolStripProgressBar1.Style = ProgressBarStyle.Marquee;
}
private void button1Clr_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
checkDestination();
}
private void checkBoxSeparate_CheckedChanged(object sender, EventArgs e)
{
checkDestination();
checkBoxAutoPreviewDest.Checked = false;
}
private void txbxDestFile_TextChanged(object sender, EventArgs e)
{
btnConvert.Enabled = checkBoxDeleteSource.Enabled = true;
buttonPreviewDest.Enabled = File.Exists(txbxDestFile.Text);
}
#endregion
#region ListBox
private void listBox1_DoubleClick(object sender, EventArgs e)
{
if ((listBox1.Items.Count > 0) & (listBox1.SelectedIndex >= 0))
Process.Start((string)listBox1.SelectedItems[0]);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
buttonDown.Enabled = buttonUp.Enabled = buttonPreviewSource.Enabled = true;
}
private void listBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete && listBox1.Items.Count > 0 && listBox1.SelectedItem != null)
{
listBox1.Items.Remove(listBox1.SelectedItem);
checkDestination();
}
}
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
for (int i = 0; i < s.Length; i++)
{
try
{
// accept only files specified in the filer
FileInfo fi = new FileInfo(s[i]);
if (ofdSrcFile.Filter.Contains(fi.Extension.ToUpper()))
listBox1.Items.Add(s[i]);
}
catch { }
}
checkDestination();
}
#endregion
#region work
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
try
{
string[] fNames = (e.Argument as string[]);
if (checkBoxSeparate.Checked)
{
XSize size = new XSize(0, 0);
for (int i = 1; i < fNames.Length; i++)
{
// each source file saeparate
PdfDocument doc = new PdfDocument();
toolStripStatusLabel1.Text = "Processing " + fNames[i];
var page = new PdfPage();
page.Size = PageSize.A4;
XPdfForm form = XPdfForm.FromFile(fNames[i]);
if (form.Width > form.Height)
page.Orientation = PageOrientation.Landscape;
else
page.Orientation = PageOrientation.Portrait;
doc.Pages.Add(page);
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
XImage img = XImage.FromFile(fNames[i]);
if (true)
{
size = new XSize(img.Width, img.Height);
page.Height = size.Height;
page.Width = size.Width;
}
xgr.DrawImage(img, 0, 0);
img.Dispose();
xgr.Dispose();
// save to destination file
FileInfo fi = new FileInfo(fNames[i]);
doc.Save(fi.FullName.Replace(fi.Extension, ".PDF"));
doc.Close();
}
}
else
{
// single document
PdfDocument doc = new PdfDocument();
XSize size = new XSize(0, 0);
for (int i = 1; i < fNames.Length; i++)
{
toolStripStatusLabel1.Text = "Processing " + fNames[i];
// each source on separate page
var page = new PdfPage();
page.Size = PageSize.Letter;
doc.Pages.Add(page);
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[i - 1]);
XImage img = XImage.FromFile(fNames[i]);
if (true)
{
size = new XSize(img.Width, img.Height);
page.Height = size.Height - 320;
page.Width = size.Width - 230;
}
xgr.DrawImage(img, 0, 0);
if (chk_waterMask.Checked)
{
var font = new XFont("Tahoma", 120.0f);
var sizes = xgr.MeasureString(txt_waterMask.Text, font);
xgr.TranslateTransform(page.Width / 2, page.Height / 2);
xgr.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
xgr.TranslateTransform(-page.Width / 2, -page.Height / 2);
var format = new XStringFormat();
format.Alignment = XStringAlignment.Near;
format.LineAlignment = XLineAlignment.Near;
XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));
xgr.DrawString(txt_waterMask.Text, font, brush,
new XPoint((page.Width - sizes.Width) / 2, (page.Height - sizes.Height) / 2),
format);
}
img.Dispose();
xgr.Dispose();
}
// save to destination file
doc.Save(fNames[0]);
doc.Close();
}
success = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
toolStripProgressBar1.Style = ProgressBarStyle.Blocks;
toolStripProgressBar1.Value = 0;
Boolean anyErr = false;
if (checkBoxDeleteSource.Checked)
{
foreach (string fName in listBox1.Items)
{
try
{
toolStripStatusLabel1.Text = "Deleting " + fName;
File.Delete(fName);
}
catch
{
anyErr = true;
}
}
listBox1.Items.Clear();
}
if (checkBoxAutoPreviewDest.Checked && File.Exists(txbxDestFile.Text))
{
Process.Start(txbxDestFile.Text);
}
if (success && !anyErr)
{
initCtrls();
toolStripStatusLabel1.Text = "The converion ended successfully.";
}
else
{
toolStripStatusLabel1.Text = "WARNING! Some errors during converion !";
}
}
#endregion
#region utils
public void MoveItem(int direction)
{
// Checking selected item
if (listBox1.SelectedItem == null || listBox1.SelectedIndex < 0)
return; // No selected item - nothing to do
// Calculate new index using move direction
int newIndex = listBox1.SelectedIndex + direction;
// Checking bounds of the range
if (newIndex < 0 || newIndex >= listBox1.Items.Count)
return; // Index out of range - nothing to do
object selected = listBox1.SelectedItem;
// Removing removable element
listBox1.Items.Remove(selected);
// Insert it in new position
listBox1.Items.Insert(newIndex, selected);
// Restore selection
listBox1.SetSelected(newIndex, true);
}
private void initCtrls()
{
errProv.Clear();
toolStripStatusLabel1.Text = "";
buttonPreviewSource.Enabled = false;
buttonUp.Enabled =
buttonDown.Enabled = (listBox1.Items.Count > 0);
checkDestination();
}
private string getCommonPart(ListBox.ObjectCollection fnames)
{
string res = "";
try
{
res = Path.GetFileNameWithoutExtension(fnames[0].ToString());
if (fnames.Count > 1)
{
int posOfCommonPart = -1;
string f1 = Path.GetFileNameWithoutExtension(fnames[0].ToString());
for (int j = 1; j < fnames.Count; j++)
{
string f2 = Path.GetFileNameWithoutExtension(fnames[j].ToString());
for (int i = Math.Min(f1.Length, f2.Length); i > 0; i--)
{
if (f1.Substring(0, i) == f2.Substring(0, i))
{
if (posOfCommonPart > 0)
posOfCommonPart = Math.Min(posOfCommonPart, i);
else
posOfCommonPart = i;
break;
}
}
}
if (posOfCommonPart > 0)
{
res = f1.Substring(0, posOfCommonPart);
}
}
}
catch { }
return res;
}
private void checkDestination()
{
txbxDestFile.Enabled = btnSelectDest.Enabled = buttonPreviewDest.Enabled = !checkBoxSeparate.Checked;
if (!checkBoxSeparate.Checked && listBox1.Items.Count > 0)
{
txbxDestFile.Text = Path.GetDirectoryName((string)listBox1.Items[0]) + "\\" + getCommonPart(listBox1.Items) + ".pdf";
}
else
{
txbxDestFile.Text = "";
}
}
#endregion
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
}
}
Thanks for watching!