テキストの操作 | Python
PSドキュメントにテキストを追加する
この記事では、PSドキュメントにテキストを追加する方法について説明します。
ドキュメント内のテキストは、指定されたフォントでレンダリングする必要があります。フォントは、システムフォントと呼ばれるシステムフォルダー、または特定の用途のためにフォントが配置されているカスタムフォルダーから取得できます。通常、カスタムフォントはシステムフォルダーには存在しません。Aspose.Page for Python via .NETライブラリは、システムフォントとカスタムフォントを利用するためのメソッドを提供します。
aspose.pydrawing を使用して、システムのfonts.Fontクラスを利用し、テキストの塗りつぶしやアウトライン処理を行います。メソッド呼び出し内の数値は、テキストの左上隅のx座標とy座標を表します。
1font = aspose.page.ExternalFontCache.create_font_by_family_name("Times New Roman", font_size, aspose.pydrawing.FontStyle.BOLD)
2document.fillText(str, font, 50, 100);
カスタムフォントを使用するには、まずPsSaveOptionsにカスタムフォントフォルダーを追加し、 aspose.page.font.DrFontを取得します。 最後に、このDrFontオブジェクトを使用してテキストを塗りつぶすか、アウトライン化します。
1options.setAdditionalFontsFolders(new String[] { FONTS_FOLDER });
2dr_font = aspose.page.ExternalFontCache.fetch_dr_font("Palatino Linotype", font_size, aspose.pydrawing.FontStyle.REGULAR)
3document.fillText(str, dr_font, 50, 100);
テキストを扱うもう一つの方法は、フォントの埋め込みです。PostScriptドキュメント内のテキスト表示に使用されるフォントは、ファイルに埋め込むことも、埋め込まないこともできます。埋め込まれている場合、フォントはドキュメントと共に移動し、PostScriptビューアやエディター間で一貫したレンダリングが保証されます。しかし、フォントが埋め込まれていない場合、PostScriptインタープリターはターゲットホストのシステムフォルダーにフォントが存在することを前提とするため、フォントが見つからない場合はエラーが発生する可能性があります。
さらに、テキストの塗りつぶし、描画、クリッピングに使用されるフォントは、様々な形式でPSファイルに埋め込むことができます。現在、埋め込みはTrueTypeとType3フォントをサポートしています。
以下の例では、.NETライブラリを介してAspose.Page for Pythonを使用して、PSドキュメントにテキストを追加する様々な方法を紹介します。
新しいPSドキュメントにテキストを追加するアルゴリズムは、以下の手順で構成されます。
新しいPSドキュメントにテキストを追加するアルゴリズムは、以下の手順で構成されます。
- 結果のPSファイルへの出力ストリームを作成します。
- PsSaveOptions を作成します。カスタムフォントを使用する場合は、保存オプションにカスタムフォントフォルダーを追加する必要があります。
- 既に作成済みの出力ストリームと保存オプションを使用して、 PsDocument を作成します。
- 必要なフォント(system または custom)を作成します。
- 作成したフォントでテキストを塗りつぶすかアウトライン化します。ここでは、テキストを塗りつぶすか描画するかに応じて、aspose.pydrawing.Brush または aspose.pydrawing.Pen を割り当てることができます。または、1 つのメソッドでテキストの塗りつぶしとアウトライン化を行うこともできます。 aspose.pydrawing.Brush または aspose.pydrawing.Pen を使用せずにメソッドを使用すると、テキストは現在のグラフィックス状態における現在のペイント/ストロークで塗りつぶされるか、アウトライン化されます。
- ページを閉じます。
- ドキュメントを保存します。
サンプル コードを小さなコード スニペットに分割したのは、PS ドキュメントの初期準備、テキストを追加するための各メソッドの使用、そしてドキュメントの完成を分離するためです。
ここでは、出力ストリームと PsSaveOptions を作成し、コード内でカスタム フォントを使用するためのカスタム フォント フォルダーを追加し、PsDocument オブジェクトを作成し、すべてのメソッドに共通のテキストを文字列変数として設定し、使用されるすべてのメソッドでも使用される fontSize 変数を作成します。
1# The path to the documents directory.
2data_dir = Util.get_data_dir_working_with_text()
3
4fonts_folder = Util.get_data_dir_data() + """necessary_fonts/"""
5
6# Create an output stream for the PostScript document
7out_ps_stream = open(data_dir + "AddText_outPS.ps", "wb")
8# Create the save options with A4 size
9options = PsSaveOptions()
10# Set the custom fonts folder. It will be added to system fonts folders for finding the specific font.
11options.additional_fonts_folders = [ fonts_folder ]
12# A text to write to the PS file
13str = "ABCDEFGHIJKLMNO"
14font_size: float = 48
15
16# Create new 1-paged PS Document
17document = PsDocument(out_ps_stream, options, False)
ここでは、グラフィックス状態の現在の色 (黒) と新しい SolidBrush を使用してテキストを塗りつぶすための システム フォント の使用法を確認できます。
1##################################### Using a sysem font (located in system fonts folders) for filling text ##################
2font = aspose.page.ExternalFontCache.create_font_by_family_name("Times New Roman", font_size, aspose.pydrawing.FontStyle.BOLD)
3# Fill the text with a default or already defined color. In the given case it is black.
4document.fill_text(str, font, 50, 100)
5# Fill the text with the Blue color.
6document.fill_text(str, font, 50, 150, aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue))
7############################################################################################################################
このコードを実行した結果は
ここで、カスタム フォント を使用してテキストを入力します。
1##################################### Using a custom font (located in custom fonts folders) for filling text ##################
2dr_font = aspose.page.ExternalFontCache.fetch_dr_font("Palatino Linotype", font_size, aspose.pydrawing.FontStyle.REGULAR)
3# Fill the text with the default or already defined color. In the given case it is black.
4document.fill_text(str, dr_font, 50, 200)
5# Fill text with the Blue color.
6document.fill_text(str, dr_font, 50, 250, aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue))
7############################################################################################################################
このコードを実行した結果は
ここでは、グラフィックス状態の現在のストローク (黒) と新しい ペン を使用してテキストのアウトラインを作成するための システム フォント の使用法を確認できます。
1##################################### Using sysem font (located in system fonts folders) for outlining text ##################
2# Outline the text with the default or already defined aspose.pydrawing.Pen. In the given case it is black colored 1-points width aspose.pydrawing.Pen.
3document.outline_text(str, font, 50, 300)
4# Outline the text the with blue-violet colored 2-points width aspose.pydrawing.Pen.
5pen = GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue_violet), 2)
6document.outline_text(str, font, 50, 350, pen)
7############################################################################################################################
このコードを実行した結果は
ここで、カスタム フォント を使用してテキストのアウトラインを作成します。
1##################################### Using a custom font (located in custom fonts folders) for outlining text /////////////////
2# Outline the text with the default or already defined aspose.pydrawing.Pen. In the given case it is a black colored 1-points width aspose.pydrawing.Pen.
3document.outline_text(str, dr_font, 50, 450)
4# Outline the text with the blue-violet colored 2-points width aspose.pydrawing.Pen.
5pen = GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue_violet), 2)
6document.outline_text(str, dr_font, 50, 500, pen)
7##############################################################################################################################
このコードを実行した結果は
ここでは、新しい SolidBrush と Pen を使用してテキストを塗りつぶしたりアウトラインしたりするために システム フォント を使用する方法を確認できます。
1# Fill the text with an orange color and stroke with a blue colored 2-points width aspose.pydrawing.Pen.
2pen = GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue_violet), 2)
3document.fill_and_stroke_text(str, font, 50, 400, aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.yellow), pen)
このコードを実行した結果は
最後に、カスタム フォント を使用してテキストを塗りつぶし、アウトラインを作成します。
1# Fill the text with the orange color and stroke with the blue colored 2-points width aspose.pydrawing.Pen.
2pen = GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.blue), 2)
3document.fill_and_stroke_text(str, dr_font, 50, 550, aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.orange), pen)
このコードを実行した結果は
現在のページを閉じてドキュメントを保存します。
1#Close current page
2document.close_page()
3
4#Save the document
5document.save()
上記の例では、PostScriptファイルに埋め込まれたフォントがTrueTypeフォントとして使用されています。これは、PsDocumentクラスでフォントを保存する際のデフォルトの動作です。この動作を変更する必要がある場合は、 PsSaveOptionsを次のように使用してください。
1# Do not embed used fonts.
2options.embed_fonts = false;
1# Embed used fonts as Type3 fonts.
2options.embed_fonts_ss(FontConstants.EMBED_FONTS_TYPE3);
サンプルとデータ ファイルは GitHub からダウンロードできます。