예제 #1
0
 public PdfItem[] ToArray(int start, int length)
 {
     PdfItem[] items = new PdfItem[length];
     for (int i = 0, j = start; i < length; i++, j++)
         items[i] = _items[j];
     return items;
 }
예제 #2
0
        /// <summary>
        /// Gets the index of the specified string in the /Opt array or -1, if no such string exists.
        /// </summary>
        protected int IndexInOptArray(string value)
        {
            PdfArray opt = Elements.GetArray(Keys.Opt);

//#if DEBUG  // Check with //R080317 implemention
//            PdfArray opt2 = null;
//            if (Elements[Keys.Opt] is PdfArray)
//                opt2 = Elements[Keys.Opt] as PdfArray;
//            else if (Elements[Keys.Opt] is Advanced.PdfReference)
//            {
//                //falls das Array nicht direkt am Element hängt,
//                //das Array aus dem referenzierten Element holen
//                opt2 = ((Advanced.PdfReference)Elements[Keys.Opt]).Value as PdfArray;
//            }
//            Debug.Assert(ReferenceEquals(opt, opt2));
//#endif

            if (opt != null)
            {
                int count = opt.Elements.Count;
                for (int idx = 0; idx < count; idx++)
                {
                    PdfItem item = opt.Elements[idx];
                    if (item is PdfString)
                    {
                        if (item.ToString() == value)
                        {
                            return(idx);
                        }
                    }
                    else if (item is PdfArray)
                    {
                        PdfArray array = (PdfArray)item;
                        if (array.Elements.Count != 0)
                        {
                            if (array.Elements[0].ToString() == value)
                            {
                                return(idx);
                            }
                        }
                    }
                }
            }
            return(-1);
        }
 /// <summary>
 /// Gets a field from the collection. For your convenience an instance of a derived class like
 /// PdfTextField or PdfCheckBox is returned if PDFsharp can guess the actual type of the dictionary.
 /// If the actual type cannot be guessed by PDFsharp the function returns an instance
 /// of PdfGenericField.
 /// </summary>
 public PdfAcroField this[int index]
 {
     get
     {
         PdfItem item = Elements[index];
         Debug.Assert(item is PdfReference);
         PdfDictionary dict = ((PdfReference)item).Value as PdfDictionary;
         Debug.Assert(dict != null);
         PdfAcroField field = dict as PdfAcroField;
         if (field == null && dict != null)
         {
             // Do type transformation
             field = CreateAcroField(dict);
             //Elements[index] = field.XRef;
         }
         return(field);
     }
 }
 /// <summary>
 /// Unfilters the stream.
 /// </summary>
 void Decode()
 {
     if (Stream != null && Stream.Value != null)
     {
         PdfItem item = Elements[PdfStream.Keys.Filter];
         if (item != null)
         {
             var    decodeParms = Elements[PdfStream.Keys.DecodeParms];
             byte[] bytes       = Filtering.Decode(Stream.Value, item, decodeParms);
             if (bytes != null)
             {
                 Stream.Value = bytes;
                 Elements.Remove(PdfStream.Keys.Filter);
                 Elements.Remove(PdfStream.Keys.DecodeParms);
                 Elements.SetInteger(PdfStream.Keys.Length, Stream.Length);
             }
         }
     }
 }
예제 #5
0
    //public PdfObject ReadObject(PdfObject obj, bool includeReferences)

    // HACK solve problem more general
    int GetStreamLength(PdfDictionary dict)
    {
      if (dict.Elements["/F"] != null)
        throw new NotImplementedException("File streams are not yet implemented.");

      PdfItem value = dict.Elements["/Length"];
      if (value is PdfInteger)
        return Convert.ToInt32(value);
      else if (value is PdfReference)
      {
        ParserState state = SaveState();
        object length = ReadObject(null, ((PdfReference)value).ObjectID, false);
        RestoreState(state);
        int l = ((PdfIntegerObject)length).Value;
        dict.Elements["/Length"] = new PdfInteger(l);
        return l;
      }
      throw new InvalidOperationException("Cannot retrieve stream length.");
    }
        public bool ExtractText(PdfPage page, StringBuilder res)
        {
            // create font table
            curr_font = null; // no default font
            this.page = page;
            fonts.Clear();

            if ((page.Resources != null) && (page.Resources.Elements["/Font"] != null))
            {
                var obj = page.Resources.Elements["/Font"];
                if (obj is PdfReference)
                {
                    obj = (obj as PdfReference).Value;
                }
                if (obj is PdfDictionary)
                {
                    foreach (var kp in (obj as PdfDictionary).Elements)
                    {
                        PdfItem fobj = kp.Value;
                        if (fobj is PdfReference)
                        {
                            fobj = ((PdfReference)fobj).Value;
                        }
                        // now we make font
                        if (fobj is PdfDictionary)
                        {
                            fonts.Add(kp.Key, new Font((PdfDictionary)fobj));
                        }
                    }
                }
            }
            try
            {
                ExtractText(ContentReader.ReadContent(page), res);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
                return(false);
            }
            return(true);
        }
예제 #7
0
    internal PdfDictionary ReadDictionary(PdfDictionary dict, bool includeReferences)
    {
      Debug.Assert(Symbol == Symbol.BeginDictionary);

#if DEBUG_
      ReadDictionaryCounter++;
      Debug.WriteLine(ReadDictionaryCounter.ToString());
      if (ReadDictionaryCounter == 101)
        GetType();
#endif

      if (dict == null)
        dict = new PdfDictionary(this.document);
      DictionaryMeta meta = dict.Meta;

      int sp = this.stack.SP;
      ParseObject(Symbol.EndDictionary);
      int count = this.stack.SP - sp;
      Debug.Assert(count % 2 == 0);
      PdfItem[] items = this.stack.ToArray(sp, count);
      this.stack.Reduce(count);
      for (int idx = 0; idx < count; idx += 2)
      {
        PdfItem val = items[idx];
        if (!(val is PdfName))
          throw new PdfReaderException("name expected");
        string key = ((PdfName)val).ToString();
#if DEBUG_
        if (key == "/ID")
        {
          GetType();
          char x = ((PdfString)(((PdfArray)items[idx + 1]).Elements[0])).Value[0];
          x.GetType();
        }
#endif
        val = items[idx + 1];
        if (includeReferences && val is PdfReference)
          val = ReadReference((PdfReference)val, includeReferences);
        dict.Elements[key] = val;
      }
      return dict;
    }
