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 12, 2014 5:14 pm

Hi,

Is SpirePDF thread safe when using true type fonts? I'm having strange errors as soon as I have more than 1 thread. It is throwing an System.ArgumentException in mscorlib.

Here is the stack trace :
à System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
à System.Collections.Generic.Dictionary`2.Resize(Int32 newSize, Boolean forceNewHashCodes)
à System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
à System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue value)
à spr᯶.ᜃ(String A_0)
à Spire.Pdf.Graphics.PdfTrueTypeFont.ᜀ(String A_0)
à Spire.Pdf.Graphics.PdfCanvas.ᜀ(String A_0, PdfTrueTypeFont A_1)
à Spire.Pdf.Graphics.PdfCanvas.ᜀ(LineInfo A_0, RectangleF A_1, PdfFontBase A_2, PdfStringFormat A_3)
à Spire.Pdf.Graphics.PdfCanvas.ᜀ(PdfStringLayoutResult A_0, PdfFontBase A_1, PdfStringFormat A_2, RectangleF A_3)
à Spire.Pdf.Graphics.PdfCanvas.ᜀ(PdfStringLayoutResult A_0, PdfFontBase A_1, PdfPen A_2, PdfBrush A_3, RectangleF A_4, PdfStringFormat A_5)


I have the latest version of Spire PDF available (version 3.1.9.5040, runtime version v4.0.30319).

Note : I tried to post an url with my test project to reproduce the problem, but it won't let me.

You can download it here : goo.gl/RpwOhi

Thank you

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

Mon Sep 15, 2014 7:52 am

Hello,

Thanks for your inquiry.
Please use the line PdfTrueTypeFont myfont = new PdfTrueTypeFont(new Font("Comic sans MS", 12f, FontStyle.Regular));
the full code :
Code: Select all
using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace SpireMultiThread
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                string filname = "test" + i.ToString() + ".pdf";
                Task t = new Task(() =>
                {
                    CreatePDF(filname);
                },
                TaskCreationOptions.LongRunning /* the LongRunning option will force threads */);
                t.Start();
            }
        }

        private void CreatePDF(string filename)
        {
            PdfDocument doc = new PdfDocument();

            PdfTrueTypeFont myfont = new PdfTrueTypeFont(new Font("Comic sans MS", 12f, FontStyle.Regular));

            PdfPageBase page = doc.Pages.Add();

            for (int i = 0; i < 10; i++)
            {
                page.Canvas.DrawString("Hello, World!", myfont, new PdfSolidBrush(Color.Black), 1, i*15);
            }

            myfont.Dispose();
            doc.SaveToFile(filename);
            doc.Close();
           
        }
    }
}

Best regards,
Lisa
E-iceblue support team
User avatar

lisa.chen
 
Posts: 21
Joined: Thu Mar 20, 2014 2:31 am

Mon Sep 15, 2014 12:49 pm

Hello,

Does it means that Spire is not thread safe with unicode and true type fonts? Do you have an alternative if I need unicode support?

Thank you

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

Tue Sep 16, 2014 7:21 am

Hello,
Thanks for your inquiry.
Our product is not thread safe with Unicode and true type fonts but we have posted the issue to our Dev team to find an alternative solution, once there are any update, we will let you know immediately.
Sincerely,
Lisa
E-iceblue support team
User avatar

lisa.chen
 
Posts: 21
Joined: Thu Mar 20, 2014 2:31 am

Mon Sep 29, 2014 8:08 am

hi,
The issue about the true type fonts of pdf has been resolved .Please download and use below code to test the new hotfix:
http://www.e-iceblue.com/downloads/hot_fix/spire.pdf_hotfix_3.1.39.zip.
the code:

Code: Select all
    private void button1_Click(object sender, EventArgs e)
        {


            for (int i = 0; i < 10; i++)
            {
                string filname = "test" + i.ToString() + ".pdf";
                Task t = new Task(() =>
                {
                    CreatePDF(filname);
                },
                TaskCreationOptions.LongRunning /* the LongRunning option will force threads */);
                t.Start();
            }
        }

        private void CreatePDF(string filename)
        {
            PdfDocument doc = new PdfDocument();
            PdfDocument.EnableFontCache = false;
            PdfTrueTypeFont myfont = new PdfTrueTypeFont(new Font("MS Mincho", 12f, FontStyle.Regular),true);
            doc.PageSettings.Size = PdfPageSize.A6;
            PdfPageBase page = doc.Pages.Add();

            for (int i = 0; i < 10; i++)
            {
                page.Canvas.DrawString("Hello, World!", myfont, new PdfSolidBrush(Color.Black), 1, i*15);
            }

            myfont.Dispose();
            doc.SaveToFile(filename);
            doc.Close();
           
        }

If you have any questions, welcome to get it back to us.
Thanks,
Lisa
E-iceblue support team
User avatar

lisa.chen
 
Posts: 21
Joined: Thu Mar 20, 2014 2:31 am

Return to Spire.PDF