Document Library for UWP
形式固有のフィルタを使用した PDF のエクスポート
PdfDocumentSource for UWP > 機能 > PDF のエクスポート > 形式固有のフィルタを使用した PDF のエクスポート

PdfDocumentSource では、C1DocumentSource クラスから継承された Export メソッドを使用して、PDF ファイルを外部形式にエクスポートできます。

PDF を HTML 形式にエクスポートするには

  1. PDF をエクスポートするためのボタンコントロールをデザインビューに追加します。
  2. コードビューに切り替え、コードビューで次の名前空間を追加します。
    Imports C1.Xaml.Document
    Imports C1.Xaml.Document.Export
    
    using C1.Xaml.Document;
    using C1.Xaml.Document.Export;
    
  3. プロジェクトに PDF ファイルを追加します。この例では、DefaultDocument.pdf という PDF ファイルを使用します。
  4. 次のコードを使用してC1PDFDocumentSource クラスのインスタンスを初期化します。
    Dim pds As New C1PdfDocumentSource()
    
    C1PdfDocumentSource pds = new C1PdfDocumentSource();
    
  5. LoadFromFileAsync メソッドを使用して、PDfファイルをC1PdfDocumentSourceのオブジェクトにロードします。
    Dim fileName As String = Nothing
    
    sf = Await StorageFile.GetFileFromApplicationUriAsync(New Uri _
         ("ms-appx:///DefaultDocument.pdf"))
    Await pds.LoadFromFileAsync(sf)
    fileName = Path.GetFileName(sf.Name)
    
    string fileName = null;
    
    sf = await StorageFile.GetFileFromApplicationUriAsync(
         new Uri("ms-appx:///DefaultDocument.pdf"));
    await pds.LoadFromFileAsync(sf);
    fileName = Path.GetFileName(sf.Name);
    
  6. 次のコードをボタンのクリックイベントに追加し、HtmlFilterクラスを使用して PDF を HTML 形式にエクスポートします。
    Try
        'HTMLFilterオブジェクトを作成します
        'HtmlFilter filter = new HtmlFilter();
        Dim filter As New RtfFilter()
        filter.ShowOptions = False
    
        Dim storageFolder As StorageFolder = ApplicationData.Current.LocalFolder
    
        'ファイルを作成します
        Dim FileForWrite As StorageFile = Await storageFolder.CreateFileAsync("TestFile.rtf", _
                                          CreationCollisionOption.ReplaceExisting)
    
        '出力するファイルの名前を指定します
        filter.StorageFile = FileForWrite
    
        'PDFへエクスポートします
    
    
        Await pds.ExportAsync(filter)
    Catch ex As Exception
        Dim md As New MessageDialog(String.Format("エクスポートに失敗しました", ex.Message), "エラー")
        Await md.ShowAsync()
    End Try
    
    try
    {
        //HTMLFilterオブジェクトを作成します
        RtfFilter filter = new RtfFilter();
        filter.ShowOptions = false;
    
        StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
    
        //ファイルを作成します
        StorageFile FileForWrite = await storageFolder.CreateFileAsync("TestFile.rtf",
                                   CreationCollisionOption.ReplaceExisting);
    
        //出力するファイルの名前を指定します
        filter.StorageFile = FileForWrite;
    
        //PDFへエクスポートします
        await pds.ExportAsync(filter);    
    }
    catch (Exception ex)
    {
        MessageDialog md = new MessageDialog(string.Format("エクスポートに失敗しました", 
                           ex.Message), "エラー");
        await md.ShowAsync();
    }
    

PDF を画像ファイル形式にエクスポートするには

上と同様のコードを使用して、サポートされているいずれかの画像形式(JPEG、PNG、TIFF など)で PDF ドキュメントを一連のページ画像ファイルにエクスポートすることができます。ページ画像を含む単一の ZIP ファイルを作成することもできます。次のコードは、画像形式フィルタクラスの 1 つ JpegFilter を使用して、複数ページから成るファイルを JPEG 形式にエクスポートし、エクスポートされた画像から成る 1 つの ZIP ファイルを作成します。

Try
    'JpegFilterオブジェクトを作成します
    Dim jpgfilter As New JpegFilter()
    jpgfilter.UseZipForMultipleFiles = True
    jpgfilter.ShowOptions = False


    Dim storageFolder As StorageFolder = ApplicationData.Current.LocalFolder

    'ファイルを作成します
    Dim file As StorageFile = Await storageFolder.CreateFileAsync("TestFile.zip", 
                              CreationCollisionOption.ReplaceExisting)

    '出力するファイルの名前を指定します
    jpgfilter.StorageFile = file


    'PDFへエクスポートします
    Await pds.ExportAsync(jpgfilter)
Catch ex As Exception
    Dim md As New MessageDialog(String.Format("エクスポートに失敗しました", 
                                                _ex.Message), "エラー")
    Await md.ShowAsync()
End Try
try
{
    //JpegFilterオブジェクトを作成します
    JpegFilter jpgfilter = new JpegFilter();
    jpgfilter.UseZipForMultipleFiles = true;
    jpgfilter.ShowOptions = false;


    StorageFolder storageFolder = ApplicationData.Current.LocalFolder;

    //ファイルを作成します
    StorageFile file = await storageFolder.CreateFileAsync("TestFile.zip",
                       CreationCollisionOption.ReplaceExisting);

    //出力するファイルの名前を指定します
    jpgfilter.StorageFile = file;
            

//PDFへエクスポートします
await pds.ExportAsync(jpgfilter);
}
catch (Exception ex)
{
    MessageDialog md = new MessageDialog(string.Format("エクスポートに失敗しました", 
                                                 _ex.Message), "エラー");
    await md.ShowAsync();
}
関連トピック