Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Fri Sep 08, 2023 11:02 am

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;
}

sureshkumar.arumugam
 
Posts: 11
Joined: Fri Apr 28, 2023 6:13 am

Mon Sep 11, 2023 7:06 am

Hi,

Thank you for your inquiry.
I simulated a Pdf file(176KB) and tested the code you provided with Spire.Officefor.NETStandard 8.1.4 and the latest version Spire.Officefor.NETStandard (V8.9.2), but I did not encounter the same problem as you.The entire process takes approximately 1-2 seconds. I suggest you update to the latest version Spire.Officefor.NETStandard through Nuget and test again. If the issue still exists, please offer the following information to help us do further investigation. Thank you in advance.
These are my test screenshots:
117A94AF-0705-44ac-A6BB-FFE172AAE310.png

1)Your input test file, you can attach here or send it to us via email (support@e-iceblue.com).
2)Application type, such as Console App, .NET Framework 4.8.
3) Your test environment, such as OS info (E.g. Windows 7, 64-bit).

Sincerely,
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Wed Oct 11, 2023 7:54 am

Hi

I would like to know if the solution we have provided has helped you solved the problem you have encountered, and our team expected to have more communication with you.Good day to you

Sincerely,
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Return to Spire.PDF

cron