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.

Mon Aug 23, 2021 8:27 pm

This piece of code crashes with null reference exception when printing "Hello world from doc 1". Using latest spire version.

Code: Select all
            // print document #1
            using (var doc1 = new PdfDocument())
            {
                var page1 = doc1.Pages.Add();

                using (var font1 = new PdfTrueTypeFont(new Font("arial", 12.0f), false))
                {
                    var brush1 = new PdfSolidBrush(Color.Black);
                    var point1 = new PointF(1, 1);

                    // print document #2
                    using (var doc2 = new PdfDocument())
                    {
                        var page2 = doc2.Pages.Add();

                        using (var font2 = new PdfTrueTypeFont(new Font("arial", 12.0f), false))
                        {
                            var brush2 = new PdfSolidBrush(Color.Black);
                            var point2 = new PointF(1, 1);

                            page2.Canvas.DrawString("Hello world from doc 2", font2, brush2, point2);

                            doc2.SaveToFile(@"C:\test\doc2.pdf");
                        }
                    }

                    // **** here it will crash with null ref exception ****
                    page1.Canvas.DrawString("Hello world from doc 1", font1, brush1, point1);

                    doc1.SaveToFile(@"C:\test\doc1.pdf");
                }
            }


Please let me know if a fix is available.

Thank you

gumby
 
Posts: 21
Joined: Tue Jan 28, 2014 8:25 pm

Tue Aug 24, 2021 10:58 am

Hello,

Thank you for your inquiry.
I tested your code and did reproduce your problem. After investigation, I found that you defined the same font for font1 and font2. As for the same fonts, our Spire.PDF will only store one font data in memory. In your code, the font that draws the text in doc1 has been disposed after finishing drawing text in doc2, hence, you got the exception. To avoid it, correct your code as below.
Code: Select all
// print document #1
using (PdfDocument doc1 = new PdfDocument())
{
    PdfPageBase page1 = doc1.Pages.Add();
    PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("arial", 20.0f), false);               
    PdfSolidBrush brush1 = new PdfSolidBrush(Color.Black);
    PointF point1 = new PointF(1, 1);               
    // print document #2
    using (PdfDocument doc2 = new PdfDocument())
    {
        PdfPageBase page2 = doc2.Pages.Add();                       
        PdfSolidBrush brush2 = new PdfSolidBrush(Color.Black);
        PointF point2 = new PointF(1, 1);
        page2.Canvas.DrawString("Hello world from doc 2", font, brush2, point2);
        doc2.SaveToFile("doc2.pdf");                         
    } 
    page1.Canvas.DrawString("Hello world from doc 1", font, brush1, point1);
    doc1.SaveToFile("doc1.pdf");             
}

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Thu Aug 26, 2021 8:34 pm

You were right about my example. However, it did not fix my issue.

After trying multiple stuff, I figure out that this is the code that is actually causing me the null reference exception :

Code: Select all
            var font = new PdfTrueTypeFont(new Font("arial", 12.0f), false);

            var rtf = @"{\rtf1\ansi\ansicpg1252\deff0\deflang3084{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}{\f1\fnil\fcharset0 Arial;}}
{\colortbl ;\red0\green0\blue0;\red0\green0\blue255;}
\viewkind4\uc1\pard\qj\cf1\f0\fs20
\par \f1 Hello world\cf1 ,
\par
\par \pard
\par \cf0\fs17
\par }";

            using (var doc1 = new PdfDocument())
            {
                var page = doc1.Pages.Add();

                page.LoadFromRTF(rtf,  500, 500, false, new PointF(0, 0));
                doc1.SaveToFile("doc1.pdf");
            }

            using (var doc2 = new PdfDocument())
            {
                var page = doc2.Pages.Add();

                page.Canvas.DrawString("DOCUMENT #2", font, new PdfSolidBrush(Color.Black), new PointF(0, 0));

                doc2.SaveToFile("doc2.pdf");
            }

gumby
 
Posts: 21
Joined: Tue Jan 28, 2014 8:25 pm

Fri Aug 27, 2021 10:45 am

Hello,

Thank you for your feedback.
I tested your code and indeed found there is something wrong with the generated PDF document doc2. I have posted this issue into our bug tracking system with the ticket number SPIREPDF-4593, once it is fixed or there is any update for you, I will inform you. Apologized for the inconvenience caused.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Tue Sep 28, 2021 12:07 pm

Hi Spire Team,

Do you have any update on this issue?

