Current filter:
                                You should refresh the page.
                                  • [DevExpress Support Team: CLONED FROM T475677: Convert PdfDocumentArea to Image ]

                                    Hi,
                                    I have PdfOrientedRectangle value from PdfDocumentProcessor.NextWord in my hand. I want to get the image of this word rectange. How can I get the image by using this idea ? How is the conversion from PdfOrientedRectangle to PdfDocumentArea? Or do we have other method to do this operation by using PdfOrientedRectangle?

                                    Thanks.

                                1 Solution

                                Creation Date Importance Sort by

                                To create a PdfDocumentArea object, it is necessary to convert a PdfOrientedRectangle to PdfRectangle. You can use the code from the Convert a PdfOrientedRectangle to a PdfRectangle thread for this purpose.

                                I have modified my sample from the Convert PdfDocumentArea to Image thread to create an image from the first rectangle returned by the PdfDocumentProcessor.NextWord method (see the attachment).

                                [C#]
                                private void ConvertRegionToImage(PdfDocumentProcessor processor, PdfDocumentArea area) { float scale = 10; //1; PdfPage page = processor.Document.Pages[area.PageNumber - 1]; Bitmap pageBitmap = processor.CreateBitmap(area.PageNumber, (int)(Math.Max(page.CropBox.Height, page.CropBox.Width) * scale)); pageBitmap.Save("source.png"); float widthInPixels = (float)area.Area.Width * scale; float heightInPixels = (float)area.Area.Height * scale; int x = Convert.ToInt32((float)area.Area.Left * scale); int y = Convert.ToInt32((float)(page.CropBox.Height - area.Area.Top) * scale); using (Bitmap bitmap = new Bitmap((int)widthInPixels, (int)heightInPixels)) { using (Graphics graphics = Graphics.FromImage(bitmap)) { graphics.DrawImage(pageBitmap, 0, 0, new Rectangle(x, y, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel); bitmap.Save("result.png"); } } }

                                Note that if the PdfWord.Rectangles property returns more than one DevExpress.Pdf.PdfOrientedRectangle object when a part of a word is carried over to the next line.

                                • emre korkmaz 08.08.2019

                                  Thanks you.
                                  I think your code will have problem when we use another dpi value except default value 72f.
                                  While creating page bitmap, we should consider dpi and make calculation for second parameter like that: DevExpress.Office.Utils.Units.PointsToPixels((int)Math.Max(page.CropBox.Height * scale, page.CropBox.Width * scale), dpi);

                                  Is it ture ?

                                  Thanks.

                                • Yulia (DevExpress Support) 08.09.2019

                                  I rewrote my previous algorithm to avoid conversion between points and pixels. There is no need to specify a DPI in this case. See the code below:

                                  [C#]
                                  private void ConvertRegionToImage(PdfDocumentProcessor processor, PdfDocumentArea area) { float scale = 10; //1; PdfPage page = processor.Document.Pages[area.PageNumber - 1]; Bitmap pageBitmap = processor.CreateBitmap(area.PageNumber, (int)(Math.Max(page.CropBox.Height, page.CropBox.Width) * scale)); pageBitmap.Save("source.png"); float widthInPixels = (float)area.Area.Width * scale; float heightInPixels = (float)area.Area.Height * scale; int x = Convert.ToInt32((float)area.Area.Left * scale); int y = Convert.ToInt32((float)(page.CropBox.Height - area.Area.Top) * scale); using (Bitmap bitmap = new Bitmap((int)widthInPixels, (int)heightInPixels)) { using (Graphics graphics = Graphics.FromImage(bitmap)) { graphics.DrawImage(pageBitmap, 0, 0, new Rectangle(x, y, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel); bitmap.Save("result.png"); } } }