Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Mon Sep 09, 2019 1:30 pm

got this error while testing your free.spire, downloaded directly from nuget inside visual studio

I believe your library has some scaling logic errors.... please fix?

symptom:
whenever saving/converting XLSX to PDF with scaling/page-adjustment, the GDI+ exception is thrown

steps tried:
(1) I've tried both the sync and async approach multiple times, all with the same result: GDI+ exceptions
(2) spire lib I used is: FreeSpire.Office (latest stable 4.3.1) from nuget (installed directly from VisualStudio)
(3) DOCX to PDF conversion is working as expected.
(4) "SrcList" is a list with several office files (docx + xlsx)
(5) I've tried re-order the file list, so the XLSX-PDF conversion could occur first, but still resulting GDI+ exceptions
(6) the "obsolete" approach (commented-out) which using PdfConverter also causes GDI+ exceptions.
(7) ONLY SUCCESS: directly save as PDF, no conversion settings used will get the success file saving result.

here're some of my code snippets
Code: Select all
        public static void DoTest()
        {
            List<Task> tasks = new List<Task>();
            int count = 0;
            foreach (var v in SrcList)
            {
                if (string.IsNullOrWhiteSpace(v)) continue;
                count++;
                var name = Path.GetFileName(v);
                var ext = Path.GetExtension(v);
                Logger.Info("try converting {name} ({ext}) to PDF ...", name, ext);
                var dest = Path.ChangeExtension(v, ".spire.conversion.test.pdf");
                if (File.Exists(dest)) File.Delete(dest);

                switch (ext.ToLowerInvariant())
                {
                    case ".docx":
                        DocxToPdf(v, dest);
                        //tasks.Add(Task.Run(() => DocxToPdf(v, dest)));
                        break;
                    case ".xlsx":
                        XlsxToPdf(v, dest);
                        //tasks.Add(Task.Run(() => XlsxToPdf(v, dest)));
                        break;
                    default:
                        Logger.Error("unrecognized file extension: {ext} of file: {v}", ext, v);
                        break;
                }
            }
            Logger.Info("wait for all conversion tasks [{count}] to complete...", count);
            Stopwatch watch = Stopwatch.StartNew();
            Task.WhenAll(tasks).Wait();
            Logger.Info("all conversion (spire test) done... time elapsed: {watch}ms", watch.ElapsedMilliseconds);
            watch.Stop();
        }


Code: Select all
        private static void XlsxToPdf(string src, string dest)
        {
            using (Workbook workbook = new Workbook())
            //using (PdfDocument pdfDocument = new PdfDocument())
            {
                // load Excel file
                workbook.LoadFromFile(src);
                //workbook.SaveToFile(dest, Spire.Xls.FileFormat.PDF); // ONLY SUCCESS: only saving files to PDF directly without adjustment can succeed
                //// set pdf template (A4 paper)
                //pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape;
                //pdfDocument.PageSettings.Width = 970;
                //pdfDocument.PageSettings.Height = 850;
                //using (PdfConverter pdfConverter = new PdfConverter(workbook))
                //{
                //    PdfConverterSettings settings = new PdfConverterSettings();
                //    settings.TemplateDocument = pdfDocument;
                //    var result = pdfConverter.Convert(settings);
                //    result.SaveToFile(dest);
                //}
                PdfConverterSettings settings = new PdfConverterSettings();
                settings.FitSheetToOnePage = FitToPageType.ScaleWidthDifferentFactor;
                workbook.SaveToFile(dest, settings);
                Logger.Info($"[{Thread.CurrentThread.ManagedThreadId}] done XLSX-PDF conversion " +
                            $"[ src:'{Path.GetFileName(src)}', dest:'{Path.GetFileName(dest)}', location:'{Path.GetDirectoryName(dest)}' ]");
            }
        }



----------------------------
project: windows console app
ide: visual studio 2019 pro
os: win 10, 1903, x64
lang: .net framework 4.7.2
testing files: several xlsx saved using MS office 365

all files:
(a) non-english (chinese) chars
(b) single sheet
(c) within free license(version) range

li.bo.jundo
 
Posts: 1
Joined: Mon Sep 09, 2019 1:04 pm

Tue Sep 10, 2019 2:45 am

Hi,

Thanks for your inquiry.
Note the method SaveToFile(this Workbook workbook, string fileName, PdfConverterSettings settings); is obsolete in our latest version, we don't maintain it anymore. Hope you could understand. Besides, I noticed you want to set FitSheetToOnePage , please refer to following code:
Code: Select all
            foreach (Worksheet sheet in workbook.Worksheets)
            {
                sheet.PageSetup.IsFitToPage = true;
            }
            //use this method which works successfully
            workbook.SaveToFile(@"result.pdf", Spire.Xls.FileFormat.PDF);


Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.XLS