In the code in the OP, there are some objects that haven't been properly disposed. The following seems to work:
Pre-requisites
- Download/install NuGet package: iText7 (version: 7.2.5)
Create a class (name: HelperiText7.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using iText.IO.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using System.IO;
using System.Drawing;
using iText.Layout.Properties;
namespace PdfiTextMutex
{
public class HelperiText7
{
//add 'Local\' for compatibility with TerminalServices
//see https://learn.microsoft.com/en-us/dotnet/api/system.threading.mutex?view=net-7.0
private static string _mutexName = System.IO.Path.Combine("Local",System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().Location));
private static bool _createdNew = false;
public static void CreatePdf(string filename, float margin = 0.5f)
{
using (var mutex = new Mutex(true, _mutexName, out _createdNew))
{
if (!_createdNew)
{
throw new Exception("Error: CreatePdf is already executing. Please try again later.");
}
Debug.WriteLine($"_mutexName: '{_mutexName}'");
//for testing, pause
System.Threading.Thread.Sleep(10000);
//create new instance
using (PdfWriter writer = new PdfWriter(filename))
{
//create new PDF document
PdfDocument pdfDoc = new PdfDocument(writer);
//create reference
PdfDocumentInfo pdfDocInfo = pdfDoc.GetDocumentInfo();
DateTime creationDateTime = PdfDate.Decode(pdfDocInfo.GetMoreInfo("CreationDate"));
Debug.WriteLine($"creationDateTime: {creationDateTime.ToString()}");
//create new instance
//Document doc = new Document(pdfDoc, new iText.Kernel.Geom.PageSize(iText.Kernel.Geom.PageSize.A4.GetWidth(), iText.Kernel.Geom.PageSize.A4.GetHeight()));
Document doc = new Document(pdfDoc);
//set margins
doc.SetLeftMargin(margin);
doc.SetRightMargin(margin);
doc.SetTopMargin(margin);
doc.SetBottomMargin(margin);
//get image
byte[] imageBytes = GetResourceImage("logo");
//add image to PDF
ImageData imData = iText.IO.Image.ImageDataFactory.Create(imageBytes);
iText.Layout.Element.Image im = new iText.Layout.Element.Image(imData);
float newWidth = 140;
float newHeight = im.GetImageHeight() * (newWidth / im.GetImageWidth());
im.SetWidth(newWidth).SetHeight(newHeight);
im.SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.RIGHT);
doc.Add(im);
Table table = new Table(new float[] { 5, 2, 7, 7, 12 })
.SetWidth(UnitValue.CreatePercentValue(100))
.SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.CENTER)
.UseAllAvailableWidth();
/*
Cell cell = new Cell(1, 5)
.Add(new Paragraph("TEST REPORT")
.SetFont(kor).SetBold()
.SetFontSize(30))
.SetTextAlignment(TextAlignment.CENTER)
.SetVerticalAlignment(VerticalAlignment.MIDDLE);
*/
Cell cell = new Cell(1, 5)
.Add(new Paragraph($"TEST REPORT - {DateTime.Now.ToString("HH:mm:ss.fff")}")
.SetFontSize(30))
.SetTextAlignment(TextAlignment.CENTER)
.SetVerticalAlignment(VerticalAlignment.MIDDLE);
//add Cell
table.AddCell(cell);
//add Table
doc.Add(table);
//close
doc.Close();
pdfDoc.Close();
}
}
}
public static void CreatePdf2(string filename, float margin = 0.5f)
{
using (var mutex = new Mutex(true, _mutexName, out _createdNew))
{
if (!_createdNew)
{
throw new Exception("Error: CreatePdf is already executing. Please try again later.");
}
Debug.WriteLine($"_mutexName: '{_mutexName}'");
if (mutex.WaitOne())
{
//for testing, pause
System.Threading.Thread.Sleep(10000);
//create new instance
using (PdfWriter writer = new PdfWriter(filename))
{
//create new PDF document
PdfDocument pdfDoc = new PdfDocument(writer);
//create reference
PdfDocumentInfo pdfDocInfo = pdfDoc.GetDocumentInfo();
DateTime creationDateTime = PdfDate.Decode(pdfDocInfo.GetMoreInfo("CreationDate"));
Debug.WriteLine($"creationDateTime: {creationDateTime.ToString()}");
//create new instance
//Document doc = new Document(pdfDoc, new iText.Kernel.Geom.PageSize(iText.Kernel.Geom.PageSize.A4.GetWidth(), iText.Kernel.Geom.PageSize.A4.GetHeight()));
Document doc = new Document(pdfDoc);
//set margins
doc.SetLeftMargin(margin);
doc.SetRightMargin(margin);
doc.SetTopMargin(margin);
doc.SetBottomMargin(margin);
//get image
byte[] imageBytes = GetResourceImage("logo");
//add image to PDF
ImageData imData = iText.IO.Image.ImageDataFactory.Create(imageBytes);
iText.Layout.Element.Image im = new iText.Layout.Element.Image(imData);
float newWidth = 140;
float newHeight = im.GetImageHeight() * (newWidth / im.GetImageWidth());
im.SetWidth(newWidth).SetHeight(newHeight);
im.SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.RIGHT);
doc.Add(im);
Table table = new Table(new float[] { 5, 2, 7, 7, 12 })
.SetWidth(UnitValue.CreatePercentValue(100))
.SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.CENTER)
.UseAllAvailableWidth();
/*
Cell cell = new Cell(1, 5)
.Add(new Paragraph("TEST REPORT")
.SetFont(kor).SetBold()
.SetFontSize(30))
.SetTextAlignment(TextAlignment.CENTER)
.SetVerticalAlignment(VerticalAlignment.MIDDLE);
*/
Cell cell = new Cell(1, 5)
.Add(new Paragraph($"TEST REPORT - {DateTime.Now.ToString("HH:mm:ss.fff")}")
.SetFontSize(30))
.SetTextAlignment(TextAlignment.CENTER)
.SetVerticalAlignment(VerticalAlignment.MIDDLE);
//add Cell
table.AddCell(cell);
//add Table
doc.Add(table);
//close
doc.Close();
pdfDoc.Close();
}
//release mutex
mutex.ReleaseMutex();
}
}
}
public static async Task CreatePdfAsync(string filename, float margin = 0.5f)
{
await Task.Run(() => { CreatePdf(filename, margin); });
//await Task.Run(() => { CreatePdf2(filename, margin); });
}
private static byte[] GetResourceImage(string imageName)
{
//https://stackoverflow.com/questions/1192054/load-image-from-resources-area-of-project-in-c-sharp
System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
string resourceName = asm.GetName().Name + ".Properties.Resources";
System.Resources.ResourceManager rm = new System.Resources.ResourceManager(resourceName, asm);
using (MemoryStream ms = new MemoryStream())
{
using (Bitmap bmp = (Bitmap)rm.GetObject(imageName))
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
return ms.ToArray();
}
}
}
}
}
Usage 1:
using (SaveFileDialog sfd = new SaveFileDialog())
{
sfd.Filter = "PDF File (*.pdf)|*.pdf";
sfd.FileName = "Test.pdf";
if (sfd.ShowDialog() == DialogResult.OK)
{
try
{
HelperiText7.CreatePdf(sfd.FileName);
}
catch(Exception ex)
{
if (ex.Message == "Error: CreatePdf is already executing. Please try again later.")
MessageBox.Show(ex.Message, "Error - Method In Use", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
Usage 2:
using (SaveFileDialog sfd = new SaveFileDialog())
{
sfd.Filter = "PDF File (*.pdf)|*.pdf";
sfd.FileName = "Test.pdf";
if (sfd.ShowDialog() == DialogResult.OK)
{
try
{
await HelperiText7.CreatePdfAsync(sfd.FileName);
}
catch (Exception ex)
{
if (ex.Message == "Error: CreatePdf is already executing. Please try again later.")
MessageBox.Show(ex.Message, "Error - Method In Use", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
Resources: