Exemple #1
0
        private CropBox GetCropBox(DictionaryToken dictionary, PageTreeMembers pageTreeMembers, MediaBox mediaBox)
        {
            CropBox cropBox;

            if (dictionary.TryGet(NameToken.CropBox, out var cropBoxObject) &&
                DirectObjectFinder.TryGet(cropBoxObject, pdfScanner, out ArrayToken cropBoxArray))
            {
                if (cropBoxArray.Length != 4)
                {
                    log.Error($"The CropBox was the wrong length in the dictionary: {dictionary}. Array was: {cropBoxArray}. Using MediaBox.");

                    cropBox = new CropBox(mediaBox.Bounds);

                    return(cropBox);
                }

                cropBox = new CropBox(cropBoxArray.ToIntRectangle(pdfScanner));
            }
            else
            {
                cropBox = pageTreeMembers.GetCropBox() ?? new CropBox(mediaBox.Bounds);
            }

            return(cropBox);
        }
Exemple #2
0
        private PageContent GetContent(IReadOnlyList <byte> contentBytes, CropBox cropBox, UserSpaceUnit userSpaceUnit, bool isLenientParsing)
        {
            var operations = pageContentParser.Parse(new ByteArrayInputBytes(contentBytes));

            var context = new ContentStreamProcessor(cropBox.Bounds, resourceStore, userSpaceUnit, isLenientParsing, pdfScanner, xObjectFactory);

            return(context.Process(operations));
        }
        public static Shape createShape(ScreenshotEditor editor, bool cropBoxActive, XPathNavigator element)
        {
            string type = element.GetAttribute("type", "");
            Shape  s    = null;

            switch (type)
            {
            case "Arrow":
                s = new Arrow(editor, element);
                break;

            case "CropBox":
                s = new CropBox(editor, element, cropBoxActive);
                break;

            case "Censor":
                s = new Censor(editor, element);
                break;

            case "Line":
                s = new Line(editor, element);
                break;

            case "Oval":
                s = new Oval(editor, element);
                break;

            case "MagnifyingGlass":
                s = new MagnifyingGlass(editor, element);
                break;

            case "Pencil":
                s = new Pencil(editor, element);
                break;

            case "Picture":
                s = new Picture(editor, element);
                break;

            case "PonyVille":
                s = new PonyVille(editor, element);
                break;

            case "RectangleShape":
                s = new RectangleShape(editor, element);
                break;

            case "TextShape":
                s = new TextShape(editor, element);
                break;

            default:
                break;
            }
            return(s);
        }
Exemple #4
0
        private PageContent GetContent(IReadOnlyList <byte> contentBytes, CropBox cropBox, UserSpaceUnit userSpaceUnit, PageRotationDegrees rotation, bool isLenientParsing)
        {
            var operations = pageContentParser.Parse(new ByteArrayInputBytes(contentBytes));

            var context = new ContentStreamProcessor(cropBox.Bounds, resourceStore, userSpaceUnit, rotation, isLenientParsing, pdfScanner,
                                                     pageContentParser,
                                                     filterProvider,
                                                     log);

            return(context.Process(operations));
        }
Exemple #5
0
        private PageContent GetContent(IReadOnlyList <byte> contentBytes, CropBox cropBox, UserSpaceUnit userSpaceUnit, bool isLenientParsing)
        {
            if (Debugger.IsAttached)
            {
                var txt = OtherEncodings.BytesAsLatin1String(contentBytes.ToArray());
            }

            var operations = pageContentParser.Parse(new ByteArrayInputBytes(contentBytes));

            var context = new ContentStreamProcessor(cropBox.Bounds, resourceStore, userSpaceUnit, isLenientParsing);

            return(context.Process(operations));
        }
