Пример #1
0
        private static float CalculatesCellHeight(CustomLocationTextExtractionStrategy.LocationTextResult reference, string text, CustomLocationTextExtractionStrategy strategy, PdfContentByte cb, IReadOnlyList <string> allStrings, ReplaceTextOptions options, float deltaY)
        {
            var value = cb.PdfDocument.PageSize.Height - cb.PdfDocument.BottomMargin - cb.PdfDocument.TopMargin - deltaY;

            CustomLocationTextExtractionStrategy.LocationTextResult nextMatchReference = null;

            var index = -1;

            for (var i = 0; i < allStrings.Count; i++)
            {
                string current = allStrings[i].Trim();
                if (current.Equals(text.Trim(), options.Comparison))
                {
                    index = i;
                    break;
                }
            }

            if (index == allStrings.Count)
            {
                return(value - (cb.PdfDocument.PageSize.Height - reference.Rect.Bottom));
            }

            if (index == -1)
            {
                return(value - (cb.PdfDocument.PageSize.Height - reference.Rect.Bottom));
            }

            var nextElementText = allStrings[index + 1];

            if (!string.IsNullOrEmpty(nextElementText.Trim()))
            {
                var nextMatchReferences = strategy.GetExtendedTextLocations(nextElementText, options);
                nextMatchReference = nextMatchReferences.FirstOrDefault();
            }

            if (nextMatchReference == null)
            {
                return(value - (cb.PdfDocument.PageSize.Height - reference.Rect.Bottom));
            }

            // Same line?
            if (nextMatchReference.StartPoint.Y <= reference.StartPoint.Y)
            {
                return(value - (cb.PdfDocument.PageSize.Height - reference.Rect.Bottom));
            }

            return(value - (cb.PdfDocument.PageSize.Height - nextMatchReference.Rect.Bottom));
        }
Пример #2
0
        private static NativeRectangle BuildRectangleByStrategies(CustomLocationTextExtractionStrategy.LocationTextResult reference, string text, CustomLocationTextExtractionStrategy strategy, PdfContentByte cb, IReadOnlyList <string> allStrings, ReplaceTextOptions options)
        {
            var x = reference.Rect.Left;
            var y = reference.Rect.Top;
            var w = reference.Rect.Width;
            var h = reference.Rect.Height;

            switch (options.StartStrategy)
            {
            case StartLocationStrategy.LeftMargin:
                x = cb.PdfDocument.LeftMargin;
                break;

            case StartLocationStrategy.PreviousElement:
                CustomLocationTextExtractionStrategy.LocationTextResult previousMatchReference = null;

                var index = -1;
                for (var i = 0; i < allStrings.Count; i++)
                {
                    string current = allStrings[i].Trim();
                    if (current.Equals(text.Trim(), options.Comparison))
                    {
                        index = i;
                        break;
                    }

                    //if (current.Contains(text.Trim()))
                    //{
                    //    index = i;
                    //    break;
                    //}
                }

                if (index == 0)
                {
                    break;
                }

                var previousElementText = allStrings[index - 1];
                if (!string.IsNullOrEmpty(previousElementText))
                {
                    var nextMatchReferences = strategy.GetExtendedTextLocations(previousElementText, options);
                    previousMatchReference = nextMatchReferences.FirstOrDefault();
                }

                if (previousMatchReference == null)
                {
                    break;
                }

                // Same line?
                if (previousMatchReference.EndPoint.X >= reference.StartPoint.X)
                {
                    break;
                }

                w = (reference.EndPoint.X + reference.TextChunk.CharSpaceWidth) - previousMatchReference.EndPoint.X;
                break;
            }

            switch (options.EndStrategy)
            {
            case EndLocationStrategy.RightMargin:
                w = cb.PdfDocument.PageSize.Width - (x + cb.PdfDocument.RightMargin);
                break;

            case EndLocationStrategy.NextElement:
                CustomLocationTextExtractionStrategy.LocationTextResult nextMatchReference = null;

                var    index           = -1;
                string nextElementText = string.Empty;
                for (var i = 0; i < allStrings.Count; i++)
                {
                    string   currentLine = allStrings[i].Trim();
                    string[] currentElementsInSameLine = currentLine.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    int      totalElementsInLine       = currentElementsInSameLine.Length;
                    foreach (var currentLineElement in currentElementsInSameLine)
                    {
                        if (!currentLineElement.Equals(text.Trim(), options.Comparison))
                        {
                            continue;
                        }

                        index = i;
                        if (totalElementsInLine > 1)
                        {
                            nextElementText = currentElementsInSameLine[1];
                        }

                        break;
                    }

                    if (index != -1)
                    {
                        break;
                    }
                }

                if (index == allStrings.Count)
                {
                    break;
                }

                if (!string.IsNullOrEmpty(nextElementText))
                {
                    var nextMatchReferences = strategy.GetExtendedTextLocations(nextElementText.Trim(), options);
                    nextMatchReference = nextMatchReferences.FirstOrDefault();
                }

                if (nextMatchReference == null)
                {
                    w = cb.PdfDocument.PageSize.Width - (x + cb.PdfDocument.RightMargin);
                    break;
                }

                // Same line?
                if (nextMatchReference.StartPoint.X <= reference.EndPoint.X)
                {
                    w = cb.PdfDocument.PageSize.Width - (x + cb.PdfDocument.RightMargin);
                    break;
                }

                // calculates new width
                if (options.StartStrategy == StartLocationStrategy.LeftMargin)
                {
                    w = nextMatchReference.StartPoint.X - (reference.StartPoint.X + reference.TextChunk.CharSpaceWidth) + (reference.StartPoint.X - cb.PdfDocument.LeftMargin);
                }
                else
                {
                    w = nextMatchReference.StartPoint.X - (reference.StartPoint.X + reference.TextChunk.CharSpaceWidth);
                }
                break;
            }

            switch (options.VerticalStrategy)
            {
            case VerticalFineStrategy.Middle:
                y -= reference.Rect.Height / 2.0f;
                break;

            case VerticalFineStrategy.Bottom:
                y -= reference.Rect.Height;
                break;

            default:
            case VerticalFineStrategy.Top:
                y += reference.TextChunk.CurFontSize / 4;
                break;
            }

            return(new NativeRectangle(x, y, w, h));
        }