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.

Thu Nov 12, 2020 11:16 pm

Hi,

I have a model in docx, after mailmerge i save it in pdf format. The model use a custom font 'Century Gothic, installed on server and copied into docx.

Randomly (non every time happen an error, you will find a copy of stack trace.
If you insist end make the same action more times at last you will be able to save the file correctely.

Best Regards.

>System.NullReferenceException: Object reference not set to an instance of an object.
>
>Server stack trace:
> at spr᯶.ᜁ(String A_0, String A_1, Single A_2, FontStyle A_3)
> at spr᯶.ᜃ(String A_0, Single A_1, FontStyle A_2, CharacterFormat A_3)
> at spr᯶.ᜂ(String A_0, Single A_1, FontStyle A_2, CharacterFormat A_3)
> at spr᯶.ᜀ(TextRange A_0, CharacterFormat A_1, RectangleF& A_2, Boolean A_3, sprគ A_4)
> at spr᯶.ᜀ(TextRange A_0, CharacterFormat A_1, RectangleF& A_2, Boolean A_3)
> at spr᯶.ᜀ(TextRange A_0, Boolean A_1)
> at Spire.Doc.Fields.TextRange.Spire.Doc.Layout.IStringWidget.GetTextAscentDescent(spr᯶ dc, Boolean isIgnoreSubsuperScript)
> at sprទ.ᜀ(spr᯶ A_0, Double& A_1, Double& A_2, Double& A_3)
> at sprទ.ᜀ(spr᯶ A_0, OfficeMath& A_1)
> at sprហ.ᜀ(sprទ A_0)
> at sprហ.ᜃ()
> at sprស.ᜂ(sprឞ A_0)
> at sprស.ᜃ(sprឞ A_0)
> at sprស.ᜌ(sprឞ A_0)
> at sprហ.ᜄ(sprឞ A_0)
> at sprស.ᜊ(sprឞ A_0)
> at sprស.ᜁ(RectangleF A_0)
> at sprស.ᜋ(sprឞ A_0)
> at sprស.ᜁ(RectangleF A_0)
> at sprន.ᜀ(sprឋ A_0, sprច A_1, spr᯶ A_2)
> at sprᯮ.ᜋ()
> at sprᯮ.ᜒ()
> at sprᯮ.ᜓ()
> at sprᯮ.ᜀ(IDocument A_0)
> at sprᰙ.ᜀ(Document A_0)
> at sprᰙ.ᜀ(Document A_0, ToPdfParameterList A_1)
> at Spire.Doc.Document.ᜀ(Stream A_0, ToPdfParameterList A_1)
> at Spire.Doc.Document.SaveToStream(Stream stream, ToPdfParameterList paramList)
> at Sofistar.Server.Consultori.DataAccess.InvoiceExtensions.MakeInvoiceData(Invoice invoice) in C:\\Users\\lbenvegnu\\Source\\Repos\\ccf\\Sofistar.Server.Consultori\\Consultori.DataAccess\\DataManipulator\\InvoiceExtensions.cs:line 45
> at Sofistar.Server.Consultori.DataAccess.CreateInvoiceDataPostUpdateInvoiceDataWriter.<>c__DisplayClass2_0.<ConfigureWriter>b__0() in C:\\Users\\lbenvegnu\\Source\\Repos\\ccf\\Sofistar.Server.Consultori\\Consultori.DataAccess\\DataWriter\\Invoice\\CreateInvoiceDataPostUpdateInvoiceDataWriter.cs:line 24
> at Sofistar.Server.Common.Utility.DataSource.DataSourceFactory.DataModificator.Execute()
>--- End of stack trace from previous location where exception was thrown ---

ilbenve71
 
Posts: 3
Joined: Thu Nov 12, 2020 11:08 pm

Fri Nov 13, 2020 3:46 am

Hello,

Thanks for your inquiry!

I simulated and tested a Word file with the latest Spire.Doc v8.10.4.0, but did not reproduce your issue. To help us reproduce your issue, please provide your input file, full test code, font file and share us with your system information (E.g. Win7, 64 bit), region setting (E.g. China, Chinese). Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Sun Nov 15, 2020 10:51 pm

Thank you for your answer,

I can add the sequece of methods call to generate the final pdf. I also attach the model file and the font file used.
To make mailmerge i use some object (POCO object) to the library. All the property are strings and none is set to null, at list the are empty string.
After code, I add definiscion of the classes usated.

Best regard.
Luca Benvegnù

CODE:

public static InvoiceData MakeInvoiceData(this Invoice invoice) {
InvoiceConfig _settings = AppSettingsProxy.Instance.CreateProxy<InvoiceConfig>(nameof(InvoiceConfig));
byte[] data = invoice.InvoiceTemplate.InvoiceTemplateData.data;
using (MemoryStream inputStream = new MemoryStream(data)) {
Document document = new Document();
document.LoadFromStream(inputStream, FileFormat.Docx);
List<InvoiceServiceInfo> invoiceServiceInfos = MakeInvoiceServiceInfos(invoice);
MailMergeDataTable invoiceServiceData = new MailMergeDataTable("Service", invoiceServiceInfos);
document.MailMerge.ExecuteGroup(invoiceServiceData);
List<InvoicePaymentInfo> invoicePaymentInfos = MakeInvoicePaymentInfos(invoice);
MailMergeDataTable invoicePaymentData = new MailMergeDataTable("Payment", invoicePaymentInfos);
document.MailMerge.ExecuteGroup(invoicePaymentData);
InvoiceInfo invoiceInfo = MakeInvoiceInfo(invoice);
string[] names = ObjectToFieldNames(invoiceInfo);
string[] values = ObjectToFieldValues(invoiceInfo);
document.MailMerge.Execute(names, values);
using (MemoryStream outputStream = new MemoryStream()) {
ToPdfParameterList paramlist = new ToPdfParameterList();
if (_settings.IncludedFonts != null && _settings.IncludedFonts.Trim().Length > 0) {
string[] includedFonts = _settings.IncludedFonts.Split(',');
foreach (string includedFont in includedFonts.OrEmpty()) {
if (includedFont != null && includedFont.Trim().Length > 0) {
paramlist.EmbeddedFontNameList.Add(includedFont.Trim());
}
}
}
document.SaveToStream(outputStream, paramlist);
InvoiceData invoiceData = new InvoiceData() {
c_organization = invoice.c_organization,
id_invoice = invoice.id_invoice,
data = outputStream.ToArray()
};
return invoiceData;
}
}
}


class InvoiceInfo {
public string BillingTo { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Zip { get; set; }
public string InvoiceCode { get; set; }
public string InvoiceOffice { get; set; }
public string Consultant { get; set; }
public string InvoiceDate { get; set; }
public string PaymentNote { get; set; }
public string PaymentType { get; set; }
public string InvoiceNote { get; set; }
public string InvoiceAmount { get; set; }
}

class InvoiceServiceInfo {
public string ServiceDate { get; set; }
public string ServiceDescription { get; set; }
public string ServiceHours { get; set; }
public string ServicePage { get; set; }
public string ServiceRate { get; set; }
public string ServiceAmount { get; set; }
}

class InvoicePaymentInfo {
public string PaymentLabel { get; set; }
public string PaymentDescription { get; set; }
}

ilbenve71
 
Posts: 3
Joined: Thu Nov 12, 2020 11:08 pm

Mon Nov 16, 2020 10:20 am

Hello,

Thanks for providing your file.

I tested your file with the latest Spire.Doc v8.10.4, but did not reproduce your issue. This is my test code and result file.Did you update to 8.10.4? If not, please do it.
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

using Spire.Doc;
using System.IO;
using Spire.Pdf;
using Spire.Doc.Reporting;
using System.Reflection;

namespace test
{
    class Program
    {

        static void Main(string[] args)
        {
            using (MemoryStream inputStream = new MemoryStream())
            {
                Document document = new Document();
                document.LoadFromFile(@"F:\recFile\Upload\ModelloFattura_CFACF_4_Singolo.docx");


                List<InvoiceServiceInfo> invoiceServiceInfos = new List<InvoiceServiceInfo>();
                InvoiceServiceInfo invoiceServiceInfo = new InvoiceServiceInfo();
                invoiceServiceInfo.ServiceAmount = "Service Amount";
                invoiceServiceInfo.ServiceDate = "Service Date";
                invoiceServiceInfo.ServiceDescription = "Service Description";
                invoiceServiceInfo.ServiceHours = "Service Hours";
                invoiceServiceInfo.ServicePage = "Service Page";
                invoiceServiceInfo.ServiceRate = "Service Rate";
                invoiceServiceInfos.Add(invoiceServiceInfo);
                MailMergeDataTable invoiceServiceData = new MailMergeDataTable("Service", invoiceServiceInfos);
                document.MailMerge.ExecuteGroup(invoiceServiceData);


                List<InvoicePaymentInfo> invoicePaymentInfos = new List<InvoicePaymentInfo>();
                InvoicePaymentInfo invoicePaymentInfo = new InvoicePaymentInfo();
                invoicePaymentInfo.PaymentDescription = "Payment Description";
                invoicePaymentInfo.PaymentLabel = "Payment  Label";
                invoicePaymentInfos.Add(invoicePaymentInfo);
                MailMergeDataTable invoicePaymentData = new MailMergeDataTable("Payment", invoicePaymentInfos);
                document.MailMerge.ExecuteGroup(invoicePaymentData);


                InvoiceInfo invoiceInfo = new InvoiceInfo();
                invoiceInfo.Address = "adress";
                invoiceInfo.BillingTo = "billing to";
                invoiceInfo.City = "city";
                invoiceInfo.Consultant = "consultant";
                invoiceInfo.Country = "country";
                invoiceInfo.InvoiceAmount = "invoice amount";
                invoiceInfo.InvoiceCode = "invoice code";
                invoiceInfo.InvoiceDate = "invoice date";
                invoiceInfo.InvoiceNote = "invoice note";
                invoiceInfo.InvoiceOffice = "invoice office";
                invoiceInfo.PaymentNote = "payment note";
                invoiceInfo.PaymentType = "paument type";
                invoiceInfo.Zip = "zip";
                string[] names = ObjectToFieldNames(invoiceInfo);
                string[] values = ObjectToFieldValues(invoiceInfo);
                document.MailMerge.Execute(names, values);



                using (MemoryStream outputStream = new MemoryStream())
                {
                    ToPdfParameterList paramlist = new ToPdfParameterList();

                    paramlist.EmbeddedFontNameList.Add("Century Gothic");

                    document.SaveToStream(outputStream, paramlist);
                    PdfDocument pdf = new PdfDocument();
                    pdf.LoadFromStream(outputStream);
                    pdf.SaveToFile("result.pdf");

                    System.Diagnostics.Process.Start("result.pdf");
                }
            }}

        private static string[] ObjectToFieldValues(InvoiceInfo invoiceInfo)
        {
            string[] res = new string[13];

            PropertyInfo[] propertyInfos = invoiceInfo.GetType().GetProperties();
            string str = "";
            int i = 0;
            foreach (PropertyInfo property in propertyInfos)
            {
                str = property.Name;
                res[i] = str;
                i++;
            }

            return res;
        }

        private static string[] ObjectToFieldNames(InvoiceInfo invoiceInfo)
        {
            string[] res = new string[13];
            res[0] = invoiceInfo.BillingTo;
            res[1] = invoiceInfo.Address;
            res[2] = invoiceInfo.City;
            res[3] = invoiceInfo.Country;
            res[4] = invoiceInfo.Zip;
            res[5] = invoiceInfo.InvoiceCode;
            res[6] = invoiceInfo.InvoiceOffice;
            res[7] = invoiceInfo.Consultant;
            res[8] = invoiceInfo.InvoiceDate;
            res[9] = invoiceInfo.PaymentNote;
            res[10] = invoiceInfo.PaymentType;
            res[11] = invoiceInfo.InvoiceNote;
            res[12] = invoiceInfo.InvoiceAmount;
            return res;
        }
    }

    class InvoiceInfo
    {
        public string BillingTo { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
        public string Zip { get; set; }
        public string InvoiceCode { get; set; }
        public string InvoiceOffice { get; set; }
        public string Consultant { get; set; }
        public string InvoiceDate { get; set; }
        public string PaymentNote { get; set; }
        public string PaymentType { get; set; }
        public string InvoiceNote { get; set; }
        public string InvoiceAmount { get; set; }
    }

    class InvoiceServiceInfo
    {
        public string ServiceDate { get; set; }
        public string ServiceDescription { get; set; }
        public string ServiceHours { get; set; }
        public string ServicePage { get; set; }
        public string ServiceRate { get; set; }
        public string ServiceAmount { get; set; }
    }

    class InvoicePaymentInfo
    {
        public string PaymentLabel { get; set; }
        public string PaymentDescription { get; set; }
    }
}

If the issue persists, please offer us your runnable project and tell us your region setting (e.g. China, Chinese) for further investigation. Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Thu Nov 26, 2020 12:13 am

Hi,
Sorry. I spent some time to make tests.
The broblem is not solved, it happen randomly but still happens.
I tried to change the saving part of the code. (you will see at the end of the post, also with this code the font stay in the result pdf).
The application is a WebApi on framework 4.7. I'm using your library Free Spire Doc 7.1.1 (that should be the last version available).
Everithing is working on IIS and is deployed on windows server 2019 standard edition.
The problem look like it appears after I added the font of the model.

I hope you will find a solution of my problem.
Best regard.


using (MemoryStream outputStream = new MemoryStream()) {
document.SaveToStream(outputStream, FileFormat.PDF);
InvoiceData invoiceData = new InvoiceData() {
c_organization = invoice.c_organization,
id_invoice = invoice.id_invoice,
data = outputStream.ToArray()
};

ilbenve71
 
Posts: 3
Joined: Thu Nov 12, 2020 11:08 pm

Thu Nov 26, 2020 10:39 am

Hello,

Thanks for your reply!

Kindly note that the latest Free Spire.Doc version is Free Spire.Doc v7.11 and I simulated a test with both document.SaveToFile("result1.pdf", Spire.Doc.FileFormat.PDF) and document.SaveToFile("result1.pdf", paramlist) methods, but neither reproduce your issue. Here is my test code and result file.

Code: Select all
                    ToPdfParameterList paramlist = new ToPdfParameterList();
                    paramlist.PrivateFontPaths = new List<PrivateFontPath>()
                    {
                        new PrivateFontPath("Century Gothic",FontStyle.Regular, @"E:\recFile\Upload\GOTHIC.ttf"),
                        new PrivateFontPath("Century Gothic",FontStyle.Bold, @"E:\recFile\Upload\GOTHICB.ttf")
                    };
                    paramlist.IsEmbeddedAllFonts = true;

                    document.SaveToFile("result1.pdf", Spire.Doc.FileFormat.PDF);
                    document.SaveToFile("result2.pdf", paramlist);

Therefore, please offer us your whole runnable project and information of the system where program ran unsuccessfully(E.g. Win7, 64 bit) to help us reproduce your issue. Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Mon Nov 30, 2020 10:26 am

Hello,

Hope you are doing well.

How is your issue now? If it still exists, could you please provide your whole runnable project and information of the system where program ran unsuccessfully(E.g. Win7, 64 bit) to us for further investigation?

Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.Doc