Пример #1
0
 private void addOperation(IPDFPageOperation operation, MemoryStream stream)
 {
     operation.WriteBytes(stream);
 }
Пример #2
0
        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;
                }
            }
        }