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); }
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 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 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); }
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); }
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); } }
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 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; } } } }
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 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); }
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; }
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; }
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 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 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); }
public static void fillListStrings(List <CoordinateText> listStrings, float[] beginMatrix, IPDFObject contents, Resources resources) { PageOperationParser parser = new PageOperationParser(contents); parser.SetParserMode(PageOperationParser.ParserMode.TextExtraction); Stack <float[]> stackCM = new Stack <float[]>(); Stack <float[]> stackTM = new Stack <float[]>(); Stack <TextState> stackTextState = new Stack <TextState>(); float[] currentCM = new float[6] { beginMatrix[0], beginMatrix[1], beginMatrix[2], beginMatrix[3], beginMatrix[4], beginMatrix[5] }; float[] currentTM = new float[6] { 1, 0, 0, 1, 0, 0 }; TextState currentTextState = new TextState(); float[] currentAllTransform = new float[6] { 1, 0, 0, 1, 0, 0 }; setAllTransform(currentTM, currentCM, ref currentAllTransform); int currentWeight = 0; bool isShowText = false; IPDFPageOperation operation = null; while ((operation = parser.Next()) != null) { switch (operation.Type) { //CanvasState case PageOperations.Transform: transform(ref currentCM, (Transform)operation); setAllTransform(currentTM, currentCM, ref currentAllTransform); break; case PageOperations.SaveGraphicsState: stackTM.Push(new float[] { currentTM[0], currentTM[1], currentTM[2], currentTM[3], currentTM[4], currentTM[5] }); stackCM.Push(new float[] { currentCM[0], currentCM[1], currentCM[2], currentCM[3], currentCM[4], currentCM[5] }); stackTextState.Push(new TextState(currentTextState)); break; case PageOperations.RestoreGraphicsState: if (stackTM.Count != 0) { currentTM = stackTM.Pop(); currentCM = stackCM.Pop(); currentTextState = stackTextState.Pop(); setAllTransform(currentTM, currentCM, ref currentAllTransform); } break; case PageOperations.DoXObject: PDFDictionaryStream dict = resources.GetResource(((DoXObject)operation).Name, ResourceType.XObject) as PDFDictionaryStream; if (dict != null) { PDFName type = dict.Dictionary["Subtype"] as PDFName; if (type != null) { if (type.GetValue() == "Form") { dict.Decode(); float[] matrix = new float[6] { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }; PDFArray array = dict.Dictionary["Matrix"] as PDFArray; if (array != null) { for (int i = 0; i < array.Count && i < 6; ++i) { PDFNumber number = array[i] as PDFNumber; if (number != null) { matrix[i] = (float)number.GetValue(); } } } mulMatrix(currentCM, ref matrix); fillListStrings(listStrings, matrix, dict, new Resources(dict.Dictionary["Resources"] as PDFDictionary, false)); } } } break; //Text case PageOperations.BeginText: isShowText = false; setDefaultTextMatrix(ref currentTM); setAllTransform(currentTM, currentCM, ref currentAllTransform); break; case PageOperations.EndText: isShowText = false; setDefaultTextMatrix(ref currentTM); setAllTransform(currentTM, currentCM, ref currentAllTransform); break; //TextPosition case PageOperations.TextMatrix: isShowText = false; setTM(ref currentTM, (TextMatrix)operation); setAllTransform(currentTM, currentCM, ref currentAllTransform); break; case PageOperations.MoveTextPos: isShowText = false; moveTextPos(ref currentTM, (MoveTextPos)operation); setAllTransform(currentTM, currentCM, ref currentAllTransform); break; case PageOperations.MoveTextPosToNextLine: isShowText = false; moveTextPos(ref currentTM, currentTextState.Leading); setAllTransform(currentTM, currentCM, ref currentAllTransform); break; case PageOperations.MoveTextPosWithLeading: isShowText = false; currentTextState.Leading = -((MoveTextPosWithLeading)operation).TY; moveTextPos(ref currentTM, (MoveTextPosWithLeading)operation); setAllTransform(currentTM, currentCM, ref currentAllTransform); break; //TextState case PageOperations.TextFont: changeFont(ref currentTextState, resources, (TextFont)operation); break; case PageOperations.TextLeading: currentTextState.Leading = ((TextLeading)operation).Leading; break; case PageOperations.TextRise: currentTextState.Rize = ((TextRise)operation).Rise; break; case PageOperations.WordSpacing: currentTextState.WordSpace = ((WordSpacing)operation).WordSpace; break; case PageOperations.CharacterSpacing: currentTextState.CharSpace = ((CharacterSpacing)operation).CharSpace; break; case PageOperations.HorizontalScaling: currentTextState.Scale = ((HorizontalScaling)operation).Scale; break; //TextRead case PageOperations.ShowText: if (currentTextState.FontBase != null) { addShowText(listStrings, currentAllTransform, ref currentWeight, currentTextState, isShowText, (ShowText)operation); isShowText = true; } break; case PageOperations.ShowTextStrings: if (currentTextState.FontBase != null) { addShowTextStrings(listStrings, currentAllTransform, ref currentWeight, currentTextState, isShowText, (ShowTextStrings)operation); isShowText = true; } break; case PageOperations.ShowTextFromNewLine: if (currentTextState.FontBase != null) { moveTextPos(ref currentTM, currentTextState.Leading); setAllTransform(currentTM, currentCM, ref currentAllTransform); addShowText(listStrings, currentAllTransform, currentWeight, currentTextState, (ShowTextFromNewLine)operation); ++currentWeight; isShowText = true; } break; case PageOperations.ShowTextFromNewLineWithSpacing: if (currentTextState.FontBase != null) { currentTextState.CharSpace = ((ShowTextFromNewLineWithSpacing)operation).CharacterSpacing; currentTextState.WordSpace = ((ShowTextFromNewLineWithSpacing)operation).WordSpacing; moveTextPos(ref currentTM, currentTextState.Leading); setAllTransform(currentTM, currentCM, ref currentAllTransform); addShowText(listStrings, currentAllTransform, currentWeight, currentTextState, (ShowTextFromNewLineWithSpacing)operation); ++currentWeight; isShowText = true; } break; } } }
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; } } } } }
private IPDFObject parseStream(PDFDictionary dict, int objNo, int genNo) { if (_stream.ReadByte() != 't') { return(dict); } if (_stream.ReadByte() != 'r') { return(dict); } if (_stream.ReadByte() != 'e') { return(dict); } if (_stream.ReadByte() != 'a') { return(dict); } if (_stream.ReadByte() != 'm') { return(dict); } int b = _stream.ReadByte(); long startPosition; for (; ;) { if (b == -1) { return(null); } if (b == '\n') { startPosition = _stream.Position; break; } else if (b == '\r') { b = _stream.ReadByte(); if (b == '\n') { startPosition = _stream.Position; break; } startPosition = _stream.Position - 1; break; } b = _stream.ReadByte(); } PDFNumber lengthObj = dict["Length"] as PDFNumber; bool wrongLength = false; if (lengthObj == null) { wrongLength = true; } int length = 0; if (lengthObj != null) { length = (int)lengthObj.GetValue(); if (length < 0) { wrongLength = true; } if (startPosition + length >= _length) { wrongLength = true; } } if (!wrongLength) { _stream.Position = startPosition + length; ReadLexeme(); if (!CurrentLexemeEquals("endstream")) { wrongLength = true; } } if (wrongLength) { long endPos = findEndStream(startPosition, _length); if (endPos < 0) { return(null); } length = (int)(endPos - startPosition); } MemoryStream ms = new MemoryStream(length); if (_encryptor == null) { ms.SetLength(length); byte[] buf = ms.GetBuffer(); _stream.Position = startPosition; _stream.Read(buf, 0, length); } else { _encryptor.ResetObjectReference(objNo, genNo, DataType.String); _stream.Position = startPosition; _encryptor.Decrypt(_stream, length, ms, DataType.Stream); } return(new PDFDictionaryStream(dict, ms)); }