Null reference error when exporting XLSX to pdf
Posted: Fri Nov 01, 2024 12:52 am
Hello,
I am encountering an issue when attempting to convert an XLSX file to a pdf file. When attempting to export the xlsx file, the XlsxDocumentEditor.Export() method is failing and throwing a null reference exception. I have ensured all argument's fields are populated if they are not nullable when debugging. I have attempted with the code below and with the documentation recommended ImageCollection method, both give the same error.
The stack trace has some unrecognizable information, but it does seem to indicate something within OpenXml.Xlsx is having issues:
Please advise on how to proceed as I cannot debug within Export to figure out what our the cause of our issue. I have a test file which will trigger it every time, but I do not see a way to upload it to this forum post creation page.
Nick Nansen
RedSail Technologies
I am encountering an issue when attempting to convert an XLSX file to a pdf file. When attempting to export the xlsx file, the XlsxDocumentEditor.Export() method is failing and throwing a null reference exception. I have ensured all argument's fields are populated if they are not nullable when debugging. I have attempted with the code below and with the documentation recommended ImageCollection method, both give the same error.
Code: Select all
public static void ConvertExcelToPDF(string filePath, string outputPath)
{
// Use OpenXML editor to export the xlsx to pdf. This allows text in the workbook
// to be captured and saved in TextRegion of page. Previously we rendered an image
// of the worksheet which didn't capture the text values.
using (XlsxDocumentEditor editor = new XlsxDocumentEditor(filePath))
{
// When exporting, the editor would create an oddly sized page. Using DocumentLayoutSettings to
// instruct the export to create a standard (8.5x11in) sized document.
DocumentLayoutSettings documentLayoutSettings = new DocumentLayoutSettings()
{
PageLayoutSettings = new PageLayoutSettings(ImageSize.FromPaperKind(PaperSizeKind.Letter, DocumentResolution)),
};
RenderingSettings renderingSettings = new RenderingSettings(DocumentResolution);
documentLayoutSettings.EnvironmentProperties.SourceFilename = outputPath;
editor.Export(outputPath, documentLayoutSettings, renderingSettings);
}
using (PdfDocument document = new PdfDocument(outputPath))
{
// This saves the converted file with our standard compression settings.
using (new StopwatchLog($"Optimizing and saving PDF file after conversion from Excel file."))
{
SavePdfDocument(document, outputPath);
}
}
}
Code: Select all
at ᥠ.珰(ᵪ A_0)
at ᥠ.謃(뇛 A_0, ᧃ A_1, ᷮ A_2)
at .珰(뇛 A_0)
at .罓(꺌 A_0)
at 뒛.珰(꺌 A_0)
at ᢰ.罓(꺌 A_0)
at 뒛.珰(꺌 A_0)
at ᣆ.罓(꺌 A_0)
at 뒛.珰(꺌 A_0)
at ᢻ.罓(꺌 A_0)
at 뒛.珰(꺌 A_0)
at 띐.窙(봳 A_0)
at 뮲.罓(꺌 A_0)
at 뒛.珰(꺌 A_0)
at 뤿.窙(봳 A_0)
at 뮲.罓(꺌 A_0)
at 뒛.珰(꺌 A_0)
at 륫.窙(봳 A_0)
at 뮲.罓(꺌 A_0)
at 뒛.珰(꺌 A_0)
at 멨.窙(봳 A_0)
at Vintasoft.Imaging.Office.OpenXml.Xlsx.XlsxDocument.珰(멨 A_0, 뇛 A_1, 멳 A_2)
at Vintasoft.Imaging.Office.OpenXml.Xlsx.XlsxDocument.珰(릢 A_0)
at Vintasoft.Imaging.Office.OpenXml.Xlsx.XlsxDocument.LayoutCore()
at Vintasoft.Imaging.Office.OpenXml.Xlsx.XlsxDocument.get_Pages()
at Vintasoft.Imaging.Office.OpenXml.Xlsx.XlsxDocument.get_PageCount()
at Vintasoft.Imaging.Codecs.Decoders.OpenXmlDecoder`1.GetPageCount()
at Vintasoft.Imaging.Codecs.Decoders.DecoderBase.get_PageCount()
at ⭺.⚢(Stream A_0, Boolean A_1, String A_2, ⚢ A_3, ⛁ A_4)
at ⦊.⚢(Stream A_0, Boolean A_1)
at Vintasoft.Imaging.ImageCollection.Insert(Int32 index, Stream stream, Boolean closeStreamAfterUse)
at Vintasoft.Imaging.ImageCollection.Insert(Int32 index, Stream stream)
at Vintasoft.Imaging.ImageCollection.Add(Stream stream)
at Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentEditor.Export(Stream stream, EncoderBase encoder, DocumentLayoutSettings layoutSettings, RenderingSettings renderingSettings)
at Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentEditor.Export(String filename, DocumentLayoutSettings layoutSettings, RenderingSettings renderingSettings)
at IS.WM.Architecture.Utilities.VintaSoftPDFLibrary.ConvertExcelToPDF(String filePath, String outputPath) in D:\Git\DocuTrack\Projects\Architecture\DocumentImagingUtilities\VintaSoftPDFLibrary.cs:line 1436
Nick Nansen
RedSail Technologies