public static void Main(string[] args) { // The path to the documents directory. string dataDir = Path.GetFullPath("../../../Data/"); // Load the source PDF file Document doc = new Document(dataDir + "input.pdf"); // Define the default resolution for image int defaultResolution = 72; System.Collections.Stack graphicsState = new System.Collections.Stack(); // Define array list object which will hold image names System.Collections.ArrayList imageNames = new System.Collections.ArrayList(doc.Pages[1].Resources.Images.Names); // Insert an object to stack graphicsState.Push(new System.Drawing.Drawing2D.Matrix(1, 0, 0, 1, 0, 0)); // Get all the operators on first page of document foreach (Operator op in doc.Pages[1].Contents) { // Use GSave/GRestore operators to revert the transformations back to previously set Operator.GSave opSaveState = op as Operator.GSave; Operator.GRestore opRestoreState = op as Operator.GRestore; // Instantiate ConcatenateMatrix object as it defines current transformation matrix. Operator.ConcatenateMatrix opCtm = op as Operator.ConcatenateMatrix; // Create Do operator which draws objects from resources. It draws Form objects and Image objects Operator.Do opDo = op as Operator.Do; if (opSaveState != null) { // Save previous state and push current state to the top of the stack graphicsState.Push(((System.Drawing.Drawing2D.Matrix)graphicsState.Peek()).Clone()); } else if (opRestoreState != null) { // Throw away current state and restore previous one graphicsState.Pop(); } else if (opCtm != null) { System.Drawing.Drawing2D.Matrix cm = new System.Drawing.Drawing2D.Matrix( (float)opCtm.Matrix.A, (float)opCtm.Matrix.B, (float)opCtm.Matrix.C, (float)opCtm.Matrix.D, (float)opCtm.Matrix.E, (float)opCtm.Matrix.F); // Multiply current matrix with the state matrix ((System.Drawing.Drawing2D.Matrix)graphicsState.Peek()).Multiply(cm); continue; } else if (opDo != null) { // In case this is an image drawing operator if (imageNames.Contains(opDo.Name)) { System.Drawing.Drawing2D.Matrix lastCTM = (System.Drawing.Drawing2D.Matrix)graphicsState.Peek(); // Create XImage object to hold images of first pdf page XImage image = doc.Pages[1].Resources.Images[opDo.Name]; // Get image dimensions double scaledWidth = Math.Sqrt(Math.Pow(lastCTM.Elements[0], 2) + Math.Pow(lastCTM.Elements[1], 2)); double scaledHeight = Math.Sqrt(Math.Pow(lastCTM.Elements[2], 2) + Math.Pow(lastCTM.Elements[3], 2)); // Get Height and Width information of image double originalWidth = image.Width; double originalHeight = image.Height; // Compute resolution based on above information double resHorizontal = originalWidth * defaultResolution / scaledWidth; double resVertical = originalHeight * defaultResolution / scaledHeight; // Display Dimension and Resolution information of each image Console.Out.WriteLine( string.Format(dataDir + "image {0} ({1:.##}:{2:.##}): res {3:.##} x {4:.##}", opDo.Name, scaledWidth, scaledHeight, resHorizontal, resVertical)); } } } }
public static void Run() { // ExStart:ExtractBorder // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Tables(); Document doc = new Document(dataDir + "input.pdf"); Stack graphicsState = new Stack(); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap((int)doc.Pages[1].PageInfo.Width, (int)doc.Pages[1].PageInfo.Height); System.Drawing.Drawing2D.GraphicsPath graphicsPath = new System.Drawing.Drawing2D.GraphicsPath(); // Default ctm matrix value is 1,0,0,1,0,0 System.Drawing.Drawing2D.Matrix lastCTM = new System.Drawing.Drawing2D.Matrix(1, 0, 0, -1, 0, 0); // System.Drawing coordinate system is top left based, while pdf coordinate system is low left based, so we have to apply the inversion matrix System.Drawing.Drawing2D.Matrix inversionMatrix = new System.Drawing.Drawing2D.Matrix(1, 0, 0, -1, 0, (float)doc.Pages[1].PageInfo.Height); System.Drawing.PointF lastPoint = new System.Drawing.PointF(0, 0); System.Drawing.Color fillColor = System.Drawing.Color.FromArgb(0, 0, 0); System.Drawing.Color strokeColor = System.Drawing.Color.FromArgb(0, 0, 0); using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bitmap)) { gr.SmoothingMode = SmoothingMode.HighQuality; graphicsState.Push(new System.Drawing.Drawing2D.Matrix(1, 0, 0, 1, 0, 0)); // Process all the contents commands foreach (Operator op in doc.Pages[1].Contents) { Operator.GSave opSaveState = op as Operator.GSave; Operator.GRestore opRestoreState = op as Operator.GRestore; Operator.ConcatenateMatrix opCtm = op as Operator.ConcatenateMatrix; Operator.MoveTo opMoveTo = op as Operator.MoveTo; Operator.LineTo opLineTo = op as Operator.LineTo; Operator.Re opRe = op as Operator.Re; Operator.EndPath opEndPath = op as Operator.EndPath; Operator.Stroke opStroke = op as Operator.Stroke; Operator.Fill opFill = op as Operator.Fill; Operator.EOFill opEOFill = op as Operator.EOFill; Operator.SetRGBColor opRGBFillColor = op as Operator.SetRGBColor; Operator.SetRGBColorStroke opRGBStrokeColor = op as Operator.SetRGBColorStroke; if (opSaveState != null) { // Save previous state and push current state to the top of the stack graphicsState.Push(((System.Drawing.Drawing2D.Matrix)graphicsState.Peek()).Clone()); lastCTM = (System.Drawing.Drawing2D.Matrix)graphicsState.Peek(); } else if (opRestoreState != null) { // Throw away current state and restore previous one graphicsState.Pop(); lastCTM = (System.Drawing.Drawing2D.Matrix)graphicsState.Peek(); } else if (opCtm != null) { System.Drawing.Drawing2D.Matrix cm = new System.Drawing.Drawing2D.Matrix( (float)opCtm.Matrix.A, (float)opCtm.Matrix.B, (float)opCtm.Matrix.C, (float)opCtm.Matrix.D, (float)opCtm.Matrix.E, (float)opCtm.Matrix.F); // Multiply current matrix with the state matrix ((System.Drawing.Drawing2D.Matrix)graphicsState.Peek()).Multiply(cm); lastCTM = (System.Drawing.Drawing2D.Matrix)graphicsState.Peek(); } else if (opMoveTo != null) { lastPoint = new System.Drawing.PointF((float)opMoveTo.X, (float)opMoveTo.Y); } else if (opLineTo != null) { System.Drawing.PointF linePoint = new System.Drawing.PointF((float)opLineTo.X, (float)opLineTo.Y); graphicsPath.AddLine(lastPoint, linePoint); lastPoint = linePoint; } else if (opRe != null) { System.Drawing.RectangleF re = new System.Drawing.RectangleF((float)opRe.X, (float)opRe.Y, (float)opRe.Width, (float)opRe.Height); graphicsPath.AddRectangle(re); } else if (opEndPath != null) { graphicsPath = new System.Drawing.Drawing2D.GraphicsPath(); } else if (opRGBFillColor != null) { fillColor = opRGBFillColor.getColor(); } else if (opRGBStrokeColor != null) { strokeColor = opRGBStrokeColor.getColor(); } else if (opStroke != null) { graphicsPath.Transform(lastCTM); graphicsPath.Transform(inversionMatrix); gr.DrawPath(new System.Drawing.Pen(strokeColor), graphicsPath); graphicsPath = new System.Drawing.Drawing2D.GraphicsPath(); } else if (opFill != null) { graphicsPath.FillMode = FillMode.Winding; graphicsPath.Transform(lastCTM); graphicsPath.Transform(inversionMatrix); gr.FillPath(new System.Drawing.SolidBrush(fillColor), graphicsPath); graphicsPath = new System.Drawing.Drawing2D.GraphicsPath(); } else if (opEOFill != null) { graphicsPath.FillMode = FillMode.Alternate; graphicsPath.Transform(lastCTM); graphicsPath.Transform(inversionMatrix); gr.FillPath(new System.Drawing.SolidBrush(fillColor), graphicsPath); graphicsPath = new System.Drawing.Drawing2D.GraphicsPath(); } } } dataDir = dataDir + "ExtractBorder_out_.png"; bitmap.Save(dataDir, ImageFormat.Png); // ExEnd:ExtractBorder Console.WriteLine("\nBorder extracted successfully as image.\nFile saved at " + dataDir); }