Skip to content

Commit 6e0f290

Browse files
CopilotKSemenenko
andcommitted
Fix PdfConverter.cs: Remove unnecessary Task.Run usage
Co-authored-by: KSemenenko <4385716+KSemenenko@users.noreply.github.com>
1 parent 4892a7c commit 6e0f290

1 file changed

Lines changed: 36 additions & 42 deletions

File tree

src/MarkItDown/Converters/PdfConverter.cs

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -271,33 +271,30 @@ private sealed class PdfPigTextExtractor : IPdfTextExtractor
271271
{
272272
public Task<string> ExtractTextAsync(byte[] pdfBytes, CancellationToken cancellationToken)
273273
{
274-
return Task.Run(() =>
275-
{
276-
var builder = new StringBuilder();
277-
278-
using var pdfDocument = PdfDocument.Open(pdfBytes);
274+
var builder = new StringBuilder();
279275

280-
for (var pageNumber = 1; pageNumber <= pdfDocument.NumberOfPages; pageNumber++)
281-
{
282-
cancellationToken.ThrowIfCancellationRequested();
283-
var page = pdfDocument.GetPage(pageNumber);
284-
var pageText = page.Text;
276+
using var pdfDocument = PdfDocument.Open(pdfBytes);
285277

286-
if (string.IsNullOrWhiteSpace(pageText))
287-
{
288-
continue;
289-
}
278+
for (var pageNumber = 1; pageNumber <= pdfDocument.NumberOfPages; pageNumber++)
279+
{
280+
cancellationToken.ThrowIfCancellationRequested();
281+
var page = pdfDocument.GetPage(pageNumber);
282+
var pageText = page.Text;
290283

291-
if (builder.Length > 0)
292-
{
293-
builder.AppendLine("\n---\n");
294-
}
284+
if (string.IsNullOrWhiteSpace(pageText))
285+
{
286+
continue;
287+
}
295288

296-
builder.AppendLine(pageText.Trim());
289+
if (builder.Length > 0)
290+
{
291+
builder.AppendLine("\n---\n");
297292
}
298293

299-
return builder.ToString();
300-
}, cancellationToken);
294+
builder.AppendLine(pageText.Trim());
295+
}
296+
297+
return Task.FromResult(builder.ToString());
301298
}
302299
}
303300

@@ -322,34 +319,31 @@ public Task<IReadOnlyList<string>> RenderImagesAsync(byte[] pdfBytes, Cancellati
322319
[SupportedOSPlatform("ios")]
323320
private static Task<IReadOnlyList<string>> RenderOnSupportedPlatformsAsync(byte[] pdfBytes, CancellationToken cancellationToken)
324321
{
325-
return Task.Run(() =>
322+
var images = new List<string>();
323+
var options = new RenderOptions
326324
{
327-
var images = new List<string>();
328-
var options = new RenderOptions
329-
{
330-
Dpi = 144,
331-
WithAnnotations = true,
332-
WithAspectRatio = true,
333-
AntiAliasing = PdfAntiAliasing.All,
334-
};
325+
Dpi = 144,
326+
WithAnnotations = true,
327+
WithAspectRatio = true,
328+
AntiAliasing = PdfAntiAliasing.All,
329+
};
335330

336331
#pragma warning disable CA1416
337-
foreach (var bitmap in Conversion.ToImages(pdfBytes, password: null, options))
332+
foreach (var bitmap in Conversion.ToImages(pdfBytes, password: null, options))
333+
{
334+
cancellationToken.ThrowIfCancellationRequested();
335+
using var bmp = bitmap;
336+
using var data = bmp.Encode(SKEncodedImageFormat.Png, quality: 90);
337+
if (data is null)
338338
{
339-
cancellationToken.ThrowIfCancellationRequested();
340-
using var bmp = bitmap;
341-
using var data = bmp.Encode(SKEncodedImageFormat.Png, quality: 90);
342-
if (data is null)
343-
{
344-
continue;
345-
}
346-
347-
images.Add(Convert.ToBase64String(data.Span));
339+
continue;
348340
}
341+
342+
images.Add(Convert.ToBase64String(data.Span));
343+
}
349344
#pragma warning restore CA1416
350345

351-
return (IReadOnlyList<string>)images;
352-
}, cancellationToken);
346+
return Task.FromResult<IReadOnlyList<string>>(images);
353347
}
354348
}
355349
}

0 commit comments

Comments
 (0)