New to Telerik Document Processing? Download free 30-day trial

How to Generate a PDF Document from Images with FixedContentEditor


Version Product Author
2024.1.124 RadPdfProcessing Desislava Yordanova


This tutorial demonstrates a sample approach how to generate a PDF document from a collection of images located in a local folder.

Folder with images


To create the PDF document, we will use a FixedContentEditor which is always associated with a single RadFixedPage. The editor maintains an internal Position inside the page at which the image block element will be inserted and drawn. The Position is adjusted after the image is rendered. If there is no remaining space on the page to draw the next image, a new page will be created and the editor's position will be moved to the beginning of the new page.

        private static void GeneratePdfFromImagesWithFixedContentEditor(string imageFolderPath)
            Size pageSize = new Size(Unit.MmToDip(210), Telerik.Windows.Documents.Media.Unit.MmToDip(297));
            Padding pageMarginsValue = new Telerik.Windows.Documents.Primitives.Padding(
            RadFixedDocument fixedDocument = new RadFixedDocument();
            RadFixedPage fixedPage = fixedDocument.Pages.AddPage();
            fixedPage.Size = pageSize;
            FixedContentEditor fixedEditor = new FixedContentEditor(fixedPage);
            double currentYposition = pageMarginsValue.Top;
            double pageYLimit = pageSize.Height - pageMarginsValue.Bottom;
            string[] imageFiles = Directory.GetFiles(imageFolderPath);

            foreach (string imageFilePath in imageFiles)
                Block imageBlock = new Block();
                imageBlock.SpacingAfter = 0;
                imageBlock.HorizontalAlignment = HorizontalAlignment.Center;
                FileStream fileStream = new FileStream(imageFilePath, FileMode.Open);
                var _imageSource = new Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource(fileStream);
                Size imageBlockDesiredSize = imageBlock.Measure();

                double desiredYLocationToDraw = currentYposition + imageBlockDesiredSize.Height;
                if (desiredYLocationToDraw > pageYLimit)
                    //indicate the page end [optional]
                    fixedEditor.Position.Translate(0, pageYLimit - Unit.MmToDip(5));
                    //above code is just for testing purposes

                    currentYposition = pageMarginsValue.Top;
                    fixedPage = fixedDocument.Pages.AddPage();
                    fixedPage.Size = pageSize;
                    fixedEditor = new FixedContentEditor(fixedPage);
                    //move the cursor to the beginning of the new page
                    fixedEditor.Position.Translate(pageMarginsValue.Left, currentYposition);
                currentYposition += imageBlock.ActualSize.Height;
                fixedEditor.Position.Translate(pageMarginsValue.Left, currentYposition);

            PdfFormatProvider provider = new PdfFormatProvider();
            string outputFilePath = @"..\..\sample.pdf";
            using (Stream output = File.OpenWrite(outputFilePath))
                provider.Export(fixedDocument, output);

The produced document is illustrated in the screenshot:

PDF with images

See Also

In this article