Appearance
Shared Images
When generating a PDF with multiple items that use the same image, processing the image repeatedly can negatively affect both performance and the final file size.
Consider the following scenario: you want to create a list of items, each displaying the same image. In the naive approach, for each item, the following steps occur:
- Load the image file from the file system.
- Parse the file into an image object.
- Scale and compress the image using the specified settings.
- Embed the processed image as a separate resource in the PDF document.
Because these steps are repeated for every list item, the overall process becomes inefficient, and the PDF may end up including multiple copies of the same image.
Example: inefficient image processing
Below is an example where the image is loaded and processed for each item:
c#
.Column(column =>
{
column.Spacing(15);
foreach (var i in Enumerable.Range(0, 5))
{
column.Item().Row(row =>
{
row.AutoItem().Width(24).Image("checkbox.png");
row.RelativeItem().PaddingLeft(8).AlignMiddle().Text(Placeholders.Label()).FontSize(16);
});
}
});
Solution: shared image resources
To avoid redundant processing, load the image once and reuse it across all items. This approach improves performance and reduces the final PDF file size:
c#
.Column(column =>
{
column.Spacing(15);
var image = Image.FromFile("checkbox.png");
foreach (var i in Enumerable.Range(0, 5))
{
column.Item().Row(row =>
{
row.AutoItem().Width(24).Image(image);
row.RelativeItem().PaddingLeft(8).AlignMiddle().Text(Placeholders.Label()).FontSize(16);
});
}
});