private int[] getIndex(PDFDictionary dict) { PDFArray Index = dict["Index"] as PDFArray; if (Index == null) { int[] ind = { 0, 0 }; PDFNumber size = dict["Size"] as PDFNumber; if (size == null || size.GetValue() <= 0) { throw new InvalidDocumentException(); } ind[1] = (int)size.GetValue(); return(ind); } int[] index = new int[Index.Count]; for (int i = 0; i < Index.Count; ++i) { PDFNumber number = Index[i] as PDFNumber; if (number == null || number.GetValue() < 0) { throw new InvalidDocumentException(); } index[i] = (int)number.GetValue(); } return(index); }
internal RotatingRectangle(PDFArray array, Page page) { _array = array; _page = page; if (array.Count != 8) { setDefault(); changeArray(); } else { float[] arrayf = new float[8]; for (int i = 0; i < 8; ++i) { PDFNumber number = array[i] as PDFNumber; if (number == null) { setDefault(); changeArray(); return; } arrayf[i] = (float)number.GetValue(); } setFromArray(arrayf); } }
protected override bool isHitInRange(PDFArray limits, object key) { if (key.GetType() != System.Type.GetType("int")) { throw new ArgumentException("Key must have a integer type for NumberTree."); } int number = (int)key; PDFNumber first = limits[0] as PDFNumber; PDFNumber last = limits[1] as PDFNumber; if (first != null && last != null) { int comp1 = ((int)first.GetValue()).CompareTo(number); int comp2 = ((int)last.GetValue()).CompareTo(number); if (comp1 <= 0 && comp2 >= 0) { return(true); } } return(false); }
protected override IPDFObject findItem(PDFArray array, object key) { if (array == null) { return(null); } int number = (int)key; for (int i = 0; i < array.Count; i += 2) { PDFNumber val = array[i] as PDFNumber; if (i + 2 < array.Count) { PDFNumber val_next = array[i + 2] as PDFNumber; if (val != null && val_next != null && ((int)val.GetValue()) <= number && number < ((int)val_next.GetValue())) { return(array[i + 1]); } } else { if (val != null && ((int)val.GetValue()) <= number) { return(array[i + 1]); } } } return(null); }
internal override float GetTextWidth(PDFString str, float fontSize) { PDFArray widths = GetDictionary()["Widths"] as PDFArray; PDFNumber fc = GetDictionary()["FirstChar"] as PDFNumber; int firstChar = 0; if (fc != null) { firstChar = (int)fc.GetValue(); } byte[] data = str.GetBytes(); float result = 0; for (int i = 0; i < data.Length; ++i) { PDFNumber w = widths[data[i] - firstChar] as PDFNumber; if (w != null) { return((float)(w.GetValue() * fontSize / 1000.0f)); } } return(result); }
internal override float GetCharWidth(char c, float fontSize) { PDFArray widths = GetDictionary()["Widths"] as PDFArray; if (widths != null) { PDFNumber fc = GetDictionary()["FirstChar"] as PDFNumber; int firstChar = 0; if (fc != null) { firstChar = (int)fc.GetValue(); } int glyf = getGlyf(c); PDFNumber w = widths[glyf - firstChar] as PDFNumber; if (w != null) { return((float)(w.GetValue() * fontSize / 1000.0f)); } } else { StandardFonts font; if (isStandardFont(out font)) { IGlyfMetrics metrics = getStandardFontMetrics(font); return(metrics.GetCharWidth(c) * fontSize / 1000.0f); } } return(0); }
internal static PDFDictionary Copy(PDFDictionary dict) { PDFDictionary newDict = new PDFDictionary(); PDFName type = dict["Type"] as PDFName; if (type != null) { newDict.AddItem("Type", type.Clone()); } PDFName s = dict["S"] as PDFName; if (s != null) { newDict.AddItem("S", s.Clone()); } PDFString p = dict["P"] as PDFString; if (p != null) { newDict.AddItem("P", p.Clone()); } PDFNumber st = dict["St"] as PDFNumber; if (st != null) { newDict.AddItem("St", st.Clone()); } return(newDict); }
private void addCount() { PDFNumber count = _dictionary["Count"] as PDFNumber; if (count == null || count.GetValue() >= 0) { _dictionary.AddItem("Count", new PDFNumber(Count)); } }
private void readPermissions(PDFDictionary dictionary) { PDFNumber perms = dictionary["P"] as PDFNumber; if (perms == null) { throw new PDFException(); } _permission = (int)perms.GetValue(); }
private int getDefaultCharWidth() { PDFNumber dw = GetDictionary()["DW"] as PDFNumber; if (dw != null) { return((int)dw.GetValue()); } return(1000); }
internal override float GetCharWidth(char c, float fontSize) { if (c < 32 || c > 255) { return(0); } PDFArray widths = GetDictionary()["Widths"] as PDFArray; PDFNumber w = widths[c - 32] as PDFNumber; return((float)w.GetValue() * fontSize / 1000.0f); }
/// <summary> /// Gets or sets the element at the specified index. /// </summary> /// <param name="index" href="http://msdn.microsoft.com/en-us/library/system.int32.aspx">The zero-based index of the element to get or set.</param> /// <returns cref="PointF" href="http://msdn.microsoft.com/en-us/library/system.drawing.pointf.aspx">The element at the specified index.</returns> public PointF this[int index] { get { if (index < 0 || index >= _array.Count / 2) { throw new IndexOutOfRangeException(); } PDFNumber numberX = _array[index * 2] as PDFNumber; PDFNumber numberY = _array[index * 2 + 1] as PDFNumber; if (numberX == null) { numberX = new PDFNumber(0); } if (numberY == null) { numberY = new PDFNumber(0); } float numX = (float)numberX.GetValue(); float numY = (float)numberY.GetValue(); if (_page != null) { numX = numX - _page.PageRect.Left; numY = _page.PageRect.Bottom - numY; } return(new PointF(numX, numY)); } set { if (index < 0 || index >= _array.Count / 2) { throw new IndexOutOfRangeException(); } if (_page != null) { value.X = value.X + _page.PageRect.Left; value.Y = _page.PageRect.Bottom - value.Y; } _array.RemoveItem(index * 2); _array.RemoveItem(index * 2); _array.Insert(index * 2, new PDFNumber(value.Y)); _array.Insert(index * 2, new PDFNumber(value.X)); if (ChangedPointsArray != null) { ChangedPointsArray(this); } } }
private float getRectValue(byte num) { PDFArray rect = _dictionary["Rect"] as PDFArray; if (rect == null) { return(0); } PDFNumber val = rect[num] as PDFNumber; if (val == null) { return(0); } return((float)val.GetValue()); }
internal override float GetTextWidth(PDFString str, float fontSize) { PDFArray widths = GetDictionary()["Widths"] as PDFArray; float result = 0.0f; byte[] buf = str.GetBytes(); for (int i = 0; i < buf.Length; ++i) { PDFNumber w = widths[buf[i] - 32] as PDFNumber; if (w != null) { result += (float)w.GetValue(); } } result = result * fontSize / 1000.0f; return(result); }
private void readAlgorithmVersion(PDFDictionary dictionary) { PDFNumber version = dictionary["V"] as PDFNumber; if (version != null) { int value = (int)version.GetValue(); if (value >= 0 && value <= 5) { _version = value; } else { throw new PDFUnsupportEncryptorException(); } return; } _revision = 0; }
internal static PDFDictionary Copy(PDFDictionary dict) { PDFDictionary newDict = new PDFDictionary(); PDFName type = dict["Type"] as PDFName; if (type != null) { newDict.AddItem("Type", type.Clone()); } PDFNumber count = dict["Count"] as PDFNumber; if (count != null) { newDict.AddItem("Count", count.Clone()); } PDFString title = dict["Title"] as PDFString; if (title != null) { newDict.AddItem("Title", title.Clone()); } PDFArray c = dict["C"] as PDFArray; if (c != null) { newDict.AddItem("C", c.Clone()); } PDFNumber f = dict["F"] as PDFNumber; if (f != null) { newDict.AddItem("F", f.Clone()); } //First, Last, Parent, Prev, Next, SE - do not //Dest, A - need set after adding return(newDict); }
private void readKeyLength(PDFDictionary dictionary) { PDFNumber lenght = dictionary["Length"] as PDFNumber; if (lenght != null) { int value = (int)lenght.GetValue(); if ((value >= 40 && value <= 128 && value % 8 == 0) || (value == 256 && _revision == 5)) { _length = value; } else { throw new InvalidDocumentException(); } return; } _length = 40; }
private void addItem(PDFArray array, IPDFObject key, IPDFObject value) { if (array == null) { return; } int number = (int)(key as PDFNumber).GetValue(); for (int i = 0; i < array.Count; i += 2) { PDFNumber val = array[i] as PDFNumber; if (i + 2 < array.Count) { PDFNumber val_next = array[i + 2] as PDFNumber; if (val != null && val_next != null && ((int)val.GetValue()) <= number && number < ((int)val_next.GetValue())) { array.Insert(i + 2, key); array.Insert(i + 3, value); return; } } else { if (val != null && ((int)val.GetValue()) <= number) { array.AddItem(key); array.AddItem(value); return; } else { array.Insert(i, key); array.Insert(i + 1, value); return; } } } }
internal override float GetTextWidth(PDFString str, float fontSize) { PDFArray widths = GetDictionary()["Widths"] as PDFArray; byte[] data = str.GetBytes(); float result = 0; if (widths != null) { PDFNumber fc = GetDictionary()["FirstChar"] as PDFNumber; int firstChar = 0; if (fc != null) { firstChar = (int)fc.GetValue(); } for (int i = 0; i < data.Length; ++i) { PDFNumber w = widths[data[i] - firstChar] as PDFNumber; if (w != null) { result += (float)w.GetValue(); } } return(result * fontSize / 1000.0f); } StandardFonts font; if (isStandardFont(out font)) { IGlyfMetrics metrics = getStandardFontMetrics(font); string tmp = ConvertFromFontEncoding(str); for (int i = 0; i < tmp.Length; ++i) { result += metrics.GetCharWidth(tmp[i]); } } return(result * fontSize / 1000.0f); }
private int[] getW(PDFDictionary dict) { PDFArray W = dict["W"] as PDFArray; if (W == null) { throw new InvalidDocumentException(); } int[] w = { 1, 1, 1 }; for (int i = 0; i < W.Count; ++i) { PDFNumber number = W[i] as PDFNumber; if (number != null && number.GetValue() >= 0) { w[i] = (int)number.GetValue(); } } return(w); }
private void deleteItem(PDFArray array, IPDFObject key) { if (array == null) { return; } int number = (int)(key as PDFNumber).GetValue(); for (int i = 0; i < array.Count; i += 2) { PDFNumber val = array[i] as PDFNumber; if (val != null && ((int)val.GetValue()) == number) { array.RemoveItem(i + 1); array.RemoveItem(i); return; } } }
internal static PDFArray CopyArrayCoordinates(PDFArray source, RectangleF oldPageRect, RectangleF newPageRect, bool oldPageIsNull) { PDFArray arr = new PDFArray(); for (int i = 0; i < source.Count; ++i) { PDFNumber num = source[i] as PDFNumber; if (num == null) { arr.AddItem(new PDFNumber(0)); } else { double val = num.GetValue(); if (i % 2 == 0)//x { if (oldPageIsNull) { arr.AddItem(new PDFNumber(val + newPageRect.Left)); } else { arr.AddItem(new PDFNumber(-oldPageRect.Left + val + newPageRect.Left)); } } else//y { if (oldPageIsNull) { arr.AddItem(new PDFNumber(newPageRect.Bottom - val)); } else { arr.AddItem(new PDFNumber(newPageRect.Bottom - (oldPageRect.Bottom - val))); } } } } return(arr); }
private void readRevision(PDFDictionary dictionary) { PDFNumber revision = dictionary["R"] as PDFNumber; if (revision != null) { int value = (int)revision.GetValue(); if (value >= 2 && value <= 5) { _revision = value; } if (_revision > 5) { throw new PDFUnsupportEncryptorException(); } return; } throw new PDFException(); }
private uint getFlag(PDFDictionary dict) { PDFNumber ff = dict["Ff"] as PDFNumber; if (ff == null) { PDFDictionary parent = dict["Parent"] as PDFDictionary; if (parent != null) { ff = parent["Ff"] as PDFNumber; } } uint flag = 0; if (ff != null) { flag = (uint)ff.GetValue(); } return(flag); }
internal override float GetCharWidth(char c, float fontSize) { byte glyf = 0; if (!_charset.TryGetValue(c, out glyf)) { return(0); } PDFNumber firstChar = GetDictionary()["FirstChar"] as PDFNumber; int f = (int)firstChar.GetValue(); PDFArray widths = GetDictionary()["Widths"] as PDFArray; PDFNumber num = widths[glyf - f] as PDFNumber; if (num != null) { return((float)(num.GetValue() * fontSize / 1000.0f)); } return(0); }
private void parsePrevAndXrefStm() { if (_xrefPositions.Count == 0) { PDFNumber Prev = _trailer["Prev"] as PDFNumber; PDFNumber XRefStm = _trailer["XRefStm"] as PDFNumber; if (XRefStm != null) { int val = (int)XRefStm.GetValue(); _trailer.RemoveItem("XRefStm"); _parsedXrefTables.Add(val); parseXref(val); } if (Prev != null) { int val = (int)Prev.GetValue(); _trailer.RemoveItem("Prev"); _parsedXrefTables.Add(val); parseXref(val); } } }
private IPDFObject parseNumberOrLink(int firstByte) { bool succes; if (_usedLastParsedNumber) { _usedLastParsedNumber = false; if (!_lastParsedNumberIsInteger) { return(new PDFNumber(_lastParsedNumber)); } if (isNumberPart(firstByte)) { readLexeme(firstByte); if (IsEOL(_lastParsedByte)) { SkipEOL(); } if (_lastParsedByte == 'R') { int second = CurrentLexemeToInteger(out succes); if (!succes) { return(null); } _lastParsedByte = _stream.ReadByte(); return(new PDFLink(_xref, (int)_lastParsedNumber)); } PDFNumber obj = new PDFNumber(_lastParsedNumber); int number = CurrentLexemeToInteger(out succes); if (!succes) { double n = CurrentLexemeToDouble(out succes); if (!succes) { return(null); } _usedLastParsedNumber = true; _lastParsedNumberIsInteger = false; _lastParsedNumber = n; } else { _usedLastParsedNumber = true; _lastParsedNumberIsInteger = true; _lastParsedNumber = number; } return(obj); } else { return(new PDFNumber(_lastParsedNumber));; } } else { readLexeme(firstByte); if (IsEOL(_lastParsedByte)) { SkipEOL(); } if (!isNumberPart(_lastParsedByte)) { double val = CurrentLexemeToDouble(out succes); if (!succes) { return(null); } return(new PDFNumber(val)); } int first = CurrentLexemeToInteger(out succes); if (!succes) { double val = CurrentLexemeToDouble(out succes); if (!succes) { return(null); } return(new PDFNumber(val)); } readLexeme(_lastParsedByte); if (IsEOL(_lastParsedByte)) { SkipEOL(); } if (_lastParsedByte == 'R') { int second = CurrentLexemeToInteger(out succes); if (!succes) { return(null); } _lastParsedByte = _stream.ReadByte(); return(new PDFLink(_xref, first)); } int number = CurrentLexemeToInteger(out succes); if (!succes) { double n = CurrentLexemeToDouble(out succes); if (!succes) { return(null); } _usedLastParsedNumber = true; _lastParsedNumberIsInteger = false; _lastParsedNumber = n; } else { _usedLastParsedNumber = true; _lastParsedNumberIsInteger = true; _lastParsedNumber = number; } return(new PDFNumber(first)); } }
private void parseProperties(PDFArray arr, IDocumentEssential owner) { PDFNumber left, top; float pageLeft = 0, pageBottom = 0; if (_page != null) { RectangleF rect = _page.PageRect; pageLeft = rect.Left; pageBottom = rect.Bottom; } switch (_zoomMode) { case ZoomMode.FitBoundingVertical: case ZoomMode.FitVertical: left = arr[2] as PDFNumber; if (left != null) { _left = (float)left.GetValue() - pageLeft; } break; case ZoomMode.FitBoundingHorizontal: case ZoomMode.FitHorizontal: top = arr[2] as PDFNumber; if (top != null) { _top = pageBottom - (float)top.GetValue(); } break; case ZoomMode.FitRectangle: //left bottom right top left = arr[2] as PDFNumber; if (left != null) { _left = (float)left.GetValue() - pageLeft; } top = arr[5] as PDFNumber; if (top != null) { _top = pageBottom - (float)top.GetValue(); } PDFNumber bottom = arr[3] as PDFNumber; PDFNumber right = arr[4] as PDFNumber; if (bottom != null) { _height = pageBottom - _top - (float)bottom.GetValue(); } if (right != null) { _width = (float)right.GetValue() - (_left + pageLeft); } break; case ZoomMode.FitXYZ: left = arr[2] as PDFNumber; if (left != null) { _left = (float)left.GetValue() - pageLeft; } top = arr[3] as PDFNumber; if (top != null) { _top = pageBottom - (float)top.GetValue(); } PDFNumber zoom = arr[4] as PDFNumber; if (zoom != null) { _zoom = (int)(zoom.GetValue() * 100); } break; } }
private int getGlyfWidth(ushort glyf) { PDFArray w = getWidthsArray(); int dw = getDefaultCharWidth(); if (w == null) { return(dw); } for (int i = 0; i < w.Count; ++i) { PDFNumber first = w[i] as PDFNumber; if (first != null) { if (first.GetValue() > glyf) { return(dw); } if (first != null) { IPDFObject second = w[i + 1]; if (second is PDFNumber) { if (first.GetValue() <= glyf && glyf <= (second as PDFNumber).GetValue()) { PDFNumber value = w[i + 2] as PDFNumber; if (value != null) { return((int)value.GetValue()); } return(dw); } else { i += 2; } } else if (second is PDFArray) { int count = (second as PDFArray).Count; if (first.GetValue() <= glyf && glyf <= first.GetValue() + count) { int index = glyf - (int)first.GetValue(); PDFNumber width = (second as PDFArray)[index] as PDFNumber; if (width != null) { return((int)width.GetValue()); } return(dw); } else { ++i; } } } } } return(dw); }
private void parseGenerationEntry(Entry entry) { PDFDictionaryStream dictStream = GetObject(entry.Offset) as PDFDictionaryStream; if (dictStream != null) { PDFNumber n = dictStream.Dictionary["N"] as PDFNumber; if (n == null) { return; } int count = (int)n.GetValue(); if (count <= 0) { return; } n = dictStream.Dictionary["First"] as PDFNumber; if (n == null) { return; } int first = (int)n.GetValue(); if (first < 0) { return; } dictStream.Decode(); List <int> objects = new List <int>(); List <int> offsets = new List <int>(); Lexer lexer = new Lexer(dictStream.GetStream(), this, null, 512); lexer.Position = 0; for (int i = 0; i < count; ++i) { bool succes; int objNo = lexer.ReadInteger(out succes); if (!succes) { break; } int offset = lexer.ReadInteger(out succes); if (!succes) { break; } objects.Add(objNo); offsets.Add(offset); } int l = objects.Count; for (int i = 0; i < l; ++i) { lexer.Position = offsets[i] + first; IPDFObject obj = lexer.ReadObject(); if (obj == null) { obj = new PDFNull(); } int index = objects[i]; if (index >= 0 && index < _entries.Count) { if (_entries[index] == null) { _entries[index] = new Entry(0, 0); } if (entry.Offset == _entries[index].Offset) { _entries[index].Object = obj; } } } } }