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.

PDF toolkit: stamps on landscape pages, and a 127-byte password in PDFium

KnifeLemonc21ce3fff1a0
diff --git a/src/Filee.Engines/Pdf/PdfFinishing.cs b/src/Filee.Engines/Pdf/PdfFinishing.cs
index 75f39cd8..49d9534c 100644
--- a/src/Filee.Engines/Pdf/PdfFinishing.cs
+++ b/src/Filee.Engines/Pdf/PdfFinishing.cs
@@ -72,7 +72,9 @@ internal static class PdfFinishing
var rotate = page.Rotate;
var orientation = page.Orientation;
page.Rotate = 0;
- page.Orientation = PdfSharp.PageOrientation.Portrait;
+ // PDFsharp swaps width and height of a page whose orientation doesn't match its media box: drawn on unturned, it
+ // must match, or a landscape page is drawn on as if it were portrait.
+ page.Orientation = media.Width > media.Height ? PdfSharp.PageOrientation.Landscape : PdfSharp.PageOrientation.Portrait;
try
{
using var graphics = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
diff --git a/tests/Filee.Engines.Tests/PdfFinishingTests.cs b/tests/Filee.Engines.Tests/PdfFinishingTests.cs
index 3812a75e..29943bfb 100644
--- a/tests/Filee.Engines.Tests/PdfFinishingTests.cs
+++ b/tests/Filee.Engines.Tests/PdfFinishingTests.cs
@@ -118,9 +118,33 @@ public class PdfFinishingTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.True(Marked(output, 0, 0.3, 0.4, 0.7, 0.6) > 50, "the watermark is centred in the crop box");
}
+ [Theory]
+ [InlineData(0)]
+ [InlineData(90)]
+ public async Task Stamps_land_at_the_bottom_of_a_landscape_page(int rotate)
+ {
+ var path = Path.Combine(fx.NewFolder(), "landscape.pdf");
+ using (var document = new PdfDocument())
+ {
+ var page = document.AddPage();
+ page.MediaBox = new PdfRectangle(new XPoint(0, 0), new XPoint(842, 595));
+ page.Rotate = rotate;
+ document.Save(path);
+ }
+
+ var job = await fx.ConvertAsync([path], new Preset { TargetFormat = "pdf", Pdf = { Watermark = "DRAFT", PageNumbers = true } });
+
+ Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));
+ var output = job.Outputs.Single();
+ Assert.True(Marked(output, 0, 0.4, 0.9, 0.6, 0.99) > 5, "the page number sits at the bottom of the shown page");
+ Assert.Equal(0, Marked(output, 0, 0.4, 0, 0.6, 0.1)); // not at the top
+ Assert.True(Marked(output, 0, 0.3, 0.4, 0.7, 0.6) > 50, "the watermark crosses the middle");
+ }
+
[Theory]
[InlineData("pässwörd")]
[InlineData("비밀번호입니다")]
+ [InlineData("비비비비비비비비비비비비비비비비비비비비비비비비비비비비비비비비비비비비비비비비비비a")] // 127 UTF-8 bytes, all a reader uses
public async Task A_non_ascii_password_opens_in_pdfium(string password)
{
var pdf = BlankPdf(fx.NewFolder(), 1);