PSファイル内のドキュメントの操作 | Python
PSドキュメントの作成
Aspose.Page for Python via .NETには、 PsDocumentクラスを作成するための2つのコンストラクターが含まれています。以下のコードスニペットは、1ページのPSドキュメントの作成方法を説明しています。
1# The path to the documents directory.
2dir = Util.get_data_dir_working_with_document()
3
4# Create an output stream for a PostScript document
5with open(dir + "document.ps", "wb") as out_ps_stream:
6 # Create save options
7 options = PsSaveOptions()
8 # If you want to aassign a page size other than A4, set the page size in options
9 options.page_size = PageConstants.get_size(PageConstants.SIZE_A4, PageConstants.ORIENTATION_PORTRAIT)
10 # If you want to aassign page margins other empty, set the page margins in options
11 options.margins = PageConstants.get_margins(PageConstants.MARGINS_ZERO)
12 # If you plan to use fonts that located in non system folders, set additional fonts folders in options
13 options.additional_fonts_folders = [ dir ]
14
15 # Set a variable that indicates if resulting PostScript document will be multipaged
16 multi_paged = False
17
18 # Create a new multipaged PS Document with one page opened
19 document = PsDocument(out_ps_stream, options, multi_paged)
20
21 # Close the current page
22 document.close_page()
23 # Save the document
24 document.save()
PSドキュメントを複数ページにしたい場合は、multiPaged変数をtrueに設定してください。
もう一つのコンストラクタを使用すると、定義したページ数を持つPsDocumentオブジェクトを作成できます。
1with open(dir + "document.ps", "wb") as out_ps_stream:
2# Create save options
3options = PsSaveOptions()
4# If you want to assign the page size other than A4, set the page size in options
5options.page_size = PageConstants.get_size(PageConstants.SIZE_A4, PageConstants.ORIENTATION_PORTRAIT)
6# If you want to aassign page margins other empty, set the page margins in options
7options.margins = PageConstants.get_margins(PageConstants.MARGINS_ZERO)
8# If you plan to use fonts that located in non system folders, set additional fonts folders in options
9options.additional_fonts_folders = [ dir ]
10
11# Create a new multipaged PS Document with one page opened
12document = PsDocument(out_ps_stream, options, 2)
13
14# Close the current page
15document.close_page()
16# Save the document
17document.save()
正しい PostScript ドキュメントを作成するのに役立つオプションをカプセル化した PsSaveOptions クラスを見てみましょう。
SaveFormat パラメーターは、ドキュメントの出力形式を PS または EPS のいずれかに指定します。Aspose.Page ライブラリでは、これら 2 つの形式の区別は主に PostScript コメントとファイル拡張子に反映されます。また、EPS ファイル仕様では、EPS ファイルは 1 ページとして扱われます。デフォルトでは PS 形式が使用されます。
page_size パラメーターは、PS ドキュメントのページサイズを指定します。ただし、必要に応じてページごとに異なるページサイズを割り当てることもできます。
ページ サイズは、次の例に示すように PageConstants クラスから取得できます。
1options.page_size = PageConstants.get_size(PageConstants.SIZE_International, PageConstants.ORIENTATION_PORTRAIT);
デフォルトのページサイズは「A4」で、印刷方向は「縦」です。
- margins は、ページ境界とページコンテンツの左、上、右、下の端の間の余白を指定します。これは PageConstants クラスから取得できます。
1options.margins = PageConstants.get_margins(PageConstants.MARGINS_SMALL)); // 20 points for each margin
デフォルトの余白は「ゼロ」(0, 0, 0, 0)です。
- back_ground_color はページの背景色を指定します。以下の値を指定できます。
1options.back_ground_color = aspose.pydrawing.Color(211, 8, 48);
または:
1options.back_ground_color = aspose.pydrawing.Color.YELLOW;
デフォルト値は「null」で、背景なしを意味します。
embed_fonts パラメーターは、ドキュメントの出力形式(PS または EPS)を指定します。Aspose.Page ライブラリでは、これら 2 つの形式の区別は主に PostScript コメントとファイル拡張子に反映されます。また、EPS ファイル仕様では、EPS ファイルは 1 ページとして扱われます。デフォルトでは PS 形式が使用されます。
embed_fonts_as パラメーターは、PS ドキュメントのページサイズを指定します。ただし、必要に応じてページごとに異なるページサイズを割り当てることもできます。この値は、次のように FontConstants クラスを使用して設定できます。
1options.embed_fonts_as = FontsConstants.EMBED_FONTS_TYPE3;
デフォルト値は「TrueType」です。
jpeg_quality_level パラメータは、生成されるPSドキュメント内の画像の圧縮レベルと品質を決定します。品質が高いほど圧縮率は低くなり、品質が低いほど圧縮率は低くなります。品質の範囲は0~100で、0は最低品質、100は最高品質を表します。デフォルト設定は75です。
additional_fonts_folder パラメータは、フォントが保存される場所を指定します。デフォルトでは、システムフォントフォルダが常に含まれます。
debug は、コンソールへのデバッグ情報の出力を有効にします。デフォルト値はfalseです。
サンプルとデータ ファイルは GitHub からダウンロードできます。