Document Library for UWP
ExportProvider を使用した PDF のエクスポート
PdfDocumentSource for UWP > 機能 > PDF のエクスポート > ExportProvider を使用した PDF のエクスポート

PdfDocumentSource では、SupportedExportProviders プロパティを使用して、ドキュメントのサポートされているエクスポート形式を列挙することができます。このプロパティは ExportProvider クラスのコレクションを返します。これには、サポートされている形式に関する情報が含まれます。また、 ExportProvider クラスの NewExporterメソッドを使用して、サポートされている形式に対応するエクスポートフィルタを作成することができます。

サポートされるエクスポート形式のセットはドキュメントタイプによって異なるため、正しい結果を得るには、SupportedExportProviders によってエクスポートフィルタを列挙および作成してください。

サポートされているエクスポータを使用して PDF をエクスポートするには

  1. ComboBox コントロールをツールボックスからフォームにドラッグアンドドロップします。
  2. コードビューに切り替え、コードビューで次の名前空間を追加します。
    Imports C1.Xaml.Document
    Imports Windows.UI.Popups
    Imports C1.Xaml.Document.Export
    Imports Windows.Storage.Pickers
    Imports Windows.Storage
    
    using C1.Xaml.Document;
    using Windows.UI.Popups;
    using C1.Xaml.Document.Export;
    using Windows.Storage.Pickers;
    using Windows.Storage;
    
  3. プロジェクトに PDF ファイルを追加します。この例では、製品サンプルにある DefaultDocument.pdf という PDF ファイルを使用します。
  4. C1PDFDocumentSourceのインスタンスを初期化し、次のコードを使用してStorageFileクラスのインスタンスを作成します。
    Dim pds As New C1PdfDocumentSource()
    Dim sf As StorageFile
    
    C1PdfDocumentSource pds = new C1PdfDocumentSource();
    StorageFile sf;
    
  5. LoadFromFileAsync メソッドを使用して、C1PdfDocumentSource のオブジェクトにPDFファイルをロードします。
    string fileName = null;
                
    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. 次のコードを InitializeComponent() メソッドの下に追加して、サポートされているエクスポータのリストを SupportedExportProviders プロパティを取得します。
    cbExporter.Items.Clear()
    Dim supportedProviders = pds.SupportedExportProviders
    For Each sep As var In supportedProviders
            cbExporter.Items.Add(sep.FormatName)
    Next
    cbExporter.SelectedIndex = 0
    
    cbExporter.Items.Clear();
    var supportedProviders = pds.SupportedExportProviders;
    foreach (var sep in supportedProviders)
        cbExporter.Items.Add(sep.FormatName);
    cbExporter.SelectedIndex = 0;
    
  7. 次のコードをボタンのクリックイベントに追加して、ExportAsync メソッドを使用したPDFファイルをエクスポートします。
    ' ExportFilterオブジェクトを作成します
    Dim ep As ExportProvider = pds.SupportedExportProviders(cbExporter.SelectedIndex)
    Dim ef As ExportFilter = TryCast(ep.NewExporter(), ExportFilter)
    
    If (TypeOf ef Is BmpFilter OrElse TypeOf ef Is JpegFilter OrElse TypeOf ef Is PngFilter OrElse TypeOf ef Is GifFilter) Then
      ' これらのエクスポートフィルタは、エクスポートするときに複数のファイルを作成します
      ' この場合はディレクトリを要求します
      If ef.UseZipForMultipleFiles = True Then
         ' zipファイルを要求します
         Dim fsp As New FileSavePicker()
         fsp.DefaultFileExtension = ".zip"
    
         fsp.SuggestedFileName = Path.GetFileNameWithoutExtension(fileName) + ".zip"
         ef.StorageFile = Await fsp.PickSaveFileAsync()
         If ef.StorageFile Is Nothing Then
            Return
         End If
    Else
    
         Dim fp As New FolderPicker()
         fp.FileTypeFilter.Add("." + ep.DefaultExtension)
         fp.FileTypeFilter.Add(".zip")
         ef.StorageFolder = Await fp.PickSingleFolderAsync()
         If ef.StorageFolder Is Nothing Then
            ' ユーザーがエクスポートをキャンセルします
            Return
         End If
       End If
    Else
    
       ' ファイルを要求します
       Dim fsp As New FileSavePicker()
       fsp.DefaultFileExtension = "." + ep.DefaultExtension
       fsp.FileTypeChoices.Add(ep.FormatName + " (." + ep.DefaultExtension + ")", New String() {"." + ep.DefaultExtension})
    
       fsp.SuggestedFileName = Path.GetFileNameWithoutExtension(fileName) + "." + ep.DefaultExtension
       ef.StorageFile = Await fsp.PickSaveFileAsync()
       If ef.StorageFile Is Nothing Then
          Return
       End If
    End If
    Try
       Await pds.ExportAsync(ef)
    Catch ex As Exception
       Dim md As New MessageDialog(String.Format("エクスポートに失敗しました", ex.Message), "エラー")
       Await md.ShowAsync()
    End Try
    
    // ExportFilterオブジェクトを作成します
    ExportProvider ep = pds.SupportedExportProviders[cbExporter.SelectedIndex];
    ExportFilter ef = ep.NewExporter() as ExportFilter;
    
    if ((ef is BmpFilter || ef is JpegFilter || ef is PngFilter || ef is GifFilter))
    {
        // これらのエクスポートフィルタは、エクスポートするときに複数のファイルを作成します
        // この場合はディレクトリを要求します
        if (ef.UseZipForMultipleFiles == true)
        {
          // zipファイルを要求します
          FileSavePicker fsp = new FileSavePicker();
          fsp.DefaultFileExtension = ".zip";
    
          fsp.SuggestedFileName = Path.GetFileNameWithoutExtension(fileName) + ".zip";
          ef.StorageFile = await fsp.PickSaveFileAsync();
          if (ef.StorageFile == null)
             return;
        }
    
        else
        {
            FolderPicker fp = new FolderPicker();
            fp.FileTypeFilter.Add("." + ep.DefaultExtension);
            fp.FileTypeFilter.Add(".zip");
            ef.StorageFolder = await fp.PickSingleFolderAsync();
            if (ef.StorageFolder == null)
                // ユーザーがエクスポートをキャンセルします
                return;
        }
    }
    else
    {
    
        // ファイルを要求します
        FileSavePicker fsp = new FileSavePicker();
        fsp.DefaultFileExtension = "." + ep.DefaultExtension;
        fsp.FileTypeChoices.Add(ep.FormatName + " (." + ep.DefaultExtension + ")",
            new string[] { "." + ep.DefaultExtension });
    
        fsp.SuggestedFileName = Path.GetFileNameWithoutExtension(fileName) + "." +
                                ep.DefaultExtension;
        ef.StorageFile = await fsp.PickSaveFileAsync();
        if (ef.StorageFile == null)
            return;
    }
    try
    {
        await pds.ExportAsync(ef);
    }
    catch (Exception ex)
    {
        MessageDialog md = new MessageDialog(string.Format("エクスポートに失敗しました", 
                                             ex.Message), "エラー");
        await md.ShowAsync();
    }
    
関連トピック