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

How to Draw a Chart with FixedContentEditor in PdfProcessing

Environment

Version Product Author
2024.1.124 RadPdfProcessing Desislava Yordanova

Description

This article demonstrates a sample solution how to draw a chart in PdfProcessing.

Solution

The powerful FixedContentEditor allows you draw a simulation of a chart element at the desired Position:

        public static System.Windows.Size pageSize = new System.Windows.Size(Unit.MmToDip(210), Telerik.Windows.Documents.Media.Unit.MmToDip(297));
        public static Padding pageMarginsValue = new Telerik.Windows.Documents.Primitives.Padding(
               Unit.MmToDip(20),//left
               Unit.MmToDip(0),//top
               Unit.MmToDip(0),//right
               Unit.MmToDip(0));//bottom
        public static double currentYposition = pageMarginsValue.Top;
        public static double currentXposition = pageMarginsValue.Left;
        public static double pageYLimit = pageSize.Height - pageMarginsValue.Bottom;
        public static double pageXLimit = pageSize.Width - pageMarginsValue.Right;

        private static Random random = new Random();
        private static Dictionary<string, int> availableLibraries = new Dictionary<string, int>();
        private static RadFixedDocument fixedDocument = new RadFixedDocument();
        static void Main(string[] args)
        {
            availableLibraries.Add("RadPdfProcessing", random.Next(20, 500));
            availableLibraries.Add("RadSpreadProcessing", random.Next(20, 500));
            availableLibraries.Add("RadWordsProcessing", random.Next(20, 500));
            availableLibraries.Add("RadSpreadStreamProcessing", random.Next(20, 500));
            availableLibraries.Add("RadZipLibrary", random.Next(20, 500)); 

            DrawChart();

            PdfFormatProvider provider = new PdfFormatProvider();
            string outputFilePath = @"..\..\sample.pdf";
            File.Delete(outputFilePath);
            using (Stream output = File.OpenWrite(outputFilePath))
            {
                provider.Export(fixedDocument, output);
            }
            Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
        }

        private static void DrawChart()
        {
            RadFixedPage fixedPage = fixedDocument.Pages.LastOrDefault();
            if (fixedPage == null) { fixedPage = fixedDocument.Pages.AddPage(); }
            FixedContentEditor fixedContentEditor = new FixedContentEditor(fixedPage);

            fixedContentEditor.Position.Translate(currentXposition, currentYposition);
            fixedContentEditor.TextProperties.Font = FontsRepository.Courier;
            double maxWidth = availableLibraries.Max(x =>
            {
                Block block = new Block();
                block.InsertText(x.Key +" - "+ x.Value );
                return block.Measure().Height;
            }); 
            Point chartStartPoint = new Point(maxWidth, 250);

            Rect chartDimensions = GetChartDimensions(chartStartPoint);

            fixedContentEditor.Position.Translate(pageMarginsValue.Left, currentYposition);
            int maxValue = availableLibraries.Max(x => x.Value);
            Block textBlock = new Block();
            textBlock.InsertText(availableLibraries.First().Key);
            double textHeight = textBlock.Measure().Height;
            double startX = 0;

            foreach (KeyValuePair<string, int> library in availableLibraries)
            {  
                Block measureBlock = new Block();
                measureBlock.InsertText(string.Format("{0} - {1}", library.Key, library.Value));
                Size measuredSize = measureBlock.Measure();
                startX = Math.Max(startX, measuredSize.Width)+10;
            }

            foreach (KeyValuePair<string, int> library in availableLibraries)
            {
                DrawBarAndText(maxValue, textHeight, startX, library);
            }
        }
        private static Rect GetChartDimensions(Point start)
        {
            Block textBlock = new Block();
            textBlock.InsertText(availableLibraries.First().Key);
            double textHeight = textBlock.Measure().Height;
            double chartHeight = textHeight * availableLibraries.Count;

            double pageWidth = fixedDocument.Pages.Last().Size.Width;
            double chartWidth = pageWidth * 0.55;

            Point leftBottomPoint = new Point(start.X, start.Y + chartHeight);
            Point rightTopPoint = new Point(chartWidth + start.X, start.Y);
            Point rightBottomPoint = new Point(chartWidth + start.X, chartHeight + start.Y);

            Rect chartRect = new Rect(start, new Size(rightTopPoint.X - start.X, chartHeight));

            return chartRect;
        }

        private static byte RandomRgbValue()
        {
            return (byte)random.Next(0, 201);
        }
        private static void DrawBarAndText(int maxValue, double textHeight, double startX, KeyValuePair<string, int> library)
        {
            string name = library.Key;
            RadFixedPage fixedPage = fixedDocument.Pages.LastOrDefault();
            if (fixedPage == null) { fixedPage = fixedDocument.Pages.AddPage(); }
            FixedContentEditor fixedContentEditor = new FixedContentEditor(fixedPage);
            RgbColor fillColor = new RgbColor(RandomRgbValue(), RandomRgbValue(), RandomRgbValue());
            Block textBlock = new Block();
            fixedContentEditor.GraphicProperties.FillColor = fillColor;
            fixedContentEditor.GraphicProperties.StrokeColor = new RgbColor(fillColor.B, fillColor.G, fillColor.R);
            textBlock.GraphicProperties.CopyFrom(fixedContentEditor.GraphicProperties);
            textBlock.InsertText(string.Format("{0} - {1}", name, library.Value));
            Size measuredText = textBlock.Measure();
            currentYposition += measuredText.Height;
            fixedContentEditor.Position.Translate(pageMarginsValue.Left, currentYposition);
            fixedContentEditor.DrawBlock(textBlock);

            int width = library.Value - (maxValue % library.Value);

            fixedContentEditor.Position.Translate(fixedContentEditor.Position.Matrix.OffsetX + startX, currentYposition);

            LinearGradient linearGradient = new Telerik.Windows.Documents.Fixed.Model.ColorSpaces.LinearGradient(new Point(0, 0), new Point(30, 30));
            RgbColor startColor=  new RgbColor(RandomRgbValue(), RandomRgbValue(), RandomRgbValue());
            RgbColor stopColor = new RgbColor(RandomRgbValue(), RandomRgbValue(), RandomRgbValue());
            linearGradient.GradientStops.Add(new GradientStop(startColor, 0));
            linearGradient.GradientStops.Add(new GradientStop(stopColor, 1));

            fixedContentEditor.GraphicProperties.FillColor = linearGradient;
            IPosition savedPosition = fixedContentEditor.Position.Clone();
            fixedContentEditor.Position.Translate(0, currentYposition);
            fixedContentEditor.DrawRectangle(new Rect(startX + pageMarginsValue.Left, textHeight / 2, width, 10));
            fixedContentEditor.Position = savedPosition; 
            fixedContentEditor.Position.Translate(fixedContentEditor.Position.Matrix.OffsetX - startX, currentYposition + measuredText.Height);
        }

The achieved result is illustrated below:

Pdf document with Chart

See Also

In this article