Fix bounding box for artifact

Add tests
This commit is contained in:
BobLd
2020-02-09 17:46:35 +00:00
committed by Eliot Jones
parent 588648d30b
commit ac1e2c49ba
3 changed files with 130 additions and 6 deletions

View File

@@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.Text;
using UglyToad.PdfPig.Content;
using Xunit;
namespace UglyToad.PdfPig.Tests.Integration
{
public class MarkedContentExtractionTests
{
private const string FileName1 = "Multiple Page - from Mortality Statistics.pdf";
private const string FileName2 = "68-1990-01_A.pdf";
[Fact]
public void CanIncrementIndex()
{
using (var document = PdfDocument.Open(GetPath1()))
{
var page = document.GetPage(2);
var mcs = page.GetMarkedContents();
Assert.NotEmpty(mcs);
Assert.Equal(37, mcs.Count);
for (int i = 0; i < mcs.Count; i++)
{
Assert.Equal(i, mcs[i].Index);
}
}
}
[Fact]
public void CanGetTree()
{
using (var document = PdfDocument.Open(GetPath2()))
{
var page = document.GetPage(10);
var mcs = page.GetMarkedContents();
Assert.NotEmpty(mcs);
Assert.Equal(86, mcs.Count);
int index = 8;
var mc = mcs[index];
Assert.Equal(1, mc.Children.Count);
Assert.Equal(index, mc.Index);
Assert.NotEmpty(mc.Children);
Assert.Equal(index, mc.Children[0].Index);
Assert.DoesNotContain(mc.Children[0], mcs);
index = 9;
mc = mcs[index];
Assert.Equal(1, mc.Children.Count);
Assert.Equal(index, mc.Index);
Assert.NotEmpty(mc.Children);
Assert.Equal(index, mc.Children[0].Index);
Assert.DoesNotContain(mc.Children[0], mcs);
index = 75;
mc = mcs[index];
Assert.Equal(1, mc.Children.Count);
Assert.Equal(index, mc.Index);
Assert.NotEmpty(mc.Children);
Assert.Equal(index, mc.Children[0].Index);
Assert.DoesNotContain(mc.Children[0], mcs);
}
}
[Fact]
public void CanGetArtifact()
{
using (var document = PdfDocument.Open(GetPath1()))
{
var page = document.GetPage(2);
var mcs = page.GetMarkedContents();
Assert.NotEmpty(mcs);
var content = mcs[0];
Assert.True(content.IsArtifact);
Assert.Equal(typeof(ArtifactMarkedContentElement), content.GetType());
var artifact = (ArtifactMarkedContentElement)mcs[0];
Assert.Equal(-1, artifact.MarkedContentIdentifier);
Assert.True(artifact.IsTopAttached);
Assert.False(artifact.IsRightAttached);
Assert.False(artifact.IsLeftAttached);
Assert.False(artifact.IsBottomAttached);
Assert.True(artifact.BoundingBox.HasValue);
Assert.Equal(89.03, artifact.BoundingBox.Value.BottomLeft.X);
Assert.Equal(717.756, artifact.BoundingBox.Value.BottomLeft.Y);
Assert.Equal(574.422, artifact.BoundingBox.Value.TopRight.X);
Assert.Equal(751.1398, artifact.BoundingBox.Value.TopRight.Y);
Assert.Equal(ArtifactMarkedContentElement.ArtifactType.Pagination, artifact.Type);
Assert.Equal("Header", artifact.SubType);
Assert.Equal(33, artifact.Letters.Count);
Assert.Equal(8, artifact.Paths.Count);
Assert.Equal(0, artifact.Images.Count);
}
}
private static string GetPath1() => IntegrationHelpers.GetDocumentPath(FileName1);
private static string GetPath2() => IntegrationHelpers.GetDocumentPath(FileName2);
}
}

View File

@@ -152,13 +152,27 @@
var attributeOwners = GetOptional(NameToken.O, pdfScanner); var attributeOwners = GetOptional(NameToken.O, pdfScanner);
var boundingBox = default(PdfRectangle?); var boundingBox = default(PdfRectangle?);
if (properties.TryGet(NameToken.Bbox, pdfScanner, out ArrayToken arrayToken) if (properties.TryGet(NameToken.Bbox, pdfScanner, out ArrayToken arrayToken))
&& arrayToken.Length == 6)
{ {
var left = arrayToken[2] as NumericToken; NumericToken left = null;
var bottom = arrayToken[3] as NumericToken; NumericToken bottom = null;
var right = arrayToken[4] as NumericToken; NumericToken right = null;
var top = arrayToken[5] as NumericToken; NumericToken top = null;
if (arrayToken.Length == 4)
{
left = arrayToken[0] as NumericToken;
bottom = arrayToken[1] as NumericToken;
right = arrayToken[2] as NumericToken;
top = arrayToken[3] as NumericToken;
}
else if (arrayToken.Length == 6)
{
left = arrayToken[2] as NumericToken;
bottom = arrayToken[3] as NumericToken;
right = arrayToken[4] as NumericToken;
top = arrayToken[5] as NumericToken;
}
if (left != null && bottom != null && right != null && top != null) if (left != null && bottom != null && right != null && top != null)
{ {