Android でプレゼンテーションを保存

概要

プレゼンテーションを作成したり、既存のプレゼンテーションを開く ときは、Presentation.save メソッドを使用して結果を書き込みます。Aspose.Slides for Android via Java は、PowerPoint、OpenDocument、PDF などの形式でプレゼンテーションをファイルまたはストリームに保存できます。以下のセクションでは、標準的な保存操作と PPTX 出力に利用できるオプションについて説明します。

ファイルへのプレゼンテーション保存

プレゼンテーションをファイルに保存するには、出力パスと SaveFormat の値を Presentation.save メソッドに渡します。format の値は、Aspose.Slides が作成するファイルの種類を決定します。

次の例はプレゼンテーションを作成し、PPTX ファイルとして保存します。

import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation();
try {
    // プレゼンテーションのコンテンツを追加または変更してください。

    presentation.save("Output.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

元の形式でプレゼンテーションを保存

ファイルとストリームの検出例、 新規作成プレゼンテーションの動作、 そしてソース形式と出力形式の区別については、元のプレゼンテーション形式を判別 を参照してください。

バッチ処理アプリケーションでは、入力形式が事前に分からないことがあります。ファイルを読み込んだ後、IPresentation.getSourceFormat メソッドで元の形式を取得します。得られた SourceFormat の値を SlideUtil.toSaveFormat に渡して対応する SaveFormat を取得し、Presentation.save で変更済みプレゼンテーションを書き込みます。

次の完全な例は、入力ディレクトリ内のすべてのファイルを処理し、タイトルを更新して、ロードされた形式のまま出力ディレクトリに保存します。

import com.aspose.slides.Presentation;
import com.aspose.slides.SlideUtil;
import java.io.File;

File inputDirectory = new File("Input");
File outputDirectory = new File("Output");

if (!outputDirectory.exists() && !outputDirectory.mkdirs()) {
    System.err.println("Cannot create the output directory.");
}

File[] inputFiles = inputDirectory.listFiles(File::isFile);
if (inputFiles != null && outputDirectory.isDirectory()) {
    for (File inputFile : inputFiles) {
        try {
            Presentation presentation = new Presentation(inputFile.getPath());
            try {
                int saveFormat = SlideUtil.toSaveFormat(presentation.getSourceFormat());
                presentation.getDocumentProperties().setTitle("Processed by the batch application");

                File outputFile = new File(outputDirectory, inputFile.getName());
                presentation.save(outputFile.getPath(), saveFormat);
            } finally {
                presentation.dispose();
            }
        } catch (IllegalArgumentException exception) {
            System.err.println("Cannot map the source format of '" + inputFile.getPath() + "': " + exception.getMessage());
        } catch (Exception exception) {
            System.err.println("Cannot process '" + inputFile.getPath() + "': " + exception.getMessage());
        }
    }
}

SlideUtil.toSaveFormat は PPT、PPTX、ODP、PPTM、PPSX、PPSM、POTX、POTM、PPS、POT、OTP、FODP、PowerPoint XML をそれぞれ対応するプレゼンテーション保存形式にマップします。これはプレゼンテーションのソース形式のみを対象とし、PDF、HTML、TIFF、画像などのエクスポート形式を選択するためのものではありません。サポートされていない、または無効な SourceFormat を渡すと、IllegalArgumentException がスローされます。

レガシーな PPT、PPS、POT ファイルは同じバイナリコンテナーを使用します。このようなプレゼンテーションを拡張子なしのストリームからロードした場合、PPS または POT ファイルが PPT として識別されることがあります。これらのレガシーサブタイプを保持する必要がある場合は、元のファイル名または形式メタデータを別途保持し、出力ファイル名と形式を選択するときに使用してください。

ストリームへのプレゼンテーション保存

最終的なファイルパスに依存せずにプレゼンテーションを書き込むには、書き込み可能なストリームと SaveFormat の値を Presentation.save メソッドに渡します。この方法は、出力を Web サービスから返す必要がある場合や、データベースに保存する場合、メモリ内で処理する場合に便利です。

次の例は新しいプレゼンテーションをファイルストリームに保存します。

import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import java.io.FileOutputStream;
import java.io.OutputStream;

Presentation presentation = new Presentation();
try {
    OutputStream outputStream = new FileOutputStream("Output.pptx");
    try {
        presentation.save(outputStream, SaveFormat.Pptx);
    } finally {
        outputStream.close();
    }
} finally {
    presentation.dispose();
}

事前定義されたビュータイプでプレゼンテーションを保存

保存されたプレゼンテーションが PowerPoint で最初に開くビューを指定できます。ViewProperties.setLastView メソッドに ViewType の値を渡してから保存してください。

次の例はスライドマスタービューを初期ビューとして設定します。

import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import com.aspose.slides.ViewType;

Presentation presentation = new Presentation();
try {
    presentation.getViewProperties().setLastView(ViewType.SlideMasterView);
    presentation.save("SlideMasterView.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

Strict Office Open XML 形式でプレゼンテーションを保存

Strict プロファイルの Office Open XML に準拠した PPTX ファイルを作成するには、PptxOptions インスタンスを作成し、setConformance メソッドに Conformance.Iso29500_2008_Strict を指定します。次にオプションを Presentation.save メソッドに渡します。

import com.aspose.slides.Conformance;
import com.aspose.slides.PptxOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

PptxOptions options = new PptxOptions();
options.setConformance(Conformance.Iso29500_2008_Strict);

Presentation presentation = new Presentation();
try {
    presentation.save("StrictOfficeOpenXml.pptx", SaveFormat.Pptx, options);
} finally {
    presentation.dispose();
}

Zip64 モードで Office Open XML 形式でプレゼンテーションを保存

標準の ZIP アーカイブは、各エントリの圧縮サイズ・非圧縮サイズ、アーカイブ全体のサイズ、エントリ数に制限があります。PPTX ファイルは ZIP アーカイブであるため、非常に大きなプレゼンテーションはこれらの制限を超えることがあります。ZIP64 拡張機能はサイズとエントリ数の上限を引き上げます。

PptxOptions.setZip64Mode メソッドで Aspose.Slides が ZIP64 拡張を書き込むかどうかを制御します。

  • IfNecessary はプレゼンテーションが標準 ZIP の制限を超える場合にのみ ZIP64 を使用します。これがデフォルトモードです。
  • Never は ZIP64 拡張を書き込みません。
  • Always は常に ZIP64 拡張を書き込みます。

次の例は出力プレゼンテーションに対して常に ZIP64 拡張を有効にします。

import com.aspose.slides.PptxOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import com.aspose.slides.Zip64Mode;

Presentation presentation = new Presentation("Sample.pptx");
try {
    PptxOptions options = new PptxOptions();
    options.setZip64Mode(Zip64Mode.Always);

    presentation.save("OutputZip64.pptx", SaveFormat.Pptx, options);
} finally {
    presentation.dispose();
}

圧縮レベルを指定して Office Open XML 形式でプレゼンテーションを保存

PPTX 出力では、PptxOptions.setCompressionLevel メソッドを使用して保存速度とファイルサイズのバランスを取れます。CompressionLevel クラスは次の値を提供します。

  • None は圧縮せずにデータを保存します。
  • Level1 は最速の圧縮で、圧縮後のサイズが最大になります。
  • Level2 から Level5 は、保存速度よりも小さな出力を優先します。
  • Level6 は保存速度とファイルサイズのバランスを取り、デフォルトレベルです。
  • Level7 と Level8 はさらに小さな出力を優先します。
  • Level9 は最強の圧縮を行い、最も多くの処理時間が必要です。

次の例は圧縮なしでプレゼンテーションを保存します。

import com.aspose.slides.CompressionLevel;
import com.aspose.slides.PptxOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation("Sample.pptx");
try {
    PptxOptions options = new PptxOptions();
    options.setCompressionLevel(CompressionLevel.None);

    presentation.save("OutputNoCompression.pptx", SaveFormat.Pptx, options);
} finally {
    presentation.dispose();
}

次の例は最大圧縮レベルで保存します。

import com.aspose.slides.CompressionLevel;
import com.aspose.slides.PptxOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation("Sample.pptx");
try {
    PptxOptions options = new PptxOptions();
    options.setCompressionLevel(CompressionLevel.Level9);

    presentation.save("OutputMaximumCompression.pptx", SaveFormat.Pptx, options);
} finally {
    presentation.dispose();
}

サムネイルを更新せずにプレゼンテーションを保存

プレゼンテーションを PPTX として保存する際、PptxOptions.setRefreshThumbnail メソッドでドキュメントサムネイルの振る舞いを制御できます。

  • true は保存時にサムネイルを再生成します。これがデフォルト値です。
  • false は既存のサムネイルを保持します。プレゼンテーションにサムネイルがない場合、Aspose.Slides はサムネイルを生成しません。

次の例はサムネイルを更新せずにプレゼンテーションを保存します。

import com.aspose.slides.PptxOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation("Sample.pptx");
try {
    PptxOptions options = new PptxOptions();
    options.setRefreshThumbnail(false);

    presentation.save("Output.pptx", SaveFormat.Pptx, options);
} finally {
    presentation.dispose();
}

進捗をパーセンテージで取得

保存操作の進捗を監視するには、IProgressCallback インターフェイスを実装し、その実装を ISaveOptions.setProgressCallback メソッドに渡します。Aspose.Slides はエクスポート中に IProgressCallback.reporting メソッドを呼び出して進捗値を通知します。

次の例は PDF エクスポートの進捗をコンソールに出力します。

import com.aspose.slides.IProgressCallback;
import com.aspose.slides.PdfOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

class ExportProgressHandler implements IProgressCallback {
    public void reporting(double progressValue) {
        int progress = (int) progressValue;
        System.out.println(progress + "% of the file has been converted.");
    }
}

PdfOptions options = new PdfOptions();
options.setProgressCallback(new ExportProgressHandler());

Presentation presentation = new Presentation("Sample.pptx");
try {
    presentation.save("Output.pdf", SaveFormat.Pdf, options);
} finally {
    presentation.dispose();
}

FAQ

Aspose.Slides はインクリメンタル保存または「高速保存」をサポートしていますか?

いいえ。各保存操作は変更された部分だけを更新するのではなく、完全な出力ファイルを書き込みます。

複数のスレッドから同じ Presentation インスタンスを保存できますか?

いいえ。Presentation インスタンスはスレッド セーフではありません。インスタンスへのアクセスおよび保存は同時に 1 つのスレッドからのみ行ってください。

プレゼンテーションを保存するとハイパーリンクや外部リンクされたファイルはどうなりますか?

ハイパーリンク はプレゼンテーションに残ります。Aspose.Slides は外部リンクされたファイルをコピーしないため、保存されたプレゼンテーションは引き続きそれらの場所にアクセスできる必要があります。

作者、タイトル、会社、作成日などのドキュメントメタデータを保存できますか?

はい。保存前に適切な ドキュメント プロパティ を設定すれば、Aspose.Slides がそれらを出力ファイルに書き込みます。