Rank: Newbie
Groups: Registered
Joined: 11/23/2016(UTC) Posts: 5   Thanks: 2 times
|
Originally Posted by: Paul Rayman  You have to calculate the offset of the target bounding box as a subtraction between the form object's bounding box and sub-object's bounding box. It worked for attached file, but not for this another file: test2.zip (448kb) downloaded 70 time(s).Coordinates are incorrect:  This is my current code: Code:
public const int DPI = 96;
public Rectangle BoundingBoxToRectangle(PdfPage page, Rectangle boundingBox, PdfFormObject form = null)
{
int width = Convert.ToInt32(page.Width / 72.0 * DPI);
int height = Convert.ToInt32(page.Height / 72.0 * DPI);
if (form != null)
{
boundingBox.Offset(form.BoundingBox.X - boundingBox.X, form.BoundingBox.Y - boundingBox.Y);
}
var top = page.PageToDevice(0, 0, width, height, PageRotate.Normal, boundingBox.Left, boundingBox.Top);
var bottom = page.PageToDevice(0, 0, width, height, PageRotate.Normal, boundingBox.Right, boundingBox.Bottom);
var rectangle = new Rectangle(top.X, top.Y, bottom.X - top.X, bottom.Y - top.Y);
if (rectangle.Width < 0)
{
rectangle.Width = -rectangle.Width;
rectangle.X -= rectangle.Width;
}
if (rectangle.Height < 0)
{
rectangle.Height = -rectangle.Height;
rectangle.Y -= rectangle.Height;
}
if (rectangle.Y < 0)
{
rectangle.Height = rectangle.Height + rectangle.Y;
rectangle.Y = 0;
}
if (rectangle.X < 0)
{
rectangle.Width = rectangle.Width + rectangle.X;
rectangle.X = 0;
}
if (rectangle.Bottom >= height)
{
rectangle.Height = rectangle.Height - (height - rectangle.Bottom + 1);
}
if (rectangle.Right >= width)
{
rectangle.Width = rectangle.Width - (width - rectangle.Right + 1);
}
return rectangle;
}
public Rectangle[] FindImagesRectangles(PdfPage page, PdfPageObjectsCollection objects, PdfFormObject form = null)
{
var rectangles = new List<Rectangle>();
foreach (var obj in objects)
{
if (obj is PdfImageObject)
{
rectangles.Add(BoundingBoxToRectangle(page, obj.BoundingBox, form));
}
else if (obj is PdfFormObject)
{
var parent = obj as PdfFormObject;
rectangles.AddRange(FindImagesRectangles(page, parent.PageObjects, parent));
}
}
return rectangles.ToArray();
}
public void GenerateImages(string filename)
{
PdfCommon.Initialize();
using (var document = PdfDocument.Load(filename))
{
for (var i = 0; i < document.Pages.Count; i++)
{
using (var page = document.Pages[i])
{
int width = Convert.ToInt32(page.Width / 72.0 * DPI);
int height = Convert.ToInt32(page.Height / 72.0 * DPI);
using (var bmp = new PdfBitmap(width, height, true))
{
bmp.FillRect(0, 0, bmp.Width, bmp.Height, Color.White);
page.Render(bmp, 0, 0, bmp.Width, bmp.Height, PageRotate.Normal, RenderFlags.FPDF_LCD_TEXT);
using (var image = new Bitmap(bmp.Image))
{
using (var g = Graphics.FromImage(image))
{
using (var pen = new Pen(Color.FromArgb(128, Color.Red), 4))
{
foreach (var rectangle in FindImagesRectangles(page, page.PageObjects))
{
g.DrawRectangle(pen, rectangle);
}
}
}
image.Save(Path.Combine(Path.GetDirectoryName(filename), string.Format("page-{0}.png", i+1)), ImageFormat.Png);
}
}
}
}
}
PdfCommon.Release();
}
|