We are having issue while converting document to PDF, Its taking forever for few documents. I research little bit and found below code is taking time (sometimes 7 minutes)
we refering this nuget package : Spire.Officefor.NETStandard version : 8.1.4
Sample code is below here
public async Task ApplyWatermark()
{
var stopwatch = new Stopwatch();
var pdfFilePath = @"5_50_MB_12040_Pages.pdf";
Stream fileStream = File.OpenRead(pdfFilePath);
List<string> watermarkData = new()
{
"Welcome 20230403_1", "I", "welcome l", "welcome@welcome.com", "::ffff:127.0.0.1", "12:39 IST, 11/04/2023"
};
var text = string.Join(Environment.NewLine, watermarkData);
var pdfBrush = PdfBrushes.Red;
var noOfMarkerLines = watermarkData.Count + 2;
var opacity = 100 / 100.0f;
Stream outputStream = new MemoryStream();
var currentPage = 1;
using (var doc = new PdfDocument())
{
doc.LoadFromStream(fileStream);
foreach (PdfPageBase page in doc.Pages)
{
var rectangleF = CalculateWatermarker(page.ActualSize.Width, page.ActualSize.Height);
var fontSize = (int)(Math.Min(rectangleF.Height, rectangleF.Width) / noOfMarkerLines);
var font = new PdfFont(PdfFontFamily.Helvetica, true ? fontSize : 40);
var pdfTilingBrush = new PdfTilingBrush(
new SizeF(page.ActualSize.Width, page.ActualSize.Height));
pdfTilingBrush.Graphics.SetTransparency(opacity);
_ = pdfTilingBrush.Graphics.Save();
var centerX = page.ActualSize.Width / 2;
var centerY = page.ActualSize.Height / 2;
rectangleF.X = page.ActualSize.Width / 2 - rectangleF.Width / 2;
rectangleF.Y = page.ActualSize.Height / 2 - rectangleF.Height / 2;
var radian = Math.Atan(page.ActualSize.Height / page.ActualSize.Width);
var degree = (float)(radian * (180 / Math.PI));
pdfTilingBrush.Graphics.RotateTransform(-degree, new PointF(centerX, centerY));
pdfTilingBrush.Graphics.DrawString(text,
font, pdfBrush, rectangleF,
new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle));
pdfTilingBrush.Graphics.Restore();
page.Canvas.DrawRectangle(pdfTilingBrush,
new RectangleF(new PointF(0, 0), page.Canvas.ClientSize));
currentPage++;
}
doc.SaveToStream(outputStream, FileFormat.PDF);
_ = outputStream.Seek(0, SeekOrigin.Begin);
var filePath = @"f:\test\samplefiles";
var path1 = Path.Combine(filePath, string.Concat(Guid.NewGuid(), "_output.pdf"));
using (var outputFileStream = new FileStream(path1, FileMode.Create))
{
await outputStream.CopyToAsync(outputFileStream);
}
}
}
private RectangleF CalculateWatermarker(float actualWidth, float actualHeight)
{
var watermakerHeightRatio = 0.23;
double tan = actualHeight / actualWidth;
var radians = Math.Atan(tan);
var cos = Math.Cos(radians);
var sin = Math.Sin(radians);
var pageDiagonal = actualWidth / cos;
RectangleF result = new RectangleF()
{
// X = (float)((pageDiagonal - result.Width) / 2 / cos - sin * result.Height),
// Y = (float)(cos * result.Height),
// Width = (float)(pageDiagonal - 3 * (result.Height / 2) / tan),
Height = (float)(Math.Min(actualHeight, actualWidth) * watermakerHeightRatio)
};
return result;
}