
בפוסט הקודם שלי, הראיתי לך כמה קל ליצור או לערוך מסמכי PostScript (PS/EPS) ו-XPS באופן פרוגרמטי באמצעות Aspose.Page for C++. במאמר זה, אדגים כיצד להמיר מסמכי PS/EPS או XPS לפורמטים של PDF או תמונות רסטר כולל PNG, JPEG, TIFF ו-BMP באמצעות C++. שאר המאמר מורכב מהחלקים הבאים:
- המרת PostScript PS/EPS ל-PDF ב-C++
- המרת PostScript PS/EPS לתמונה ב-C++
- המר XPS ל-PDF ב-C++
- המר XPS לתמונה ב-C++
לפני שנתחיל, אני מניח שהורדת את Aspose.Page עבור C++ והפנית אליו בפרויקט C++ שלך. עם זאת, אם לא עשית זאת, תוכל להתקין אותו מ-NuGet או להוריד את החבילה השלמה יחד עם אפליקציית קונסולת plug and play מהסעיף הורדות.
המרת PostScript PS/EPS ל-PDF ב-C++
להלן השלבים להמרת מסמך PostScript PS/EPS ל-PDF:
- צור אובייקט FileStream עבור קובץ הפלט PDF.
- טען את מסמך PostScript הקלט באובייקט FileStream.
- צור ואתחל את האובייקט PsDocument עם זרם הקלט.
- צור ואתחול את האובייקט PdfDevice עם זרם הפלט.
- עבדו את המסמך ושמרו אותו כקובץ PDF בשיטת PsDocument->Save.
דוגמת הקוד הבאה מראה כיצד להמיר מסמך PostScript PS ל-PDF ב-C++.
// אתחול זרם פלט PDF
System::SharedPtr<System::IO::FileStream> pdfStream = System::MakeObject<System::IO::FileStream>(value + u"PStoPDF.pdf", System::IO::FileMode::Create, System::IO::FileAccess::Write);
// אתחול זרם קלט PostScript
System::SharedPtr<System::IO::FileStream> psStream = System::MakeObject<System::IO::FileStream>(value + u"input.ps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(psStream);
// אם אתה רוצה להמיר קובץ Postscript למרות שגיאות קלות, הגדר את הדגל הזה
bool suppressErrors = true;
// אתחול אובייקט אפשרויות עם פרמטרים נחוצים.
System::SharedPtr<PdfSaveOptions> options = System::MakeObject<PdfSaveOptions>(suppressErrors);
// אם אתה רוצה להוסיף תיקיה מיוחדת שבה מאוחסנים גופנים. תיקיית ברירת המחדל של גופנים במערכת ההפעלה כלולה תמיד.
options->set_AdditionalFontsFolders(System::MakeArray<System::String>({ u"{FONT_FOLDER}" }));
// גודל העמוד המוגדר כברירת מחדל הוא 595x842 ואין חובה להגדיר אותו ב-PdfDevice
System::SharedPtr<Aspose::Page::EPS::Device::PdfDevice> device = System::MakeObject<Aspose::Page::EPS::Device::PdfDevice>(pdfStream);
// אבל אם אתה צריך לציין גודל ופורמט תמונה השתמש בשורה הבאה:
// Aspose.Page.EPS.Device.PdfDevice device = new Aspose.Page.EPS.Device.PdfDevice(pdfStream, New System.Drawing.Size(595, 842));
{
auto __finally_guard_0 = ::System::MakeScopeGuard([&psStream, &pdfStream]()
{
psStream->Close();
pdfStream->Close();
});
try
{
document->Save(device, options);
}
catch (...)
{
throw;
}
}
// בדוק שגיאות
if (suppressErrors)
{
//auto ex_enumerator = (System::DynamicCastEnumerableTo<PsConverterException> (options->get_Exceptions()))->GetEnumerator();
auto ex_enumerator = (options->get_Exceptions())->GetEnumerator();
decltype(ex_enumerator->get_Current()) ex;
while (ex_enumerator->MoveNext() && (ex = ex_enumerator->get_Current(), true))
{
System::Console::WriteLine(ex->get_Message());
}
}
המרת PostScript PS/EPS לתמונה ב-C++
להלן השלבים להמרת PS/EPS לפורמט תמונה.
- צור אובייקט של ImageFormat כדי להגדיר את הפורמט של תמונת הפלט, כלומר PNG.
- טען את מסמך PostScript הקלט באובייקט FileStream.
- צור ואתחל את האובייקט PsDocument עם זרם הקלט.
- צור אובייקט של ImageDevice.
- עבדו את המסמך ושמרו אותו כתמונה בשיטת PsDocument->Save.
דוגמת הקוד הבאה מראה כיצד להמיר PostScript PS/EPS לתמונה ב-C++.
// אתחול זרם פלט PDF
System::SharedPtr<System::Drawing::Imaging::ImageFormat> imageFormat = System::Drawing::Imaging::ImageFormat::get_Png();
// אתחול זרם קלט PostScript
System::SharedPtr<System::IO::FileStream> psStream = System::MakeObject<System::IO::FileStream>(value + u"inputForImage.ps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(psStream);
// אם אתה רוצה להמיר קובץ PostScript למרות שגיאות קלות, הגדר את הדגל הזה
bool suppressErrors = true;
// אתחול אובייקט אפשרויות עם פרמטרים נחוצים.
System::SharedPtr<ImageSaveOptions> options = System::MakeObject<ImageSaveOptions>(suppressErrors);
// אם אתה רוצה להוסיף תיקיה מיוחדת שבה מאוחסנים גופנים. תיקיית ברירת המחדל של גופנים במערכת ההפעלה כלולה תמיד.
options->set_AdditionalFontsFolders(System::MakeArray<System::String>({ u"{FONT_FOLDER}" }));
// פורמט תמונה המוגדר כברירת מחדל הוא PNG ואין חובה להגדיר אותו ב-ImageDevice
// גודל התמונה המוגדר כברירת מחדל הוא 595x842 ואין חובה להגדיר אותו ב-ImageDevice
System::SharedPtr<Aspose::Page::EPS::Device::ImageDevice> device = System::MakeObject<Aspose::Page::EPS::Device::ImageDevice>();
// אבל אם אתה צריך לציין גודל ופורמט תמונה השתמש בקונסטרוקטור עם פרמטרים
//ImageDevice device = new ImageDevice(new System.Drawing.Size(595, 842), System.Drawing.Imaging.ImageFormat.Jpeg);
{
auto __finally_guard_0 = ::System::MakeScopeGuard([&psStream]()
{
psStream->Close();
});
try
{
document->Save(device, options);
}
catch (...)
{
throw;
}
}
System::ArrayPtr<System::ArrayPtr<uint8_t>> imagesBytes = device->get_ImagesBytes();
int32_t i = 0;
{
for (System::ArrayPtr<uint8_t> imageBytes : imagesBytes)
{
System::String imagePath = System::IO::Path::GetFullPath(value + System::String(u"out_image") + System::Convert::ToString(i) + u"." + System::ObjectExt::ToString(imageFormat).ToLower());
{
System::SharedPtr<System::IO::FileStream> fs = System::MakeObject<System::IO::FileStream>(imagePath, System::IO::FileMode::Create, System::IO::FileAccess::Write);
// ניקוי משאבים תחת הצהרת 'משתמש'
System::Details::DisposeGuard<1> __dispose_guard_1({ fs });
try
{
fs->Write(imageBytes, 0, imageBytes->get_Length());
}
catch (...)
{
__dispose_guard_1.SetCurrentException(std::current_exception());
}
}
i++;
}
}
// בדוק שגיאות
if (suppressErrors)
{
//auto ex_enumerator = (System::DynamicCastEnumerableTo<PsConverterException> (options->get_Exceptions()))->GetEnumerator();
//decltype(ex_enumerator->get_Current()) ex;
//while (ex_enumerator->MoveNext() && (ex = ex_enumerator->get_Current(), true))
//{
// System::Console::WriteLine(ex->get_Message());
//}
}
המר XPS ל-PDF ב-C++
להלן השלבים להמרת מסמך XPS ל-PDF:
- צור אובייקטים FileStream עבור קלט XPS ופלט קובצי PDF.
- טען את זרם המסמכים של XPS לאובייקט XpsDocument.
- צור אובייקט מהמחלקה PdfDevice ואתחל אותו עם זרם הפלט.
- המר את מסמך XPS ל-PDF בשיטת XpsDocument->Save.
דוגמת הקוד הבאה מראה כיצד להמיר את מסמך XPS ל-PDF ב-C++.
System::SharedPtr<System::IO::Stream> pdfStream = System::IO::File::Open(u"XPStoPDF.pdf", System::IO::FileMode::Create, System::IO::FileAccess::Write);
// ניקוי משאבים תחת הצהרת 'משתמש'
System::Details::DisposeGuard<1> __dispose_guard_1({ pdfStream });
try {
System::SharedPtr<System::IO::Stream> xpsStream = System::IO::File::Open(u"input.xps", System::IO::FileMode::Open, System::IO::FileAccess::Read);
// ניקוי משאבים תחת הצהרת 'משתמש'
System::Details::DisposeGuard<1> __dispose_guard_0({ xpsStream });
try
{
// טען מסמך XPS מהזרם
System::SharedPtr<XpsDocument> document = System::MakeObject<XpsDocument>(xpsStream, System::MakeObject<XpsLoadOptions>());
// או טען מסמך XPS ישירות מהקובץ. אז אין צורך ב-xpsStream.
// XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
// אתחול אובייקט אפשרויות עם פרמטרים נחוצים.
System::SharedPtr<Aspose::Page::XPS::Presentation::Pdf::PdfSaveOptions> options = [&] { auto tmp_0 = System::MakeObject<Aspose::Page::XPS::Presentation::Pdf::PdfSaveOptions>(); tmp_0->set_JpegQualityLevel(100); tmp_0->set_ImageCompression(Aspose::Page::XPS::Presentation::Pdf::PdfImageCompression::Jpeg); tmp_0->set_TextCompression(Aspose::Page::XPS::Presentation::Pdf::PdfTextCompression::Flate); tmp_0->set_PageNumbers(System::MakeArray<int32_t>({ 1, 2, 6 })); return tmp_0; }();
// צור התקן עיבוד לפורמט PDF
System::SharedPtr<Aspose::Page::XPS::Presentation::Pdf::PdfDevice> device = System::MakeObject<Aspose::Page::XPS::Presentation::Pdf::PdfDevice>(pdfStream);
document->Save(device, options);
}
catch (...)
{
__dispose_guard_0.SetCurrentException(std::current_exception());
}
}
catch (...)
{
__dispose_guard_1.SetCurrentException(std::current_exception());
}
המר XPS לתמונה ב-C++
להלן השלבים להמרת מסמך XPS לפורמטים של תמונת רסטר:
- טען את מסמך XPS הקלט לתוך אובייקט FileStream.
- צור אובייקט של XpsDocument ואתחול אותו עם אובייקט זרם הקלט.
- הגדר את אפשרויות השמירה על ידי יצירת אובייקט מהמחלקה PngSaveOptions.
- המר XPS לתמונה בשיטת XpsDocument->Save.
דגימת הקוד הבאה מראה כיצד להמיר תמונת XPS ל-PNG ב-C++.
// קובץ קלט
System::String inputFileName = u"input.xps";
// קובץ פלט
System::String outputFileName = u"XPStoImage_out.png";
// אתחול זרם קלט XPS
{
System::SharedPtr<System::IO::Stream> xpsStream = System::IO::File::Open(inputFileName, System::IO::FileMode::Open, System::IO::FileAccess::Read);
// ניקוי משאבים תחת הצהרת 'משתמש'
System::Details::DisposeGuard<1> __dispose_guard_1({ xpsStream });
try
{
// טען מסמך XPS מהזרם
System::SharedPtr<XpsDocument> document = System::MakeObject<XpsDocument>(xpsStream, System::MakeObject<XpsLoadOptions>());
// או טען מסמך XPS ישירות מהקובץ. אז אין צורך ב-xpsStream.
// XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
// אתחול אובייקט אפשרויות עם פרמטרים נחוצים.
System::SharedPtr<PngSaveOptions> options = [&] { auto tmp_0 = System::MakeObject<PngSaveOptions>(); tmp_0->set_SmoothingMode(System::Drawing::Drawing2D::SmoothingMode::HighQuality); tmp_0->set_Resolution(300); tmp_0->set_PageNumbers(System::MakeArray<int32_t>({ 1, 2, 6 })); return tmp_0; }();
// צור התקן עיבוד לפורמט PDF
System::SharedPtr<ImageDevice> device = System::MakeObject<ImageDevice>();
document->Save(device, options);
// חזור דרך מחיצות מסמכים (מסמכים קבועים, במונחי XPS)
for (int32_t i = 0; i < device->get_Result()->get_Length(); i++)
{
for (int32_t j = 0; j < device->get_Result()[i]->get_Length(); j++)
{
// אתחול זרם פלט תמונה
{
System::SharedPtr<System::IO::Stream> imageStream = System::IO::File::Open(System::IO::Path::GetDirectoryName(outputFileName) + u"\\" + System::IO::Path::GetFileNameWithoutExtension(outputFileName) + u"_" + (i + 1) + u"_" + (j + 1) + System::IO::Path::GetExtension(outputFileName), System::IO::FileMode::Create, System::IO::FileAccess::Write);
// ניקוי משאבים תחת הצהרת 'משתמש'
System::Details::DisposeGuard<1> __dispose_guard_0({ imageStream });
try
{
imageStream->Write(device->get_Result()[i][j], 0, device->get_Result()[i][j]->get_Length());
}
catch (...)
{
__dispose_guard_0.SetCurrentException(std::current_exception());
}
}
}
}
}
catch (...)
{
__dispose_guard_1.SetCurrentException(std::current_exception());
}
}
להמרת מסמך XPS ל-BMP, TIFF ו-JPEG, בקר במאמר הבא.
למידע נוסף על Aspose.Page עבור C++
במאמר זה, ראית כיצד להמיר מסמכי PS, EPS ו-XPS ל-PDF, PNG, JPEG, TIFF ו-BMP באמצעות C++. אתה יכול ללמוד עוד על Aspose.Page for C++ באמצעות תיעוד ודוגמאות קוד מקור.