Exemple #6
0
        public Page Create(int number, PdfDictionary dictionary, PageTreeMembers pageTreeMembers, IRandomAccessRead reader,
                           bool isLenientParsing)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException(nameof(dictionary));
            }

            var type = dictionary.GetName(CosName.TYPE);

            if (type != null && !type.Equals(CosName.PAGE) && !isLenientParsing)
            {
                throw new InvalidOperationException($"Page {number} had its type was specified as {type} rather than 'Page'.");
            }

            MediaBox mediaBox = GetMediaBox(number, dictionary, pageTreeMembers, isLenientParsing);
            CropBox  cropBox  = GetCropBox(dictionary, pageTreeMembers, mediaBox);

            UserSpaceUnit userSpaceUnit = GetUserSpaceUnits(dictionary);

            LoadResources(dictionary, reader, isLenientParsing);

            PageContent content = default(PageContent);

            var contentObject = dictionary.GetItemOrDefault(CosName.CONTENTS) as CosObject;

            if (contentObject != null)
            {
                var contentStream = pdfObjectParser.Parse(contentObject.ToIndirectReference(), reader, false) as PdfRawStream;

                if (contentStream == null)
                {
                    throw new InvalidOperationException("Failed to parse the content for the page: " + number);
                }

                var contents = contentStream.Decode(filterProvider);

                var txt = OtherEncodings.BytesAsLatin1String(contents);

                var operations = pageContentParser.Parse(new ByteArrayInputBytes(contents));

                var context = new ContentStreamProcessor(cropBox.Bounds, resourceStore, userSpaceUnit);

                content = context.Process(operations);
            }

            var page = new Page(number, mediaBox, cropBox, content);

            return(page);
        }
Exemple #7
0
        private static CropBox GetCropBox(DictionaryToken dictionary, PageTreeMembers pageTreeMembers, MediaBox mediaBox)
        {
            CropBox cropBox;

            if (dictionary.TryGet(NameToken.CropBox, out var cropBoxObject) && cropBoxObject is ArrayToken cropBoxArray)
            {
                var x1 = cropBoxArray.GetNumeric(0).Int;
                var y1 = cropBoxArray.GetNumeric(1).Int;
                var x2 = cropBoxArray.GetNumeric(2).Int;
                var y2 = cropBoxArray.GetNumeric(3).Int;

                cropBox = new CropBox(new PdfRectangle(x1, y1, x2, y2));
            }
            else
            {
                cropBox = pageTreeMembers.GetCropBox() ?? new CropBox(mediaBox.Bounds);
            }

            return(cropBox);
        }
Exemple #8
0
        private static CropBox GetCropBox(PdfDictionary dictionary, PageTreeMembers pageTreeMembers, MediaBox mediaBox)
        {
            CropBox cropBox;

            if (dictionary.TryGetItemOfType(CosName.CROP_BOX, out COSArray cropBoxArray))
            {
                var x1 = cropBoxArray.getInt(0);
                var y1 = cropBoxArray.getInt(1);
                var x2 = cropBoxArray.getInt(2);
                var y2 = cropBoxArray.getInt(3);

                cropBox = new CropBox(new PdfRectangle(x1, y1, x2, y2));
            }
            else
            {
                cropBox = pageTreeMembers.GetCropBox() ?? new CropBox(mediaBox.Bounds);
            }

            return(cropBox);
        }
        private static Matrix GetInitialMatrix(int rotation, CropBox mediaBox)
        {
            float cos, sin;
            float dx = 0, dy = 0;

            switch (rotation)
            {
            case 0:
                cos = 1;
                sin = 0;
                break;

            case 90:
                cos = 0;
                sin = 1;
                dy  = (float)mediaBox.Bounds.Height;
                break;

            case 180:
                cos = -1;
                sin = 0;
                dx  = (float)mediaBox.Bounds.Width;
                dy  = (float)mediaBox.Bounds.Height;
                break;

            case 270:
                cos = 0;
                sin = -1;
                dx  = (float)mediaBox.Bounds.Width;
                break;

            default:
                throw new InvalidOperationException($"Invalid value for page rotation: {rotation}.");
            }

            return(new Matrix(cos, -sin,
                              sin, cos,
                              dx, dy));
        }
