Tambahkan teks ke file PS
Solusi API C++ untuk bekerja dengan teks file PS
PostScript, bahasa deskripsi halaman yang kuat, menawarkan kontrol granular atas penempatan dan pemformatan teks. Berikut adalah beberapa nuansa utama yang perlu dipertimbangkan saat bekerja dengan teks dalam PostScript:
- PostScript memungkinkan Anda memilih font menggunakan nama PostScript mereka. Font umum seperti Times Roman, Helvetica, dan Courier sering kali tersedia secara default. Untuk memastikan rendering yang konsisten di berbagai sistem, Anda dapat menyematkan (embed) font secara langsung ke dalam file PostScript Anda. Ini sangat penting untuk font khusus atau keluarga font yang kurang umum.
- Ukuran dan Gaya Font - Operator
fontsizemenetapkan ukuran font dalam poin. Untuk mengubah gaya font gunakan operator sepertisetfontyang menentukan tebal, miring, atau variasi lainnya. - Penempatan Teks - Operator
movetomenempatkan kursor teks ke titik tertentu pada halaman. Meskipun PostScript tidak memiliki operator penyelarasan langsung, Anda dapat mencapai penyelarasan melalui perhitungan yang cermat. - Rotasi dan Penskalaan Teks - Operator
rotatedapat digunakan untuk memutar teks ke sudut tertentu, dan operatorscaledapat digunakan untuk memperbesar atau memperkecil teks. - Rendering Teks - PostScript mendukung anti-aliasing, yang menghaluskan tepi teks untuk keterbacaan yang lebih baik. Anda juga dapat mengontrol spasi antar karakter (kerning) dan baris (leading) untuk menyempurnakan tampilan teks.
Memahami aspek-aspek ini dapat membantu memastikan penggunaan font yang tepat dan rendering yang konsisten dalam workflow berbasis PostScript. Namun apa pun yang terjadi, - Aspose.Page memberi Anda fungsionalitas untuk mengelola font file PS dengan mudah. Dengan API ini, Anda dapat menambahkan teks dengan warna dan brush yang berbeda menggunakan font khusus atau font sistem. Untuk mempelajari lebih lanjut tentang cara menangani file PS dan cara bekerja dengan teks file PS secara khusus, ikuti dokumentasi.
Untuk menyisipkan teks ke dokumen PS kita perlu:
API Aspose.Page untuk C++ yang merupakan API manipulasi dan konversi dokumen C++ yang kaya fitur, kuat, dan mudah digunakan.
Buka manajer paket NuGet, cari Aspose.Page.Cpp dan instal. Anda juga dapat menggunakan perintah berikut dari Konsol Manajer Paket.
Package Manager Console Command
PM> Install-Package Aspose.Page.Cpp
Langkah-langkah untuk menambahkan teks ke file PS.
Cuplikan kode di bawah ini menunjukkan cara menambahkan teks menggunakan Unicode string ke dokumen PostScript (PS) menggunakan library Aspose.Page di C#. Untuk melihat versi contoh yang lebih lengkap dan contoh lainnya, buka proyek GitHub Aspose.Page-for-C++ .
- Inisialisasi variabel dataDir dengan path ke direktori yang berisi dokumen.
- Tetapkan variabel FONTS_FOLDER ke path folder yang berisi font yang diperlukan.
- Buat stream output untuk dokumen PostScript menggunakan kelas FileStream.
- Tentukan opsi penyimpanan untuk dokumen PostScript menggunakan objek PsSaveOptions .
- Properti AdditionalFontsFolders dari objek options diatur ke array yang berisi path ke FONTS_FOLDER. Ini memungkinkan sistem untuk menemukan font apa pun yang diperlukan di folder tersebut.
- Tentukan teks dan ukurannya.
- Buat instance baru dari PsDocument dengan stream output, opsi, dan false sebagai parameter. Ini menginisialisasi dokumen PostScript baru dengan opsi yang ditentukan.
- Panggil metode ClosePage() pada objek dokumen, yang menunjukkan bahwa halaman saat ini telah selesai.
- Gunakan metode Save() untuk menyimpan perubahan yang dilakukan pada dokumen PS.
Kode C++ untuk menambahkan teks ke file 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 What is PS File Format
PS (PostScript) adalah bahasa deskripsi halaman yang menggabungkan teks dan grafik dalam satu file. Didukung luas oleh editor gambar dan printer, berisi instruksi rendering untuk printer.