예제 #8
0
        /// <summary>
        /// Gets the first or second document identifier.
        /// </summary>
        public string GetDocumentID(int index)
        {
            if (index < 0 || index > 1)
            {
                throw new ArgumentOutOfRangeException("index", index, "Index must be 0 or 1.");
            }

            PdfArray array = Elements[Keys.ID] as PdfArray;

            if (array == null || array.Elements.Count < 2)
            {
                return("");
            }
            PdfItem item = array.Elements[index];

            if (item is PdfString)
            {
                return(((PdfString)item).Value);
            }
            return("");
        }
        /// <summary>
        /// Decrypts an array.
        /// </summary>
        void DecryptArray(PdfArray array)
        {
            int count = array.Elements.Count;

            for (int idx = 0; idx < count; idx++)
            {
                PdfItem item = array.Elements[idx];
                if (item is PdfString value1)
                {
                    DecryptString(value1);
                }
                else if (item is PdfDictionary value2)
                {
                    DecryptDictionary(value2);
                }
                else if (item is PdfArray value3)
                {
                    DecryptArray(value3);
                }
            }
        }
예제 #10
0
        int IndexInOptStrings(string value)
        {
            PdfArray opt = Elements[Keys.Opt] as PdfArray;

            if (opt != null)
            {
                int count = opt.Elements.Count;
                for (int idx = 0; idx < count; idx++)
                {
                    PdfItem item = opt.Elements[idx];
                    if (item is PdfString)
                    {
                        if (item.ToString() == value)
                        {
                            return(idx);
                        }
                    }
                }
            }
            return(-1);
        }
예제 #11
0
    public PdfArray ReadArray(PdfArray array, bool includeReferences)
    {
      Debug.Assert(Symbol == Symbol.BeginArray);

      if (array == null)
        array = new PdfArray(this.document);

      int sp = this.stack.SP;
      ParseObject(Symbol.EndArray);
      int count = this.stack.SP - sp;
      PdfItem[] items = this.stack.ToArray(sp, count);
      this.stack.Reduce(count);
      for (int idx = 0; idx < count; idx++)
      {
        PdfItem val = items[idx];
        if (includeReferences && val is PdfReference)
          val = ReadReference((PdfReference)val, includeReferences);
        array.Elements.Add(val);
      }
      return array;
    }
예제 #12
0
        internal PdfContents(PdfArray array)
            : base(array)
        {
            int count = Elements.Count;

            for (int idx = 0; idx < count; idx++)
            {
                // Convert the references from PdfDictionary to PdfContent
                PdfItem      item = Elements[idx];
                PdfReference iref = item as PdfReference;
                if (iref != null && iref.Value is PdfDictionary)
                {
                    // The following line is correct!
                    new PdfContent((PdfDictionary)iref.Value);
                }
                else
                {
                    throw new InvalidOperationException("Unexpected item in a content stream array.");
                }
            }
        }
예제 #13
0
 /// <summary>
 /// Search the XMP metadata
 /// </summary>
 /// <param name="doc">A open PdfDocument</param>
 private void ReadXMPMetadata(PdfDocument doc)
 {
     if (doc.Internals.Catalog.Elements.ContainsKey("/Metadata"))
     {
         PdfItem pi = doc.Internals.Catalog.Elements["/Metadata"];
         //doc.Internals.Catalog.Elements.Remove("/Metadata");
         if (pi is PdfSharp.Pdf.Advanced.PdfReference)
         {
             int           intXMPObjectNumber = (pi as PdfSharp.Pdf.Advanced.PdfReference).ObjectNumber;
             PdfDictionary pDic = (PdfDictionary)doc.Internals.GetObject(new PdfObjectID(intXMPObjectNumber));
             string        xmp  = pDic.Stream.ToString();
             if (xmp != string.Empty)
             {
                 XmlDocument xDoc = new XmlDocument();
                 xDoc.XmlResolver = null;
                 xDoc.LoadXml(xmp);
                 this.ExtractFromXMP(xDoc);
             }
         }
     }
 }
예제 #14
0
 /// <summary>
 /// Decodes the data with the specified filter.
 /// </summary>
 public static byte[] Decode(byte[] data, PdfItem filterItem)
 {
     byte[] result = null;
     if (filterItem is PdfName)
     {
         Filter filter = GetFilter(filterItem.ToString());
         if (filter != null)
         {
             result = filter.Decode(data);
         }
     }
     else if (filterItem is PdfArray)
     {
         PdfArray array = (PdfArray)filterItem;
         foreach (PdfItem item in array)
         {
             data = Decode(data, item);
         }
         result = data;
     }
     return(result);
 }
예제 #15
0
        void FillStreamBox()
        {
            PdfItem       item = this.explorer.MainForm.Process.Navigator.Current;
            PdfDictionary dict = item as PdfDictionary;

            if (dict.Stream == null)
            {
                this.pnlBottom.Enabled = false;
                this.tbxStream.Text    = "";
                return;
            }

            this.pnlBottom.Enabled = true;

            if (dict != null && dict.Stream != null && dict.Stream.Value != null)
            {
                byte[] stream = this.btnNoFilter.Checked ? dict.Stream.UnfilteredValue : dict.Stream.Value;
                if (btnHexdump.Checked)
                {
                    this.tbxStream.Text = ExplorerHelper.HexDump(stream);
                }
                else
                {
                    int    count = stream.Length;
                    char[] chars = new char[count];
                    for (int idx = 0; idx < count; idx++)
                    {
                        byte b = stream[idx];
                        if (b == 0)
                        {
                            b = 183;
                        }
                        chars[idx] = (char)b;
                    }
                    this.tbxStream.Text = new string(chars);
                }
            }
        }
