Tambahkan teks ke file PS
Solusi C++ API untuk bekerja dengan teks file PS
PostScript, bahasa deskripsi halaman yang canggih, menawarkan kontrol terperinci atas penempatan dan pemformatan teks. Berikut adalah beberapa nuansa utama yang perlu dipertimbangkan ketika bekerja dengan teks di PostScript:
- PostScript memungkinkan Anda memilih font menggunakan nama PostScriptnya. Font umum seperti Times Roman, Helvetica, dan Courier sering kali tersedia secara default. Untuk memastikan rendering yang konsisten di berbagai sistem, Anda dapat menyematkan font langsung ke file PostScript Anda. Hal ini sangat penting terutama untuk font khusus atau jenis font yang kurang umum.
- Ukuran dan Gaya Font - Operator
ukuran font
menetapkan ukuran font dalam poin. Untuk mengubah gaya font, gunakan operator sepertisetfont
yang menentukan huruf tebal, miring, atau variasi lainnya. - Pemosisian Teks - Operator
moveto
memposisikan kursor teks ke titik tertentu di halaman. Meskipun PostScript tidak memiliki operator penyelarasan langsung, Anda dapat mencapai penyelarasan dengan hati-hati. - Rotasi dan Penskalaan Teks - Operator
putar
dapat digunakan untuk memutar teks ke sudut tertentu, dan operatorskala
dapat digunakan untuk menaikkan atau menurunkan skala teks. - Rendering Teks - PostScript mendukung anti-aliasing, yang memperhalus tepi teks agar lebih mudah dibaca. Anda juga dapat mengontrol jarak antar karakter (kerning) dan garis (terdepan) untuk menyempurnakan tampilan teks.
Memahami aspek-aspek ini dapat membantu memastikan penggunaan font yang tepat dan rendering yang konsisten dalam alur kerja berbasis PostScript. Tapi 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 kuas berbeda menggunakan font khusus atau sistem. Untuk mempelajari lebih lanjut tentang cara menangani file PS dan cara bekerja dengan teks file PS khususnya ikuti dokumentasinya.
Untuk menyisipkan teks ke dokumen PS kita membutuhkan:
Aspose.Page untuk C++ API yang merupakan manipulasi dokumen dan konversi C++ API yang kaya fitur, kuat, dan mudah digunakan.
Buka manajer paket NuGet, dan cari Aspose.Page.Cpp dan instal. Anda juga dapat menggunakan perintah berikut dari Package Manager Console.
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 string Unicode ke dokumen PostScript (PS) menggunakan pustaka 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 jalur ke direktori yang berisi dokumen.
- Atur variabel FONTS_FOLDER untuk mengatur jalur folder yang berisi font yang diperlukan.
- Buat aliran keluaran untuk dokumen PostScript menggunakan kelas FileStream.
- Tentukan opsi penyimpanan untuk dokumen PostScript menggunakan objek PsSaveOptions .
- Properti TambahanFontsFolders dari objek opsi diatur ke array yang berisi jalur ke FONTS_FOLDER. Hal ini memungkinkan sistem untuk menemukan font apa pun yang diperlukan di folder itu.
- Tentukan teks dan ukurannya.
- Buat instance baru PsDocument dengan aliran keluaran, 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 Simpan() untuk menyimpan perubahan yang dilakukan pada dokumen PS.
Tambahkan gambar ke 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 Apa itu Format File PS
Format PS adalah salah satu format bahasa deskripsi halaman (PDL). Hal ini mampu berisi grafis serta informasi teks pada halaman. Itulah sebabnya format ini didukung oleh sebagian besar program untuk mengedit gambar. File postscript itu sendiri adalah semacam instruksi untuk printer. Ini berisi informasi tentang apa dan bagaimana mencetak dari halamannya.