/// <summary>
        /// Gets the resource name of the specified form within this dictionary.
        /// </summary>
        internal string GetFormName(XForm form)
        {
            PDFFormXObject pdfForm = _document.ExternalDocumentTable.GetForm(form);

            Debug.Assert(pdfForm != null);
            string name = Resources.AddForm(pdfForm);

            return(name);
        }
Example #2
0
 /// <summary>
 /// Initializes a new instance of this class with the document the objects are imported from.
 /// </summary>
 public PDFImportedObjectTable(PDFDocument owner, PDFDocument externalDocument)
 {
     if (externalDocument == null)
     {
         throw new ArgumentNullException("externalDocument");
     }
     Owner = owner ?? throw new ArgumentNullException("owner");
     _externalDocumentHandle = externalDocument.Handle;
     _xObjects = new PDFFormXObject[externalDocument.PageCount];
 }
Example #3
0
 /// <summary>
 /// Adds the specified form object to this resource dictionary
 /// and returns its local resource name.
 /// </summary>
 public string AddForm(PDFFormXObject form)
 {
     if (!_resources.TryGetValue(form, out string name))
     {
         name             = NextFormName;
         _resources[form] = name;
         if (form.Reference == null)
         {
             Owner.IrefTable.Add(form);
         }
         XObjects.Elements[name] = form.Reference;
     }
     return(name);
 }
        /// <summary>
        /// Gets a PDFFormXObject from an XPDFForm. Because the returned objects must be unique, always
        /// a new instance of PDFFormXObject is created if none exists for the specified form.
        /// </summary>
        public PDFFormXObject GetForm(XForm form)
        {
            // If the form already has a PDFFormXObject, return it.
            if (form._pdfForm != null)
            {
                Debug.Assert(form.IsTemplate, "An XPDFForm must not have a PDFFormXObject.");
                if (ReferenceEquals(form._pdfForm.Owner, Owner))
                {
                    return(form._pdfForm);
                }
                //throw new InvalidOperationException("Because of a current limitation of PDFSharp an XPDFForm object can be used only within one single PDFDocument.");

                // Dispose PDFFromXObject when document has changed
                form._pdfForm = null;
            }

            if (form is XPDFForm pdfForm)
            {
                // Is the external PDF file from which is imported already known for the current document?
                Selector selector = new Selector(form);
                if (!_forms.TryGetValue(selector, out PDFImportedObjectTable importedObjectTable))
                {
                    // No: Get the external document from the form and create ImportedObjectTable.
                    PDFDocument doc = pdfForm.ExternalDocument;
                    importedObjectTable = new PDFImportedObjectTable(Owner, doc);
                    _forms[selector]    = importedObjectTable;
                }

                PDFFormXObject xObject = importedObjectTable.GetXObject(pdfForm.PageNumber);
                if (xObject == null)
                {
                    xObject = new PDFFormXObject(Owner, importedObjectTable, pdfForm);
                    importedObjectTable.SetXObject(pdfForm.PageNumber, xObject);
                }
                return(xObject);
            }
            Debug.Assert(form.GetType() == typeof(XForm));
            form._pdfForm = new PDFFormXObject(Owner, form);
            return(form._pdfForm);
        }
Example #5
0
 public void SetXObject(int pageNumber, PDFFormXObject xObject) => _xObjects[pageNumber - 1] = xObject;