ページの操作 | Python

PSドキュメントにページを追加する

Aspose.Page for Python via .NET には、 PsDocument オブジェクトにページを追加するための2つのメソッドが用意されています。

以下のコードスニペットは、2ページのPSドキュメントを8つのステップで作成する方法を示しています。

  1. 出力されたPSファイル用の出力ストリームを作成します。
  2. デフォルトのオプションを使用して、 PsSaveOptions オブジェクトをインスタンス化します。
  3. 先ほど作成した出力ストリームと保存オプションを使用して、2ページのPsDocumentを作成します。
  4. ドキュメントのデフォルトのページサイズ(A4縦向き)で1ページ目を開きます。
  5. ページを閉じます。
  6. 新しいサイズで2ページ目を開きます。
  7. ページを閉じます。
  8. ドキュメントを保存します。
 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()

PSドキュメント内のページ操作については、 .NETJava を参照してください。

以下のコードスニペットも2ページのPSドキュメントを作成しますが、7つの手順を実行する必要があります。

  1. 生成されたPSファイル用の出力ストリームを作成します。
  2. デフォルトのオプションで PsSaveOptionsオブジェクトを初期化します。
  3. 既に作成済みの出力ストリームと保存オプションを使用して、複数ページのPsDocumentを作成します。この場合、最初のページは既に開かれており、そのサイズはドキュメントのデフォルトのページサイズ(A4縦向き)です。
  4. ページを閉じます。
  5. 2ページ目を新しいサイズで開きます。
  6. ページを閉じます。
  7. ドキュメントを保存します。 このページ追加方法は、ドキュメントが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 からダウンロードできます。

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.