PostScript でのテキスト操作 | .NET
PS ドキュメントにテキストを追加する
この記事では、PS ドキュメントにテキストを追加する方法について説明します。
当然のことながら、テキストは何らかのフォントを使用してドキュメントに記述する必要があります。フォントはシステムフォルダーに保存することができ、この場合は システムフォント と呼びます。また、カスタムフォルダーに保存することもできます。カスタムフォルダーとは、特定の用途のためにフォントを配置するフォルダーです。通常、これらのフォントはシステムフォルダーには存在しません。このようなフォントを カスタムフォント と呼びます。 Aspose.Page for .NET ライブラリは、システム フォントと カスタム フォントの両方を使用するためのメソッドを提供しています。
システムフォントを使用する場合は、ネイティブ .NET の System.Drawing.Font を使用してテキストを塗りつぶすか、アウトラインを作成します。メソッド呼び出し内の数値は、テキストの左上隅の x 座標と y 座標です。
1document.FillText(str, new System.Drawing.Font("Times New Roman", fontSize, FontStyle.Bold), 50, 100);カスタムフォントを使用するには、まずPsSaveOptionsにカスタムフォントフォルダーを追加し、 Aspose.Page.Font.DrFontを取得します。最後に、このDrFontオブジェクトを使用してテキストを塗りつぶしたり、アウトライン化したりします。
1options.AdditionalFontsFolders = new string[] { FONTS_FOLDER };
2Aspose.Page.Font.DrFont drFont = ExternalFontCache.FetchDrFont("Palatino Linotype", fontSize, FontStyle.Regular);
3document.FillText(str, drFont, 50, 100);もう1つのオプションは、フォントにも属するテキストを扱うことです。PostScriptドキュメント内のテキストを印刷するために使用されるフォントは、このファイルに埋め込むことも、埋め込まないこともできます。
前者の場合、テキストは常にテキストと共に存在するため、どのPostScriptビューアやエディターでも常にレンダリングされます。後者の場合、使用されているフォントがターゲットホストのシステムフォルダーに存在することを想定する必要があります。使用されているフォントが存在しない場合、PostScriptインタープリターはエラーをスローする可能性があります。
3つ目のオプションもフォントに関するもので、テキストを追加する上でフォントが中心となります。テキストの塗りつぶし、描画(またはクリッピング)に使用されるフォントは、さまざまな形式でPSファイルに埋め込むことができます。現在、埋め込みではTrueTypeとType3フォントがサポートされています。
4つ目のオプションは、指定されたアドバンス(幅)でテキストのグリフを書き込む機能です。これにより、テキストに芸術的な要素を加えることができます。
以下の例では、Aspose.Page for .NETライブラリを使用してPSドキュメントにテキストを追加するさまざまな方法を示しています。
新しいPSドキュメントにテキストを追加するアルゴリズムは、以下の手順で構成されます。
- 生成されるPSファイル用の出力ストリームを作成します。
- PsSaveOptions を作成します。カスタムフォントを使用する場合は、保存オプションにカスタムフォントフォルダーを追加します。
- 既に作成済みの出力ストリームと保存オプションを使用して、 PsDocument を作成します。
- 必要なフォント(system または custom)を作成します。
- 作成したフォントでテキストを塗りつぶすかアウトライン化します。ここでは、テキストを塗りつぶすか描画するかに応じて、System.Drawing.Brush または System.Drawing.Pen を割り当てます。または、1つのメソッドでテキストの塗りつぶしとアウトライン化を行うこともできます。 System.Drawing.Brush または System.Drawing.Pen を使用せずにこのメソッドを使用すると、テキストは現在のグラフィック状態における現在のペイント/ストロークで塗りつぶされるか、アウトライン化されます。
- ページを閉じます。
- ドキュメントを保存します。
サンプル コードを小さなコード スニペットに分割することで、PS ドキュメントの初期準備、テキストを追加するための各メソッドの使用、そしてドキュメントの完成を分離しています。
このコードでは、出力ストリームと PsSaveOptions を作成し、コード内でカスタム フォントを使用するためのカスタム フォント フォルダーを追加し、PsDocument オブジェクトを作成し、すべてのメソッドに共通のテキストを文字列変数として設定し、使用されるすべてのメソッドで使用される fontSize 変数を作成します。
 1//Create an output stream for the PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "AddText_outPS.ps", FileMode.Create))
 3{
 4    //Create save options with the A4 size
 5    PsSaveOptions options = new PsSaveOptions();
 6    // Set the custom fonts folder. It will be added to system fonts folders for finding the needed font.
 7    options.AdditionalFontsFolders = new string[] { FONTS_FOLDER };
 8    //A text to write to the PS file
 9    string str = "ABCDEFGHIJKLMNO";
10    int fontSize = 48;
11
12    // Create a new 1-paged PS Document
13    PsDocument document = new PsDocument(outPsStream, options, false);ここでは、グラフィックス状態の現在の色 (黒) と新しい SolidBrush を使用してテキストを塗りつぶすための システム フォント の使用方法を示します。
1////////////////////////////////////// Using the sysem font (located in system fonts folders) for filling the text //////////////////
2    Font font = new Font("Times New Roman", fontSize, FontStyle.Bold);
3    //Fill the text with the default or already defined color. In given case it is black.
4    document.FillText(str, font, 50, 100);
5    //Fill the text with Blue color.
6    document.FillText(str, font, 50, 150, new SolidBrush(Color.Blue));
7/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Linux、macOS、その他のWindows以外のオペレーティングシステムでは、 Aspose.Page.Drawing NuGetパッケージをご利用いただけます。このパッケージは、System.Drawingシステムライブラリではなく、Aspose.Drawingバックエンドを使用します。
そのため、System.Drawing名前空間ではなく、Aspose.Page.Drawing名前空間をインポートしてください。上記および以下のコードスニペットでは、System.Drawing.Fontの代わりにAspose.Page.Drawing.Fontが、System.Drawing.SolidBrushの代わりにAspose.Page.Drawing.SolidBrushが使用されるなど、必要な置換はすべてGitHubのコード例に記載されています。
結果


Now we fill the text with the custom font.
1////////////////////////////////////// Using the custom font (located in custom fonts folders) for filling the text /////////////////
2	  DrFont drFont = ExternalFontCache.FetchDrFont("Palatino Linotype", fontSize, FontStyle.Regular);
3	  //Fill the text with default or already defined color. In the given case it is black.
4	  document.FillText(str, drFont, 50, 200);
5	  //Fill the text with the Blue color.
6	  document.FillText(str, drFont, 50, 250, new SolidBrush(Color.Blue));
7/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////結果


ここでは、現在のグラフィック状態のストローク (黒) と新しい ペン を使用してテキストのアウトラインを作成するための システム フォント の使用方法を示します。
1////////////////////////////////////// Using the sysem font (located in system fonts folders) for outlining the text ////////////////
2	  //Outline the text with default or already defined pen. In the given case it is a black colored 1-points width pen.
3	  document.OutlineText(str, font, 50, 300);
4	  //Outline the text with the blue-violet colored 2-points width pen.
5	  document.OutlineText(str, font, 50, 350, new Pen(new SolidBrush(Color.BlueViolet), 2));
6/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////結果


ここで、カスタム フォント を使用してテキストのアウトラインを作成します。
1////////////////////////////////////// Using the custom font (located in custom fonts folders) for outlining the text /////////////////
2		//Outline the text with the default or already defined pen. In given case it is a black colored 1-points width pen.
3		document.OutlineText(str, drFont, 50, 450);
4		//Outline the text with the blue-violet colored 2-points width pen.
5		document.OutlineText(str, drFont, 50, 500, new Pen(new SolidBrush(Color.BlueViolet), 2));		
6///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////結果


ここでは、新しい SolidBrush と Pen を使用してテキストを塗りつぶしたりアウトラインしたりするために システム フォント を使用する方法を示します。
1    //Fill the text with the orange color and stroke with the blue colored 2-points width pen.
2    document.FillAndStrokeText(str, font, 50, 400, new SolidBrush(Color.Yellow), new Pen(new SolidBrush(Color.BlueViolet), 2));結果

ここで、カスタム フォント を使用してテキストを塗りつぶし、アウトラインを作成します。
1		//Fill the text with the orange color and stroke with the blue colored 2-points width pen.
2		document.FillAndStrokeText(str, drFont, 50, 550, new SolidBrush(Color.Orange), new Pen(new SolidBrush(Color.Blue), 2));結果

最後に、グリフの幅を指定してテキストを埋めていきます。幅の数はグリフの数と同じでなければなりません。
1	//Glyphs widths
2    float[] widths = new float[] { 87, 87, 87, 87, 34, 87, 87 };    
3    //Fill ASCII text using with assigning glyphs widths.
4    document.FillText("BAMBOOK", widths, drFont, 50, 600, new SolidBrush(Color.Blue));
5    
6    //Glyphs widths
7    widths = new float[] { 87, 34, 87, 87, 87, 87, 87 };
8    //Fill Unicode text using with assigning glyphs widths.
9    document.FillText("ЗООПАРК", widths, drFont, 50, 650, new SolidBrush(Color.Orange));結果

現在のページを閉じて、ドキュメントを保存します。
1    //Close the current page
2    document.ClosePage();
3    
4    //Save the document
5    document.Save();PSドキュメント内のテキストの操作については、 Java を参照してください。
上記の例では、PostScriptファイルに埋め込まれたフォントがTrueTypeフォントとして使用されています。これは、PsDocumentクラスでフォントを保存する際のデフォルトの動作です。この動作を変更する必要がある場合は、次のように PsSaveOptionsを使用してください。
1		//Do not embed used fonts.
2		options.EmbedFonts = false;1		//Embed used fonts as Type3 fonts.
2		options.EmbedFontsAs = FontConstants.EMBED_FONTS_TYPE3;サンプルとデータ ファイルは GitHub からダウンロードできます。