Thank you !

gumby
 
Posts: 21
Joined: Tue Jan 28, 2014 8:25 pm

Wed Sep 29, 2021 9:19 am

Hello,

Thank you for your follow-up.
After our dev team's investigation, we found this issue was caused by the font cache. Please use the following code to turn off the font cache to eliminate the issue. Looking forward to your test feedback.
Code: Select all
//Turn off font cache
PdfDocument.EnableFontCache = false;
var font = new PdfTrueTypeFont(new Font("arial", 12.0f), false);
...

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Fri Oct 08, 2021 10:06 am

Hello,

Hope you're doing well!
Have you tried the code we provide? Any feedback will be greatly appreciated.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Thu Nov 04, 2021 8:23 pm

Sorry about the delay.

It does fix the problem in a basic test case, but in a real case scenario, it increases the execution time dramatically (like 100 times slower when working with LoadRTF), and also crashes with an out of memory exception. So I would not consider this a success :)

gumby
 
Posts: 21
Joined: Tue Jan 28, 2014 8:25 pm

Fri Nov 05, 2021 3:55 am

Hello,

Thank you for your feedback.
I tested to load the rtf string you provided before, the execution time didn't change much and didn't reproduce the "out of memory" issue. To help further look into your issue, please provide the following information for testing. You could attach them here or send them to us via email (support@e-iceblue.com). Thanks in advance.
1) Please share your RTF file if you tested a different one.
2) Your test environment, such as OS info (E.g. Win7, 64-bit, RAM:8GB) and region setting (E.g. China, Chinese).
3) Your application type, such as Console app (. Net Framework 4.5). It would be better if you can provide a project that can reproduce your problem.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Fri Nov 05, 2021 1:05 pm

Hi, this piece of code causes the issue, I've narrowed it down to the \'ab text in the rtf when the font cache is disabled. For the speed issue it is resolved (I was using an old spire version).
This code eats 2gb of memory in like 30 seconds, causing an out of memory exception.

My test environment is Windows server 2019 datacenter (vm), 64 bits, 32 gb of ram, region settings is en-ca (english canada)
The test project is a console app with .net framework 4.5.2.

Code: Select all
            var rtf = @"{\rtf1\ansi\ansicpg1252\deff0\deflang3084{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}}
\viewkind4\uc1\pard\f0\fs16\'ab
\par }";

            PdfDocument.EnableFontCache = false;
            using (var doc1 = new PdfDocument())
            {
                for (var p = 0; p < 100; p++)
                {
                    var page = doc1.Pages.Add();
                    for (var i = 0; i < 10; i++)
                    {
                        page.LoadFromRTF(rtf, 500, 500, false, new PointF(0, 0));
                    }
                }
                doc1.SaveToFile("doc1.pdf");
            }

gumby
 
Posts: 21
Joined: Tue Jan 28, 2014 8:25 pm

Mon Nov 08, 2021 10:29 am

Hello,

Thank you for your sharing.
I tested your code in the same environment as yours, and found the memory consumption is up to 1.5GB. Actually, the page.LoadFromRTF method In Spire.PDF is deprecated, so I suggest you use Spire.Doc to load RTF code and save it as a PDF file, this method is faster and causes less memory consumption. Please refer to the following code. If there is any question, please feel free to write back.
Code: Select all
string rtf = @"{\rtf1\ansi\ansicpg1252\deff0\deflang3084{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}}
\viewkind4\uc1\pard\f0\fs16\'ab
\par }";
                     
            using (var doc1 = new Document())
            {
                for (var p = 0; p < 100; p++)
                {
                    Section section = doc1.AddSection();
                    Paragraph para = section.AddParagraph();
                    for (var i = 0; i < 10; i++)
                    {
                        para.AppendRTF(rtf);
                       
                    }
                }
                doc1.SaveToFile("doc1.pdf",FileFormat.PDF);
            }

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Mon Nov 08, 2021 1:00 pm

Is there any way to print a "RTF" on a PdfDocument at a location (X,Y)?

Also could you quote us an amount to fix the bug even thought it is not supported anymore?

gumby
 
Posts: 21
Joined: Tue Jan 28, 2014 8:25 pm

Tue Nov 09, 2021 3:05 am

Hi,

Thank you for your feedback.
Sorry we do not maintain obsolete methods, and obsolete methods will be deleted in the future. Meanwhile, there is no way to print an "RTF" at the location(X, Y) of the PDF document.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF