Docs / Guides

Images

SoftZenPdf places JPEG, PNG, GIF, BMP and TIFF images at any size, and turns images and scans into PDF pages. JPEG data is embedded unchanged; the other formats are decoded by SoftZenPdf's own decoders, including transparency.

Loading and placing images

PdfImage.FromFile or FromBytes loads an image; the format is detected from the content, not the file extension. DrawImage(image, x, y, width, height) stretches it to a box whose lower-left corner is (x, y); DrawImage(image, x, y, width) keeps the aspect ratio.

Program.cs
using SoftZenPdf;

var photo = PdfImage.FromFile("color-chart.jpg");
var logo = PdfImage.FromFile("logo.png");

var document = new PdfDocument();
document.AddPage().Content
    .DrawImage(photo, 72, 560, 300, 200)   // stretched to a 300 × 200 pt box
    .DrawImage(logo, 400, 600, 120);        // 120 pt wide, height from the aspect ratio

Console.WriteLine($"{photo.Format}: {photo.PixelWidth}×{photo.PixelHeight}; {logo.Format} with transparency: {logo.HasTransparency}");
document.Save("images.pdf");

Fitting an image into a box

To fit an image inside a box without distorting it, scale by its AspectRatio and centre it:

Program.cs
using SoftZenPdf;

var photo = PdfImage.FromFile("color-chart.jpg");
double boxX = 72, boxY = 500, boxWidth = 200, boxHeight = 200;

// Scale to fit inside the box without distortion, then centre it.
var width = Math.Min(boxWidth, boxHeight * photo.AspectRatio);
var height = width / photo.AspectRatio;

var document = new PdfDocument();
document.AddPage().Content
    .DrawRectangle(boxX, boxY, boxWidth, boxHeight, stroke: PdfColor.Gray, lineWidth: 0.5)
    .DrawImage(photo, boxX + (boxWidth - width) / 2, boxY + (boxHeight - height) / 2, width, height);

document.Save("fit-image.pdf");

Converting images to PDF

PdfDocument.FromImages makes a document with a page for each image file, and a page for each page of a multi-page TIFF. With a PageSize, every image is scaled to fit inside the margins and centred, and the page turns landscape for landscape images:

Program.cs
using SoftZenPdf;

// One page per image, and one per page of a multi-page TIFF, each on A4 with a half-inch margin.
var options = new ImagePageOptions { PageSize = PageSize.A4, Margin = 36 };
var document = PdfDocument.FromImages(["color-chart.jpg", "pages.tif", "fax.tif"], options);

document.Save("images-to-pdf.pdf");
Console.WriteLine($"{document.Pages.Count} pages");
  • Fit: ImageFit.Contain (the default) scales every image up or down to fill the area; ImageFit.ShrinkToFit keeps small images at their own size and only shrinks large ones.
  • AutoRotate: on by default; turn it off to keep every page in the orientation of PageSize.
  • Margin: the blank border on every side, in points.

Scans at their real size

Without a PageSize, each page is exactly the size of its image at the resolution stored in the file, so an A4 sheet scanned at 300 dpi becomes an A4 page. AddImagePage adds one image page to any document, which you can then draw on like any other page:

Program.cs
using SoftZenPdf;

// Without a page size, each page is exactly the size of the scan at its own resolution.
var document = new PdfDocument();
foreach (var scan in PdfImage.AllFromFile("fax.tif"))
{
    var page = document.AddImagePage(scan);
    Console.WriteLine($"{scan.PixelWidth}×{scan.PixelHeight} px at {scan.HorizontalResolution} dpi, {scan.BitsPerComponent}-bit: {page.Width:F0}×{page.Height:F0} pt");
}
document.Save("scan.pdf");
Output
2600×24 px at 200 dpi, 1-bit: 936×9 pt

HorizontalResolution and VerticalResolution come from the file (JPEG JFIF or EXIF data, PNG pHYs, the BMP header, TIFF resolution tags). Images without one, such as GIFs, are measured at DefaultResolution (96 dpi unless you change it). A page larger than PDF allows (200 inches) is scaled down. Black-and-white scans stay at one bit per pixel, so a fax page adds only a few kilobytes.

Orientation

Cameras often store photos sideways with an EXIF orientation tag, and TIFF files can carry the same tag. SoftZenPdf applies it when drawing, so photos and scans appear upright, and AspectRatio describes the displayed image. PixelWidth and PixelHeight describe the stored pixels, and Orientation gives the tag's value.

Transparency

PNG alpha channels and tRNS transparency, GIF transparent colours, TIFF alpha channels and BMP alpha masks become a soft mask, so content behind the image shows through. HasTransparency tells you whether an image has one.

Reuse and file size

  • Each PdfImage object is stored once per document, however many times you draw it; reuse the object for logos and repeated graphics.
  • JPEG data is copied as-is, so a JPEG adds roughly its own file size. SoftZenPdf does not downsample, so resize large photos before embedding them.
  • Other formats are re-compressed with Flate at 8 bits per component, except black-and-white images, which are stored at one bit per pixel.

Supported formats

  • JPEG: baseline and progressive, 8 bits per component, greyscale, RGB or CMYK. Not supported: lossless, hierarchical, arithmetic-coded and 12-bit JPEGs.
  • PNG: every standard colour type and bit depth, with palettes and interlacing. 16-bit images are stored at 8 bits per component.
  • GIF: the first frame of the file, with its transparent colour; later frames of an animation are ignored.
  • BMP: 1 to 32 bits per pixel, RLE4 and RLE8 compression, bit-field masks, top-down and OS/2 bitmaps.
  • TIFF: every page, in strips or tiles; no compression, LZW, PackBits, Deflate, CCITT fax (Modified Huffman, Group 3, Group 4) or single-strip JPEG; black-and-white, greyscale (up to 16 bits), RGB, palette and CMYK, with or without alpha.
  • Not supported: WebP, HEIC, JPEG 2000 and SVG; floating-point, YCbCr (uncompressed), Lab and old-style JPEG TIFFs; BigTIFF.

In PDF/A mode, CMYK JPEGs are refused, because the document's output intent is sRGB.

API: PdfImage, PdfDocument.FromImages, PdfDocument.AddImagePage, ImagePageOptions, ContentStreamWriter.DrawImage.