Files
PdfPig/src/UglyToad.PdfPig.Tests/Integration/SinglePageLibreOfficeImages.cs
davebrokit f3e37eafae
Some checks failed
Build, test and publish draft / build (push) Has been cancelled
Build and test [MacOS] / build (push) Has been cancelled
Run Common Crawl Tests / build (0000-0001) (push) Has been cancelled
Run Common Crawl Tests / build (0002-0003) (push) Has been cancelled
Run Common Crawl Tests / build (0004-0005) (push) Has been cancelled
Run Common Crawl Tests / build (0006-0007) (push) Has been cancelled
Run Common Crawl Tests / build (0008-0009) (push) Has been cancelled
Run Common Crawl Tests / build (0010-0011) (push) Has been cancelled
Run Common Crawl Tests / build (0012-0013) (push) Has been cancelled
Run Integration Tests / build (push) Has been cancelled
Tag Release / tag_if_version_changed (push) Has been cancelled
Nightly Release / Check if this commit has already been published (push) Has been cancelled
Nightly Release / tests (push) Has been cancelled
Nightly Release / build_and_publish_nightly (push) Has been cancelled
Introduce IBlock and ILettersBlock interfaces (Round 2) (#1263)
* Code review changes
- Keep the Bounds property on the image classes so this isn't a major breaking API change
- Don't expose letters collection

* Minor fix

* Switch to using BoundingBox in the library

---------

Co-authored-by: davmarksman <david@brokit.co.uk>
2026-02-28 16:25:51 +00:00

107 lines
3.6 KiB
C#

namespace UglyToad.PdfPig.Tests.Integration
{
#if NET9_0_OR_GREATER
using Microsoft.AspNetCore.WebUtilities;
#endif
public class SinglePageLibreOfficeImages
{
private static string GetFilePath() => IntegrationHelpers.GetDocumentPath(@"Single Page Images - from libre office.pdf");
[Fact]
public void Has3Images()
{
using (var document = PdfDocument.Open(GetFilePath(), ParsingOptions.LenientParsingOff))
{
var page = document.GetPage(1);
var images = page.GetImages().ToList();
Assert.Equal(3, images.Count);
}
}
#if NET9_0_OR_GREATER
[Fact]
public void CanUseFileBufferingReadStream()
{
var bytes = File.ReadAllBytes(GetFilePath());
using var mem = new MemoryStream(bytes);
using var fbrs = new FileBufferingReadStream(mem, 256);
using var doc = PdfDocument.Open(fbrs);
var page = doc.GetPage(1);
Assert.NotEmpty(page.Text);
}
#endif
[Fact]
public void ImagesHaveCorrectDimensionsAndLocations()
{
var doubleComparer = new DoubleComparer(0.1);
using (var document = PdfDocument.Open(GetFilePath(), ParsingOptions.LenientParsingOff))
{
var page = document.GetPage(1);
var images = page.GetImages().OrderBy(x => x.BoundingBox.Width).ToList();
var pdfPigSquare = images[0];
Assert.Equal(148.3d, pdfPigSquare.BoundingBox.Width, doubleComparer);
Assert.Equal(148.3d, pdfPigSquare.BoundingBox.Height, doubleComparer);
Assert.Equal(60.1d, pdfPigSquare.BoundingBox.Left, doubleComparer);
Assert.Equal(765.8d, pdfPigSquare.BoundingBox.Top, doubleComparer);
var pdfPigSquished = images[1];
Assert.Equal(206.8d, pdfPigSquished.BoundingBox.Width, doubleComparer);
Assert.Equal(83.2d, pdfPigSquished.BoundingBox.Height, doubleComparer);
Assert.Equal(309.8d, pdfPigSquished.BoundingBox.Left, doubleComparer);
Assert.Equal(552.1d, pdfPigSquished.BoundingBox.Top, doubleComparer);
var birthdayPigs = images[2];
Assert.Equal(391d, birthdayPigs.BoundingBox.Width, doubleComparer);
Assert.Equal(267.1d, birthdayPigs.BoundingBox.Height, doubleComparer);
Assert.Equal(102.2d, birthdayPigs.BoundingBox.Left, doubleComparer);
Assert.Equal(426.3d, birthdayPigs.BoundingBox.Top, doubleComparer);
}
}
[Fact]
public void HasCorrectText()
{
using (var document = PdfDocument.Open(GetFilePath(), ParsingOptions.LenientParsingOff))
{
var page = document.GetPage(1);
Assert.Equal("Oink oink", page.Text);
}
}
[Fact]
public void CanAccessImageBytesExceptUnsupported()
{
using (var document = PdfDocument.Open(GetFilePath(), ParsingOptions.LenientParsingOff))
{
var page = document.GetPage(1);
foreach (var image in page.GetImages())
{
if (image.TryGetBytesAsMemory(out var bytes))
{
Assert.False(bytes.IsEmpty);
}
else
{
Assert.False(image.RawMemory.IsEmpty);
}
}
}
}
}
}