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

โซลูชัน C++ API เพื่อทำงานกับข้อความของไฟล์ PS

 

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

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

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

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

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

  • เปิดตัวจัดการแพ็คเกจ 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. คุณสมบัติ AddFontsFolders ของออบเจ็กต์ตัวเลือกถูกตั้งค่าเป็นอาร์เรย์ที่มีเส้นทางไปยัง FONTS_FOLDER ซึ่งจะทำให้ระบบสามารถค้นหาแบบอักษรที่จำเป็นในโฟลเดอร์นั้นได้
  6. ระบุข้อความและขนาดของข้อความ
  7. สร้างอินสแตนซ์ใหม่ของ PsDocument โดยมีสตรีมเอาต์พุต ตัวเลือก และ false เป็นพารามิเตอร์ ซึ่งจะเริ่มต้นเอกสาร PostScript ใหม่ด้วยตัวเลือกที่ระบุ
  8. เรียกใช้เมธอด ClosePage() บนออบเจ็กต์เอกสาร ซึ่งระบุว่าเพจปัจจุบันเสร็จสมบูรณ์แล้ว
  9. ใช้เมธอด Save() เพื่อบันทึกการเปลี่ยนแปลงที่ทำกับเอกสาร PS
เพิ่มรูปภาพใน 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 เป็นหนึ่งในรูปแบบภาษาคำอธิบายหน้า (PDL) สามารถใส่ข้อมูลกราฟิกและข้อความบนหน้าได้ นั่นคือเหตุผลที่รูปแบบได้รับการสนับสนุนโดยโปรแกรมส่วนใหญ่สำหรับการแก้ไขภาพ ไฟล์ postscript เป็นคำสั่งชนิดหนึ่งสำหรับเครื่องพิมพ์ มันมีข้อมูลเกี่ยวกับสิ่งที่และวิธีการพิมพ์จากหน้า