Aggiungi testo ai file PS
Soluzione API C++ per lavorare con testi di file PS
PostScript, un potente linguaggio di descrizione delle pagine, offre un controllo granulare sul posizionamento e sulla formattazione del testo. Ecco alcune sfumature chiave da considerare quando si lavora con il testo in PostScript:
- PostScript consente di selezionare i caratteri utilizzando i relativi nomi PostScript. I caratteri comuni come Times Roman, Helvetica e Courier sono spesso disponibili per impostazione predefinita. Per garantire un rendering coerente su diversi sistemi, puoi incorporare i caratteri direttamente nel file PostScript. Ciò è particolarmente importante per i caratteri personalizzati o le famiglie di caratteri meno comuni.
- Dimensione e stile del carattere - L’operatore
fontsize
imposta la dimensione del carattere in punti. Per modificare gli stili dei caratteri utilizza operatori come “setfont” che specifica grassetto, corsivo o altre variazioni. - Posizionamento del testo - L’operatore “moveto” posiziona il cursore del testo in un punto specifico della pagina. Sebbene PostScript non disponga di operatori di allineamento diretto, è possibile ottenere l’allineamento facendo attenzione.
- Rotazione e ridimensionamento del testo: l’operatore “rotate” può essere utilizzato per ruotare il testo rispetto a un angolo specifico e l’operatore “scale” può essere utilizzato per ridimensionare il testo verso l’alto o verso il basso.
- Rendering del testo - PostScript supporta l’anti-aliasing, che smussa i bordi del testo per una migliore leggibilità. Puoi anche controllare la spaziatura tra i caratteri (crenatura) e le linee (interlinea) per ottimizzare l’aspetto del testo.
Comprendere questi aspetti può aiutare a garantire un utilizzo corretto dei caratteri e un rendering coerente nei flussi di lavoro basati su PostScript. Ma qualunque cosa accada, Aspose.Page ti offre la funzionalità per gestire facilmente i caratteri dei file PS. Con questa API puoi aggiungere testi di diversi colori e pennelli utilizzando caratteri personalizzati o di sistema. Per saperne di più su come gestire i file PS e come lavorare con i testi dei file PS in particolare seguire la documentazione.
Per inserire testi nei documenti PS abbiamo bisogno di:
Aspose.Page per l'API C++ che è un'API C++ per la manipolazione e la conversione di documenti ricca di funzionalità, potente e facile da usare.
Aprire il gestore pacchetti NuGet, cercare Aspose.Page.Cpp e installarlo. Puoi anche utilizzare il seguente comando dalla Console di gestione pacchetti.
Package Manager Console Command
PM> Install-Package Aspose.Page.Cpp
Passaggi per aggiungere testo a un file PS.
Il frammento di codice seguente illustra come aggiungere testo utilizzando una stringa Unicode a un documento PostScript (PS) utilizzando la libreria Aspose.Page in C#. Per vedere una versione più completa dell’esempio e altri esempi, vai a progetto GitHub Aspose.Page-for-C++ .
- Inizializza la variabile dataDir con il percorso della directory contenente i documenti.
- Impostare la variabile FONTS_FOLDER da impostare sul percorso della cartella contenente i caratteri necessari.
- Crea un flusso di output per il documento PostScript utilizzando la classe FileStream.
- Specificare le opzioni di salvataggio per il documento PostScript utilizzando l’oggetto PsSaveOptions .
- La proprietà AdditionalFontsFolders dell’oggetto opzioni è impostata su un array contenente il percorso di FONTS_FOLDER. Ciò consente al sistema di individuare tutti i font richiesti in quella cartella.
- Specificare il testo e la sua dimensione.
- Crea una nuova istanza di PsDocument con il flusso di output, opzioni e false come parametri. Ciò inizializza un nuovo documento PostScript con le opzioni specificate.
- Chiamare il metodo ClosePage() sull’oggetto documento, indicando che la pagina corrente è completata.
- Utilizzare il metodo Save() per salvare le modifiche apportate al documento PS.
Aggiungi immagini a 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 Cos'è il formato file PS
Il formato PS è uno dei formati PDL (Page Description Language). È in grado di contenere informazioni grafiche e di testo sulla pagina. Ecco perché il formato è stato supportato dalla maggior parte dei programmi per l'editing delle immagini. Il file PostScript stesso è una sorta di istruzione per le stampanti. Contiene informazioni su cosa e come stampare dalla sua pagina.