PSファイルへのテキスト追加

PSファイルのテキストを操作するためのC++ APIソリューション

 

強力なページ記述言語であるPostScriptは、テキストの配置と書式設定を細かく制御できます。PostScriptでテキストを操作する際に考慮すべき主なニュアンスは次のとおりです。

  • PostScriptでは、PostScript名を使用してフォントを選択できます。Times Roman、Helvetica、Courierなどの一般的なフォントは、多くの場合デフォルトで使用可能です。異なるシステム間で一貫したレンダリングを確実にするために、フォントをPostScriptファイルに直接埋め込むことができます。これは、カスタムフォントやあまり一般的ではないフォントファミリーの場合に特に重要です。
  • フォントサイズとスタイル - fontsize演算子はフォントサイズをポイント単位で設定します。フォントスタイルを変更するには、太字、斜体、またはその他のバリエーションを指定するsetfontなどの演算子を使用します。
  • テキストの配置 - moveto演算子は、テキストカーソルをページ上の特定のポイントに配置します。PostScriptには直接的な配置演算子はありませんが、慎重な計算によって配置を実現できます。
  • テキストの回転とスケーリング - rotate演算子を使用してテキストを特定の角度に回転させたり、scale演算子を使用してテキストを拡大または縮小したりできます。
  • テキストレンダリング - PostScriptはアンチエイリアシングをサポートしており、テキストのエッジを滑らかにして読みやすさを向上させます。また、文字間隔(カーニング)や行間(リーディング)を制御して、テキストの外観を微調整することもできます。

これらの側面を理解することで、PostScriptベースのワークフローにおいて適切なフォントの使用と一貫したレンダリングを保証できます。どのような場合でも、Aspose.PageはPSファイルのフォントを簡単に管理する機能を提供します。このAPIを使用すると、カスタムフォントやシステムフォントを使用して、さまざまな色やブラシのテキストを追加できます。PSファイルの 扱い方 、特にPSファイルの テキスト操作 について詳しくは、ドキュメントを参照してください。

PSドキュメントにテキストを挿入するには、以下の準備が必要です。

  • 機能豊富で強力、かつ使いやすいドキュメント操作および変換C++ APIであるAspose.Page for C++ API。

  • NuGetパッケージマネージャーを開き、Aspose.Page.Cppを検索してインストールします。パッケージマネージャーコンソールから次のコマンドを使用することもできます。

Package Manager Console Command

    PM> Install-Package Aspose.Page.Cpp

PSファイルにテキストを追加する手順。

以下のコードスニペットは、C++のAspose.Pageライブラリを使用してUnicode文字列でテキストをPostScript(PS)ドキュメントに追加する方法を示しています。この例の完全なバージョンやその他の例については、 Aspose.Page-for-C++ GitHubプロジェクト を参照してください。

  1. ドキュメントを含むディレクトリへのパスでdataDir変数を初期化します。
  2. 必要なフォントを含むフォルダのパスにFONTS_FOLDER変数を設定します。
  3. FileStreamクラスを使用して、PostScriptドキュメントの出力ストリームを作成します。
  4. PsSaveOptions オブジェクトを使用して、PostScriptドキュメントの保存オプションを指定します。
  5. optionsオブジェクトのAdditionalFontsFoldersプロパティに、FONTS_FOLDERへのパスを含む配列を設定します。これにより、システムがそのフォルダ内の必要なフォントを特定できるようになります。
  6. テキストとそのサイズを指定します。
  7. 出力ストリーム、オプション、およびfalseをパラメータとして、 PsDocument の新しいインスタンスを作成します。これにより、指定されたオプションで新しいPostScriptドキュメントが初期化されます。
  8. ドキュメントオブジェクトの ClosePage() メソッドを呼び出し、現在のページが完了したことを示します。
  9. *Save()*メソッドを使用して、PSドキュメントに加えられた変更を保存します。