예제 #16
0
        /// <summary>
        /// Encrypts an array.
        /// </summary>
        void EncryptArray(PdfArray array)
        {
            int count = array.Elements.Count;

            for (int idx = 0; idx < count; idx++)
            {
                PdfItem       item = array.Elements[idx];
                PdfString     value1;
                PdfDictionary value2;
                PdfArray      value3;
                if ((value1 = item as PdfString) != null)
                {
                    EncryptString(value1);
                }
                else if ((value2 = item as PdfDictionary) != null)
                {
                    EncryptDictionary(value2);
                }
                else if ((value3 = item as PdfArray) != null)
                {
                    EncryptArray(value3);
                }
            }
        }
            public Font(PdfDictionary dictionary)
            {
                font = dictionary;

                // font.Elements["SubType"]
                // font.Elements["Encoding"]
                if (font.Elements.Keys.Contains("/Encoding"))
                {
                    PdfItem item = font.Elements["/Encoding"];
                    if (item is PdfReference)
                    {
                        var dict = (((item as PdfReference).Value) as PdfDictionary);
                        if (dict.Elements.ContainsKey("/BaseEncoding"))
                        {
                            Encoding = dict.Elements["/BaseEncoding"].ToString();
                        }
                    }
                    else
                    {
                        Encoding = item.ToString();
                    }
                }

                if (Encoding != null && Encoding.StartsWith("/Identity-"))
                {
                    IsTwoByte = true;
                }

                if (font.Elements.Keys.Contains("/Subtype"))
                {
                    switch (font.Elements["/Subtype"].ToString())
                    {
                    case "/TrueType":
                        Type = font_type.TrueType;
                        break;

                    case "/Type0":
                        Type = font_type.Type0;
                        break;

                    case "/Type1":
                        Type = font_type.Type1;
                        break;

                    case "/Type3":
                        Type = font_type.Type3;
                        break;

                    default:
                        Type = font_type.Other;
                        break;
                    }
                }

                if (font.Elements.ContainsKey("/FontDescriptor"))
                {
                    var obj = font.Elements["/FontDescriptor"];
                    if (obj is PdfReference)
                    {
                        obj = (obj as PdfReference).Value;
                    }
                    obj = (obj as PdfDictionary).Elements["/Flags"];
                    if (obj != null)
                    {
                        if (obj is PdfReference)
                        {
                            obj = (obj as PdfReference).Value;
                        }
                        if (obj is PdfInteger)
                        {
                            Flags = (obj as PdfInteger).Value;
                        }
                    }
                }

                if (font.Elements.Keys.Contains("/ToUnicode"))
                { // parse to unicode
                    PdfItem item = font.Elements["/ToUnicode"];
                    if (item is PdfReference)
                    {
                        item = (item as PdfReference).Value;
                    }
                    if (item is PdfDictionary)
                    {
                        string map = (item as PdfDictionary).Stream.ToString();
                        toUnicode = ParseCMap(map);
                    }
                }
                else
                {
                    if (Encoding != null)
                    {
                        switch (Encoding)
                        {
                        case "/MacRomanEncoding":
                            toUnicode = EncodingTables.MacRoman;
                            break;

                        case "/WinAnsiEncoding":
                            toUnicode = EncodingTables.WinAnsi;
                            break;

                        case "/MacExpertEncoding":
                            toUnicode = EncodingTables.MacExpert;
                            break;

                        case "/Standard":
                            toUnicode = EncodingTables.Standard;
                            break;

                        case "/Symbol":
                            toUnicode = EncodingTables.Symbol;
                            break;
                        }
                    }
                    else
                    {
                        if ((Flags & Flag_Symbolic) != 0)
                        {
                            toUnicode = EncodingTables.Symbol;
                        }
                        else
                        {
                            toUnicode = EncodingTables.Standard;
                        }
                    }
                }
            }
예제 #18
0
        /// <summary>
        /// Draws an image over a rectange (like those specified in a form field)
        /// </summary>
        /// <param name="PDFDocument"></param>
        /// <param name="sImage">Should work for PNG, JPG, GIF and BMP</param>
        /// <param name="rect"></param>
        /// <param name="focusPageReference"></param>
        private static void OverlayImageOnField(PdfDocument PDFDocument, byte[] image, PdfRectangle rect, PdfItem focusPageReference)
        {
            var stream = new MemoryStream(image, 0, image.Length);


            //also a bit of pain to locate the acutaly page the field is on... but OK.
            XGraphics gfxObj = null;

            foreach (var page in PDFDocument.Pages)
            {
                if (page.Reference == focusPageReference)
                {
                    if (gfxObj != null)   //dispose of object before getting a new one.  Should actually never happen... but better safe than sorry....
                    {
                        gfxObj.Dispose();
                    }
                    gfxObj = XGraphics.FromPdfPage(page);
                    break;
                }
            }

            // Draw the image


            XImage ximage = XImage.FromStream(stream);

            double xscaling = 1;
            double yscaling = 1;

            if (ximage.PointWidth / ximage.PointHeight > rect.Width / rect.Height)
            {
                yscaling = (ximage.PointWidth / ximage.PointHeight) / (rect.Width / rect.Height);
            }
            else
            {
                xscaling = (rect.Width / rect.Height) / (ximage.PointWidth / ximage.PointHeight);
            }

            double scaling = Math.Max(ximage.PointHeight / rect.Height, ximage.PointWidth / rect.Width);

            if (gfxObj.PageDirection == XPageDirection.Downwards)
            {
                gfxObj.DrawImage(ximage, rect.X1, gfxObj.PageSize.Height - rect.Y1 - rect.Height, rect.Width / xscaling, rect.Height / yscaling);
            }
            else
            {
                gfxObj.DrawImage(ximage, rect.X1, rect.Y1, rect.Width / scaling, rect.Height / scaling);
            }
            gfxObj.Dispose();
        }
