Added integration test for cropped document, and a cropped+rotated document

with an annotation as well.
Added annotations to visual verification test (blue outlines).
This commit is contained in:
mvantzet 2023-03-13 18:08:20 +01:00
parent 17681472cc
commit a439b43246
5 changed files with 51 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

View File

@ -0,0 +1,30 @@
namespace UglyToad.PdfPig.Tests.Integration
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
public class RotationAndCroppingTests
{
[Fact]
public void CroppedPageHasCorrectTextCoordinates()
{
var path = IntegrationHelpers.GetDocumentPath("SPARC - v9 Architecture Manual");
using (var document = PdfDocument.Open(path))
{
var page = document.GetPage(1);
Assert.Equal(page.Width, 612); // Due to cropping
Assert.Equal(page.Height, 792); // Due to cropping
var minX = page.Letters.Select(l => l.GlyphRectangle.Left).Min();
var maxX = page.Letters.Select(l => l.GlyphRectangle.Right).Max();
Assert.Equal(72, minX, 0); // If cropping is not applied correctly, these values will be off
Assert.Equal(540, maxX, 0); // If cropping is not applied correctly, these values will be off
// The page is cropped at
Assert.NotNull(page.Content);
}
}
}
}

View File

@ -19,7 +19,9 @@
private const string SinglePage90ClockwiseRotation = "SinglePage90ClockwiseRotation - from PdfPig";
private const string SinglePage180ClockwiseRotation = "SinglePage180ClockwiseRotation - from PdfPig";
private const string SinglePage270ClockwiseRotation = "SinglePage270ClockwiseRotation - from PdfPig";
private const string SPARCv9ArchitectureManual = "SPARC - v9 Architecture Manual";
private const string CroppedAndRotatedFile = "cropped-and-rotated";
private static string GetFilename(string name)
{
var documentFolder = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", "Integration", "Documents"));
@ -116,6 +118,18 @@
Run(SinglePage270ClockwiseRotation, 595);
}
[Fact]
public void SPARCv9ArchitectureManualTest()
{
Run(SPARCv9ArchitectureManual);
}
[Fact]
public void CroppedAndRotatedTest()
{
Run(CroppedAndRotatedFile, 205);
}
private static void Run(string file, int imageHeight = 792)
{
var pdfFileName = GetFilename(file);
@ -127,6 +141,7 @@
var violetPen = new Pen(Color.BlueViolet, 1);
var redPen = new Pen(Color.Crimson, 1);
var bluePen = new Pen(Color.GreenYellow, 1);
using (var bitmap = new Bitmap(image))
using (var graphics = Graphics.FromImage(bitmap))
@ -141,6 +156,11 @@
DrawRectangle(letter.GlyphRectangle, graphics, violetPen, imageHeight);
}
foreach (var annotation in page.ExperimentalAccess.GetAnnotations())
{
DrawRectangle(annotation.Rectangle, graphics, bluePen, imageHeight);
}
var imageName = $"{file}.jpg";
if (!Directory.Exists("Images"))