load resources dictionary from pages as well as page node and throw informative error when the font is not found.

This commit is contained in:
Eliot Jones
2018-01-01 10:49:05 +00:00
parent b77b7ec0d8
commit 874f713566
8 changed files with 71 additions and 9 deletions

View File

@@ -2,6 +2,7 @@
{
using System;
using System.IO;
using System.Linq;
using Content;
using Xunit;
@@ -35,5 +36,18 @@
Assert.Equal(PageSize.A4, page.Size);
}
}
[Fact]
public void GetsCorrectPageTextIgnoringHiddenCharacters()
{
using (var document = PdfDocument.Open(GetFilename()))
{
var page = document.GetPage(1);
var text = string.Join(string.Empty, page.Letters.Select(x => x.Value));
Assert.Equal("36pt font14 pt font6pt font", text);
}
}
}
}

View File

@@ -0,0 +1,39 @@
namespace UglyToad.Pdf.Tests.Integration
{
using System;
using System.IO;
using Content;
using Xunit;
public class SinglePageSimpleOpenOfficeTests
{
private static string GetFilename()
{
var documentFolder = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", "Integration", "Documents"));
return Path.Combine(documentFolder, "Single Page Simple - from open office.pdf");
}
[Fact]
public void HasCorrectNumberOfPages()
{
var file = GetFilename();
using (var document = PdfDocument.Open(File.ReadAllBytes(file)))
{
Assert.Equal(1, document.NumberOfPages);
}
}
[Fact]
public void HasCorrectPageSize()
{
using (var document = PdfDocument.Open(GetFilename()))
{
var page = document.GetPage(1);
Assert.Equal(PageSize.Letter, page.Size);
}
}
}
}

View File

@@ -13,6 +13,7 @@
<None Remove="Integration\Documents\Font Size Test - from google chrome print pdf.pdf" />
<None Remove="Integration\Documents\Font Size Test - from libre office.pdf" />
<None Remove="Integration\Documents\Single Page Simple - from google drive.pdf" />
<None Remove="Integration\Documents\Single Page Simple - from open office.pdf" />
</ItemGroup>
<ItemGroup>
@@ -31,6 +32,9 @@
<Content Include="Integration\Documents\Single Page Simple - from google drive.pdf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Integration\Documents\Single Page Simple - from open office.pdf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>

View File

@@ -7,5 +7,7 @@
{
Page Create(int number, PdfDictionary dictionary, PageTreeMembers pageTreeMembers, IRandomAccessRead reader,
bool isLenientParsing);
void LoadResources(PdfDictionary dictionary, IRandomAccessRead reader, bool isLenientParsing);
}
}

View File

@@ -45,7 +45,7 @@
UserSpaceUnit userSpaceUnit = GetUserSpaceUnits(dictionary);
LoadResources(number, dictionary, reader, isLenientParsing);
LoadResources(dictionary, reader, isLenientParsing);
PageContent content = default(PageContent);
@@ -136,7 +136,7 @@
return mediaBox;
}
private void LoadResources(int pageNumber, PdfDictionary dictionary, IRandomAccessRead reader, bool isLenientParsing)
public void LoadResources(PdfDictionary dictionary, IRandomAccessRead reader, bool isLenientParsing)
{
var resources = dictionary.GetItemOrDefault(CosName.RESOURCES);
@@ -155,13 +155,8 @@
if (resourceDictionary is PdfDictionary resolvedDictionary)
{
resourceStore.LoadResourceDictionary(resolvedDictionary, reader, isLenientParsing);
return;
}
}
throw new InvalidOperationException(
$"No resource dictionary was found for this page ({pageNumber}), the page dictionary was {dictionary}.");
}
}
}

View File

@@ -64,6 +64,7 @@
{
if (locatedPages.TryGetValue(pageNumber, out PdfDictionary targetPageDictionary))
{
// TODO: cache the page
return pageFactory.Create(pageNumber, targetPageDictionary, new PageTreeMembers(), reader,
isLenientParsing);
}
@@ -119,6 +120,8 @@
var kids = currentPageDictionary.GetDictionaryObject(CosName.KIDS) as COSArray;
pageFactory.LoadResources(currentPageDictionary, reader, isLenientParsing);
bool childFound = false;
foreach (var kid in kids.OfType<CosObject>())
{

View File

@@ -79,10 +79,15 @@
public void ShowText(IInputBytes bytes)
{
var font = resourceStore.GetFont(GetCurrentState().FontState.FontName);
var currentState = GetCurrentState();
var font = resourceStore.GetFont(currentState.FontState.FontName);
if (font == null)
{
throw new InvalidOperationException($"Could not find the font with name {currentState.FontState.FontName} in the resource store. It has not been loaded yet.");
}
var fontSize = currentState.FontState.FontSize;
var horizontalScaling = currentState.FontState.HorizontalScaling;
var characterSpacing = currentState.FontState.CharacterSpacing;