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.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
OCR: other PDF options, non-ASCII paths, cancel (review of #429)
cre8ive-marketing327224b4e594
diff --git a/src/Filee.App/ViewModels/PresetEditorViewModel.cs b/src/Filee.App/ViewModels/PresetEditorViewModel.csindex 4cdddb97..1fee4e9b 100644--- a/src/Filee.App/ViewModels/PresetEditorViewModel.cs+++ b/src/Filee.App/ViewModels/PresetEditorViewModel.cs@@ -214,7 +214,7 @@ public sealed partial class PresetEditorViewModel : ObservableObject[ObservableProperty][NotifyPropertyChangedFor(nameof(ShowImage), nameof(ShowTurnForPdf), nameof(ShowQuality), nameof(ShowTiff), nameof(ShowMultiPageTiff), nameof(ShowWebp), nameof(ShowIco),nameof(ShowZipResult), nameof(ShowPdf), nameof(ShowPdfBuild), nameof(ShowDocument), nameof(ShowLeaveOutPictures), nameof(ShowPdfSplit), nameof(ShowPdfCombine),- nameof(ShowPdfCompression), nameof(ShowPdfCompressionOptions), nameof(ShowOcr),+ nameof(ShowPdfCompression), nameof(ShowPdfCompressionOptions), nameof(ShowOcr), nameof(ShowPageSize),nameof(ShowMedia), nameof(ShowMediaQuality), nameof(ShowMaxHeight), nameof(ShowAudioBitrate), nameof(ShowRemoveAudio),nameof(ShowAudioChannels), nameof(ShowWav), nameof(VideoCodecs), nameof(VideoCodecChoice), nameof(ShowVideoCodec), nameof(ShowHardwareEncoding),nameof(AudioCodecs), nameof(AudioCodecChoice), nameof(ShowAudioCodec),@@ -400,7 +400,15 @@ public sealed partial class PresetEditorViewModel : ObservableObject[ObservableProperty] private bool _compressWithGhostscript;/// <summary>Recognize the text of scans and pictures (PdfOptions.Ocr), in <see cref="OcrLanguage"/>.</summary>- [ObservableProperty] private bool _ocr;+ [ObservableProperty]+ [NotifyPropertyChangedFor(nameof(ShowPdfSplit), nameof(ShowPdfCompression), nameof(ShowPdfCompressionOptions), nameof(ShowPageSize))]+ private bool _ocr;++ /// <summary>+ /// Text recognition makes the PDF itself (Tesseract): one file with each page the size of its scan or picture, so one+ /// file per page, compression (and with it removing metadata), page size and margin don't apply and are hidden.+ /// </summary>+ private bool Recognizes => ShowOcr && Ocr;[ObservableProperty] private Choice<string> _ocrLanguage;/// <summary>PDF → image: one tall image of all pages.</summary>@@ -508,10 +516,13 @@ public sealed partial class PresetEditorViewModel : ObservableObject/// <summary>"Save as ZIP" is offered for geospatial targets, whose results are several files (a Shapefile) or travel zipped.</summary>public bool ShowZipResult => TargetCategory == FormatCategory.Geo;public bool ShowPdfBuild => TargetId == "pdf";- public bool ShowPdfSplit => TargetId == "pdf";++ /// <summary>Page size and margin of a PDF built from pictures; not with text recognition (<see cref="Recognizes"/>).</summary>+ public bool ShowPageSize => ShowPdfBuild && !Recognizes;+ public bool ShowPdfSplit => TargetId == "pdf" && !Recognizes;/// <summary>Compression is for PDF → PDF; Ghostscript and metadata only matter while it is on.</summary>- public bool ShowPdfCompression => TargetId == "pdf";+ public bool ShowPdfCompression => TargetId == "pdf" && !Recognizes;public bool ShowPdfCompressionOptions => ShowPdfCompression && CompressionLevel.Value != PdfCompression.None;/// <summary>Text from scans goes into the formats Filee writes from a PDF's text itself (PdfOptions.OcrTargets).</summary>@@ -624,8 +635,17 @@ public sealed partial class PresetEditorViewModel : ObservableObjectpdf.Compression = CompressionLevel.Value;pdf.CompressWithGhostscript = CompressWithGhostscript;// Only where the option is shown, as with SaveEveryFrame below.- pdf.Ocr = ShowOcr && Ocr;+ pdf.Ocr = Recognizes;pdf.OcrLanguage = OcrLanguage.Value;+ if (pdf.Ocr)+ {+ // Hidden while text is recognized, so nothing is kept behind it that the PDF wouldn't do (Recognizes).+ pdf.SplitPages = false;+ pdf.Compression = PdfCompression.None;+ pdf.CompressWithGhostscript = false;+ pdf.PageSize = PdfPageSize.FitImage;+ pdf.MarginMm = 0;+ }_original.Document.PdfA = PdfA;// Only where the option is shown, as with SaveEveryFrame below._original.Document.LeaveOutPictures = ShowLeaveOutPictures && LeaveOutPictures;diff --git a/src/Filee.App/Views/PresetEditorView.axaml b/src/Filee.App/Views/PresetEditorView.axamlindex 8e105efd..81a1db4d 100644--- a/src/Filee.App/Views/PresetEditorView.axaml+++ b/src/Filee.App/Views/PresetEditorView.axaml@@ -355,7 +355,7 @@<TextBlock Classes="caption" TextWrapping="Wrap" Margin="28,0,0,0" Text="{DynamicResource presets.split_tall_image_hint}" /></StackPanel></StackPanel>- <Grid ColumnDefinitions="*,12,*" IsVisible="{Binding ShowPdfBuild}">+ <Grid ColumnDefinitions="*,12,*" IsVisible="{Binding ShowPageSize}"><StackPanel><TextBlock Classes="label" Text="{DynamicResource presets.page_size}" /><ComboBox ItemsSource="{Binding PageSizes}" SelectedItem="{Binding PageSize}" HorizontalAlignment="Stretch" />diff --git a/src/Filee.Core/Presets/BuiltInData.cs b/src/Filee.Core/Presets/BuiltInData.csindex af5cd208..e6fe44ac 100644--- a/src/Filee.Core/Presets/BuiltInData.cs+++ b/src/Filee.Core/Presets/BuiltInData.cs@@ -197,7 +197,7 @@ public static class BuiltInData];/// <summary>- /// "Searchable PDF": scans and pictures with their text recognized by Tesseract (added in settings schema 33). On no+ /// "Searchable PDF": scans and pictures with their text recognized by Tesseract (added in settings schema 40). On no/// donut, like "Compress PDF"./// </summary>public static List<Preset> OcrPresets() =>diff --git a/src/Filee.Engines/Ocr/TesseractConverter.cs b/src/Filee.Engines/Ocr/TesseractConverter.csindex a5c8f9f5..89c5a09f 100644--- a/src/Filee.Engines/Ocr/TesseractConverter.cs+++ b/src/Filee.Engines/Ocr/TesseractConverter.cs@@ -92,7 +92,7 @@ public sealed class TesseractConverter : IConvertervar tessdata = Path.Combine(scratch, "tessdata");Directory.CreateDirectory(tessdata);File.Copy(data, Path.Combine(tessdata, Path.GetFileName(data)));- var ocr = new Run(tesseract, tessdata, language.Code);+ var ocr = new Run(tesseract, scratch, language.Code);progress?.Report(0.05);var output = step.Output.Allocate("pdf");@@ -104,9 +104,11 @@ public sealed class TesseractConverter : IConverter}else{- var target = Path.Combine(scratch, "page");- await ocr.ToPdfAsync(step.InputPath, target, HasResolution(step.InputPath) ? null : Dpi, cancellationToken);- File.Move(target + ".pdf", output, overwrite: true);+ // A copy with a plain name: the picture's own folder and name may not be readable for Tesseract (Run).+ var picture = "picture" + Path.GetExtension(step.InputPath).ToLowerInvariant();+ File.Copy(step.InputPath, Path.Combine(scratch, picture));+ await ocr.ToPdfAsync(picture, "page", HasResolution(step.InputPath) ? null : Dpi, cancellationToken);+ File.Move(Path.Combine(scratch, "page.pdf"), output, overwrite: true);}progress?.Report(1);return [output];@@ -136,6 +138,8 @@ public sealed class TesseractConverter : IConverterusing var free = new SemaphoreSlim(Runs);var runs = new List<Task>();var done = 0;+ try+ {foreach (var page in scans){await free.WaitAsync(cancellationToken);@@ -154,14 +158,13 @@ public sealed class TesseractConverter : IConvertercatch{free.Release();- await Task.WhenAll(runs).ContinueWith(_ => { }, TaskScheduler.Default); // let the running pages end firstthrow;}runs.Add(Task.Run(async () =>{try{- await ocr.ToPdfAsync(picture, Path.Combine(scratch, $"p{page}"), Dpi, cancellationToken);+ await ocr.ToPdfAsync($"p{page}.png", $"p{page}", Dpi, cancellationToken);File.Delete(picture);progress?.Report(0.05 + 0.9 * Interlocked.Increment(ref done) / scans.Count);}@@ -171,15 +174,25 @@ public sealed class TesseractConverter : IConverter}}, cancellationToken));}+ }+ finally+ {+ // Rendering stopped (a failed page, a cancel): the running pages end before the work folder can go.+ await Task.WhenAll(runs).ContinueWith(_ => { }, TaskScheduler.Default);+ }await Task.WhenAll(runs);- // A new document: the pages and the document information come along, bookmarks and form fields don't.+ // A new document: the pages and the document information come along (not when the preset removes metadata),+ // bookmarks and form fields don't.using var source = PdfSharpConverter.Open(step.InputPath, PdfDocumentOpenMode.Import);using var result = new PdfDocument();- result.Info.Title = source.Info.Title;- result.Info.Author = source.Info.Author;- result.Info.Subject = source.Info.Subject;- result.Info.Keywords = source.Info.Keywords;+ if (!step.Preset.Pdf.RemoveMetadata)+ {+ result.Info.Title = source.Info.Title;+ result.Info.Author = source.Info.Author;+ result.Info.Subject = source.Info.Subject;+ result.Info.Keywords = source.Info.Keywords;+ }var recognized = new List<PdfDocument>();try{@@ -254,18 +267,22 @@ public sealed class TesseractConverter : IConverter}}- /// <summary>One language, one Tesseract.</summary>- private sealed record Run(string Executable, string Tessdata, string Language)+ /// <summary>+ /// One language, one Tesseract, working in <paramref name="Folder"/> (the job's scratch folder, with the language in+ /// its <c>tessdata</c>). Tesseract only ever sees plain relative names there: some Windows builds of Tesseract and+ /// Leptonica open files through the ANSI code page and fail on a user name or folder in Korean or Cyrillic.+ /// </summary>+ private sealed record Run(string Executable, string Folder, string Language){- /// <summary>Writes <c>{outputBase}.pdf</c>.</summary>+ /// <summary>Reads <paramref name="input"/> and writes <c>{outputBase}.pdf</c>, both names in <see cref="Folder"/>.</summary>public async Task ToPdfAsync(string input, string outputBase, int? dpi, CancellationToken cancellationToken){// tessedit_create_pdf instead of the "pdf" config file, which lives in Tesseract's own tessdata folder.- List<string> args = [input, outputBase, "--tessdata-dir", Tessdata, "-l", Language, "-c", "tessedit_create_pdf=1"];+ List<string> args = [input, outputBase, "--tessdata-dir", "tessdata", "-l", Language, "-c", "tessedit_create_pdf=1"];if (dpi is { } value)args.AddRange(["--dpi", value.ToString(System.Globalization.CultureInfo.InvariantCulture)]);- var result = await ProcessRunner.RunAsync(Executable, args, Timeout, cancellationToken, environment: Environment);- if (result.ExitCode != 0 || !File.Exists(outputBase + ".pdf"))+ var result = await ProcessRunner.RunAsync(Executable, args, Timeout, cancellationToken, workingDirectory: Folder, environment: Environment);+ if (result.ExitCode != 0 || !File.Exists(Path.Combine(Folder, outputBase + ".pdf")))throw new InvalidOperationException($"Tesseract failed (exit {result.ExitCode}). {result.StandardError.Trim()}");}}diff --git a/tests/Filee.App.Tests/RenderTests.cs b/tests/Filee.App.Tests/RenderTests.csindex cffc0059..eb6346d9 100644--- a/tests/Filee.App.Tests/RenderTests.cs+++ b/tests/Filee.App.Tests/RenderTests.cs@@ -1194,6 +1194,36 @@ public class RenderTestsAssert.False(preset.Pdf.Ocr);}+ [AvaloniaFact]+ public void Preset_editor_hides_and_clears_what_text_recognition_does_not_do()+ {+ TestServices.EnsureInitialized("en");+ var loc = AppHost.Get<Filee.Core.Localization.ILocalizer>();+ var preset = new Filee.Core.Presets.Preset+ {+ TargetFormat = "pdf", Name = "Scans",+ Pdf = { SplitPages = true, Compression = Filee.Core.Presets.PdfCompression.Screen, PageSize = Filee.Core.Presets.PdfPageSize.A4, MarginMm = 10 },+ };+ var vm = new PresetEditorViewModel(preset, loc);+ Assert.True(vm.ShowPdfSplit && vm.ShowPdfCompression && vm.ShowPageSize);++ vm.Ocr = true;++ Assert.False(vm.ShowPdfSplit);+ Assert.False(vm.ShowPdfCompression);+ Assert.False(vm.ShowPdfCompressionOptions);+ Assert.False(vm.ShowPageSize);+ Assert.True(vm.Apply());+ var pdf = preset.Pdf;+ Assert.True(pdf.Ocr);+ Assert.Equal((false, Filee.Core.Presets.PdfCompression.None, Filee.Core.Presets.PdfPageSize.FitImage, 0.0),+ (pdf.SplitPages, pdf.Compression, pdf.PageSize, pdf.MarginMm));++ // Without it they are back.+ vm.Ocr = false;+ Assert.True(vm.ShowPdfSplit && vm.ShowPdfCompression && vm.ShowPageSize);+ }+[AvaloniaFact]public void Preset_editor_keeps_rotation_visible_for_pdf_and_drops_it_for_targets_that_cannot_turn(){diff --git a/tests/Filee.Engines.Tests/OcrTests.cs b/tests/Filee.Engines.Tests/OcrTests.csindex b8582e33..ea0d514b 100644--- a/tests/Filee.Engines.Tests/OcrTests.cs+++ b/tests/Filee.Engines.Tests/OcrTests.cs@@ -149,6 +149,51 @@ public class OcrTests(EngineFixture fx) : IClassFixture<EngineFixture>Assert.Contains("searchable", text);}+ [Fact]+ public async Task Pictures_and_scans_with_korean_and_cyrillic_names_are_recognized()+ {+ Assert.SkipUnless(Installed, Skip);+ var folder = Path.Combine(fx.NewFolder(), "김민수 Документы");+ Directory.CreateDirectory(folder);+ var picture = Scan(folder, "스캔.png", "Filee reads scanned pages");+ var scanPdf = (await fx.ConvertAsync([Scan(folder, "Скан.png", "The quarterly report is ready")], new Preset { TargetFormat = "pdf" })).Outputs.Single();++ var job = await fx.ConvertAsync([picture, scanPdf], Ocr("pdf"));++ AssertDone(job);+ Assert.Contains("scanned", PageText(job.Outputs.Single(o => Path.GetFileName(o).StartsWith("스캔", StringComparison.Ordinal)), 1));+ Assert.Contains("quarterly", PageText(job.Outputs.Single(o => Path.GetFileName(o).StartsWith("Скан", StringComparison.Ordinal)), 1));+ }++ [Fact]+ public async Task Removing_metadata_leaves_the_document_information_behind()+ {+ Assert.SkipUnless(Installed, Skip);+ var folder = fx.NewFolder();+ var scanPdf = (await fx.ConvertAsync([Scan(folder, "scan.png", "Private letter")], new Preset { TargetFormat = "pdf" })).Outputs.Single();+ using (var document = PdfReader.Open(scanPdf, PdfDocumentOpenMode.Modify))+ {+ document.Info.Title = "Secret title";+ document.Info.Author = "Someone";+ document.Save(scanPdf);+ }+ var keep = Ocr("pdf");+ var remove = Ocr("pdf");+ remove.Pdf.RemoveMetadata = true;++ var kept = (await fx.ConvertAsync([scanPdf], keep)).Outputs.Single();+ var removed = (await fx.ConvertAsync([scanPdf], remove)).Outputs.Single();++ using (var document = PdfReader.Open(kept, PdfDocumentOpenMode.Import))+ Assert.Equal("Secret title", document.Info.Title);+ using (var document = PdfReader.Open(removed, PdfDocumentOpenMode.Import))+ {+ Assert.Equal("", document.Info.Title);+ Assert.Equal("", document.Info.Author);+ }+ Assert.Contains("Private", PageText(removed, 1));+ }+[Fact]public async Task A_scanned_pdf_becomes_a_docx_with_its_text_and_without_the_scan(){