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 라이브러리를 사용하여 PostScript(PS) 문서에 Unicode 문자열을 사용하여 텍스트를 추가하는 방법을 보여줍니다. 예제의 전체 버전과 더 많은 예제는 Aspose.Page-for-C++ GitHub 프로젝트 를 참조하십시오.

  1. 문서가 포함된 디렉터리 경로로 dataDir 변수를 초기화합니다.
  2. 필요한 글꼴이 포함된 폴더 경로로 FONTS_FOLDER 변수를 설정합니다.
  3. FileStream 클래스를 사용하여 PostScript 문서에 대한 출력 스트림을 생성합니다.
  4. PsSaveOptions 개체를 사용하여 PostScript 문서에 대한 저장 옵션을 지정합니다.
  5. 옵션 개체의 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 ファイルに統合するページ記述言語です。多くの画像編集・印刷ソフトでサポートされ、プリンタ向けの描画指示を含みます。