private Filter[] getFilters() { IPDFObject obj = _dictionary["Filter"]; if (obj == null) { return(new Filter[0]); } List <Filter> filters = new List <Filter>(); if (obj is PDFArray) { PDFArray arr = obj as PDFArray; for (int i = 0; i < arr.Count; ++i) { PDFName filter = arr[i] as PDFName; if (filter != null) { filters.Add(convertPDFNameToFilter(filter.GetValue())); } } } else if (obj is PDFName) { filters.Add(convertPDFNameToFilter((obj as PDFName).GetValue())); } return(filters.ToArray()); }
private void initImages() { _images = new ReadOnlyCollection <Image>(); PDFDictionary dictionary = _dictionary["XObject"] as PDFDictionary; if (dictionary != null) { string[] keys = dictionary.GetKeys(); for (int i = 0; i < keys.Length; ++i) { PDFDictionaryStream dict = dictionary[keys[i]] as PDFDictionaryStream; if (dict != null) { PDFName name = dict.Dictionary["Subtype"] as PDFName; if (name != null) { if (name.GetValue() == "Image") { _images.AddItem(new Image(dict)); } } } } } }
public string AddResources(ResourceType type, IPDFObject obj) { string key = resourceTypeToString(type); addDictionaryType(key); PDFDictionary res = _dictionary[key] as PDFDictionary; string name; if (res.Contains(obj, out name)) { return(name); } name = getVacantResourseName(res, type); res.AddItem(name, obj); if (type == ResourceType.XObject) { PDFDictionaryStream stream = obj as PDFDictionaryStream; if (stream != null) { PDFName imagename = stream.Dictionary["Subtype"] as PDFName; if (imagename != null) { if (imagename.GetValue() == "Image") { _images.AddItem(new Image(stream)); } } } } return(name); }
private void initBaseEncoding(PDFName baseEncoding) { if (baseEncoding == null || baseEncoding.GetValue() == "StandardEncoding") { for (int i = 0; i < 256; ++i) { _charset.Add(Encoding.GetChar((byte)i)); } return; } else if (baseEncoding.GetValue() == "WinAnsiEncoding") { for (int i = 0; i < 256; ++i) { _charset.Add(WinAnsiEncoding.GetChar((byte)i)); } return; } else if (baseEncoding.GetValue() == "MacRomanEncoding") { for (int i = 0; i < 256; ++i) { _charset.Add(MacRomanEncoding.GetChar((byte)i)); } return; } for (int i = 0; i < 256; ++i) { _charset.Add(Encoding.GetChar((byte)i)); } }
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 readPages(PDFArray kids, PDFArray newKids, IPDFObject resources, IPDFObject mediaBox, IPDFObject cropBox, IPDFObject rotate) { if (kids != null) { for (int i = 0; i < kids.Count; ++i) { PDFDictionary dict = kids[i] as PDFDictionary; if (dict != null) { PDFName type = dict["Type"] as PDFName; if (type != null) { if (type.GetValue() == "Pages") { IPDFObject tmp = dict["Resources"]; if (tmp != null) { resources = tmp; } tmp = dict["MediaBox"]; if (tmp != null) { mediaBox = tmp; } tmp = dict["CropBox"]; if (tmp != null) { cropBox = tmp; } tmp = dict["Rotate"]; if (tmp != null) { rotate = tmp; } readPages(dict["Kids"] as PDFArray, newKids, resources, mediaBox, cropBox, rotate); } else if (type.GetValue() == "Page") { newKids.AddItem(dict); dict.AddItem("Parent", GetDictionary()); dict.AddItemIfNotHave("Resources", resources); dict.AddItemIfNotHave("MediaBox", mediaBox); dict.AddItemIfNotHave("CropBox", cropBox); dict.AddItemIfNotHave("Rotate", rotate); Page page = new Page(dict, _owner); _pages.Add(page); } } } } } }
private void readFilter(PDFDictionary dictionary) { PDFName filter = dictionary["Filter"] as PDFName; if (filter == null) { throw new PDFException(); } if (filter.GetValue() != "Standard") { throw new PDFUnsupportEncryptorException(); } }
public void Write(SaveParameters param) { Stream stream = param.Stream; stream.WriteByte((byte)'<'); stream.WriteByte((byte)'<'); int count = _items.Count; for (int i = 0; i < count; ++i) { KeyValuePair <string, IPDFObject> pair = _items[i]; PDFName.Write(stream, pair.Key); stream.WriteByte((byte)' '); IPDFObject val = pair.Value; if (val is PDFDictionary || val is PDFDictionaryStream) { if (!param.WriteInheritableObjects) { StringUtility.WriteToStream(val.ObjNo, stream); stream.WriteByte((byte)' '); stream.WriteByte((byte)'0'); stream.WriteByte((byte)' '); stream.WriteByte((byte)'R'); } else { val.Write(param); } } else { val.Write(param); } if (i != count - 1) { stream.WriteByte((byte)'\n'); } } stream.WriteByte((byte)'>'); stream.WriteByte((byte)'>'); }
private string getNameColorSpace() { PDFName name = _dict.Dictionary["ColorSpace"] as PDFName; if (name != null) { return(name.GetValue()); } else { throw new PDFUnsupportImageFormat(); /*PDFArray array = _dict.Dictionary["ColorSpace"] as PDFArray; * name = array[0] as PDFName; * if (name != null) * return name.GetValue();*/ } }
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); }
internal PDFDictionaryStream ParseCrossRefObject() { bool succes; ReadInteger(out succes); if (!succes) { return(null); } ReadLexeme(); if (!CurrentLexemeEquals("obj")) { return(null); } PDFDictionary obj = readObject(_lastParsedByte, 0, 0) as PDFDictionary; if (obj == null) { return(null); } PDFName type = obj["Type"] as PDFName; if (type == null || type.GetValue() != "XRef") { return(null); } if (IsEOL(_lastParsedByte)) { SkipEOL(); } if (_lastParsedByte != 's') { return(null); } return(parseStream(obj, 0, 0) as PDFDictionaryStream); }
private void readCFM(PDFDictionary CF, string key, out CryptFilter filter) { filter = CryptFilter.Identity; if (CF == null) { return; } PDFDictionary StdCF = CF[key] as PDFDictionary; if (StdCF != null) { PDFName cfm = StdCF["CFM"] as PDFName; if (cfm == null) { return; } switch (cfm.GetValue()) { case "V2": filter = CryptFilter.V2; break; case "AESV2": filter = CryptFilter.AESV2; break; case "AESV3": filter = CryptFilter.AESV3; break; default: filter = CryptFilter.Identity; break; } } }
private void readCryptFilters(PDFDictionary dictionary) { PDFName stmf = dictionary["StmF"] as PDFName; PDFName strf = dictionary["StrF"] as PDFName; if (stmf == null) { _stmF = CryptFilter.Identity; } else { readCFM(dictionary["CF"] as PDFDictionary, stmf.GetValue(), out _stmF); } if (strf == null) { _strF = CryptFilter.Identity; } else { readCFM(dictionary["CF"] as PDFDictionary, strf.GetValue(), out _strF); } }
//internal ByteRange //internal Contents static internal int Write(SaveParameters param, PDFDictionary dict, out int[] byteRange) { int byteRangeOffset = -1; byteRange = new int[4]; byteRange[0] = 0; Stream stream = param.Stream; stream.WriteByte((byte)'<'); stream.WriteByte((byte)'<'); string[] keys = dict.GetKeys(); int count = keys.Length; for (int i = 0; i < count; ++i) { string key = keys[i]; PDFName.Write(stream, key); stream.WriteByte((byte)' '); IPDFObject val = dict[key]; if (key == "ByteRange") { byteRangeOffset = (int)stream.Position; } else if (key == "Contents") { byteRange[1] = (int)stream.Position; } if (val is PDFDictionary || val is PDFDictionaryStream) { if (!param.WriteInheritableObjects) { StringUtility.WriteToStream(val.ObjNo, stream); stream.WriteByte((byte)' '); stream.WriteByte((byte)'0'); stream.WriteByte((byte)' '); stream.WriteByte((byte)'R'); } else { val.Write(param); } } else { val.Write(param); } if (key == "ByteRange") { for (int j = 0; j < 30; j++) { stream.WriteByte((byte)' '); } } else if (key == "Contents") { byteRange[2] = (int)stream.Position; } if (i != count - 1) { stream.WriteByte((byte)'\n'); } } stream.WriteByte((byte)'>'); stream.WriteByte((byte)'>'); return(byteRangeOffset); }
public void Write(SaveParameters param) { _stream.Position = 0; MemoryStream output = _stream; //set compression bool filter = false; if (param.Compression == Compression.Flate) { PDFName subtype = (PDFName)_dictionary["Subtype"]; Filter[] filters = getFilters(); if (subtype != null && subtype.GetValue() == "Image" && filters.Length > 0 && filters[0] == Filter.DCT) { output = param.Buffer; output.SetLength(0); _stream.WriteTo(output); filter = true; } else if (param.Compression != Compression.None && getFilters().Length == 0) { output = param.Buffer; output.SetLength(0); FlateDecoder.Code(_stream, output); _dictionary.AddItem("Filter", new PDFName("FlateDecode")); filter = true; } } //set encryption if (param.Encryptor != null) { param.Encryptor.ResetObjectReference(param.ObjNo, param.GenNo, DataType.Stream); if (filter) { byte[] buffer = output.GetBuffer(); int length = (int)output.Length; output.SetLength(0); param.Encryptor.Encrypt(buffer, 0, length, output, DataType.Stream); } else { output = param.Buffer; output.SetLength(0); param.Encryptor.Encrypt(_stream.GetBuffer(), 0, (int)_stream.Length, output, DataType.Stream); } } output.Position = 0; _dictionary.AddItem("Length", new PDFNumber(output.Length)); _dictionary.Write(param); param.Stream.Write(StartStream, 0, StartStream.Length); param.Stream.Write(output.GetBuffer(), 0, (int)output.Length); param.Stream.Write(EndStream, 0, EndStream.Length); if (filter) { _dictionary.RemoveItem("Filter"); } }
private void parseAnnotations() { for (int i = 0; i < _array.Count; ++i) { PDFDictionary annotDict = _array[i] as PDFDictionary; if (annotDict != null) { if (annotDict["IRT"] == null) { PDFName subtype = annotDict["Subtype"] as PDFName; if (subtype != null) { Annotation annot = null; switch (subtype.GetValue()) { case "Text": annot = new TextAnnotation(annotDict, _owner); break; case "Link": annot = new LinkAnnotation(annotDict, _owner); break; case "FreeText": annot = new FreeTextAnnotation(annotDict, _owner); break; case "Line": annot = new LineAnnotation(annotDict, _owner); break; case "Square": annot = new SquareAnnotation(annotDict, _owner); break; case "Circle": annot = new CircleAnnotation(annotDict, _owner); break; case "Polygon": annot = new PolygonAnnotation(annotDict, _owner); break; case "PolyLine": annot = new PolylineAnnotation(annotDict, _owner); break; case "Highlight": annot = new HighlightAnnotation(annotDict, _owner); break; case "Underline": annot = new UnderlineAnnotation(annotDict, _owner); break; case "Squiggly": annot = new SquigglyAnnotation(annotDict, _owner); break; case "StrikeOut": annot = new StrikeOutAnnotation(annotDict, _owner); break; case "Stamp": annot = new RubberStampAnnotation(annotDict, _owner); break; case "Caret": annot = new CaretAnnotation(annotDict, _owner); break; case "Ink": annot = new InkAnnotation(annotDict, _owner); break; case "FileAttachment": annot = new FileAttachmentAnnotation(annotDict, _owner); break; case "Sound": annot = new SoundAnnotation(annotDict, _owner); break; case "Movie": annot = new MovieAnnotation(annotDict, _owner); break; case "Screen": annot = new ScreenAnnotation(annotDict, _owner); break; case "3D": annot = new ThreeDAnnotation(annotDict, _owner); break; case "Widget": PDFName ft = annotDict["FT"] as PDFName; if (ft == null) { PDFDictionary parent = annotDict["Parent"] as PDFDictionary; if (parent != null) { ft = parent["FT"] as PDFName; } } if (ft != null) { switch (ft.GetValue()) { case "Tx": annot = new EditBox(annotDict, _owner); break; case "Ch": uint flag = getFlag(annotDict); if ((flag >> 17) % 2 != 0) { annot = new ComboBox(annotDict, _owner); } else { annot = new ListBox(annotDict, _owner); } break; case "Btn": flag = getFlag(annotDict); if ((flag >> 16) % 2 != 0) { annot = new PushButton(annotDict, _owner); } else if ((flag >> 15) % 2 != 0) { annot = new RadioButton(annotDict, _owner); } else { annot = new CheckBox(annotDict, _owner); } break; } } break; } if (annot != null) { annot.SetPage(_page, false); _annotations.Add(annot); } } } } } }
private long initSettings(FileStream fs) { long positionTRNS = 0; byte [] buf = new byte [4]; int size = 0; string chunkname = ""; while (chunkname != "IDAT" && chunkname != "IEND") { fs.Read(buf, 0, buf.Length); size = toInt32(buf); fs.Read(buf, 0, buf.Length); chunkname = Encoding.GetString(buf); byte[] data = null; switch (chunkname) { case "PLTE": data = new byte[size]; fs.Read(data, 0, size); IPDFObject colorspace = null; if (Dictionary["ColorSpace"] != null) { colorspace = Dictionary["ColorSpace"]; } else { colorspace = new PDFName("DeviceRGB"); } PDFArray array = new PDFArray(); array.AddItem(new PDFName("Indexed")); array.AddItem(colorspace); array.AddItem(new PDFNumber(size / 3 - 1)); array.AddItem(new PDFString(data, true)); Dictionary.AddItem("ColorSpace", array); fs.Position += 4; break; case "gAMA": data = new byte[size]; fs.Read(data, 0, size); PDFArray arrayCal = new PDFArray(); if (_colortype == 0 || _colortype == 4) { initCalGray(arrayCal, toInt32(data)); } else { initCalRGB(arrayCal, toInt32(data)); } Dictionary.AddItem("ColorSpace", arrayCal); fs.Position += 4; break; case "tRNS": positionTRNS = fs.Position - 8; fs.Position += 4 + size; break; case "IDAT": case "IEND": fs.Position -= 8; break; default: fs.Position += 4 + size; break; } } return(positionTRNS); }
internal static Action Create(PDFDictionary dict, IDocumentEssential owner) { if (dict == null) { return(null); } PDFName type = dict["S"] as PDFName; switch (type.GetValue()) { case "GoTo": return(new GoToAction(dict, owner)); case "GoToR": return(new GoToRemoteAction(dict, owner)); case "GoToE": return(new GoToEmbeddedAction(dict, owner)); case "Launch": return(new LaunchAction(dict, owner)); case "Thread": return(new ThreadAction(dict, owner)); case "URI": return(new URIAction(dict, owner)); case "Sound": return(new SoundAction(dict, owner)); case "Movie": return(new MovieAction(dict, owner)); case "Hide": return(new HideAction(dict, owner)); case "Named": return(new NamedAction(dict, owner)); case "SubmitForm": return(new SubmitFormAction(dict, owner)); case "ResetForm": return(new ResetFormAction(dict, owner)); case "ImportData": return(new ImportDataAction(dict, owner)); case "JavaScript": return(new JavaScriptAction(dict, owner)); case "SetOCGState": return(new SetOptionalContentGroupsStateAction(dict, owner)); case "Rendition": return(new RenditionAction(dict, owner)); case "Trans": return(new TransitionAction(dict, owner)); case "GoTo3DView": return(new GoTo3DViewAction(dict, owner)); } return(null); }
private void parseZoomMode(PDFArray arr) { PDFName mode = arr[1] as PDFName; _zoomMode = TypeConverter.PDFNameToPDFZoomMode(mode); }
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; } } }