Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

fix(pdf): keep the text of pages turned with /Rotate (#740)

KnifeLemon7846463e0c40
diff --git a/src/Filee.Engines/Pdf/PdfDocumentReader.Tables.cs b/src/Filee.Engines/Pdf/PdfDocumentReader.Tables.cs
index cc33d4db..5241844d 100644
--- a/src/Filee.Engines/Pdf/PdfDocumentReader.Tables.cs
+++ b/src/Filee.Engines/Pdf/PdfDocumentReader.Tables.cs
@@ -79,9 +79,9 @@ internal sealed partial class PdfDocumentReader
/// <summary>
/// The rules of a page (stroked lines and edges, thin filled rectangles) and the larger filled rectangles that
- /// may be cell shading, shifted like the words. Invisible (white, clipping) paths are left out.
+ /// may be cell shading, placed like the words. Invisible (white, clipping) paths are left out.
/// </summary>
- private static (List<Rule> Rules, List<(PdfRectangle Bounds, string Color)> Shades) PageRules(IEnumerable<PdfPath> paths, PdfRectangle crop)
+ private static (List<Rule> Rules, List<(PdfRectangle Bounds, string Color)> Shades) PageRules(IEnumerable<PdfPath> paths)
{
var rules = new List<Rule>();
var shades = new List<(PdfRectangle, string)>();
@@ -123,12 +123,11 @@ internal sealed partial class PdfDocumentReader
segments += lines.Count;
if (segments > MaxSegments)
return ([], []);
- var shifted = lines.Select(l => (From: new PdfPoint(l.From.X - crop.Left, l.From.Y - crop.Bottom), To: new PdfPoint(l.To.X - crop.Left, l.To.Y - crop.Bottom))).ToList();
if (stroke is not null && !IsWhite(stroke))
{
var thickness = Math.Max(0.25, path.LineWidth);
- foreach (var (from, to) in shifted)
+ foreach (var (from, to) in lines)
{
if (Math.Abs(from.Y - to.Y) <= 0.5 && Math.Abs(from.X - to.X) >= MinRuleLength)
rules.Add(new Rule(true, (from.Y + to.Y) / 2, Math.Min(from.X, to.X), Math.Max(from.X, to.X), thickness, stroke, dashed));
@@ -138,13 +137,13 @@ internal sealed partial class PdfDocumentReader
}
// A filled shape of straight, upright edges: a thin one is a rule, a larger one may shade a cell.
- if (fill is null || curved || shifted.Count < 3 || IsWhite(fill)
- || !shifted.All(l => Math.Abs(l.From.X - l.To.X) <= 0.5 || Math.Abs(l.From.Y - l.To.Y) <= 0.5))
+ if (fill is null || curved || lines.Count < 3 || IsWhite(fill)
+ || !lines.All(l => Math.Abs(l.From.X - l.To.X) <= 0.5 || Math.Abs(l.From.Y - l.To.Y) <= 0.5))
continue;
- var left = shifted.Min(l => Math.Min(l.From.X, l.To.X));
- var right = shifted.Max(l => Math.Max(l.From.X, l.To.X));
- var bottom = shifted.Min(l => Math.Min(l.From.Y, l.To.Y));
- var top = shifted.Max(l => Math.Max(l.From.Y, l.To.Y));
+ var left = lines.Min(l => Math.Min(l.From.X, l.To.X));
+ var right = lines.Max(l => Math.Max(l.From.X, l.To.X));
+ var bottom = lines.Min(l => Math.Min(l.From.Y, l.To.Y));
+ var top = lines.Max(l => Math.Max(l.From.Y, l.To.Y));
var (width, height) = (right - left, top - bottom);
if (height <= MaxRuleThickness && width >= MinRuleLength && width > 2 * height)
rules.Add(new Rule(true, (top + bottom) / 2, left, right, Math.Max(0.25, height), fill, false));
@@ -207,9 +206,9 @@ internal sealed partial class PdfDocumentReader
/// Ruled tables of a page; the words in them are taken out of <paramref name="words"/>. Grids are tried largest
/// first, and a grid inside a table already found (a table in a cell) stays part of that cell.
/// </summary>
- private static List<RuledTable> RuledTables(IEnumerable<PdfPath> paths, PdfRectangle crop, List<TextWord> words)
+ private static List<RuledTable> RuledTables(IEnumerable<PdfPath> paths, List<TextWord> words)
{
- var (rules, shades) = PageRules(paths, crop);
+ var (rules, shades) = PageRules(paths);
var tables = new List<RuledTable>();
foreach (var group in Grids(rules).OrderByDescending(g => Area(Extent(g))))
{
diff --git a/src/Filee.Engines/Pdf/PdfDocumentReader.cs b/src/Filee.Engines/Pdf/PdfDocumentReader.cs
index ac3436bc..4cf94aed 100644
--- a/src/Filee.Engines/Pdf/PdfDocumentReader.cs
+++ b/src/Filee.Engines/Pdf/PdfDocumentReader.cs
@@ -8,6 +8,10 @@
// the result is complete. With "Recognize text" (PdfOptions.Ocr) Tesseract reads the scans first, and their invisible
// OCR layer is the page's text (ReadWithOcrLayer).
//
+// A page turned with /Rotate is read as it is shown: landscape pages that printer drivers (PScript5, Acrobat
+// Distiller) write as a portrait page turned by 90° keep their text, tables and pictures. Only a page whose text runs
+// sideways as shown (a portrait page turned in a viewer) has no lines to read and becomes the rendered page.
+//
// Rows laid out with tab stops (a number, a label and a date far to the right) stay one line with tabs at the PDF's
// positions, and a line that ends early although the next line's first word would have fitted (an address) keeps its
// line break.
@@ -280,14 +284,14 @@ internal sealed partial class PdfDocumentReader
private PageContent ReadPage(Page page)
{
- var crop = page.CropBox.Bounds;
- // A page turned by 90° is shown (and rendered) with width and height swapped.
- var rotated = page.Rotation.Value % 180 != 0;
- var content = new PageContent(page.Number - 1, rotated ? crop.Height : crop.Width, rotated ? crop.Width : crop.Height);
+ // PdfPig gives every position (letters, lines, pictures, links) on the page as it is shown: turned by its
+ // /Rotate and measured from the bottom-left corner of its visible part (the crop box). Page.Width and Height
+ // are that part's size, width and height swapped on a page turned by 90°, as PDFium renders it.
+ var content = new PageContent(page.Number - 1, page.Width, page.Height);
var links = page.GetHyperlinks()
.Where(h => h.Uri is { } uri && (uri.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || uri.StartsWith("https://", StringComparison.OrdinalIgnoreCase) || uri.StartsWith("mailto:", StringComparison.OrdinalIgnoreCase)))
- .Select(h => (Bounds: Shift(h.Bounds, crop), Uri: h.Uri!))
+ .Select(h => (h.Bounds, Uri: h.Uri!))
.ToList();
// Text that needs shaping (Arabic, Indic, ...) is read in content-stream order (PdfDocumentReader.ComplexText).
@@ -311,7 +315,7 @@ internal sealed partial class PdfDocumentReader
if (!_textOnly && !_ocrLayer && invisible)
continue;
var lead = string.IsNullOrWhiteSpace(word.Lead.Value) ? letters[0] : word.Lead;
- var box = Shift(word.Box, crop);
+ var box = word.Box;
var size = Median(letters.Select(LetterSize));
var center = new PdfPoint((box.Left + box.Right) / 2, (box.Top + box.Bottom) / 2);
var link = links.FirstOrDefault(l => Inside(l.Bounds, center)).Uri;
@@ -320,12 +324,13 @@ internal sealed partial class PdfDocumentReader
if (invisible)
format = format with { Font = null, Bold = false, Italic = false };
words.Add(new TextWord(CleanText(word.Text), box.Left, box.Right, box.Top, box.Bottom,
- lead.StartBaseLine.Y - crop.Bottom, Math.Max(1, size), format, link));
+ lead.StartBaseLine.Y, Math.Max(1, size), format, link));
}
- if (words.Count == 0 || (rotated && !_textOnly))
+ if (words.Count == 0)
{
- // A scan, a page of drawings, or a turned page: the rendered page keeps everything.
+ // A scan, a page of drawings, or a page whose text runs sideways as shown (a portrait page turned by
+ // /Rotate): the rendered page keeps everything.
content.Scanned = true;
if (!_textOnly)
content.ScanPath = RenderScan(page.Number - 1);
@@ -333,11 +338,11 @@ internal sealed partial class PdfDocumentReader
}
// Plain text (TXT, page texts for slides) keeps table cells as paragraphs in reading order.
- var tables = _textOnly ? [] : RuledTables(page.Paths, crop, words);
+ var tables = _textOnly ? [] : RuledTables(page.Paths, words);
content.Items = words.Count > 0 ? TextItems(words) : [];
content.Items.AddRange(tables.Select(t => new Item { Table = t }));
if (!_textOnly)
- content.Items.AddRange(Pictures(page, content, crop));
+ content.Items.AddRange(Pictures(page, content));
return content;
}
@@ -351,9 +356,6 @@ internal sealed partial class PdfDocumentReader
return items;
}
- private static PdfRectangle Shift(PdfRectangle box, PdfRectangle crop) =>
- new(box.Left - crop.Left, box.Bottom - crop.Bottom, box.Right - crop.Left, box.Top - crop.Bottom);
-
private static bool Inside(PdfRectangle box, PdfPoint point) =>
point.X >= box.Left && point.X <= box.Right && point.Y >= box.Bottom && point.Y <= box.Top;
@@ -436,7 +438,7 @@ internal sealed partial class PdfDocumentReader
// ───────────────────────── Pictures ─────────────────────────
/// <summary>Pictures of a text page. A picture covering the page behind text is the scan under an OCR layer: skipped.</summary>
- private IEnumerable<Item> Pictures(Page page, PageContent content, PdfRectangle crop)
+ private IEnumerable<Item> Pictures(Page page, PageContent content)
{
var result = new List<Item>();
SKBitmap? rendered = null;
@@ -444,8 +446,8 @@ internal sealed partial class PdfDocumentReader
{
foreach (var image in page.GetImages().Take(200))
{
- var bounds = Shift(image.BoundingBox, crop);
- bounds = new PdfRectangle(Math.Max(0, bounds.Left), Math.Max(0, bounds.Bottom), Math.Min(content.Width, bounds.Right), Math.Min(content.Height, bounds.Top));
+ var box = image.BoundingBox;
+ var bounds = new PdfRectangle(Math.Max(0, box.Left), Math.Max(0, box.Bottom), Math.Min(content.Width, box.Right), Math.Min(content.Height, box.Top));
if (bounds.Width < 6 || bounds.Height < 6 || image.IsImageMask)
continue;
if (bounds.Width * bounds.Height > 0.85 * content.Width * content.Height)
diff --git a/tests/Filee.Engines.Tests/PdfBuilder.cs b/tests/Filee.Engines.Tests/PdfBuilder.cs
index 40672876..6225190d 100644
--- a/tests/Filee.Engines.Tests/PdfBuilder.cs
+++ b/tests/Filee.Engines.Tests/PdfBuilder.cs
@@ -1,9 +1,12 @@
// Builds small PDF files for the PDF reader tests with PdfPig's writer: text in the standard 14 fonts (every PDF
// reader has them, so no font file is needed and the tests run on machines without Windows fonts), wrapped
// paragraphs, colours, pictures, links, lines and filled bars (ruled tables) and several pages. PDFsharp adds password
-// protection to a finished file.
+// protection to a finished file, and turns or crops its pages as printer drivers and viewers do.
+using System.Globalization;
+using System.Text;
using ImageMagick;
+using PdfSharp.Drawing;
using PdfSharp.Pdf.IO;
using UglyToad.PdfPig.Core;
using UglyToad.PdfPig.Fonts.Standard14Fonts;
@@ -141,6 +144,38 @@ internal sealed class PdfBuilder
return path;
}
+ /// <summary>
+ /// Rewrites every page the way printer drivers (PScript5, Acrobat Distiller) write a landscape page: a portrait
+ /// page whose content is drawn turned by 90°, and /Rotate <paramref name="rotate"/> (90 or 270) turns it back, so
+ /// it is shown as before. Without <paramref name="drawTurned"/> only /Rotate is set, as a viewer turns a page (its
+ /// text then runs sideways). With <paramref name="rotate"/> 0 the page stays as it is. A crop box cuts
+ /// <paramref name="inset"/> points off every side of the page.
+ /// </summary>
+ public static void Turn(string path, int rotate, bool drawTurned = true, double inset = 0)
+ {
+ using var document = PdfReader.Open(path, PdfDocumentOpenMode.Modify);
+ foreach (var page in document.Pages)
+ {
+ var box = page.Elements.GetRectangle("/MediaBox");
+ var (width, height) = (box.Width, box.Height);
+ if (rotate != 0 && drawTurned)
+ {
+ var turn = rotate == 90
+ ? string.Create(CultureInfo.InvariantCulture, $"0 1 -1 0 {height} 0 cm")
+ : string.Create(CultureInfo.InvariantCulture, $"0 -1 1 0 0 {width} cm");
+ page.Contents.PrependContent().CreateStream(Encoding.ASCII.GetBytes($"q {turn}\n"));
+ page.Contents.AppendContent().CreateStream("Q\n"u8.ToArray());
+ (width, height) = (height, width);
+ page.Elements.SetRectangle("/MediaBox", new PdfSharp.Pdf.PdfRectangle(new XPoint(0, 0), new XPoint(width, height)));
+ }
+ if (rotate != 0)
+ page.Elements.SetInteger("/Rotate", rotate);
+ if (inset > 0)
+ page.Elements.SetRectangle("/CropBox", new PdfSharp.Pdf.PdfRectangle(new XPoint(inset, inset), new XPoint(width - inset, height - inset)));
+ }
+ document.Save(path);
+ }
+
/// <summary>Encrypts a PDF with a password needed to open it (PDFsharp, no fonts involved).</summary>
public static void Protect(string path, string password)
{
diff --git a/tests/Filee.Engines.Tests/PdfReaderTests.cs b/tests/Filee.Engines.Tests/PdfReaderTests.cs
index 7b21386b..5c908225 100644
--- a/tests/Filee.Engines.Tests/PdfReaderTests.cs
+++ b/tests/Filee.Engines.Tests/PdfReaderTests.cs
@@ -1,7 +1,7 @@
// PDF → DOCX / HWPX / TXT / Markdown with the built-in PDF reader (PdfPig): paragraphs in reading order (two-column
// pages too), headings from font sizes, bold / italic / colour, links, pictures (JPEG kept as it is; next to a
// Markdown file in "<name>_files"), repeated headers, footers, page numbers and logos left out, scanned pages as page
-// pictures, and a clear error for password-protected files.
+// pictures, turned (/Rotate) and cropped pages read as they are shown, and a clear error for password-protected files.
// The test PDFs use the standard 14 fonts, so no installed font is needed (only the Korean test embeds one, if any).
using System.IO.Compression;
@@ -214,6 +214,87 @@ public class PdfReaderTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.Equal("Text after the scan\r\n", await File.ReadAllTextAsync(await ConvertAsync(path, "txt"), TestContext.Current.CancellationToken));
}
+ /// <summary>
+ /// A landscape page (842 × 595) with a heading, a ruled 3 × 2 table, a picture and a paragraph, 80 pt from the left
+ /// edge, written as <see cref="PdfBuilder.Turn"/> turns it.
+ /// </summary>
+ private static string LandscapePage(string folder, int rotate, bool drawTurned = true, double inset = 0)
+ {
+ var pdf = new PdfBuilder().NewPage(842, 595);
+ var regular = pdf.Font();
+ pdf.Line("Supplier comparison", 16, pdf.Font(bold: true), 80, 90);
+ double[] xs = [80, 300, 520, 760];
+ double[] ys = [120, 145, 170];
+ foreach (var y in ys)
+ pdf.Rule(xs[0], y, xs[^1], y);
+ foreach (var x in xs)
+ pdf.Rule(x, ys[0], x, ys[^1]);
+ string[][] cells = [["Supplier", "A", "B"], ["Price", "100", "200"]];
+ for (var r = 0; r < 2; r++)
+ {
+ for (var c = 0; c < 3; c++)
+ pdf.Line(cells[r][c], 10, regular, xs[c] + 4, ys[r] + 16);
+ }
+ pdf.Picture(PdfBuilder.Photo(folder, "product.png", MagickFormat.Png, 150, 100), 80, 200, 150, 100);
+ pdf.Line("Prices include delivery.", 11, regular, 80, 340);
+ var path = pdf.Save(Path.Combine(folder, $"landscape-{rotate}.pdf"));
+ PdfBuilder.Turn(path, rotate, drawTurned, inset);
+ return path;
+ }
+
+ /// <summary>
+ /// Issue #740: printer drivers write a landscape page as a portrait page with turned content and /Rotate. Such a
+ /// page was taken for a scan and came out as one picture without text.
+ /// </summary>
+ [Theory]
+ [InlineData(90, 0)]
+ [InlineData(270, 0)]
+ [InlineData(90, 40)]
+ [InlineData(0, 40)]
+ public async Task Turned_and_cropped_pages_keep_their_text_tables_and_pictures(int rotate, double inset)
+ {
+ var path = LandscapePage(fx.NewFolder(), rotate, inset: inset);
+
+ var docx = await ConvertAsync(path, "docx");
+
+ ValidPackage(docx);
+ var section = Assert.Single(ReadBack(docx).Sections);
+ // The page as it is shown: landscape, less the cropped edges; the left margin is measured from the crop box.
+ var size = (Width: (int)((842 - 2 * inset) * 100), Height: (int)((595 - 2 * inset) * 100));
+ Assert.Equal(size, (section.Page!.Width, section.Page!.Height));
+ Assert.Equal((int)((80 - inset) * 100), section.Page.Left);
+ var blocks = section.Blocks.Where(b => b is HTable || (b is HParagraph p && (TextOf(p).Trim().Length > 0 || p.Inlines.OfType<HImage>().Any()))).ToList();
+ Assert.Equal("Supplier comparison", TextOf(Assert.IsType<HParagraph>(blocks[0])).Trim());
+ var table = Assert.IsType<HTable>(blocks[1]);
+ Assert.Equal([["Supplier", "A", "B"], ["Price", "100", "200"]],
+ table.Rows.Select(r => r.Cells.Select(c => string.Concat(Paragraphs(c.Blocks).Select(TextOf)).Trim()).ToArray()));
+ var picture = Assert.Single(Assert.IsType<HParagraph>(blocks[2]).Inlines.OfType<HImage>());
+ Assert.Equal((15000, 10000), (picture.Width, picture.Height));
+ Assert.Equal("Prices include delivery.", TextOf(Assert.IsType<HParagraph>(blocks[3])).Trim());
+ Assert.Equal(4, blocks.Count);
+
+ var hwpx = await ConvertAsync(path, "hwpx");
+ HwpxAssert.ValidPackage(hwpx, expectImages: 1);
+ Assert.Contains("Supplier comparison", HwpxAssert.Xml(hwpx).Value);
+
+ var text = await File.ReadAllTextAsync(await ConvertAsync(path, "txt"), TestContext.Current.CancellationToken);
+ Assert.Contains("Supplier comparison", text);
+ Assert.Contains("Prices include delivery.", text);
+ }
+
+ [Fact]
+ public async Task Page_turned_in_a_viewer_with_sideways_text_stays_a_page_picture()
+ {
+ var path = LandscapePage(fx.NewFolder(), 90, drawTurned: false);
+
+ var docx = await ConvertAsync(path, "docx");
+
+ var section = Assert.Single(ReadBack(docx).Sections);
+ var scan = section.Blocks.OfType<HParagraph>().Single().Inlines.OfType<HImage>().Single();
+ // The landscape page turned by 90° is shown upright, 595 wide and 842 high.
+ Assert.Equal((59500, 84200), (scan.Width, scan.Height));
+ }
+
[Fact]
public async Task Pdf_to_hwpx_goes_through_the_document_model()
{