Exemple #10
0
        public Page Create(int number, DictionaryToken dictionary, PageTreeMembers pageTreeMembers,
                           bool isLenientParsing)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException(nameof(dictionary));
            }

            var type = dictionary.GetNameOrDefault(NameToken.Type);

            if (type != null && !type.Equals(NameToken.Page) && !isLenientParsing)
            {
                throw new InvalidOperationException($"Page {number} had its type specified as {type} rather than 'Page'.");
            }

            var rotation = new PageRotationDegrees(pageTreeMembers.Rotation);

            if (dictionary.TryGet(NameToken.Rotate, pdfScanner, out NumericToken rotateToken))
            {
                rotation = new PageRotationDegrees(rotateToken.Int);
            }

            MediaBox mediaBox = GetMediaBox(number, dictionary, pageTreeMembers);
            CropBox  cropBox  = GetCropBox(dictionary, pageTreeMembers, mediaBox);

            var stackDepth = 0;

            while (pageTreeMembers.ParentResources.Count > 0)
            {
                var resource = pageTreeMembers.ParentResources.Dequeue();

                resourceStore.LoadResourceDictionary(resource, isLenientParsing);
                stackDepth++;
            }

            if (dictionary.TryGet(NameToken.Resources, pdfScanner, out DictionaryToken resources))
            {
                resourceStore.LoadResourceDictionary(resources, isLenientParsing);
                stackDepth++;
            }

            UserSpaceUnit userSpaceUnit = GetUserSpaceUnits(dictionary);

            PageContent content = default(PageContent);

            if (!dictionary.TryGet(NameToken.Contents, out var contents))
            {
                // ignored for now, is it possible? check the spec...
            }
            else if (DirectObjectFinder.TryGet <ArrayToken>(contents, pdfScanner, out var array))
            {
                var bytes = new List <byte>();

                for (var i = 0; i < array.Data.Count; i++)
                {
                    var item = array.Data[i];

                    if (!(item is IndirectReferenceToken obj))
                    {
                        throw new PdfDocumentFormatException($"The contents contained something which was not an indirect reference: {item}.");
                    }

                    var contentStream = DirectObjectFinder.Get <StreamToken>(obj, pdfScanner);

                    if (contentStream == null)
                    {
                        throw new InvalidOperationException($"Could not find the contents for object {obj}.");
                    }

                    bytes.AddRange(contentStream.Decode(filterProvider));

                    if (i < array.Data.Count - 1)
                    {
                        bytes.Add((byte)'\n');
                    }
                }

                content = GetContent(number, bytes, cropBox, userSpaceUnit, rotation, isLenientParsing);
            }
            else
            {
                var contentStream = DirectObjectFinder.Get <StreamToken>(contents, pdfScanner);

                if (contentStream == null)
                {
                    throw new InvalidOperationException("Failed to parse the content for the page: " + number);
                }

                var bytes = contentStream.Decode(filterProvider);

                content = GetContent(number, bytes, cropBox, userSpaceUnit, rotation, isLenientParsing);
            }

            var page = new Page(number, dictionary, mediaBox, cropBox, rotation, content,
                                new AnnotationProvider(pdfScanner, dictionary, isLenientParsing),
                                pdfScanner);

            for (var i = 0; i < stackDepth; i++)
            {
                resourceStore.UnloadResourceDictionary();
            }

            return(page);
        }
