๐ผ๏ธ Images in PDFs
JPEG and PNG images with alt text, bounding boxes, and aspect-preserving scaling.
C# โ Accessible Images
// Load image from file
var image = PdfImage.FromFile("photo.jpg");
// Create figure structure with alt text
var figure = root.AddChild(StructureType.Figure,
"Company logo on blue background");
// REQUIRED for PDF/UA: bounding box
figure.BBox = new[] {
72.0, 500.0, 300.0, 680.0
};
// Tag the image content
page.BeginTaggedContent(figure);
page.AddImage(image, 72, 500, 228, 180);
page.EndTaggedContent();
// Scale to fit preserving aspect ratio
var photo = PdfImage.FromFile("landscape.jpg");
var photoFig = root.AddChild(StructureType.Figure,
"Sunset over the ocean");
photoFig.BBox = new[] {
72.0, 250.0, 472.0, 450.0
};
page.BeginTaggedContent(photoFig);
page.AddImageScaled(photo,
72, 250, maxWidth: 400, maxHeight: 200);
page.EndTaggedContent();
You should see a bold "Working with Images" heading, a short introduction paragraph, and a light blue-grey filled rectangle (228 ร 180 pt) with a thin grey border representing an image placeholder. Below it, three lines of Courier code show the API calls needed to embed a real JPEG or PNG.
File: 09_images.pdf
Supported Formats
| Format | Method | Notes |
|---|---|---|
| JPEG | PdfImage.FromFile("photo.jpg") | DCTDecode passthrough โ no re-encoding |
| PNG | PdfImage.FromFile("logo.png") | Deconstructed with row filters, alpha โ SMask |
| Stream | PdfImage.FromStream(stream) | Any seekable stream |
| Bytes | PdfImage.FromBytes(data) | In-memory byte array |