Wed Jun 14, 2023 1:50 pm
I created a smaller sample of a code where you can see this behavior, we load file from disk in to PdfDocument , do some watermarking , and save it to disk . When we load saved version it shows 0 pages instead of 27 as document originally had. This behavior we can't see in all files just some. My file has a little over 3M so I can't upload it is there any other way to send it?
Code :
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"someLocation\Fridge Freezer.pdf");
SizeF sizeP = doc.Pages[0].Size;
int fontSize = 0;
string text = "ITS";
foreach (PdfPageBase page in doc.Pages)//page count is 27
{
PdfTilingBrush brush = new PdfTilingBrush(new SizeF(page.Canvas.ClientSize.Width / 2, page.Canvas.ClientSize.Height / 3));
if (page.Canvas.ClientSize.Width > page.Canvas.ClientSize.Height * 1.3)
{
brush = new PdfTilingBrush(new SizeF(page.Canvas.ClientSize.Width / 3, page.Canvas.ClientSize.Height / 2));
}
else if (page.Canvas.ClientSize.Width * 1.3 < page.Canvas.ClientSize.Height)
{
brush = new PdfTilingBrush(new SizeF(page.Canvas.ClientSize.Width / 2, page.Canvas.ClientSize.Height / 3));
}
else
{
brush = new PdfTilingBrush(new SizeF(page.Canvas.ClientSize.Width / 2, page.Canvas.ClientSize.Height / 2));
}
if (fontSize == 0 || sizeP != page.Size)
{
sizeP = page.Size;
int textLength = text.Length;
int margin = 0;
if (textLength > 30) margin = 5;
else if (textLength > 20) margin = 10;
else if (textLength > 10) margin = 20;
else if (textLength > 5) margin = 40;
else margin = 50;
if (brush.Size.Width < brush.Size.Height)
{
fontSize = CalculateFont(brush.Size.Width * 1.14 - brush.Size.Width * margin / 100, text, 20);
}
else
{
fontSize = CalculateFont(brush.Size.Height * 1.14 - brush.Size.Height * margin / 100, text, 20);
}
}
brush.Graphics.SetTransparency(0.3f);
brush.Graphics.Save();
brush.Graphics.TranslateTransform(brush.Size.Width / 2, brush.Size.Height / 2);
brush.Graphics.RotateTransform(-45);
brush.Graphics.DrawString(text, new PdfFont(PdfFontFamily.Helvetica, fontSize), PdfBrushes.Violet, 0, 0, new PdfStringFormat(PdfTextAlignment.Center));
brush.Graphics.Restore();
brush.Graphics.SetTransparency(1);
page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.ClientSize));
}
doc.SaveToFile(@"someLocation\Fridge Freezer_new.pdf");
doc.LoadFromFile(@"someLocation\Fridge Freezer_new.pdf");
int a = doc.Pages.Count; //count is 0