예제 #19
0
        private static PdfPTable CreateTable(PdfItem item, float width)
        {
            var tbl = new PdfPTable(10)
            {
                TotalWidth          = width,
                LockedWidth         = true,
                HorizontalAlignment = Element.ALIGN_LEFT
            };

            float[] wcol =
            {
                35.0F,  //left padding
                54.0F,  //Label
                229.0F, //Part Number/Description
                28.0F,  //QTY
                30.0F,  //REC
                32.0F,  //B.O.
                40.0F,  //CAT
                68.0F,  //UNIT PRICE
                75.0F,  //ITEM TOTAL
                0.0F    //RIGHT PADDING
            };

            wcol[9] = width - (wcol[0] + wcol[1] + wcol[2] + wcol[3] + wcol[4] + wcol[5] + wcol[6] + wcol[7] + wcol[8]);

            if (wcol[9] < 0)
            {
                throw new Exception($"Total column widths exceed page width by {Math.Abs(wcol[9])}");
            }

            tbl.SetTotalWidth(wcol);

            if (!item.IsNotes)
            {
                // first row
                tbl.AddCell(GetCell(string.Empty, 1, Element.ALIGN_UNDEFINED, Element.ALIGN_UNDEFINED, false));
                tbl.AddCell(GetCell("Part Number:", 1, Element.ALIGN_LEFT, Element.ALIGN_TOP, false));
                tbl.AddCell(GetCell(item.PartNum, 1, Element.ALIGN_LEFT, Element.ALIGN_TOP, false));
                tbl.AddCell(GetCell(string.Empty, 7, Element.ALIGN_UNDEFINED, Element.ALIGN_UNDEFINED, false));

                // second row
                tbl.AddCell(GetCell(string.Empty, 1, Element.ALIGN_UNDEFINED, Element.ALIGN_UNDEFINED, false));
                tbl.AddCell(GetCell("Description:", 1, Element.ALIGN_LEFT, Element.ALIGN_TOP, true));
                tbl.AddCell(GetCell(item.Description, 1, Element.ALIGN_LEFT, Element.ALIGN_TOP, true));
                tbl.AddCell(GetCell(GetQtyAndUnit(item), 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM, true));
                tbl.AddCell(GetCell(string.Empty, 1, Element.ALIGN_UNDEFINED, Element.ALIGN_UNDEFINED, true));
                tbl.AddCell(GetCell(string.Empty, 1, Element.ALIGN_UNDEFINED, Element.ALIGN_UNDEFINED, true));
                tbl.AddCell(GetCell(item.CategoryNumber, 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM, true));
                tbl.AddCell(GetCell(item.UnitPrice.ToString("C"), 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM, true));
                tbl.AddCell(GetCell(item.ExtPrice.ToString("C"), 1, Element.ALIGN_RIGHT, Element.ALIGN_BOTTOM, true));
                tbl.AddCell(GetCell(string.Empty, 1, Element.ALIGN_UNDEFINED, Element.ALIGN_UNDEFINED, false));
            }
            else
            {
                tbl.AddCell(GetCell(string.Empty, 10, Element.ALIGN_UNDEFINED, Element.ALIGN_UNDEFINED, false));
                tbl.AddCell(GetCell(string.Empty, 10, Element.ALIGN_UNDEFINED, Element.ALIGN_UNDEFINED, false));
                tbl.AddCell(GetCell(string.Empty, 1, Element.ALIGN_UNDEFINED, Element.ALIGN_UNDEFINED, false));
                tbl.AddCell(GetCell("Notes:", 1, Element.ALIGN_LEFT, Element.ALIGN_TOP, false));
                tbl.AddCell(GetCell(item.Description, 1, Element.ALIGN_LEFT, Element.ALIGN_TOP, false));
                tbl.AddCell(GetCell(string.Empty, 7, Element.ALIGN_UNDEFINED, Element.ALIGN_UNDEFINED, false));
            }

            return(tbl);
        }
예제 #20
0
 /// <summary>
 /// Pushes the specified item onto the stack.
 /// </summary>
 public void Shift(PdfItem item)
 {
     Debug.Assert(item != null);
     _items.Add(item);
     _sp++;
 }
예제 #21
0
        /// <summary>
        /// Replace all indirect references to external objects by their cloned counterparts
        /// owned by the importer document.
        /// </summary>
        void FixUpObject_old(PdfImportedObjectTable iot, PdfObject value)
        {
            // TODO: merge with PdfXObject.FixUpObject
            PdfDictionary dict;
            PdfArray      array;

            if ((dict = value as PdfDictionary) != null)
            {
                // Set document for cloned direct objects
                if (dict.Owner == null)
                {
                    dict.Document = Owner;
                }
                else
                {
                    Debug.Assert(dict.Owner == Owner);
                }

                // Search for indirect references in all keys
                PdfName[] names = dict.Elements.KeyNames;
                foreach (PdfName name in names)
                {
                    PdfItem item = dict.Elements[name];
                    // Is item an iref?
                    PdfReference iref = item as PdfReference;
                    if (iref != null)
                    {
                        // Does the iref already belong to this document?
                        if (iref.Document == Owner)
                        {
                            // Yes: fine
                            continue;
                        }
                        else
                        {
                            Debug.Assert(iref.Document == iot.ExternalDocument);
                            // No: replace with iref of cloned object
                            PdfReference newXRef = iot[iref.ObjectID];
                            Debug.Assert(newXRef != null);
                            Debug.Assert(newXRef.Document == Owner);
                            dict.Elements[name] = newXRef;
                        }
                    }
                    else if (item is PdfObject)
                    {
                        // Fix up inner objects
                        FixUpObject_old(iot, (PdfObject)item);
                    }
                }
            }
            else if ((array = value as PdfArray) != null)
            {
                // Set document for cloned direct objects
                if (array.Owner == null)
                {
                    array.Document = Owner;
                }
                else
                {
                    Debug.Assert(array.Owner == Owner);
                }

                // Search for indirect references in all array elements
                int count = array.Elements.Count;
                for (int idx = 0; idx < count; idx++)
                {
                    PdfItem item = array.Elements[idx];
                    // Is item an iref?
                    PdfReference iref = item as PdfReference;
                    if (iref != null)
                    {
                        // Does the iref belongs to this document?
                        if (iref.Document == Owner)
                        {
                            // Yes: fine
                            continue;
                        }
                        else
                        {
                            Debug.Assert(iref.Document == iot.ExternalDocument);
                            // No: replace with iref of cloned object
                            PdfReference newXRef = iot[iref.ObjectID];
                            Debug.Assert(newXRef != null);
                            Debug.Assert(newXRef.Document == Owner);
                            array.Elements[idx] = newXRef;
                        }
                    }
                    else if (item is PdfObject)
                    {
                        // Fix up inner objects
                        FixUpObject_old(iot, (PdfObject)item);
                    }
                }
            }
        }
