Skip to content

Commit

Permalink
Add missing ToImages
Browse files Browse the repository at this point in the history
  • Loading branch information
sungaila committed Jul 24, 2024
1 parent 995c67e commit 7a2b28d
Show file tree
Hide file tree
Showing 22 changed files with 277 additions and 178 deletions.
42 changes: 39 additions & 3 deletions src/PDFtoImage/Conversion.Base64.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using PDFtoImage.Internals;
using SkiaSharp;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;

#if NET6_0_OR_GREATER
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -74,6 +72,25 @@ public static IEnumerable<SKBitmap> ToImages(string pdfAsBase64String, string? p
}
}

/// <summary>
/// Renders all pages of a given PDF into images.
/// </summary>
/// <param name="pdfAsBase64String">The PDF encoded as Base64.</param>
/// <param name="pages">The specific pages to be converted.</param>
/// <param name="password">The password for opening the PDF. Use <see langword="null"/> if no password is needed.</param>
/// <param name="options">Additional options for PDF rendering.</param>
/// <returns>The rendered PDF pages as images.</returns>
public static IEnumerable<SKBitmap> ToImages(string pdfAsBase64String, IEnumerable<int> pages, string? password = null, RenderOptions options = default)
{
if (pdfAsBase64String == null)
throw new ArgumentNullException(nameof(pdfAsBase64String));

foreach (var image in ToImages(Convert.FromBase64String(pdfAsBase64String), pages, password, options))
{
yield return image;
}
}

#if NET6_0_OR_GREATER
/// <summary>
/// Returns the PDF page size for a given page number.
Expand Down Expand Up @@ -184,6 +201,25 @@ public static SKBitmap ToImage(string pdfAsBase64String, Index page, string? pas
return ToImage(Convert.FromBase64String(pdfAsBase64String), page, password, options);
}

/// <summary>
/// Renders a range of pages of a given PDF into images.
/// </summary>
/// <param name="pdfAsBase64String">The PDF encoded as Base64.</param>
/// <param name="pages">The specific pages to be converted.</param>
/// <param name="password">The password for opening the PDF. Use <see langword="null"/> if no password is needed.</param>
/// <param name="options">Additional options for PDF rendering.</param>
/// <returns>The rendered PDF pages as images.</returns>
public static IEnumerable<SKBitmap> ToImages(string pdfAsBase64String, Range pages, string? password = null, RenderOptions options = default)
{
if (pdfAsBase64String == null)
throw new ArgumentNullException(nameof(pdfAsBase64String));

foreach (var image in ToImages(Convert.FromBase64String(pdfAsBase64String), pages, password, options))
{
yield return image;
}
}

/// <summary>
/// Renders a range of pages of a given PDF into images.
/// </summary>
Expand Down
48 changes: 45 additions & 3 deletions src/PDFtoImage/Conversion.ByteArray.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using PDFtoImage.Internals;
using SkiaSharp;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;

#if NET6_0_OR_GREATER
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -83,6 +81,28 @@ public static IEnumerable<SKBitmap> ToImages(byte[] pdfAsByteArray, string? pass
}
}

/// <summary>
/// Renders all pages of a given PDF into images.
/// </summary>
/// <param name="pdfAsByteArray">The PDF as a byte array.</param>
/// <param name="pages">The specific pages to be converted.</param>
/// <param name="password">The password for opening the PDF. Use <see langword="null"/> if no password is needed.</param>
/// <param name="options">Additional options for PDF rendering.</param>
/// <returns>The rendered PDF pages as images.</returns>
public static IEnumerable<SKBitmap> ToImages(byte[] pdfAsByteArray, IEnumerable<int> pages, string? password = null, RenderOptions options = default)
{
if (pdfAsByteArray == null)
throw new ArgumentNullException(nameof(pdfAsByteArray));

// Base64 string -> byte[] -> MemoryStream
using var pdfStream = new MemoryStream(pdfAsByteArray, false);

foreach (var image in ToImages(pdfStream, pages, false, password, options))
{
yield return image;
}
}

