Skip to content

Commit

Permalink
The native PDFium image is chosen by process architecture; added page…
Browse files Browse the repository at this point in the history
… selection for the console tool
  • Loading branch information
sungaila committed May 29, 2021
1 parent 510bca8 commit 60c8fd8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
35 changes: 26 additions & 9 deletions src/Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static int Main(string[] args)

try
{
ParseArguments(args, out string? inputPath, out string? outputPath, out int dpi);
ParseArguments(args, out string? inputPath, out string? outputPath, out int page, out int dpi);

if (inputPath == null)
throw new InvalidOperationException("There is no PDF file path.");
Expand All @@ -33,21 +33,21 @@ static int Main(string[] args)
switch (Path.GetExtension(outputPath).ToLower())
{
case ".bmp":
Conversion.SaveBmp(outputPath, inputStream, dpi: dpi);
Conversion.SaveBmp(outputPath, inputStream, page: page - 1, dpi: dpi);
break;
case ".png":
Conversion.SavePng(outputPath, inputStream, dpi: dpi);
Conversion.SavePng(outputPath, inputStream, page: page - 1, dpi: dpi);
break;
case ".gif":
Conversion.SaveGif(outputPath, inputStream, dpi: dpi);
Conversion.SaveGif(outputPath, inputStream, page: page - 1, dpi: dpi);
break;
case ".jpg":
case ".jpeg":
Conversion.SaveJpeg(outputPath, inputStream, dpi: dpi);
Conversion.SaveJpeg(outputPath, inputStream, page: page - 1, dpi: dpi);
break;
case ".tif":
case ".tiff":
Conversion.SaveTiff(outputPath, inputStream, dpi: dpi);
Conversion.SaveTiff(outputPath, inputStream, page: page - 1, dpi: dpi);
break;
default:
throw new InvalidOperationException("Only the following file extensions are supported: bmp, png, gif, jpg/jpeg and tif/tiff.");
Expand All @@ -64,7 +64,7 @@ static int Main(string[] args)
return 0;
}

private static void ParseArguments(string[] args, out string? inputPath, out string? outputPath, out int dpi)
private static void ParseArguments(string[] args, out string? inputPath, out string? outputPath, out int page, out int dpi)
{
if (args == null)
throw new ArgumentNullException(nameof(args));
Expand Down Expand Up @@ -99,17 +99,34 @@ private static void ParseArguments(string[] args, out string? inputPath, out str

outputPath = outputPath.Trim('\"');

dpi = 300;
page = 1;

if (args.Length >= 3)
{
outputPath = args[2];
}
else
{
System.Console.Write("Enter PDF page number: ");

if (!int.TryParse(System.Console.ReadLine(), out page) || page <= 0)
{
page = 1;
System.Console.WriteLine($"PDF page number defaulting to {page}.");
}
}

dpi = 300;

if (args.Length >= 4)
{
outputPath = args[3];
}
else
{
System.Console.Write("Enter the target resolution in DPI: ");

if (!int.TryParse(System.Console.ReadLine(), out dpi))
if (!int.TryParse(System.Console.ReadLine(), out dpi) || dpi <= 0)
{
dpi = 300;
System.Console.WriteLine($"Target DPI defaulting to {dpi}.");
Expand Down
4 changes: 2 additions & 2 deletions src/PDFtoImage/PDFtoImage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<!-- NuGet -->
<PropertyGroup>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionPrefix>1.0.1</VersionPrefix>
<VersionSuffix></VersionSuffix>
<Authors>David Sungaila</Authors>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
Expand All @@ -21,7 +21,7 @@
<PackageProjectUrl>https://github.com/sungaila/PDFtoImage</PackageProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/sungaila/PDFtoImage/master/etc/Icon_128.png</PackageIconUrl>
<Description>A .NET library to render PDF files into images.</Description>
<PackageReleaseNotes>Initial release</PackageReleaseNotes>
<PackageReleaseNotes>Fixed an issue that caused the OS architecture to be evaluated instead of the process architecture.</PackageReleaseNotes>
<PackageTags>PDF Bitmap Image Convert Conversion C# PDFium</PackageTags>
<RepositoryUrl>https://github.com/sungaila/PDFtoImage.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down
6 changes: 3 additions & 3 deletions src/PDFtoImage/PdfiumViewer/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private static bool LoadNativeLibrary(string path)
#if NETCOREAPP3_0_OR_GREATER
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
_pdfiumLibPath = Path.Combine(_pdfiumLibPath, (RuntimeInformation.OSArchitecture) switch
_pdfiumLibPath = Path.Combine(_pdfiumLibPath, (RuntimeInformation.ProcessArchitecture) switch
{
Architecture.X86 => "win-x86",
Architecture.X64 => "win-x64",
Expand All @@ -36,7 +36,7 @@ private static bool LoadNativeLibrary(string path)
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
_pdfiumLibPath = Path.Combine(_pdfiumLibPath, (RuntimeInformation.OSArchitecture) switch
_pdfiumLibPath = Path.Combine(_pdfiumLibPath, (RuntimeInformation.ProcessArchitecture) switch
{
Architecture.X64 => "linux-x64",
_ => throw new PlatformNotSupportedException("Only x86-64 is supported on Linux.")
Expand All @@ -46,7 +46,7 @@ private static bool LoadNativeLibrary(string path)
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
_pdfiumLibPath = Path.Combine(_pdfiumLibPath, (RuntimeInformation.OSArchitecture) switch
_pdfiumLibPath = Path.Combine(_pdfiumLibPath, (RuntimeInformation.ProcessArchitecture) switch
{
Architecture.X64 => "osx-x64",
Architecture.Arm64 => "osx-arm64",
Expand Down
6 changes: 3 additions & 3 deletions src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,8 @@

<!-- NuGet Icon -->
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.4" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.4" />
</ItemGroup>
</Project>

0 comments on commit 60c8fd8

Please sign in to comment.