예제 #22
0
        /// <summary>
        /// Search the XMP metadata
        /// </summary>
        /// <param name="doc">A open PdfDocument</param>
        private void ReadXMPMetadata(PdfDocument doc)
        {
            if (doc.Internals.Catalog.Elements.ContainsKey("/Metadata"))
            {
                PdfItem pi = doc.Internals.Catalog.Elements["/Metadata"];
                //doc.Internals.Catalog.Elements.Remove("/Metadata");
                if (pi is PdfSharp.Pdf.Advanced.PdfReference)
                {
                    int           intXMPObjectNumber = (pi as PdfSharp.Pdf.Advanced.PdfReference).ObjectNumber;
                    PdfDictionary pDic = (PdfDictionary)doc.Internals.GetObject(new PdfObjectID(intXMPObjectNumber));
                    string        xmp  = pDic.Stream.ToString();
                    if (xmp != string.Empty)
                    {
                        System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
                        xDoc.XmlResolver = null;
                        xDoc.LoadXml(xmp);
                        #region Metadatos como atributos
                        XmlNodeList xnl = xDoc.GetElementsByTagName("rdf:Description");
                        foreach (XmlNode xn in xnl)
                        {
                            XmlAttribute xa = xn.Attributes["pdf:Creator"];
                            if (xa != null && !string.IsNullOrEmpty(xa.Value))
                            {
                                string strValue = Analysis.ApplicationAnalysis.GetApplicationsFromString(xa.Value);
                                if (strValue.Trim() != string.Empty)
                                {
                                    if (!FoundMetaData.Applications.Items.Any(A => A.Name == strValue.Trim()))
                                    {
                                        FoundMetaData.Applications.Items.Add(new ApplicationsItem(strValue.Trim()));
                                    }
                                }
                                //No se ha localizado ninguna aplicación conocida, aun así mostrar la aplicación encontrada
                                else
                                {
                                    if (xa.Value.Trim() != string.Empty && !FoundMetaData.Applications.Items.Any(A => A.Name == xa.Value.Trim()))
                                    {
                                        FoundMetaData.Applications.Items.Add(new ApplicationsItem(xa.Value.Trim()));
                                    }
                                }
                            }
                            xa = xn.Attributes["pdf:CreationDate"];
                            if (xa != null && !string.IsNullOrEmpty(xa.Value))
                            {
                                string   strValue = xa.Value;
                                DateTime d;
                                if (DateTime.TryParse(strValue.Replace('T', ' ').Replace('Z', ' '), out d))
                                {
                                    //Si existe una fecha de creación anterior, sobreescribir
                                    if (!FoundDates.CreationDateSpecified || FoundDates.CreationDate > d)
                                    {
                                        FoundDates.CreationDateSpecified = true;
                                        FoundDates.CreationDate          = d;
                                    }
                                }
                            }
                            xa = xn.Attributes["pdf:Title"];
                            if (xa != null && !string.IsNullOrEmpty(xa.Value))
                            {
                                string strValue = xa.Value;
                                if (string.IsNullOrEmpty(FoundMetaData.Title) || FoundMetaData.Title.Length < strValue.Length)
                                {
                                    FoundMetaData.Title = strValue;
                                }
                            }
                            xa = xn.Attributes["pdf:Author"];
                            if (xa != null && !string.IsNullOrEmpty(xa.Value))
                            {
                                FoundUsers.AddUniqueItem(xa.Value, true);
                            }
                            xa = xn.Attributes["pdf:Producer"];
                            if (xa != null && !string.IsNullOrEmpty(xa.Value))
                            {
                                string strValue = Analysis.ApplicationAnalysis.GetApplicationsFromString(xa.Value);
                                if (strValue.Trim() != string.Empty)
                                {
                                    if (!FoundMetaData.Applications.Items.Any(A => A.Name == strValue.Trim()))
                                    {
                                        FoundMetaData.Applications.Items.Add(new ApplicationsItem(strValue.Trim()));
                                    }
                                }
                                //No se ha localizado ninguna aplicación conocida, aun así mostrar la aplicación encontrada
                                else
                                {
                                    if (xa.Value.Trim() != string.Empty && !FoundMetaData.Applications.Items.Any(A => A.Name == xa.Value.Trim()))
                                    {
                                        FoundMetaData.Applications.Items.Add(new ApplicationsItem(xa.Value.Trim()));
                                    }
                                }
                            }
                            xa = xn.Attributes["pdf:ModDate"];
                            if (xa != null && !string.IsNullOrEmpty(xa.Value))
                            {
                                string   strValue = xa.Value;
                                DateTime d;
                                if (DateTime.TryParse(strValue.Replace('T', ' ').Replace('Z', ' '), out d))
                                {
                                    FoundDates.ModificationDateSpecified = true;
                                    FoundDates.ModificationDate          = d;
                                }
                            }
                            xa = xn.Attributes["xap:CreateDate"];
                            if (xa != null && !string.IsNullOrEmpty(xa.Value))
                            {
                                string   strValue = xa.Value;
                                DateTime d;
                                if (DateTime.TryParse(strValue.Replace('T', ' ').Replace('Z', ' '), out d))
                                {
                                    //Si existe una fecha de creación anterior, sobreescribir
                                    if (!FoundDates.CreationDateSpecified || FoundDates.CreationDate > d)
                                    {
                                        //Si existe una fecha de modificación posterior, sobreescribir
                                        if (!FoundDates.ModificationDateSpecified || FoundDates.ModificationDate < d)
                                        {
                                            FoundDates.CreationDateSpecified = true;
                                            FoundDates.CreationDate          = d;
                                        }
                                    }
                                }
                            }
                            xa = xn.Attributes["xap:Title"];
                            if (xa != null && !string.IsNullOrEmpty(xa.Value))
                            {
                                string strValue = xa.Value;
                                //Si ya existe un título y es mas pequeño, sobreescribirle.
                                if ((string.IsNullOrEmpty(FoundMetaData.Title) || FoundMetaData.Title.Length < strValue.Length))
                                {
                                    FoundMetaData.Title = strValue;
                                }
                            }
                            xa = xn.Attributes["xap:Author"];
                            if (xa != null && !string.IsNullOrEmpty(xa.Value))
                            {
                                FoundUsers.AddUniqueItem(xa.Value, true);
                            }
                            xa = xn.Attributes["xap:ModifyDate"];
                            if (xa != null && !string.IsNullOrEmpty(xa.Value))
                            {
                                string   strValue = xa.Value;
                                DateTime d;
                                if (DateTime.TryParse(strValue.Replace('T', ' ').Replace('Z', ' '), out d))
                                {
                                    //Si existe una fecha de modificación posterior, sobreescribir
                                    if (!FoundDates.ModificationDateSpecified || FoundDates.ModificationDate < d)
                                    {
                                        FoundDates.ModificationDateSpecified = true;
                                        FoundDates.ModificationDate          = d;
                                    }
                                }
                            }
                            xa = xn.Attributes["xap:CreatorTool"];
                            if (xa != null && !string.IsNullOrEmpty(xa.Value))
                            {
                                string strValue = Analysis.ApplicationAnalysis.GetApplicationsFromString(xa.Value);
                                if (strValue.Trim() != string.Empty)
                                {
                                    if (!FoundMetaData.Applications.Items.Any(A => A.Name == strValue.Trim()))
                                    {
                                        FoundMetaData.Applications.Items.Add(new ApplicationsItem(strValue.Trim()));
                                    }
                                }
                                //No se ha localizado ninguna aplicación conocida, aun así mostrar la aplicación encontrada
                                else
                                {
                                    if (xa.Value.Trim() != string.Empty && !FoundMetaData.Applications.Items.Any(A => A.Name == xa.Value.Trim()))
                                    {
                                        FoundMetaData.Applications.Items.Add(new ApplicationsItem(xa.Value.Trim()));
                                    }
                                }
                            }
                            //xap:MetadataDate, fecha en la que se añadieron los metadatos
                            xa = xn.Attributes["dc:title"];
                            if (xa != null && !string.IsNullOrEmpty(xa.Value))
                            {
                                string strValue = xa.Value;
                                //Si ya existe un título y es mas pequeño, sobreescribirle.
                                if (string.IsNullOrEmpty(FoundMetaData.Title) || FoundMetaData.Title.Length < strValue.Length)
                                {
                                    FoundMetaData.Title = strValue;
                                }
                            }
                            xa = xn.Attributes["dc:creator"];
                            if (xa != null && !string.IsNullOrEmpty(xa.Value))
                            {
                                string strValue = xa.Value;
                                if (!string.IsNullOrEmpty(strValue))
                                {
                                    FoundUsers.AddUniqueItem(strValue, true);
                                }
                            }
                        }
                        #endregion

                        #region Metadatos como nodos independientes
                        xnl = xDoc.GetElementsByTagName("pdf:Creator");
                        if (xnl != null && xnl.Count != 0 && xnl[0].HasChildNodes && !string.IsNullOrEmpty(xnl[0].FirstChild.Value))
                        {
                            string strValue = Analysis.ApplicationAnalysis.GetApplicationsFromString(xnl[0].FirstChild.Value);
                            if (strValue.Trim() != string.Empty)
                            {
                                if (!FoundMetaData.Applications.Items.Any(A => A.Name == strValue.Trim()))
                                {
                                    FoundMetaData.Applications.Items.Add(new ApplicationsItem(strValue.Trim()));
                                }
                            }
                            //No se ha localizado ninguna aplicación conocida, aun así mostrar la aplicación encontrada
                            else
                            {
                                if (xnl[0].FirstChild.Value.Trim() != string.Empty && !FoundMetaData.Applications.Items.Any(A => A.Name == xnl[0].FirstChild.Value.Trim()))
                                {
                                    FoundMetaData.Applications.Items.Add(new ApplicationsItem(xnl[0].FirstChild.Value.Trim()));
                                }
                            }
                        }
                        xnl = xDoc.GetElementsByTagName("pdf:CreationDate");
                        if (xnl != null && xnl.Count != 0 && xnl[0].HasChildNodes && !string.IsNullOrEmpty(xnl[0].FirstChild.Value))
                        {
                            string   strValue = xnl[0].FirstChild.Value;
                            DateTime d;
                            if (DateTime.TryParse(strValue.Replace('T', ' ').Replace('Z', ' '), out d))
                            {
                                //Si existe una fecha de creación anterior, sobreescribir
                                if (!FoundDates.CreationDateSpecified || FoundDates.CreationDate > d)
                                {
                                    FoundDates.CreationDateSpecified = true;
                                    FoundDates.CreationDate          = d;
                                }
                            }
                        }
                        xnl = xDoc.GetElementsByTagName("pdf:Title");
                        if (xnl != null && xnl.Count != 0 && xnl[0].HasChildNodes && !string.IsNullOrEmpty(xnl[0].FirstChild.Value))
                        {
                            string strValue = xnl[0].FirstChild.Value;
                            if ((string.IsNullOrEmpty(FoundMetaData.Title) || FoundMetaData.Title.Length < strValue.Length))
                            {
                                FoundMetaData.Title = strValue;
                            }
                        }
                        xnl = xDoc.GetElementsByTagName("pdf:Author");
                        if (xnl != null && xnl.Count != 0 && xnl[0].HasChildNodes && !string.IsNullOrEmpty(xnl[0].FirstChild.Value))
                        {
                            FoundUsers.AddUniqueItem(xnl[0].FirstChild.Value, true);
                        }
                        xnl = xDoc.GetElementsByTagName("pdf:Producer");
                        if (xnl != null && xnl.Count != 0 && xnl[0].HasChildNodes && !string.IsNullOrEmpty(xnl[0].FirstChild.Value))
                        {
                            string strValue = Analysis.ApplicationAnalysis.GetApplicationsFromString(xnl[0].FirstChild.Value);
                            if (strValue.Trim() != string.Empty)
                            {
                                if (!FoundMetaData.Applications.Items.Any(A => A.Name == strValue.Trim()))
                                {
                                    FoundMetaData.Applications.Items.Add(new ApplicationsItem(strValue.Trim()));
                                }
                            }
                            //No se ha localizado ninguna aplicación conocida, aun así mostrar la aplicación encontrada
                            else
                            {
                                if (xnl[0].FirstChild.Value.Trim() != string.Empty && !FoundMetaData.Applications.Items.Any(A => A.Name == xnl[0].FirstChild.Value.Trim()))
                                {
                                    FoundMetaData.Applications.Items.Add(new ApplicationsItem(xnl[0].FirstChild.Value.Trim()));
                                }
                            }
                        }
                        xnl = xDoc.GetElementsByTagName("pdf:ModDate");
                        if (xnl != null && xnl.Count != 0 && xnl[0].HasChildNodes && !string.IsNullOrEmpty(xnl[0].FirstChild.Value))
                        {
                            string   strValue = xnl[0].FirstChild.Value;
                            DateTime d;
                            if (DateTime.TryParse(strValue.Replace('T', ' ').Replace('Z', ' '), out d))
                            {
                                FoundDates.ModificationDateSpecified = true;
                                FoundDates.ModificationDate          = d;
                            }
                        }
                        xnl = xDoc.GetElementsByTagName("xap:CreateDate");
                        if (xnl != null && xnl.Count != 0 && xnl[0].HasChildNodes && !string.IsNullOrEmpty(xnl[0].FirstChild.Value))
                        {
                            string   strValue = xnl[0].FirstChild.Value;
                            DateTime d;
                            if (DateTime.TryParse(strValue.Replace('T', ' ').Replace('Z', ' '), out d))
                            {
                                //Si existe una fecha de creación anterior, sobreescribir
                                if (!FoundDates.CreationDateSpecified || FoundDates.CreationDate > d)
                                {
                                    //Si existe una fecha de modificación posterior, sobreescribir
                                    if (!FoundDates.ModificationDateSpecified || FoundDates.ModificationDate < d)
                                    {
                                        FoundDates.CreationDateSpecified = true;
                                        FoundDates.CreationDate          = d;
                                    }
                                }
                            }
                        }
                        xnl = xDoc.GetElementsByTagName("xap:Title");
                        if (xnl != null && xnl.Count != 0 && xnl[0].HasChildNodes)
                        {
                            XmlNode xn = xnl[0].FirstChild;
                            //Busca el primer subnodo con valor
                            while (xn.Value == null && xn.HasChildNodes)
                            {
                                xn = xn.FirstChild;
                            }
                            if (!string.IsNullOrEmpty(xn.Value))
                            {
                                string strValue = xn.Value;
                                //Si ya existe un título y es mas pequeño, sobreescribirle.
                                if ((string.IsNullOrEmpty(FoundMetaData.Title) || FoundMetaData.Title.Length < strValue.Length))
                                {
                                    FoundMetaData.Title = strValue;
                                }
                            }
                        }
                        xnl = xDoc.GetElementsByTagName("xap:Author");
                        if (xnl != null && xnl.Count != 0 && xnl[0].HasChildNodes && !string.IsNullOrEmpty(xnl[0].FirstChild.Value))
                        {
                            FoundUsers.AddUniqueItem(xnl[0].FirstChild.Value, true);
                        }
                        xnl = xDoc.GetElementsByTagName("xap:ModifyDate");
                        if (xnl != null && xnl.Count != 0 && xnl[0].HasChildNodes && !string.IsNullOrEmpty(xnl[0].FirstChild.Value))
                        {
                            string   strValue = xnl[0].FirstChild.Value;
                            DateTime d;
                            if (DateTime.TryParse(strValue.Replace('T', ' ').Replace('Z', ' '), out d))
                            {
                                //Si existe una fecha de modificación posterior, sobreescribir
                                if (!FoundDates.ModificationDateSpecified || FoundDates.ModificationDate < d)
                                {
                                    FoundDates.ModificationDateSpecified = true;
                                    FoundDates.ModificationDate          = d;
                                }
                            }
                        }
                        xnl = xDoc.GetElementsByTagName("xap:CreatorTool");
                        if (xnl != null && xnl.Count != 0 && xnl[0].HasChildNodes && !string.IsNullOrEmpty(xnl[0].FirstChild.Value))
                        {
                            string strValue = Analysis.ApplicationAnalysis.GetApplicationsFromString(xnl[0].FirstChild.Value);
                            if (strValue.Trim() != string.Empty)
                            {
                                if (!FoundMetaData.Applications.Items.Any(A => A.Name == strValue.Trim()))
                                {
                                    FoundMetaData.Applications.Items.Add(new ApplicationsItem(strValue.Trim()));
                                }
                            }
                            //No se ha localizado ninguna aplicación conocida, aun así mostrar la aplicación encontrada
                            else
                            {
                                if (xnl[0].FirstChild.Value.Trim() != string.Empty && !FoundMetaData.Applications.Items.Any(A => A.Name == xnl[0].FirstChild.Value.Trim()))
                                {
                                    FoundMetaData.Applications.Items.Add(new ApplicationsItem(xnl[0].FirstChild.Value.Trim()));
                                }
                            }
                        }
                        //xap:MetadataDate, fecha en la que se añadieron los metadatos
                        xnl = xDoc.GetElementsByTagName("dc:title");
                        if (xnl != null && xnl.Count != 0 && xnl[0].HasChildNodes)
                        {
                            XmlNode xn = xnl[0].FirstChild;
                            //Busca el primer subnodo con valor
                            while (xn.Value == null && xn.HasChildNodes)
                            {
                                xn = xn.FirstChild;
                            }
                            if (!string.IsNullOrEmpty(xn.Value))
                            {
                                string strValue = xn.Value;
                                //Si ya existe un título y es mas pequeño, sobreescribirle.
                                if ((string.IsNullOrEmpty(FoundMetaData.Title) || FoundMetaData.Title.Length < strValue.Length))
                                {
                                    FoundMetaData.Title = strValue;
                                }
                            }
                        }

                        //if (xnl != null && xnl.Count != 0 && xnl[0].HasChildNodes && !string.IsNullOrEmpty(xnl[0].FirstChild.Value))
                        //FoundUsers.AddUniqueItem(xnl[0].FirstChild.Value, true);
                        #endregion
                    }
                }
            }
        }
