Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue May 24, 2022 9:14 am

I'm having a PDF in memory stream to which I'm trying to stamp a text. Below is the code fragment which is used to stamp text into the PDF. This piece of code was running all well until we upgraded the Spire.Office version to 6.12.1. Now the PDF which is generated after this stampText method is called is corrupted. and throwing an error ( There was an error processing a page. there was a problem reading this document (18) ) when we try to open the pdf file with acrobat. PDF opens fine with browsers though.

Code: Select all
public static PdfDocument stampText(PdfDocument pdfFile, string text, float size, TextPosition position, SizeF offsets, float rotateDegrees = 0f, string customFont = null)
{

   Font font = null;

   if (customFont != null)
      font = getCustomFont(customFont, size);

   if (font == null && customFont != null)
   {
      font = new Font(customFont, size);

      //If the font doesn't exist, font.Name won't match customFont
      if (!font.Name.Equals(customFont, StringComparison.CurrentCultureIgnoreCase))
         font = null;
   }

   if (font == null)
      font = new Font("Arial", size, FontStyle.Regular, GraphicsUnit.Point);

   PdfTrueTypeFont ttFont = new PdfTrueTypeFont(font);
   PdfSolidBrush brush = new PdfSolidBrush(Color.Black);
   SizeF stringSize = ttFont.MeasureString(text);

   int numPages = pdfFile.Pages.Count;
   for (int pageCount = 0; pageCount < numPages; pageCount++)
   {
      PdfPageBase page = pdfFile.Pages[pageCount];
      PdfGraphicsState state = page.Canvas.Save();

      PointF docPosition = textPosition(position, offsets, page.Size, stringSize);
      page.Canvas.TranslateTransform(docPosition.X, docPosition.Y);
      page.Canvas.RotateTransform(rotateDegrees);

      page.Canvas.DrawString(text, ttFont, brush, 0, 0);

      page.Canvas.Restore(state);
   }

   ttFont.Dispose();
   font.Dispose();

   return pdfFile;
}

kalyadan77
 
Posts: 12
Joined: Sat May 21, 2022 11:38 pm

Wed May 25, 2022 6:48 am

Any update on this issue?

kalyadan77
 
Posts: 12
Joined: Sat May 21, 2022 11:38 pm

Wed May 25, 2022 9:31 am

Hi,

Thank you for your inquiry.
I noticed that you called the ttFont.Dispose();font.Dispose(); to release the fonts. Kindly note when using the PdfTrueTypeFont interface to create font, it will store font resources in memory. It is not allowed to dispose the font resources before saving PDF file. Please call the Dispose() method after saving, there will be no problem when reading document.

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Wed May 25, 2022 12:18 pm

Got your point but this was the code which was working all well until the upgrade of Spire Office to 6.12.1 version. Is there a reason for this change?

kalyadan77
 
Posts: 12
Joined: Sat May 21, 2022 11:38 pm

Thu May 26, 2022 3:22 am

i found a similar thread on your support portal (https://www.e-iceblue.com/forum/calling-dispose-on-pdftruetypefont-t6548.html)

As per the last message from your team on this, Spire DEV team is looking into this and prividing dispose of PDFTrueTypeFont cache when the PDF file is disposed. Is there an update on this? The thread was around 5 yrs old now.

Will there be memory leaks if I leave the PDFTrueTypeFont object without disposing?

kalyadan77
 
Posts: 12
Joined: Sat May 21, 2022 11:38 pm

Thu May 26, 2022 10:44 am

Hi,

Actually, this is determined by our product mechanism, it is not feasible to dispose font resources before saving the document. Currently we have no plans to adjust the internal mechanism to dispose font resources before saving the document. And we generally recommend that users manually call dispose() to release font.
In addition, you mentioned that the old version has no problem, please provide the detailed version for our reference. Thanks in advance.

Sincerely
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Thu May 26, 2022 11:21 am

Previous version what we were using was Spire Office 2.16.27.
There was no issue with the above code while creating PDFs with this version.

kalyadan77
 
Posts: 12
Joined: Sat May 21, 2022 11:38 pm

Fri May 27, 2022 3:38 am

Hi,

Thank you for sharing the version information.
I will report this behavior to dev team for further investigation. Once there is any good news, I will inform you.

Sincerely
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Tue Jun 07, 2022 9:34 am

Hi,

Thanks for your waiting.
I got a feedback from our dev team about your reported issue. The reason why the old version Spire Office 2.16.27 can dispose the font before saving the PDF is that it doesn't properly embed the font into the PDF at all. When drawing text in PDF using Spire.PDF, it needs to embed fonts, so the practice of not embedding fonts itself does not conform to the specification and is incorrect. Please dispose the fonts after saving PDF as I suggested earlier. Please feel free to contact me if you have any questions

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Mon Jun 13, 2022 11:18 am

Thanks for the reply Kylie. Appreciate your time

kalyadan77
 
Posts: 12
Joined: Sat May 21, 2022 11:38 pm

Tue Jun 14, 2022 9:57 am

Hi,

You're welcome !
If you need assistance in the future, please don't hesitate to contact us.
Wish you all the best

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Return to Spire.Doc