private bool Match(Link link, PdfTextBlock text) { return(link.bottom <= text.Bottom && link.top >= text.Top && link.left <= text.Left && link.right >= text.Right); }
public void AssignRtl(PdfTextBlock item, bool reversed) { var text = item.Value; if (IsRTL(text)) { item.IsRightToLeft = true; if (!reversed) { ReverseText(item); } } else if (!IsNeutral(text)) { item.IsRightToLeft = false; if (reversed) { ReverseText(item); } } // else if (IsDigit(text)) // { // item.IsDigit = true; // } }
public virtual void ParseText(TextRenderInfo textRenderInfo) { var text = textRenderInfo.GetText(); LineSegment baseline = textRenderInfo.GetBaseline(); if (textRenderInfo.GetRise() != 0) { Matrix m = new Matrix(0.0f, -textRenderInfo.GetRise()); baseline = baseline.TransformBy(m); } var start = baseline.GetStartPoint(); LineSegment ascentLine = textRenderInfo.GetAscentLine(); PdfTextBlock item = new PdfTextBlock { Value = text, Bottom = pageContext.PageHeight - start.Get(Vector.I2), Top = pageContext.PageHeight - ascentLine.GetStartPoint().Get(Vector.I2), Left = start.Get(Vector.I1), Width = baseline.GetEndPoint().Get(Vector.I1) - start.Get(Vector.I1), FontSize = FontManager.Instance.GetFontSize(textRenderInfo, baseline, ascentLine), StrokeColore = ColorManager.Instance.GetColor(textRenderInfo), CharSpacing = textRenderInfo.GetSingleSpaceWidth(), Font = GetFont(textRenderInfo), }; RightToLeftManager.Instance.AssignRtl(item, textRenderInfo.GetUnscaledWidth() < 0); pageContext.LinkManager.AssignLink(item); texts.Add(item); }
void InitProperties() { blocks = new List <PdfTextBlock>(); left = int.MaxValue; right = 0; top = group.Key; last = null; }
public void AssignLink(PdfTextBlock text) { var link = links.FirstOrDefault(l => Match(l, text)); if (link != null) { text.Link = link.uri; } }
private IList <PdfTextResult> MergeTextInLine() { var result = new LinkedList <PdfTextResult> { }; LinkedListNode <PdfTextResult> firstNode = null; PdfTextBlock lastBlock = null; PdfTextResult text = null; PdfLinkResult link = null; for (var index = 0; index < blocks.Count; index++) { var current = blocks[index]; bool currentRTL = RightToLeftManager.Instance.AssignNeutral(pageContext.PageRTL, current, blocks, index); bool opositeDirection = pageContext.PageRTL != currentRTL; // bool digitLtr = current.IsDigit && last?.IsDigit==true; if (lastBlock != null && Equals(lastBlock.StrokeColore, current.StrokeColore) && lastBlock.FontSize == current.FontSize && lastBlock.Font == current.Font && lastBlock.Link == current.Link && lastBlock.IsRightToLeft == current.IsRightToLeft) { if (opositeDirection) //&&!digitLtr) { text.Value = current.Value + text.Value; } else { text.Value += current.Value; } } else { // var stateRtl = // RightToLeftManager.Instance.PageElemtRtl(pageRTL, currentRightToLeft); // && !digitLtr); SeperateRtlLtr(opositeDirection, pageContext.PageRTL, current, lastBlock, text); text = new PdfTextResult { FontSize = current.FontSize, Font = current.Font, StrokeColore = current.StrokeColore, Value = current.Value, }; pageContext.LinkManager.AssignLink(current, text, ref link); AddNewText(opositeDirection, result, text, ref firstNode); } lastBlock = current; } return(result.ToArray()); }
public bool AssignNeutral(bool pageRightToLeft, PdfTextBlock current, IList <PdfTextBlock> blocks, int index) { if (current.IsRightToLeft != null) { return(current.IsRightToLeft.Value); } var currentRightToLeft = FindRtlOfNeutral(pageRightToLeft, blocks, index); current.IsRightToLeft = currentRightToLeft; if (currentRightToLeft) // && !current.IsDigit) { var chars = new char[current.Value.Length]; for (var i = 0; i < current.Value.Length; i++) { var c = current.Value[i]; switch (c) { case '[': c = ']'; break; case ']': c = '['; break; case '(': c = ')'; break; case ')': c = '('; break; case '{': c = '}'; break; case '}': c = '{'; break; } chars[chars.Length - 1 - i] = c; } current.Value = new string(chars); } return(currentRightToLeft); }
private void CalculateLines() { lines = new List <PdfTextLineDetails>(); InitProperties(); foreach (var current in group) { if (string.IsNullOrWhiteSpace(current.Value) && (last == null || last.End >= current.End || string.IsNullOrWhiteSpace(last.Value))) { continue; } if (last != null) { if (last.End + last.CharSpacing < current.Start) { if (last.End + last.CharSpacing * 2 < current.Start) { AddLine(); InitProperties(); } else { AddSpace(current); } } } blocks.Add(current); if (left > current.Left) { left = current.Left; } if (right < current.Right) { right = current.Right; } if (top > current.Top) { top = current.Top; } last = current; } AddLine(); }
public void AssignLink(PdfTextBlock current, PdfTextResult text, ref PdfLinkResult link) { if (current.Link != null) { if (!string.Equals(link?.Link, current.Link)) { if (link != null && Log.DebugSupported) { Log.Debug("link:" + link); } link = new PdfLinkResult { Link = current.Link }; } link.Children.Add(text); text.LinkParent = link; } }
private void AddSpace(PdfTextBlock current) { if (last.Value == " ") { last.Width = current.Start - last.Start; if (pageContext.PageRTL) { last.Left = current.Right; } } else if (current.Value == " ") { current.Width = current.End - last.End; if (!pageContext.PageRTL) { current.Left = last.Right; } } else { Log.Debug($"adding space between '{last.Value}'-'{current.Value}'"); blocks.Add(new PdfTextBlock { Bottom = current.Bottom, CharSpacing = current.CharSpacing, Font = current.Font, FontSize = current.FontSize, StrokeColore = current.StrokeColore, IsRightToLeft = current.IsRightToLeft, Left = pageContext.PageRTL ? current.Right : last.Right, Width = current.Start - last.End, Top = current.Top, Link = current.Link, Value = " " }); } }
private void SeperateRtlLtr(bool opositeDirection, bool pageRtl, PdfTextBlock current, PdfTextBlock lastBlock, PdfTextResult lastText) { if (opositeDirection) { if (lastBlock?.IsRightToLeft == pageRtl && //!lastBlock.IsDigit && !RightToLeftManager.Instance.IsNeutral(lastBlock.Value[lastBlock.Value.Length - 1])) { lastText.Value = lastText.Value + " "; Log.Debug("seperated:" + lastBlock.Value + "-" + current.Value); } } else { if (lastBlock?.IsRightToLeft == !pageRtl && // !lastBlock.IsDigit && !RightToLeftManager.Instance.IsNeutral(current.Value[0])) { current.Value = " " + current.Value; Log.Debug("seperated:" + lastText.Value + "-" + current.Value); } } }
private void ReverseText(PdfTextBlock item) { item.Value = new string(item.Value.Reverse().ToArray()); }