예제 #23
0
        public CharacterMap(string name, PdfItem item)
        {
            Name = name;
            var dictionary = (PdfDictionary)item;

            if (!dictionary.Elements.KeyNames.Select(n => n.Value).Contains("/ToUnicode"))
            {
                return;
            }

            var cmapItem = dictionary.Elements["/ToUnicode"];

            if (cmapItem != null && cmapItem is PdfReference)
            {
                cmapItem = ((PdfReference)cmapItem).Value;
            }

            var cmap = ((PdfDictionary)cmapItem).Stream.ToString();

            // space range
            var spaceRangeMatch = Regex.Match(cmap, "begincodespacerange\\s*<(\\w+)>\\s*<(\\w+)>\\s*endcodespacerange", RegexOptions.Singleline);

            if (!spaceRangeMatch.Success)
            {
                CodeSpaceRange = Tuple.Create("0000", "FFFF");
            }
            else
            {
                CodeSpaceRange = Tuple.Create(
                    FromHex(spaceRangeMatch.Groups[1].Value).ToString("X4"),
                    FromHex(spaceRangeMatch.Groups[2].Value).ToString("X4"));
            }

            // bfrange
            var bfRangeMatch = Regex.Match(cmap, "beginbfrange\\s*(.*?)\\s*endbfrange", RegexOptions.Singleline);

            if (bfRangeMatch.Success)
            {
                // find the rows and split them
                var bfRangeRowRegex = new Regex("<(\\w+)>\\s*<(\\w+)>\\s*([\\[\\]\\w <>]*)");
                BfRange = bfRangeMatch.Groups[1].Value.Split('\n').Select(r => r.Replace("\r", string.Empty)).Select(r =>
                {
                    var match = bfRangeRowRegex.Match(r);
                    if (match.Success)
                    {
                        return(Tuple.Create(
                                   FromHex(match.Groups[1].Value).ToString("X4"),
                                   FromHex(match.Groups[2].Value).ToString("X4"),
                                   Regex.Matches(match.Groups[3].Value, "<(\\w+)>")
                                   .Cast <Match>()
                                   .Select(m => FromHex(m.Groups[1].Value).ToString("X4"))));
                    }
                    return(null);
                }).Where(t => t != null);
            }

            // bfchar
            var bfCharMatch = Regex.Match(cmap, "beginbfchar\\s*(.+?)\\s*endbfchar", RegexOptions.Singleline);

            if (bfCharMatch.Success)
            {
                var bfCharRowRegex = new Regex("<(\\w+)>\\s*<(\\w+)>");
                BfChar = bfCharMatch.Groups[1].Value.Split('\n').Select(r => r.Replace("\r", string.Empty)).Select(r =>
                {
                    var match = bfCharRowRegex.Match(r);
                    if (match.Success)
                    {
                        return(new KeyValuePair <string, string>(
                                   FromHex(match.Groups[1].Value).ToString("X4"),
                                   FromHex(match.Groups[2].Value).ToString("X4")));
                    }
                    return(new KeyValuePair <string, string>(null, null));
                }).Where(p => p.Key != null && p.Value != null).ToDictionary(p => p.Key, p => p.Value);
            }
        }