Exemple #11
0
        public Page Create(int number, DictionaryToken dictionary, PageTreeMembers pageTreeMembers, bool clipPaths)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException(nameof(dictionary));
            }

            var type = dictionary.GetNameOrDefault(NameToken.Type);

            if (type != null && !type.Equals(NameToken.Page))
            {
                log?.Error($"Page {number} had its type specified as {type} rather than 'Page'.");
            }

            MediaBox mediaBox = GetMediaBox(number, dictionary, pageTreeMembers);
            CropBox  cropBox  = GetCropBox(dictionary, pageTreeMembers, mediaBox);

            var rotation = new PageRotationDegrees(pageTreeMembers.Rotation);

            if (dictionary.TryGet(NameToken.Rotate, pdfScanner, out NumericToken rotateToken))
            {
                rotation = new PageRotationDegrees(rotateToken.Int);
            }

            var stackDepth = 0;

            while (pageTreeMembers.ParentResources.Count > 0)
            {
                var resource = pageTreeMembers.ParentResources.Dequeue();

                resourceStore.LoadResourceDictionary(resource);
                stackDepth++;
            }

            if (dictionary.TryGet(NameToken.Resources, pdfScanner, out DictionaryToken resources))
            {
                resourceStore.LoadResourceDictionary(resources);
                stackDepth++;
            }

            // Apply rotation.
            if (rotation.SwapsAxis)
            {
                mediaBox = new MediaBox(new PdfRectangle(mediaBox.Bounds.Bottom,
                                                         mediaBox.Bounds.Left,
                                                         mediaBox.Bounds.Top,
                                                         mediaBox.Bounds.Right));
                cropBox = new CropBox(new PdfRectangle(cropBox.Bounds.Bottom,
                                                       cropBox.Bounds.Left,
                                                       cropBox.Bounds.Top,
                                                       cropBox.Bounds.Right));
            }

            UserSpaceUnit userSpaceUnit = GetUserSpaceUnits(dictionary);

            PageContent content;

            if (!dictionary.TryGet(NameToken.Contents, out var contents))
            {
                content = new PageContent(EmptyArray <IGraphicsStateOperation> .Instance,
                                          EmptyArray <Letter> .Instance,
                                          EmptyArray <PdfPath> .Instance,
                                          EmptyArray <Union <XObjectContentRecord, InlineImage> > .Instance,
                                          EmptyArray <MarkedContentElement> .Instance,
                                          pdfScanner,
                                          filterProvider,
                                          resourceStore);
                // ignored for now, is it possible? check the spec...
            }
            else if (DirectObjectFinder.TryGet <ArrayToken>(contents, pdfScanner, out var array))
            {
                var bytes = new List <byte>();

                for (var i = 0; i < array.Data.Count; i++)
                {
                    var item = array.Data[i];

                    if (!(item is IndirectReferenceToken obj))
                    {
                        throw new PdfDocumentFormatException($"The contents contained something which was not an indirect reference: {item}.");
                    }

                    var contentStream = DirectObjectFinder.Get <StreamToken>(obj, pdfScanner);

                    if (contentStream == null)
                    {
                        throw new InvalidOperationException($"Could not find the contents for object {obj}.");
                    }

                    bytes.AddRange(contentStream.Decode(filterProvider, pdfScanner));

                    if (i < array.Data.Count - 1)
                    {
                        bytes.Add((byte)'\n');
                    }
                }

                content = GetContent(number, bytes, cropBox, userSpaceUnit, rotation, clipPaths, mediaBox);
            }
            else
            {
                var contentStream = DirectObjectFinder.Get <StreamToken>(contents, pdfScanner);

                if (contentStream == null)
                {
                    throw new InvalidOperationException("Failed to parse the content for the page: " + number);
                }

                var bytes = contentStream.Decode(filterProvider, pdfScanner);

                content = GetContent(number, bytes, cropBox, userSpaceUnit, rotation, clipPaths, mediaBox);
            }

            var page = new Page(number, dictionary, mediaBox, cropBox, rotation, content,
                                new AnnotationProvider(pdfScanner, dictionary),
                                pdfScanner);

            for (var i = 0; i < stackDepth; i++)
            {
                resourceStore.UnloadResourceDictionary();
            }

            return(page);
        }
