Compress file size

Stimulsoft Reports.NET discussion
Post Reply
schoenhalsiwm
Posts: 4
Joined: Tue Jun 25, 2024 10:29 am

Compress file size

Post by schoenhalsiwm »

Hey, is it possible to somehow compress the StiReport and thus reduce the size? I attach a PDF to a report. I read the PDF and convert each page of the PDF into a bitmap image. This image becomes a StiImage and I add the StiImage to a StiPage. All the StiPages then end up in the StiReport. Unfortunately, the PDF becomes extremely large when exported > 20MB. My guess is that the images are very large and are not converted efficiently. Do you know of another alternative for attaching PDF pages? Or maybe compressing the size at the end of the StiReport
Lech Kulikowski
Posts: 6651
Joined: Tue Mar 20, 2018 5:34 am

Re: Compress file size

Post by Lech Kulikowski »

Hello,

Please send us a sample report with test data that reproduces the issue for analysis.

Thank you.
schoenhalsiwm
Posts: 4
Joined: Tue Jun 25, 2024 10:29 am

Re: Compress file size

Post by schoenhalsiwm »

I am using the following code, and call it right after the report ist generated out of Sage 100. The pdf i want to add, is a normal pdf with text in it.

Code: Select all

  public static void AddPage(int id, Image bild, StiReport report, ExtraPages zd)
        {
            
            //report.Document
            var page = new StiPage(report)
            {
                Margins = new StiMargins((double)zd.AbstandLinks, (double)zd.AbstandRechts, (double)zd.AbstandOben, (double)zd.AbstandUnten)
            };

            var image = new StiImage()
            {
                Name = "xtra" + id,
                HorAlignment = zd.Zentrieren ? Stimulsoft.Base.Drawing.StiHorAlignment.Center : Stimulsoft.Base.Drawing.StiHorAlignment.Left,
                VertAlignment = zd.Zentrieren ? Stimulsoft.Base.Drawing.StiVertAlignment.Center : Stimulsoft.Base.Drawing.StiVertAlignment.Top,
                Top = 0,
                Left = 0,
                Width = page.Width,
                Height = page.Height,
                AspectRatio = zd.Verhaeltnis,
                Stretch = zd.Strecken,
                Image = bild
            };
            
            page.Components.Add(image);
            report.Pages.Add(page);
        }

        public static void AddPdf(ExtraPages zd, StiReport report)
        {
            using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
            {
                processor.LoadDocument(zd.Dokument, true);

                for (int i = 1; i <= processor.Document.Pages.Count && i <= 10; i++)
                {
                    Bitmap image = processor.CreateBitmap(i, zd.PDFQualitaet);
                    AddPage(zd.ID * 100 + i, image, report, zd);
                }
            }
        }
    }
visitsquare
Posts: 1
Joined: Wed Jul 10, 2024 9:27 am

Re: Compress file size

Post by visitsquare »

When you convert each page of the PDF into a bitmap image, you should ensure that the image is optimized in terms of resolution and quality. Here’s an example of how you can do that:
using System.Drawing.Imaging;

public static Bitmap OptimizeBitmap(Bitmap originalBitmap, long quality)
{
var encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);

var codec = ImageCodecInfo.GetImageDecoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid);

using (var stream = new MemoryStream())
{
originalBitmap.Save(stream, codec, encoderParameters);
return new Bitmap(stream);
}
}
Integrate the OptimizeBitmap function into your AddPdf method to ensure that each bitmap image is compressed before being added to the report:
public static void AddPdf(ExtraPages zd, StiReport report)
{
using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
processor.LoadDocument(zd.Dokument, true);

for (int i = 1; i <= processor.Document.Pages.Count && i <= 10; i++)
{
Bitmap originalImage = processor.CreateBitmap(i, zd.PDFQualitaet);
Bitmap optimizedImage = OptimizeBitmap(originalImage, 50L); // Adjust quality as needed (0-100)
AddPage(zd.ID * 100 + i, optimizedImage, report, zd);
}
}
}
Stimulsoft Reports have built-in settings for image compression. You can adjust these settings to ensure the images are compressed when exporting the report:
report.ExportSettings.Pdf.CompressionMethod = Stimulsoft.Report.Export.StiPdfImageCompressionMethod.Jpeg;
report.ExportSettings.Pdf.JpegQuality = 50; // Adjust quality as needed (0-100)
schoenhalsiwm
Posts: 4
Joined: Tue Jun 25, 2024 10:29 am

Re: Compress file size

Post by schoenhalsiwm »

Hello, first of all, thank you for your help. Unfortunately, that didn't help. The images are extremely large, which I don't understand. Apparently they don't need to be compressed before being inserted into the STIReport, but rather in the StiReport. I also have no way of accessing ExportSettings.
Lech Kulikowski
Posts: 6651
Joined: Tue Mar 20, 2018 5:34 am

Re: Compress file size

Post by Lech Kulikowski »

Hello,

Please send sample images that you insert in the report for analysis.

Also, in Net-version we have several picture compression settings in pdf-export: Jpeg, Flate, Indexed, Monochrome. And depending on the content of pictures different methods can compress differently, it is necessary to choose for your content of pictures.

Thank you.
schoenhalsiwm
Posts: 4
Joined: Tue Jun 25, 2024 10:29 am

Re: Compress file size

Post by schoenhalsiwm »

Hello.
you can take any pdf you've got, that has mostly text in it. It's like a Terms and Conditions pdf i want to add to my original pdf.
Lech Kulikowski
Posts: 6651
Joined: Tue Mar 20, 2018 5:34 am

Re: Compress file size

Post by Lech Kulikowski »

Hello,

It all depends on the content of your files, and on what settings you use to convert pdf to pictures. also, you didn't specify whether your pictures are color, gray or monochrome? and what are the pdf-export settings?
We can give you a more precise answer if you send us a simple test project on which we can analyze the whole process of creating your output file.

Thank you.
Post Reply