예제 #24
0
 public void NavigateTo(PdfItem item)
 {
 }
예제 #25
0
 /// <summary>
 /// Pushes the specified item onto the stack.
 /// </summary>
 public void Shift(PdfItem item)
 {
     Debug.Assert(item != null);
     _items.Add(item);
     _sp++;
 }
예제 #26
0
        private static IEnumerable <Tuple <string, PdfItem> > FindObjects(string[] objectHierarchy, PdfItem startingObject, bool followHierarchy)
        {
            var results = new List <Tuple <string, PdfItem> >();

            FindObjects(objectHierarchy, startingObject, followHierarchy, ref results, 0);
            return(results);
        }
예제 #27
0
 internal virtual void SetObject(PdfItem value)
 {
 }
예제 #28
0
        internal PdfFormXObject(PdfDocument thisDocument, PdfImportedObjectTable importedObjectTable, XPdfForm form)
            : base(thisDocument)
        {
            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;
            }
            Debug.Assert(importedObjectTable != null);

            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;
                if (res is PdfReference)
                {
                    root = ((PdfReference)res).Value;
                }
                else
                {
                    root = (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 ealier 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.Keys.MediaBox);
            int          rotate = importPage.Elements.GetInteger(PdfPage.Keys.Rotate);
            //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));

                if (rotate != 180)
                {
                    // Translate the image such that its center lies on the center of the rotated bounding box
                    double offset = (height - width) / 2;
                    if (height > width)
                    {
                        matrix.TranslatePrepend(offset, offset);
                    }
                    else
                    {
                        matrix.TranslatePrepend(-offset, -offset);
                    }
                }

                //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);
        }
