Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
413ebe3
merge pull request #46 from vadik299/master
EliotJones Jul 29, 2019
2b43867
check if 'fontProgram' is null in Type2CidFont.GetWidthFromFont()
Jul 30, 2019
5ee9c49
merge pull request #49 from BobLd/master
EliotJones Jul 30, 2019
364bd25
#48 add handling of inline image data to pdf content parsing
EliotJones Aug 3, 2019
f07ab7d
version 0.0.7
EliotJones Aug 3, 2019
83889cf
Document Layout Analysis - Text edges extractor
Aug 6, 2019
9694b1f
Update TextEdgesExtractor.cs
BobLd Aug 6, 2019
d6c4d62
Update README.md
BenyErnest Aug 6, 2019
7092949
Merge pull request #51 from BenyErnest/patch-1
EliotJones Aug 6, 2019
85d5bb7
Adding enum EdgeType
Aug 7, 2019
e19b030
Updating woth comments
Aug 7, 2019
7de6de3
Updating with comments
Aug 7, 2019
801ea3b
Modified PublicApiScannerTests
Aug 7, 2019
fe270aa
merge pull request #50 from BobLd/master
EliotJones Aug 8, 2019
31e15ea
remove unused docstrum class
EliotJones Aug 8, 2019
f70b7c6
Change StreamInputBytes.Seek to reset isAtEnd to false
Numpsy Aug 8, 2019
54cd0ae
Extend the ArrayAndStreamBehaveTheSame test to test seeking back to t…
Numpsy Aug 8, 2019
d6757e6
Merge pull request #52 from Numpsy/rw/streaminputbytes
EliotJones Aug 9, 2019
ac065e9
Adding Centroid to PdfRectangle.
Aug 9, 2019
5399456
Making the RecursiveXYCut class static.
Aug 9, 2019
f243117
Merge pull request #53 from BobLd/master
EliotJones Aug 9, 2019
474ce9a
Improving PdfPoint
Aug 9, 2019
bd58879
Update from comments
Aug 10, 2019
9b24223
Removing ToDouble()
Aug 10, 2019
2d6e494
Merge pull request #54 from BobLd/master
EliotJones Aug 10, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ New in v0.0.5 - To create documents use the class ```PdfDocumentBuilder```. Thou

byte[] documentBytes = builder.Build();

File.WriteAllBytes(@"C:\git\newPdf.pdf");
File.WriteAllBytes(@"C:\git\newPdf.pdf", documentBytes);

Each font must be registered with the PdfDocumentBuilder prior to use enable pages to share the font resources. Currently only Standard 14 fonts and TrueType fonts (.ttf) are supported.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Reflection;
using PdfPig.Graphics.Operations;
using PdfPig.Graphics.Operations.InlineImages;
using PdfPig.Tokens;
using Xunit;

Expand Down Expand Up @@ -41,6 +42,10 @@ public void AllOperationsCanBeWritten()

