/// <summary> /// Indica si el PdfTextChunck pasado como argumento /// es el último de la matriz _PdfTextChunks. /// </summary> /// <param name="chunk">True si es el último de la colección.</param> /// <returns>True si es el último de la colección, false si no.</returns> protected bool IsLastChunck(PdfColorFontTextChunk chunk) { return(_PdfColorFontTextChunks.IndexOf(chunk) == (_PdfColorFontTextChunks.Count - 1)); }
/// <summary> /// Implementa la extracción de grupos de palabras /// como texto contenido en rectangulos; /// con el color, tamaño y nombre de la fuente. /// </summary> /// <returns>Matriz con los rectángulos obtenidos.</returns> public List <PdfColorFontTextRectangle> GetColorFontWordGroups() { List <PdfColorFontTextRectangle> CFWordGroups = new List <PdfColorFontTextRectangle>(); _PdfColorFontTextChunks.Sort(); StringBuilder sb = new StringBuilder(); Rectangle rec = null; PdfColorFontTextChunk lastChunk = null; foreach (PdfColorFontTextChunk chunk in _PdfColorFontTextChunks) { if (lastChunk == null) { sb.Append(chunk.Text); rec = new Rectangle(chunk.Ll[Vector.I1], chunk.Ll[Vector.I2], chunk.Ur[Vector.I1], chunk.Ur[Vector.I2]); } else { bool isLastChunk = IsLastChunck(chunk); if ((IsChunkAtWordBoundary(chunk, lastChunk)) || !chunk.SameLine(lastChunk) || isLastChunk) { if (isLastChunk) // Guardo la última palabra { if (!IsChunkAtWordBoundary(chunk, lastChunk)) // Si son la misma palabra, lo junto. { rec = Merge(rec, new Rectangle(chunk.Ll[Vector.I1], chunk.Ll[Vector.I2], chunk.Ur[Vector.I1], chunk.Ur[Vector.I2])); sb.Append(chunk.Text); CFWordGroups.Add(new PdfColorFontTextRectangle(chunk.FillColor, chunk.StrokeColor, chunk.FontName, chunk.FontSize, rec) { Text = sb.ToString().Trim() }); } else { // En caso contrario, lo guardo separado. CFWordGroups.Add(new PdfColorFontTextRectangle(lastChunk.FillColor, lastChunk.StrokeColor, lastChunk.FontName, lastChunk.FontSize, rec) // Añadimos el valor del último chunk visto. { Text = sb.ToString().Trim() }); // reset sb + rec para almacenar el chunk que vemos en siguientes iteraciones como WordGroup rec = new Rectangle(chunk.Ll[Vector.I1], chunk.Ll[Vector.I2], chunk.Ur[Vector.I1], chunk.Ur[Vector.I2]); sb = new StringBuilder(); sb.Append(chunk.Text); CFWordGroups.Add(new PdfColorFontTextRectangle(chunk.FillColor, chunk.StrokeColor, chunk.FontName, chunk.FontSize, rec) { Text = sb.ToString().Trim(), }); } } else { CFWordGroups.Add(new PdfColorFontTextRectangle(lastChunk.FillColor, lastChunk.StrokeColor, lastChunk.FontName, lastChunk.FontSize, rec) // Añadimos el valor del último chunk visto. { Text = sb.ToString().Trim() }); // reset sb + rec para almacenar el chunk que vemos en siguientes iteraciones como WordGroup rec = new Rectangle(chunk.Ll[Vector.I1], chunk.Ll[Vector.I2], chunk.Ur[Vector.I1], chunk.Ur[Vector.I2]); sb = new StringBuilder(); } } else { rec = Merge(rec, new Rectangle(chunk.Ll[Vector.I1], chunk.Ll[Vector.I2], chunk.Ur[Vector.I1], chunk.Ur[Vector.I2])); } if (IsChunkAtWordBoundary(chunk, lastChunk) && !StartsWithSpace(chunk.Text) && !EndsWithSpace(lastChunk.Text)) { sb.Append(' '); } sb.Append(chunk.Text); } lastChunk = chunk; } return(CFWordGroups); }