#if NET6_0_OR_GREATER
/// <summary>
/// Returns the PDF page size for a given page number.
Expand Down Expand Up @@ -199,6 +219,28 @@ public static SKBitmap ToImage(byte[] pdfAsByteArray, Index page, string? passwo
return ToImage(pdfStream, page, false, password, options);
}

/// <summary>
/// Renders a range of pages of a given PDF into images.
/// </summary>
/// <param name="pdfAsByteArray">The PDF as a byte array.</param>
/// <param name="pages">The specific pages to be converted.</param>
/// <param name="password">The password for opening the PDF. Use <see langword="null"/> if no password is needed.</param>
/// <param name="options">Additional options for PDF rendering.</param>
/// <returns>The rendered PDF pages as images.</returns>
public static IEnumerable<SKBitmap> ToImages(byte[] pdfAsByteArray, Range pages, string? password = null, RenderOptions options = default)
{
if (pdfAsByteArray == null)
throw new ArgumentNullException(nameof(pdfAsByteArray));

// Base64 string -> byte[] -> MemoryStream
using var pdfStream = new MemoryStream(pdfAsByteArray, false);

foreach (var image in ToImages(pdfStream, pages, false, password, options))
{
yield return image;
}
}

/// <summary>
/// Renders a range of pages of a given PDF into images.
/// </summary>
Expand Down
65 changes: 62 additions & 3 deletions src/PDFtoImage/Conversion.Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@ public static IEnumerable<SKBitmap> ToImages(Stream pdfStream, bool leaveOpen =
return ToImagesImpl(pdfStream, leaveOpen, password, options, null);
}

/// <summary>
/// Renders a selection of pages of a given PDF into images.
/// </summary>
/// <param name="pdfStream">The PDF as a stream.</param>
/// <param name="pages">The specific pages to be converted.</param>
/// <param name="leaveOpen"><see langword="true"/> to leave the <paramref name="pdfStream"/> open after the PDF document is loaded; otherwise, <see langword="false"/>.</param>
/// <param name="password">The password for opening the PDF. Use <see langword="null"/> if no password is needed.</param>
/// <param name="options">Additional options for PDF rendering.</param>
/// <returns>The rendered PDF pages as images.</returns>
public static IEnumerable<SKBitmap> ToImages(Stream pdfStream, IEnumerable<int> pages, bool leaveOpen = false, string? password = null, RenderOptions options = default)
{
if (pdfStream == null)
throw new ArgumentNullException(nameof(pdfStream));

// Stream -> Internals.PdfDocument
using var pdfDocument = PdfDocument.Load(pdfStream, password, !leaveOpen);

var pageCount = pdfDocument.PageSizes.Count;

if (pages.Any(p => p >= pageCount))
throw new ArgumentOutOfRangeException(nameof(pages), $"The page numbers must be between 0 and {pageCount - 1}. The PDF has {pageCount} pages in total.");

foreach (var bitmap in ToImagesImpl(pdfStream, leaveOpen, password, options, pages))
{
yield return bitmap;
}
}

