ページの操作 | Python
Contents
[
Hide
Show
]PSドキュメントにページを追加する
Aspose.Page for Python via .NET には、 PsDocument オブジェクトにページを追加するための2つのメソッドが用意されています。
以下のコードスニペットは、2ページのPSドキュメントを8つのステップで作成する方法を示しています。
- 出力されたPSファイル用の出力ストリームを作成します。
- デフォルトのオプションを使用して、 PsSaveOptions オブジェクトをインスタンス化します。
- 先ほど作成した出力ストリームと保存オプションを使用して、2ページのPsDocumentを作成します。
- ドキュメントのデフォルトのページサイズ(A4縦向き)で1ページ目を開きます。
- ページを閉じます。
- 新しいサイズで2ページ目を開きます。
- ページを閉じます。
- ドキュメントを保存します。
1# The path to the documents directory.
2data_dir = Util.get_data_dir_working_with_pages()
3
4# Create an output stream for the PostScript document
5with open(data_dir + "document1.ps", "wb") as out_ps_stream:
6 # Create save options with A4 size
7 options = PsSaveOptions()
8
9 # Create a new 2-paged PS Document
10 document = PsDocument(out_ps_stream, options, 2)
11
12 # Add the first page
13 document.open_page(None)
14
15 # Add content
16
17 # Close the first page
18 document.close_page()
19
20 # Add the second page with a different size
21 document.open_page(400, 700)
22
23 # Add content
24
25 # Close the second page
26 document.close_page()
27
28 # Save the document
29 document.save()
以下のコードスニペットも2ページのPSドキュメントを作成しますが、7つの手順を実行する必要があります。
- 生成されたPSファイル用の出力ストリームを作成します。
- デフォルトのオプションで PsSaveOptionsオブジェクトを初期化します。
- 既に作成済みの出力ストリームと保存オプションを使用して、複数ページのPsDocumentを作成します。この場合、最初のページは既に開かれており、そのサイズはドキュメントのデフォルトのページサイズ(A4縦向き)です。
- ページを閉じます。
- 2ページ目を新しいサイズで開きます。
- ページを閉じます。
- ドキュメントを保存します。 このページ追加方法は、ドキュメントが1ページの場合、または1ページになるか2ページになるか分からない場合に便利です。
1# The path to the documents directory.
2data_dir = Util.get_data_dir_working_with_pages()
3
4# Create an output stream for PostScript document
5with open(data_dir + "document2.ps", "wb") as out_ps_stream:
6 # Create save options with A4 size
7 options = PsSaveOptions()
8
9 # Set a variable that indicates if resulting PostScript document will be multipaged
10 multi_paged = True
11
12 # Create new multipaged PS Document with one page opened
13 document = PsDocument(out_ps_stream, options, multi_paged)
14
15 # Add content
16
17 # Close the first page
18 document.close_page()
19
20 # Add the second page with different size
21 document.open_page(500, 300)
22
23 # Add content
24
25 # Close the second page
26 document.close_page()
27
28 # Save the document
29 document.save()
サンプルとデータ ファイルは GitHub からダウンロードできます。