Exemple #12
0
        private PageContent GetContent(int pageNumber, IReadOnlyList <byte> contentBytes, CropBox cropBox, UserSpaceUnit userSpaceUnit,
                                       PageRotationDegrees rotation, bool clipPaths, MediaBox mediaBox)
        {
            var operations = pageContentParser.Parse(pageNumber, new ByteArrayInputBytes(contentBytes),
                                                     log);

            var context = new ContentStreamProcessor(cropBox.Bounds, resourceStore, userSpaceUnit, rotation, pdfScanner,
                                                     pageContentParser,
                                                     filterProvider,
                                                     log,
                                                     clipPaths,
                                                     new PdfVector(mediaBox.Bounds.Width, mediaBox.Bounds.Height));

            return(context.Process(pageNumber, operations));
        }
Exemple #13
0
        public Page Create(int number, DictionaryToken dictionary, PageTreeMembers pageTreeMembers,
                           bool isLenientParsing)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException(nameof(dictionary));
            }

            var type = dictionary.GetNameOrDefault(NameToken.Type);

            if (type != null && !type.Equals(NameToken.Page) && !isLenientParsing)
            {
                throw new InvalidOperationException($"Page {number} had its type was specified as {type} rather than 'Page'.");
            }

            MediaBox mediaBox = GetMediaBox(number, dictionary, pageTreeMembers, isLenientParsing);
            CropBox  cropBox  = GetCropBox(dictionary, pageTreeMembers, mediaBox);

            UserSpaceUnit userSpaceUnit = GetUserSpaceUnits(dictionary);

            LoadResources(dictionary, isLenientParsing);

            PageContent content = default(PageContent);

            if (!dictionary.TryGet(NameToken.Contents, out var contents))
            {
                // ignored for now, is it possible? check the spec...
            }
            else if (DirectObjectFinder.TryGet <ArrayToken>(contents, pdfScanner, out var array))
            {
                var bytes = new List <byte>();

                foreach (var item in array.Data)
                {
                    if (!(item is IndirectReferenceToken obj))
                    {
                        throw new PdfDocumentFormatException($"The contents contained something which was not an indirect reference: {item}.");
                    }

                    var contentStream = DirectObjectFinder.Get <StreamToken>(obj, pdfScanner);

                    if (contentStream == null)
                    {
                        throw new InvalidOperationException($"Could not find the contents for object {obj}.");
                    }

                    bytes.AddRange(contentStream.Decode(filterProvider));
                }

                content = GetContent(bytes, cropBox, userSpaceUnit, isLenientParsing);
            }
            else
            {
                var contentStream = DirectObjectFinder.Get <StreamToken>(contents, pdfScanner);

                if (contentStream == null)
                {
                    throw new InvalidOperationException("Failed to parse the content for the page: " + number);
                }

                var bytes = contentStream.Decode(filterProvider);

                content = GetContent(bytes, cropBox, userSpaceUnit, isLenientParsing);
            }

            var page = new Page(number, mediaBox, cropBox, content);

            return(page);
        }
        public ActionResult PositionCrop(string folderName, string uuid, 
            string ImageField = "Image", string CropInfoField="CropInfo", String poisitionName="default")
        {
            TextFolder tf = new TextFolder(Repository, folderName).AsActual();
            TextContent tc = tf.CreateQuery().WhereEquals("UUID", uuid).FirstOrDefault();

            string ImageUrl = (String)tc[ImageField];
            string crop_json = (string)tc[CropInfoField];

            CropBox cb = new CropBox();

            if (!String.IsNullOrEmpty(crop_json)) {
                CropInfo ci = Newtonsoft.Json.JsonConvert.DeserializeObject<CropInfo>(crop_json.ToString());
                cb = (CropBox)ci[CropInfoField] ?? new CropBox();
            }

            ViewData["SourceUrl"] = ImageUrl;
            ViewData["CropParam"] = new
            {
                Url = ImageUrl,
                X = cb.x,
                Y = cb.y,
                Width = cb.width,
                Height = cb.height
            };

            return View();
        }