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 Nov 15, 2024 10:48 am

The following code throws "System.NullReferenceException: 'Object reference not set to an instance of an object.'" with a specific PDF file I am trying to convert to SVG.

It is specifically the SaveToFile function that throws the exception.

I have the PDF file but it contains sensive data so I need a secure upload space to send it to you.

Using FreeSpire.PDF version 10.2.0 for .Net Standard 2.0
Running on Windows 11.
Visual Studio 2022
.Net 7

Code: Select all
            using (var pdfStream = new MemoryStream(Convert.FromBase64String(pdfBase64)))
            {
                PdfDocument document = new PdfDocument();

                document.LoadFromStream(pdfStream);

                var path = Path.GetTempPath() + @"PDF2SVGconvertions\";
                Directory.CreateDirectory(path);
                var workingFolder = Directory.CreateDirectory(Path.Combine(path, Guid.NewGuid().ToString()));
                var workingFolderPath = workingFolder.FullName;

                var fileName = Path.Combine(workingFolderPath, Guid.NewGuid().ToString() + ".svg");
                document.SaveToFile(fileName, FileFormat.SVG);

            }

hholm720
 
Posts: 3
Joined: Fri Nov 15, 2024 10:30 am

Fri Nov 15, 2024 2:52 pm

Seems like the PDF is a PDF/A format. If I open in Acrobat and choose to make it editable and save it it will not throw an error when saving to SVG.

Is there a way to remove the PDF/A flag with FreeSpire.PDF?

hholm720
 
Posts: 3
Joined: Fri Nov 15, 2024 10:30 am

Mon Nov 18, 2024 7:54 am

Hi,

Thank you for your inquiry.
Please refer to code below to convert PDFA to PDF.
And kindly note the latest commercial version of our Spire.PDF is 10.11.1. Since the commercial version includes numerous fixes and we rarely maintain the free version, we recommend testing with the latest commercial version. If you encounter any issues even with the latest commercial version, please send the test file to our email at support@e-iceblue.com for further investigation. Thank you for your assistance.

Spire.PDFfor.NETStandard:https://www.nuget.org/packages/Spire.PDFfor.NETStandard/10.11.1
Spire.PDF:https://www.nuget.org/packages/Spire.PDF/10.11.1

Code: Select all
            //Pdf file
            String input = @"Data\SamplePDFA.pdf";

            //Open pdf document
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(input);

            //Create a new pdf and draw content on new file
            PdfNewDocument newDoc = new PdfNewDocument();
            newDoc.CompressionLevel = PdfCompressionLevel.None;

            foreach (PdfPageBase page in doc.Pages)
            {
                SizeF size = page.Size;
                PdfPageBase p = newDoc.Pages.Add(size, new Spire.Pdf.Graphics.PdfMargins(0));
                page.CreateTemplate().Draw(p, 0, 0);
            }


            string output = "PDFAToPdf-result.pdf";

            newDoc.Save(output);
            newDoc.Close();


Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 409
Joined: Mon Nov 07, 2022 8:10 am

Mon Nov 18, 2024 9:00 am

<ItemGroup>
<PackageReference Include="Spire.PDF" Version="10.11.1" />
</ItemGroup>

Updated reference and tested your code. It seems that the PDF/A flag is removed but still the document cannot be converted to SVG. Throws the 'Object reference not set to an instance of an object.' exception.

Using Guids as file name to get random names on output files to not have to clean up all the time.

I've attached the code I try below. I will also send the document to the provided email and reference this thread.

Code: Select all
    private static void SpireFunc()
    {
        string input = @"Data\Faulty.pdf";
        string output = Path.Combine("Data", Guid.NewGuid().ToString() + ".pdf");

        PdfDocument doc = new PdfDocument();
        doc.LoadFromFile(input);

        //Create a new pdf and draw content on new file
        var newDoc = new PdfNewDocument
        {
            CompressionLevel = PdfCompressionLevel.None
        };

        foreach (PdfPageBase page in doc.Pages)
        {
            SizeF size = page.Size;
            PdfPageBase p = newDoc.Pages.Add(size, new Spire.Pdf.Graphics.PdfMargins(0));
            page.CreateTemplate().Draw(p, 0, 0);
        }

        newDoc.Save(output);
        newDoc.Close();

        var outputDocument = new PdfDocument();
        outputDocument.LoadFromFile(output);

        var fileName = Path.Combine("Data", Guid.NewGuid().ToString() + ".svg");
        outputDocument.SaveToFile(fileName, FileFormat.SVG);
    }

hholm720
 
Posts: 3
Joined: Fri Nov 15, 2024 10:30 am

Mon Nov 18, 2024 10:01 am

Hi,

Thank you for your prompt reply.
I have received the file you sent. I will conduct further investigation and reply to you via email.

Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 409
Joined: Mon Nov 07, 2022 8:10 am

Return to Spire.PDF

cron