예제 #29
0
        /// <summary>
        /// Draws an image over a rectange (like those specified in a form field)
        /// </summary>
        /// <param name="PDFDocument"></param>
        /// <param name="b64Image">Should work for PNG, JPG, GIF and BMP</param>
        /// <param name="rect"></param>
        /// <param name="focusPageReference"></param>
        private static void OverlayImageOnField(PdfDocument PDFDocument, string b64Image, PdfRectangle rect, PdfItem focusPageReference)
        {
            byte[] data = Convert.FromBase64String(b64Image);
            //byte[] data = Helper.GetJohnHancockPNGB64();

            OverlayImageOnField(PDFDocument, data, rect, focusPageReference);
        }
예제 #30
0
 /// <summary>
 /// Decodes the data with the specified filter.
 /// </summary>
 public static byte[] Decode(byte[] data, PdfItem filterItem)
 {
   byte[] result = null;
   if (filterItem is PdfName)
   {
     Filter filter = GetFilter(filterItem.ToString());
     if (filter != null)
       result = filter.Decode(data);
   }
   else if (filterItem is PdfArray)
   {
     PdfArray array = (PdfArray)filterItem;
     foreach (PdfItem item in array)
       data = Filtering.Decode(data, item);
     result = data;
   }
   return result;
 }
예제 #31
0
 /// <summary>
 /// Replaces the last 'count' items with the specified item.
 /// </summary>
 public void Reduce(PdfItem item, int count)
 {
     Debug.Assert(item != null);
     Reduce(count);
     _items.Add(item);
     _sp++;
 }