#if NET6_0_OR_GREATER
/// <summary>
/// Renders a single page of a given PDF and saves it as a JPEG.
Expand Down Expand Up @@ -215,10 +243,41 @@ public static SKBitmap ToImage(Stream pdfStream, Index page, bool leaveOpen = fa
/// <param name="pdfStream">The PDF as a stream.</param>
/// <param name="pages">The specific pages to be converted.</param>
/// <param name="leaveOpen"><see langword="true"/> to leave the <paramref name="pdfStream"/> open after the PDF document is loaded; otherwise, <see langword="false"/>.</param>
/// <param name="cancellationToken">The cancellation token to cancel the conversion. Please note that an ongoing rendering cannot be cancelled (the next page will not be rendered though).</param>
/// <param name="password">The password for opening the PDF. Use <see langword="null"/> if no password is needed.</param>
/// <param name="options">Additional options for PDF rendering.</param>
/// <returns>The rendered PDF pages as images.</returns>
public static IEnumerable<SKBitmap> ToImages(Stream pdfStream, Range pages, bool leaveOpen = false, string? password = null, RenderOptions options = default)
{
if (pdfStream == null)
throw new ArgumentNullException(nameof(pdfStream));

// Stream -> Internals.PdfDocument
using var pdfDocument = PdfDocument.Load(pdfStream, password, !leaveOpen);

var pageCount = pdfDocument.PageSizes.Count;
var (offset, length) = pages.GetOffsetAndLength(pageCount);

if (offset + length > pageCount)
throw new ArgumentOutOfRangeException(nameof(pages), $"The page numbers must be between 0 and {pageCount - 1}. The PDF has {pageCount} pages in total.");

var pageNumbers = Enumerable.Range(offset, length);

foreach (var bitmap in ToImagesImpl(pdfStream, leaveOpen, password, options, pageNumbers))
{
yield return bitmap;
}
}

/// <summary>
/// Renders a range of pages of a given PDF into images.
/// </summary>
/// <param name="pdfStream">The PDF as a stream.</param>
/// <param name="pages">The specific pages to be converted.</param>
/// <param name="leaveOpen"><see langword="true"/> to leave the <paramref name="pdfStream"/> open after the PDF document is loaded; otherwise, <see langword="false"/>.</param>
/// <param name="password">The password for opening the PDF. Use <see langword="null"/> if no password is needed.</param>
/// <param name="options">Additional options for PDF rendering.</param>
/// <param name="cancellationToken">The cancellation token to cancel the conversion. Please note that an ongoing rendering cannot be cancelled (the next page will not be rendered though).</param>
/// <returns>The rendered PDF pages as images.</returns>
public static async IAsyncEnumerable<SKBitmap> ToImagesAsync(Stream pdfStream, Range pages, bool leaveOpen = false, string? password = null, RenderOptions options = default, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
if (pdfStream == null)
Expand Down Expand Up @@ -247,9 +306,9 @@ public static async IAsyncEnumerable<SKBitmap> ToImagesAsync(Stream pdfStream, R
/// <param name="pdfStream">The PDF as a stream.</param>
/// <param name="pages">The specific pages to be converted.</param>
/// <param name="leaveOpen"><see langword="true"/> to leave the <paramref name="pdfStream"/> open after the PDF document is loaded; otherwise, <see langword="false"/>.</param>
/// <param name="cancellationToken">The cancellation token to cancel the conversion. Please note that an ongoing rendering cannot be cancelled (the next page will not be rendered though).</param>
/// <param name="password">The password for opening the PDF. Use <see langword="null"/> if no password is needed.</param>
/// <param name="options">Additional options for PDF rendering.</param>
/// <param name="cancellationToken">The cancellation token to cancel the conversion. Please note that an ongoing rendering cannot be cancelled (the next page will not be rendered though).</param>
/// <returns>The rendered PDF pages as images.</returns>
public static async IAsyncEnumerable<SKBitmap> ToImagesAsync(Stream pdfStream, IEnumerable<int> pages, bool leaveOpen = false, string? password = null, RenderOptions options = default, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
Expand All @@ -275,9 +334,9 @@ public static async IAsyncEnumerable<SKBitmap> ToImagesAsync(Stream pdfStream, I
/// </summary>
/// <param name="pdfStream">The PDF as a stream.</param>
/// <param name="leaveOpen"><see langword="true"/> to leave the <paramref name="pdfStream"/> open after the PDF document is loaded; otherwise, <see langword="false"/>.</param>
/// <param name="cancellationToken">The cancellation token to cancel the conversion. Please note that an ongoing rendering cannot be cancelled (the next page will not be rendered though).</param>
/// <param name="password">The password for opening the PDF. Use <see langword="null"/> if no password is needed.</param>
/// <param name="options">Additional options for PDF rendering.</param>
/// <param name="cancellationToken">The cancellation token to cancel the conversion. Please note that an ongoing rendering cannot be cancelled (the next page will not be rendered though).</param>
/// <returns>The rendered PDF pages as images.</returns>
public static async IAsyncEnumerable<SKBitmap> ToImagesAsync(Stream pdfStream, bool leaveOpen = false, string? password = null, RenderOptions options = default, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
Expand Down
Loading

0 comments on commit 7a2b28d

Please sign in to comment.