เพิ่มข้อความลงในไฟล์ PS

โซลูชัน C++ API สำหรับทำงานกับข้อความในไฟล์ PS

 

PostScript เป็นภาษาบรรยายหน้าที่มีประสิทธิภาพซึ่งให้การควบคุมที่ละเอียดแม่นยำในการวางตำแหน่งและการจัดรูปแบบข้อความ ต่อไปนี้คือแง่มุมสำคัญบางประการที่ควรพิจารณาเมื่อทำงานกับข้อความใน PostScript:

  • PostScript ช่วยให้คุณเลือกแบบอักษรโดยใช้ชื่อ PostScript ของแบบอักษรเหล่านั้น แบบอักษรทั่วไปเช่น Times Roman, Helvetica และ Courier มักจะมีให้ใช้งานเป็นค่าเริ่มต้น เพื่อให้แน่ใจว่าการเรนเดอร์สอดคล้องกันข้ามระบบต่างๆ คุณสามารถฝังแบบอักษรลงในไฟล์ PostScript ได้โดยตรง ซึ่งสำคัญอย่างยิ่งสำหรับแบบอักษรที่กำหนดเองหรือตระกูลแบบอักษรที่ไม่ค่อยพบบ่อย
  • ขนาดและสไตล์ของแบบอักษร - ตัวดำเนินการ fontsize กำหนดขนาดแบบอักษรเป็นพอยต์ ในการเปลี่ยนสไตล์แบบอักษรให้ใช้ตัวดำเนินการอย่าง setfont ซึ่งกำหนดตัวหนา ตัวเอียง หรือรูปแบบอื่นๆ
  • การวางตำแหน่งข้อความ - ตัวดำเนินการ moveto วางตำแหน่งเคอร์เซอร์ข้อความไปยังจุดที่กำหนดบนหน้า แม้ว่า PostScript จะไม่มีตัวดำเนินการจัดแนวโดยตรง แต่คุณสามารถจัดแนวได้โดยการคำนวณอย่างรอบคอบ
  • การหมุนและการปรับขนาดข้อความ - ตัวดำเนินการ rotate สามารถใช้เพื่อหมุนข้อความไปยังมุมที่กำหนด และตัวดำเนินการ scale สามารถใช้เพื่อขยายหรือย่อขนาดข้อความได้
  • การเรนเดอร์ข้อความ - PostScript รองรับการลบรอยหยัก ซึ่งจะช่วยให้ขอบของข้อความเรียบขึ้นเพื่อให้อ่านง่ายขึ้น คุณยังสามารถควบคุมระยะห่างระหว่างอักขระ (kerning) และระยะห่างระหว่างบรรทัด (leading) เพื่อปรับแต่งลักษณะที่ปรากฏของข้อความได้

การทำความเข้าใจแง่มุมเหล่านี้จะช่วยให้มั่นใจได้ว่ามีการใช้แบบอักษรที่เหมาะสมและการเรนเดอร์ที่สอดคล้องกันในเวิร์กโฟลว์ที่ใช้ PostScript แต่อย่างไรก็ตาม - Aspose.Page มอบคุณสมบัติให้คุณจัดการแบบอักษรของไฟล์ PS ได้อย่างง่ายดาย ด้วย API นี้ คุณสามารถเพิ่มข้อความที่มีสีและแปรงที่แตกต่างกันโดยใช้แบบอักษรที่กำหนดเองหรือแบบอักษรของระบบ หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับ วิธีจัดการกับไฟล์ PS และ วิธีทำงานกับข้อความในไฟล์ PS โดยเฉพาะ โปรดดูที่เอกสารประกอบ

ในการแทรกข้อความลงในเอกสาร PS เราต้องการ:

  • Aspose.Page สำหรับ C++ API ซึ่งเป็น API สำหรับจัดการและแปลงเอกสารที่เป็นภาษา C++ ที่มีคุณสมบัติครบถ้วน มีประสิทธิภาพ และใช้งานง่าย

  • เปิดตัวจัดการแพ็กเกจ NuGet และค้นหา Aspose.Page.Cpp แล้วติดตั้ง คุณยังสามารถใช้คำสั่งต่อไปนี้จาก Package Manager Console

Package Manager Console Command

    PM> Install-Package Aspose.Page.Cpp

ขั้นตอนในการเพิ่มข้อความลงในไฟล์ PS

รหัสตัวอย่างด้านล่างแสดงวิธีเพิ่มข้อความโดยใช้สตริง Unicode ลงในเอกสาร PostScript (PS) โดยใช้ไลบรารี Aspose.Page ใน C++ หากต้องการดูเวอร์ชันเต็มของตัวอย่างและตัวอย่างเพิ่มเติม โปรดไปที่ โครงการ Aspose.Page-for-C++ บน GitHub

  1. เริ่มต้นตัวแปร dataDir ด้วยเส้นทางไปยังไดเรกทอรีที่ประกอบด้วยเอกสาร
  2. ตั้งค่าตัวแปร FONTS_FOLDER เป็นเส้นทางของโฟลเดอร์ที่มีแบบอักษรที่จำเป็น
  3. สร้างสตรีมเอาต์พุตสำหรับเอกสาร PostScript โดยใช้คลาส FileStream
  4. กำหนดตัวเลือกการบันทึกสำหรับเอกสาร PostScript โดยใช้วัตถุ PsSaveOptions
  5. คุณสมบัติ AdditionalFontsFolders ของวัตถุตัวเลือกถูกตั้งค่าเป็นอาร์เรย์ที่ประกอบด้วยเส้นทางไปยัง FONTS_FOLDER ซึ่งช่วยให้ระบบสามารถค้นหาแบบอักษรที่จำเป็นในโฟลเดอร์นั้นได้
  6. กำหนดข้อความและขนาดของข้อความ
  7. สร้างอินสแตนซ์ใหม่ของ PsDocument พร้อมสตรีมเอาต์พุต ตัวเลือก และค่า false เป็นพารามิเตอร์ ซึ่งจะเริ่มต้นเอกสาร PostScript ใหม่พร้อมกับตัวเลือกที่ระบุ
  8. เรียกใช้วิธี ClosePage() บนวัตถุเอกสาร เพื่อระบุว่าหน้าปัจจุบันเสร็จสมบูรณ์แล้ว
  9. ใช้วิธี Save() เพื่อบันทึกการเปลี่ยนแปลงที่ทำกับเอกสาร PS
รหัส C++ เพื่อเพิ่มข้อความลงในไฟล์ PS
    // 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 PS รูปแบบไฟล์คืออะไร

PS (PostScript) เป็นภาษาการอธิบายหน้า ที่รวมข้อความและกราฟิกในไฟล์เดียว รองรับโดยโปรแกรมแก้ไขภาพและเครื่องพิมพ์หลายรุ่น