Unplanned
Last Updated: 30 Jul 2024 14:14 by Mathew
Mathew
Created on: 30 Jul 2024 14:14
Category: PdfProcessing
Type: Bug Report
6
PdfProcessing: Creating certain images in .NET Standard cause Adobe Acrobat shows "Insufficient data for an image"

This is the code for inserting the image: 

        static void Main(string[] args)
        {
            FixedExtensibilityManager.ImagePropertiesResolver = new ImagePropertiesResolver();

            //Telerik.Windows.Documents.Extensibility.JpegImageConverterBase defaultJpegImageConverter = new Telerik.Documents.ImageUtils.JpegImageConverter();
            //Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.JpegImageConverter = defaultJpegImageConverter;

            //Output("fyb-64.png", "output-working.pdf");
             Output("fyb.png", "output-broken.pdf");
        }
        private static void Output(string resourceName, string outputFileName)
        {
            var document = new RadFixedDocument();
            using (var editor = new RadFixedDocumentEditor(document))
            {
                Stream image = new FileStream(resourceName, FileMode.Open);

                var table = new Table
                {
                    LayoutType = TableLayoutType.FixedWidth,
                    Margin = new Thickness(10, 0, 0, 0),
                };

                var row = table.Rows.AddTableRow();
                var cell = row.Cells.AddTableCell();
                var block = cell.Blocks.AddBlock();
                block.InsertImage(image);
                editor.InsertTable(table);

                var pdfData = ExportToPdf(document);
                File.Delete(outputFileName);
                File.WriteAllBytes(outputFileName, pdfData);

                Process.Start(new ProcessStartInfo() { FileName = outputFileName, UseShellExecute = true });
            }
        }

        private static byte[] ExportToPdf(RadFixedDocument document)
        {
            byte[] pdfData;

            using (var ms = new MemoryStream())
            {
                var pdfFormatProvider = new PdfFormatProvider();
                pdfFormatProvider.Export(document, ms);
                pdfData = ms.ToArray();
            }

            return pdfData;
        }

Workaround: Instead of setting the ImagePropertiesResolver, set the JpegImageConverter: 

            Telerik.Windows.Documents.Extensibility.JpegImageConverterBase defaultJpegImageConverter = new Telerik.Documents.ImageUtils.JpegImageConverter();
            Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.JpegImageConverter = defaultJpegImageConverter;

0 comments