operation = (IGraphicsStateOperation)field.GetValue(null);
}
else if (operationType == typeof(EndInlineImage))
{
operation = new EndInlineImage(new List<IToken>(), new List<byte>());
}
else
{
var constructor = constructors[0];
Expand Down
6 changes: 6 additions & 0 deletions src/UglyToad.PdfPig.Tests/IO/InputBytesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public void ArrayAndStreamBehaveTheSame()

Assert.True(stream.IsAtEnd());
Assert.True(array.IsAtEnd());

stream.Seek(0);
array.Seek(0);

Assert.False(stream.IsAtEnd());
Assert.False(array.IsAtEnd());
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/UglyToad.PdfPig.Tests/PublicApiScannerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public void OnlyExposedApiIsPublic()
"UglyToad.PdfPig.DocumentLayoutAnalysis.RecursiveXYCut",
"UglyToad.PdfPig.DocumentLayoutAnalysis.XYNode",
"UglyToad.PdfPig.DocumentLayoutAnalysis.XYLeaf",
"UglyToad.PdfPig.DocumentLayoutAnalysis.TextEdgesExtractor",
"UglyToad.PdfPig.DocumentLayoutAnalysis.EdgeType",
"UglyToad.PdfPig.Exceptions.PdfDocumentEncryptedException",
"UglyToad.PdfPig.Exceptions.PdfDocumentFormatException",
"UglyToad.PdfPig.Fonts.DescriptorFontFile",
Expand Down Expand Up @@ -174,6 +176,7 @@ public void OnlyExposedApiIsPublic()
"UglyToad.PdfPig.Tokens.HexToken",
"UglyToad.PdfPig.Tokens.IDataToken`1",
"UglyToad.PdfPig.Tokens.IndirectReferenceToken",
"UglyToad.PdfPig.Tokens.InlineImageDataToken",
"UglyToad.PdfPig.Tokens.IToken",
"UglyToad.PdfPig.Tokens.NameToken",
"UglyToad.PdfPig.Tokens.NullToken",
Expand Down
18 changes: 9 additions & 9 deletions src/UglyToad.PdfPig/Content/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Util;
using Util.JetBrains.Annotations;
using XObjects;
using UglyToad.PdfPig.Geometry;
using Geometry;

/// <summary>
/// Contains the content and provides access to methods of a single page in the <see cref="PdfDocument"/>.
Expand All @@ -30,23 +30,18 @@ public class Page

internal CropBox CropBox { get; }

internal PageContent Content { get; }

/// <summary>
/// The rotation of the page in degrees (clockwise). Valid values are 0, 90, 180 and 270.
/// </summary>
public PageRotationDegrees Rotation { get; }

internal PageContent Content { get; }

/// <summary>
/// The set of <see cref="Letter"/>s drawn by the PDF content.
/// </summary>
public IReadOnlyList<Letter> Letters => Content?.Letters ?? new Letter[0];

/// <summary>
/// The set of <see cref="PdfPath"/>s drawn by the PDF content.
/// </summary>
public IReadOnlyList<PdfPath> Paths => Content?.Paths ?? new List<PdfPath>();


/// <summary>
/// The full text of all characters on the page in the order they are presented in the PDF content.
/// </summary>
Expand Down Expand Up @@ -136,6 +131,11 @@ public class Experimental
private readonly Page page;
private readonly AnnotationProvider annotationProvider;

/// <summary>
/// The set of <see cref="PdfPath"/>s drawn by the PDF content.
/// </summary>
public IReadOnlyList<PdfPath> Paths => page.Content?.Paths ?? new List<PdfPath>();

internal Experimental(Page page, AnnotationProvider annotationProvider)
{
this.page = page;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace UglyToad.PdfPig.DocumentLayoutAnalysis
/// https://en.wikipedia.org/wiki/Recursive_X-Y_cut
/// <para>See 'Recursive X-Y Cut using Bounding Boxes of Connected Components' by Jaekyu Ha, Robert M.Haralick and Ihsin T. Phillips</para>
/// </summary>
public class RecursiveXYCut
public static class RecursiveXYCut
{
/// <summary>
/// Get the blocks.
Expand Down
109 changes: 109 additions & 0 deletions src/UglyToad.PdfPig/DocumentLayoutAnalysis/TextEdgesExtractor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UglyToad.PdfPig.Content;
using UglyToad.PdfPig.Geometry;

namespace UglyToad.PdfPig.DocumentLayoutAnalysis
{
/// <summary>
/// Text edges extractor. Text edges are where words have either their BoundingBox's left, right or mid coordinates aligned on the same vertical line.
/// <para>Useful to detect text columns, tables, justified text, lists, etc.</para>
/// </summary>
public static class TextEdgesExtractor
{
/// <summary>
/// Functions used to define left, middle and right edges.
/// </summary>
private static readonly Tuple<EdgeType, Func<PdfRectangle, decimal>>[] edgesFuncs = new Tuple<EdgeType, Func<PdfRectangle, decimal>>[]
{
Tuple.Create<EdgeType, Func<PdfRectangle, decimal>>(EdgeType.Left, x => Math.Round(x.Left, 0)), // use BoundingBox's left coordinate
Tuple.Create<EdgeType, Func<PdfRectangle, decimal>>(EdgeType.Mid, x => Math.Round(x.Left + x.Width / 2, 0)), // use BoundingBox's mid coordinate
Tuple.Create<EdgeType, Func<PdfRectangle, decimal>>(EdgeType.Right, x => Math.Round(x.Right, 0)) // use BoundingBox's right coordinate
};

/// <summary>
/// Get the text edges.
/// </summary>
/// <param name="pageWords">The words in the page.</param>
/// <param name="minimumElements">The minimum number of elements to define a text edge.</param>
public static IReadOnlyDictionary<EdgeType, List<PdfLine>> GetEdges(IEnumerable<Word> pageWords, int minimumElements = 4)
{
if (minimumElements < 0)
{
throw new ArgumentException("TextEdgesExtractor.GetEdges(): The minimum number of elements should be positive.", "minimumElements");
}

var cleanWords = pageWords.Where(x => !string.IsNullOrWhiteSpace(x.Text.Trim()));

ConcurrentDictionary<EdgeType, List<PdfLine>> dictionary = new ConcurrentDictionary<EdgeType, List<PdfLine>>();

Parallel.ForEach(edgesFuncs, f =>
{
dictionary.TryAdd(f.Item1, GetVerticalEdges(cleanWords, f.Item2, minimumElements));
});
return dictionary.ToDictionary(x => x.Key, x => x.Value);
}

private static List<PdfLine> GetVerticalEdges(IEnumerable<Word> pageWords, Func<PdfRectangle, decimal> func, int minimumElements)
{
Dictionary<decimal, List<Word>> edges = pageWords.GroupBy(x => func(x.BoundingBox))
.Where(x => x.Count() >= minimumElements).ToDictionary(gdc => gdc.Key, gdc => gdc.ToList());
Dictionary<decimal, List<List<Word>>> cleanEdges = new Dictionary<decimal, List<List<Word>>>();

foreach (var edge in edges)
{
var sortedEdges = edge.Value.OrderBy(x => x.BoundingBox.Bottom).ToList();
cleanEdges.Add(edge.Key, new List<List<Word>>());

var cuttings = pageWords.Except(edge.Value) // remove selected words
// words that cut the vertical line
.Where(x => x.BoundingBox.Left < edge.Key && x.BoundingBox.Right > edge.Key)
// and that are within the boundaries of the edge
.Where(k => k.BoundingBox.Bottom > edge.Value.Min(z => z.BoundingBox.Bottom)
&& k.BoundingBox.Top < edge.Value.Max(z => z.BoundingBox.Top))
.OrderBy(x => x.BoundingBox.Bottom).ToList();

if (cuttings.Count > 0)
{
foreach (var cut in cuttings)
{
var group1 = sortedEdges.Where(x => x.BoundingBox.Top < cut.BoundingBox.Bottom).ToList();
if (group1.Count >= minimumElements) cleanEdges[edge.Key].Add(group1);
sortedEdges = sortedEdges.Except(group1).ToList();
}
if (sortedEdges.Count >= minimumElements) cleanEdges[edge.Key].Add(sortedEdges);
}
else
{
cleanEdges[edge.Key].Add(sortedEdges);
}
}

return cleanEdges.SelectMany(x => x.Value.Select(y => new PdfLine(x.Key, y.Min(w => w.BoundingBox.Bottom), x.Key, y.Max(w => w.BoundingBox.Top)))).ToList();
}
}

/// <summary>
/// The type of text edge.
/// </summary>
public enum EdgeType
{
/// <summary>
/// Text edges where words have their BoundingBox's left coordinate aligned on the same vertical line.
/// </summary>
Left = 0,

/// <summary>
/// Text edges where words have their BoundingBox's mid coordinate aligned on the same vertical line.
/// </summary>
Mid = 1,

/// <summary>
/// Text edges where words have their BoundingBox's right coordinate aligned on the same vertical line.
/// </summary>
Right = 2
}
}
5 changes: 5 additions & 0 deletions src/UglyToad.PdfPig/Fonts/CidFonts/Type2CidFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public Type2CidFont(NameToken type, NameToken subType, NameToken baseFont, Chara

public decimal GetWidthFromFont(int characterIdentifier)
{
if (fontProgram == null)
{
return GetWidthFromDictionary(characterIdentifier);
}

if (fontProgram.TryGetBoundingAdvancedWidth(characterIdentifier, cidToGid.GetGlyphIndex, out var width))
{
return width;
Expand Down
Loading