C#/WinRT - Windows.Data.Pdfを使ってみた の続編になります。
コンパイル用バッチファイル
compile.bat ソースファイル名.cs
でコンパイル
compile.bat
csc /r:C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Runtime.WindowsRuntime\v4.0_4.0.0.0__b77a5c561934e089\system.runtime.windowsruntime.dll ^
/r:C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Runtime.InteropServices.WindowsRuntime\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Runtime.InteropServices.WindowsRuntime.dll ^
/r:C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Runtime\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Runtime.dll ^
/r:C:\Windows\Microsoft.NET\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0__31bf3856ad364e35\WindowsBase.dll ^
/r:C:\Windows\Microsoft.NET\assembly\GAC_64\PresentationCore\v4.0_4.0.0.0__31bf3856ad364e35\PresentationCore.dll ^
"/r:C:\Program Files (x86)\Windows Kits\8.1\References\CommonConfiguration\Neutral\Annotated\Windows.winmd" %*
ソースコード
PdfWinRT_Test.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using Windows.Data.Pdf;
namespace MyPdfTest
{
public class MyPdf
{
public static async Task<uint> LoadPdfFile(string fileName, int startPageIndex, int endPageIndex)
{
Windows.Storage.StorageFile pdfFile = await Windows.Storage.StorageFile.GetFileFromPathAsync(fileName);
uint t = LoadPdfDocumentAsync(pdfFile, startPageIndex, endPageIndex).Result;
return t;
}
static async Task<uint> LoadPdfDocumentAsync(Windows.Storage.StorageFile pdfFile, int startPageIndex, int endPageIndex)
{
var folderBase = await Windows.Storage.KnownFolders.DocumentsLibrary.CreateFolderAsync("PdfWinRT_Test", Windows.Storage.CreationCollisionOption.OpenIfExists);
var folder = await folderBase.CreateFolderAsync(System.IO.Path.GetFileNameWithoutExtension(pdfFile.Name), Windows.Storage.CreationCollisionOption.OpenIfExists);
PdfDocument _pdfDoc = await PdfDocument.LoadFromFileAsync(pdfFile);
Console.WriteLine("Destination: " + Windows.Storage.KnownFolders.DocumentsLibrary.Path);
for (int i=startPageIndex; i<_pdfDoc.PageCount && i<=endPageIndex; i++) {
Console.Write((i+1).ToString()+" / "+_pdfDoc.PageCount.ToString());
Console.Write("\r");
using (Windows.Data.Pdf.PdfPage pdfPage = _pdfDoc.GetPage((uint)i)) {
Windows.Storage.StorageFile file = await folder.CreateFileAsync("p"+(i+1).ToString("D4")+".png",Windows.Storage.CreationCollisionOption.ReplaceExisting);
Windows.Storage.Streams.IRandomAccessStream outputStream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
await pdfPage.RenderToStreamAsync(outputStream);
}
}
Console.Write("\n");
return _pdfDoc.PageCount;
}
static void ShowUsage()
{
Console.WriteLine("usage:");
Console.WriteLine("PdfWinRT_Test.exe pdfFilePath [page]");
Console.WriteLine("PdfWinRT_Test.exe pdfFilePath [page-page]");
}
[STAThread]
static void Main(string[] args)
{
int startPageIndex = 0;
int endPageIndex = Int32.MaxValue-1; // no limit
if ( args.Length == 0 || args.Length > 2 ) {
ShowUsage();
return;
}
if ( System.IO.Path.GetExtension(args[0]).ToLowerInvariant() != ".pdf" ) {
ShowUsage();
return;
}
if ( args.Length == 2 ) {
Regex r = new Regex(@"^([0-9]{1,6})-([0-9]{1,6})$");
Match m = r.Match(args[1]);
if ( m.Success ) {
startPageIndex = Convert.ToInt32(m.Groups[1].Value) - 1;
endPageIndex = Convert.ToInt32(m.Groups[2].Value) - 1;
}
else if ((new Regex(@"^([0-9]{1,6})$")).IsMatch(args[1])) {
startPageIndex = Convert.ToInt32(args[1]) - 1;
}
else {
ShowUsage();
return;
}
if ( startPageIndex < 0 ) { startPageIndex = 0; }
}
Task<uint> t = LoadPdfFile(System.IO.Path.GetFullPath(args[0]), startPageIndex, endPageIndex);
uint t2 = t.Result;
}
}
}