PSファイルにテキストを追加するためのC++コード
    // The path to the documents directory.
    System::String dataDir = RunExamples::GetDataDir_WorkingWithText();
    
    System::String FONTS_FOLDER = RunExamples::GetDataDir_Data() + u"necessary_fonts/";
    
    //Create output stream for PostScript document
    {
        System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"AddText_outPS.ps", System::IO::FileMode::Create);
        // Clearing resources under 'using' statement
        System::Details::DisposeGuard<1> __dispose_guard_0({ outPsStream});
        // ------------------------------------------
        
        try
        {
            //Create save options with A4 size
            System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
            // Set custom fonts folder. It will be added to system fonts folders for finding needed font.
            options->set_AdditionalFontsFolders(System::MakeArray<System::String>({FONTS_FOLDER}));
            //A text to write to PS file
            System::String str = u"ABCDEFGHIJKLMNO";
            int32_t fontSize = 48;
            
            // Create new 1-paged PS Document
            System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, false);
            
            ////////////////////////////////////// Using sysem font (located in system fonts folders) for filling text //////////////////
            System::SharedPtr<System::Drawing::Font> font = System::MakeObject<System::Drawing::Font>(u"Times New Roman", static_cast<float>(fontSize), System::Drawing::FontStyle::Bold);
            //Fill text with default or already defined color. In given case it is black.
            document->FillText(str, font, 50.0f, 100.0f);
            //Fill text with Blue color.
            document->FillText(str, font, 50.0f, 150.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Blue()));
            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            
            ////////////////////////////////////// Using custom font (located in custom fonts folders) for filling text /////////////////
            System::SharedPtr<DrFont> drFont = ExternalFontCache::FetchDrFont(u"Palatino Linotype", static_cast<float>(fontSize), System::Drawing::FontStyle::Regular);
            //Fill text with default or already defined color. In given case it is black.
            document->FillText(str, drFont, 50.0f, 200.0f);
            //Fill text with Blue color.
            document->FillText(str, drFont, 50.0f, 250.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Blue()));
            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            
            ////////////////////////////////////// Using sysem font (located in system fonts folders) for outlining text ////////////////
            //Outline text with default or already defined pen. In given case it is black colored 1-points width pen.
            document->OutlineText(str, font, 50.0f, 300.0f);
            //Outline text with blue-violet colored 2-points width pen.
            document->OutlineText(str, font, 50.0f, 350.0f, System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_BlueViolet()), 2.0f));
            //Fill text with orange color and stroke with blue colored 2-points width pen.
            document->FillAndStrokeText(str, font, 50.0f, 400.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Yellow()), System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_BlueViolet()), 2.0f));
            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            
            ////////////////////////////////////// Using custom font (located in custom fonts folders) for outlining text /////////////////
            //Outline text with default or already defined pen. In given case it is black colored 1-points width pen.
            document->OutlineText(str, drFont, 50.0f, 450.0f);
            //Outline text with blue-violet colored 2-points width pen.
            document->OutlineText(str, drFont, 50.0f, 500.0f, System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_BlueViolet()), 2.0f));
            //Fill text with orange color and stroke with blue colored 2-points width pen.
            document->FillAndStrokeText(str, drFont, 50.0f, 550.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Orange()), System::MakeObject<System::Drawing::Pen>(System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Blue()), 2.0f));
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            
            /////////////////////////////// Using custom font (located in custom fonts folders) ang glyphs widths for filling text ////////
            drFont = ExternalFontCache::FetchDrFont(u"Palatino Linotype", static_cast<float>(fontSize), System::Drawing::FontStyle::Regular);
            //Glyphs widths
            System::ArrayPtr<float> widths = System::MakeArray<float>({87, 87, 87, 87, 34, 87, 87});
            //Fill ASCII text using with assigning glyphs widths.
            document->FillText(u"BAMBOOK", widths, drFont, 50.0f, 600.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Blue()));
            ///////////////////////////// Using custom font (located in custom fonts folders) ang glyphs widths for filling unicode text //
            //Glyphs widths
            widths = System::MakeArray<float>({87, 34, 87, 87, 87, 87, 87});
            //Fill Unicode text using with assigning glyphs widths.
            document->FillText(u"ЗООПАРК", widths, drFont, 50.0f, 650.0f, System::MakeObject<System::Drawing::SolidBrush>(System::Drawing::Color::get_Orange()));
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            
            //Close current page
            document->ClosePage();
            
            //Save the document
            document->Save();
        }
        catch(...)
        {
            __dispose_guard_0.SetCurrentException(std::current_exception());
        }
    }

PS What is PS File Format

PS (PostScript) は、テキストとグラフィックを 1 ファイルに統合するページ記述言語です。多くの画像編集・印刷ソフトでサポートされ、プリンタ向けの描画指示を含みます。