internal void Save(string fileName, ImageFormat imageformat) { _dict.Decode(); string nameColorSpace = getNameColorSpace(); switch (nameColorSpace) { case "DeviceRGB": saveRGB(fileName, imageformat); break; case "DeviceCMYK": saveCMYK(fileName, imageformat); break; case "DeviceGRAY": saveGRAY(fileName, imageformat); break; case "Indexed": saveIndexed(fileName, imageformat); break; case "ICCBased": saveICC(fileName, imageformat); break; default: throw new PDFUnsupportImageFormat(); } }
private void readCrossReferencesEntries(PDFDictionaryStream xrefStream) { int[] W = getW(xrefStream.Dictionary); int[] Index = getIndex(xrefStream.Dictionary); xrefStream.Decode(); Stream stream = xrefStream.GetStream(); stream.Position = 0; byte[] bType = new byte[W[0]]; byte[] bOffset = new byte[W[1]]; byte[] bGeneration = new byte[W[2]]; int prevCount, count, type, offset, generation; for (int i = 0; i < Index.Length / 2; ++i) { prevCount = Index[2 * i]; count = Index[2 * i + 1]; addEntries(prevCount + count); for (int j = 0; j < count; ++j) { if (stream.Read(bType, 0, bType.Length) < 0) { return; } type = bytesToInt(bType); if (stream.Read(bOffset, 0, bOffset.Length) < 0) { return; } offset = bytesToInt(bOffset); if (stream.Read(bGeneration, 0, bGeneration.Length) < 0) { return; } if (W[2] == 0) { generation = 0; } else { generation = bytesToInt(bGeneration); } if (_xref.Entries[prevCount + j] == null) { _xref.Entries[prevCount + j] = new Entry((byte)type, offset, generation); } } } }
internal ToUnicode(PDFDictionaryStream toUnicode) { if (toUnicode != null) { toUnicode.Decode(); toUnicode.GetStream().Position = 0; m_chars = new List <BfChar>(); m_ranges = new List <BfRange>(); Lexer lexer = new Lexer(toUnicode.GetStream(), null, null, 256); parse(lexer); } }
private void writeContents(PDFDictionaryStream dictStream, byte[] buf, Stream output) { if (dictStream != null) { dictStream.Decode(); Stream str = dictStream.GetStream(); str.Position = 0; int numread; while ((numread = str.Read(buf, 0, buf.Length)) > 0) { output.Write(buf, 0, numread); } } }
private void saveEmbedded(Stream stream) { try { FileSpecification fs = FileSpecification; PDFDictionaryStream dictStream = (PDFDictionaryStream)fs.EF["F"]; dictStream.Decode(); MemoryStream s = dictStream.GetStream(); s.Position = 0; s.WriteTo(stream); } catch { // ignored } }
public JBIG2Parameters(PDFDictionary dict) { m_stream = null; if (dict == null) { return; } PDFDictionaryStream dictStream = dict["JBIG2Globals"] as PDFDictionaryStream; if (dictStream == null) { return; } dictStream.Decode(); m_stream = dictStream.GetStream(); }
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 void save(Stream stream) { _stream.Decode(); _stream.GetStream().WriteTo(stream); }
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; } } }