Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the XForm class such that it can be drawn on the specified graphics
        /// object.
        /// </summary>
        /// <param name="gfx">The graphics object that later is used to draw this form.</param>
        /// <param name="size">The size in points of the form.</param>
        public XForm(XGraphics gfx, XSize size)
        {
            if (gfx == null)
            {
                throw new ArgumentNullException("gfx");
            }
            if (size.Width < 1 || size.Height < 1)
            {
                throw new ArgumentNullException("size", "The size of the XPDFForm is to small.");
            }

            _formState = FormState.Created;
            //templateSize = size;
            _viewBox.Width  = size.Width;
            _viewBox.Height = size.Height;

            // If gfx belongs to a PDFPage also create the PDFFormXObject
            if (gfx.PDFPage != null)
            {
                _document = gfx.PDFPage.Owner;
                _pdfForm  = new PDFFormXObject(_document, this);
                PDFRectangle rect = new PDFRectangle(new XPoint(), size);
                _pdfForm.Elements.SetRectangle(PDFFormXObject.Keys.BBox, rect);
            }
        }
Ejemplo n.º 2
0
        public void Write(PDFRectangle rect)
        {
            const string format = Config.SignificantFigures3;

            WriteSeparator(CharCat.Delimiter, '/');
            WriteRaw(PDFEncoders.Format("[{0:" + format + "} {1:" + format + "} {2:" + format + "} {3:" + format + "}]", rect.X1, rect.Y1, rect.X2, rect.Y2));
            _lastCat = CharCat.Delimiter;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a link to the web.
        /// </summary>
        public static PDFLinkAnnotation CreateWebLink(PDFRectangle rect, string url)
        {
            PDFLinkAnnotation link = new PDFLinkAnnotation
            {
                _linkType = LinkType.Web,
                Rectangle = rect,
                _url      = url
            };

            return(link);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a link to a file.
        /// </summary>
        public static PDFLinkAnnotation CreateFileLink(PDFRectangle rect, string fileName)
        {
            PDFLinkAnnotation link = new PDFLinkAnnotation
            {
                _linkType = LinkType.File,
                // TODO: Adjust bleed box here (if possible)
                Rectangle = rect,
                _url      = fileName
            };

            return(link);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="XForm"/> class that represents a page of a PDF document.
        /// </summary>
        /// <param name="document">The PDF document.</param>
        /// <param name="viewBox">The view box of the page.</param>
        public XForm(PDFDocument document, XRect viewBox)
        {
            if (viewBox.Width < 1 || viewBox.Height < 1)
            {
                throw new ArgumentNullException("viewBox", "The size of the XPDFForm is to small.");
            }
            _formState = FormState.Created;
            Owner      = document ?? throw new ArgumentNullException("document", "An XPDFForm template must be associated with a document at creation time.");
            _pdfForm   = new PDFFormXObject(document, this);
            //_templateSize = size;
            ViewBox = viewBox;
            PDFRectangle rect = new PDFRectangle(viewBox);

            _pdfForm.Elements.SetRectangle(PDFFormXObject.Keys.BBox, rect);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a link within the current document.
        /// </summary>
        /// <param name="rect">The link area in default page coordinates.</param>
        /// <param name="destinationPage">The one-based destination page number.</param>
        public static PDFLinkAnnotation CreateDocumentLink(PDFRectangle rect, int destinationPage)
        {
            if (destinationPage < 1)
            {
                throw new ArgumentException("Invalid destination page in call to CreateDocumentLink: page number is one-based and must be 1 or higher.", "destinationPage");
            }

            PDFLinkAnnotation link = new PDFLinkAnnotation
            {
                _linkType = LinkType.Document,
                Rectangle = rect,
                _destPage = destinationPage
            };

            return(link);
        }
Ejemplo n.º 7
0
        internal PDFFormXObject(PDFDocument thisDocument, PDFImportedObjectTable importedObjectTable, XPDFForm form)
            : base(thisDocument)
        {
            Debug.Assert(importedObjectTable != null);
            Debug.Assert(ReferenceEquals(thisDocument, importedObjectTable.Owner));
            Elements.SetName(Keys.Type, "/XObject");
            Elements.SetName(Keys.Subtype, "/Form");

            if (form.IsTemplate)
            {
                Debug.Assert(importedObjectTable == null);
                // TODO more initialization here???
                return;
            }

            XPDFForm pdfForm = form;
            // Get import page
            PDFPages importPages = importedObjectTable.ExternalDocument.Pages;

            if (pdfForm.PageNumber < 1 || pdfForm.PageNumber > importPages.Count)
            {
                PSSR.ImportPageNumberOutOfRange(pdfForm.PageNumber, importPages.Count, form._path);
            }
            PDFPage importPage = importPages[pdfForm.PageNumber - 1];

            // Import resources
            PDFItem res = importPage.Elements["/Resources"];

            if (res != null) // unlikely but possible
            {
#if true
                // Get root object
                PDFObject root = res is PDFReference ? ((PDFReference)res).Value : (PDFDictionary)res;
                root = ImportClosure(importedObjectTable, thisDocument, root);
                // If the root was a direct object, make it indirect.
                if (root.Reference == null)
                {
                    thisDocument.IrefTable.Add(root);
                }

                Debug.Assert(root.Reference != null);
                Elements["/Resources"] = root.Reference;
#else
                // Get transitive closure
                PDFObject[] resources = importPage.Owner.Internals.GetClosure(resourcesRoot);
                int         count     = resources.Length;
#if DEBUG_
                for (int idx = 0; idx < count; idx++)
                {
                    Debug.Assert(resources[idx].XRef != null);
                    Debug.Assert(resources[idx].XRef.Document != null);
                    Debug.Assert(resources[idx].Document != null);
                    if (resources[idx].ObjectID.ObjectNumber == 12)
                    {
                        GetType();
                    }
                }
#endif
                // 1st step. Already imported objects are reused and new ones are cloned.
                for (int idx = 0; idx < count; idx++)
                {
                    PDFObject obj = resources[idx];
                    if (importedObjectTable.Contains(obj.ObjectID))
                    {
                        // external object was already imported
                        PDFReference iref = importedObjectTable[obj.ObjectID];
                        Debug.Assert(iref != null);
                        Debug.Assert(iref.Value != null);
                        Debug.Assert(iref.Document == Owner);
                        // replace external object by the already clone counterpart
                        resources[idx] = iref.Value;
                    }
                    else
                    {
                        // External object was not imported earlier and must be cloned
                        PDFObject clone = obj.Clone();
                        Debug.Assert(clone.Reference == null);
                        clone.Document = Owner;
                        if (obj.Reference != null)
                        {
                            // add it to this (the importer) document
                            Owner.irefTable.Add(clone);
                            Debug.Assert(clone.Reference != null);
                            // save old object identifier
                            importedObjectTable.Add(obj.ObjectID, clone.Reference);
                            //Debug.WriteLine("Cloned: " + obj.ObjectID.ToString());
                        }
                        else
                        {
                            // The root object (the /Resources value) is not an indirect object
                            Debug.Assert(idx == 0);
                            // add it to this (the importer) document
                            Owner.irefTable.Add(clone);
                            Debug.Assert(clone.Reference != null);
                        }
                        // replace external object by its clone
                        resources[idx] = clone;
                    }
                }
#if DEBUG_
                for (int idx = 0; idx < count; idx++)
                {
                    Debug.Assert(resources[idx].XRef != null);
                    Debug.Assert(resources[idx].XRef.Document != null);
                    Debug.Assert(resources[idx].Document != null);
                    if (resources[idx].ObjectID.ObjectNumber == 12)
                    {
                        GetType();
                    }
                }
#endif

                // 2nd step. Fix up indirect references that still refers to the import document.
                for (int idx = 0; idx < count; idx++)
                {
                    PDFObject obj = resources[idx];
                    Debug.Assert(obj.Owner != null);
                    FixUpObject(importedObjectTable, importedObjectTable.Owner, obj);
                }

                // Set resources key to the root of the clones
                Elements["/Resources"] = resources[0].Reference;
#endif
            }

            // Take /Rotate into account.
            PDFRectangle rect = importPage.Elements.GetRectangle(PDFPage.InheritablePageKeys.MediaBox);
            // Reduce rotation to 0, 90, 180, or 270.
            int rotate = (importPage.Elements.GetInteger(PDFPage.InheritablePageKeys.Rotate) % 360 + 360) % 360;
            //rotate = 0;
            if (rotate == 0)
            {
                // Set bounding box to media box.
                Elements["/BBox"] = rect;
            }
            else
            {
                // TODO: Have to adjust bounding box? (I think not, but I'm not sure -> wait for problem)
                Elements["/BBox"] = rect;

                // Rotate the image such that it is upright.
                XMatrix matrix = new XMatrix();
                double  width  = rect.Width;
                double  height = rect.Height;
                matrix.RotateAtPrepend(-rotate, new XPoint(width / 2, height / 2));

                // Translate the image such that its center lies on the center of the rotated bounding box.
                double offset = (height - width) / 2;
                if (rotate == 90)
                {
                    // TODO It seems we can simplify this as the sign of offset changes too.
                    if (height > width)
                    {
                        matrix.TranslatePrepend(offset, offset); // Tested.
                    }
                    else
                    {
                        matrix.TranslatePrepend(offset, offset); // TODO Test case.
                    }
                }
                else if (rotate == 270)
                {
                    // TODO It seems we can simplify this as the sign of offset changes too.
                    if (height > width)
                    {
                        matrix.TranslatePrepend(-offset, -offset); // Tested.
                    }
                    else
                    {
                        matrix.TranslatePrepend(-offset, -offset); // Tested.
                    }
                }

                //string item = "[" + PDFEncoders.ToString(matrix) + "]";
                //Elements[Keys.Matrix] = new PDFLiteral(item);
                Elements.SetMatrix(Keys.Matrix, matrix);
            }

            // Preserve filter because the content keeps unmodified.
            PDFContent content = importPage.Contents.CreateSingleContent();
#if !DEBUG
            content.Compressed = true;
#endif
            PDFItem filter = content.Elements["/Filter"];
            if (filter != null)
            {
                Elements["/Filter"] = filter.Clone();
            }

            // (no cloning needed because the bytes keep untouched)
            Stream = content.Stream; // new PDFStream(bytes, this);
            Elements.SetInteger("/Length", content.Stream.Value.Length);
        }