تحويل PS وEPS وXPS

حل واجهة برمجة تطبيقات محول PS وEPS وXPS لـ Java

 

كلما كانت هناك حاجة لتحويل ملفات PostScript PS و Encapsulated PostScript EPS وكذلك مستندات XPS برمجياً، يمكن لـ Java API القيام بذلك بسلاسة وتحويل ملفات متعددة. بالنسبة لـ PS وEPS، تدعم واجهة برمجة التطبيقات مشغلات PostScript من المستويات 1-3 ومعظم تعليقات رأس EPS، بالإضافة إلى تحويل مستندات PostScript مع أقصى قدر من المطابقة باستثناء حالات قليلة من الخطوط، وتتعامل واجهة برمجة التطبيقات مع هذه الخطوط مثل Time New Roman.

علاوة على ذلك، بالنسبة لتحويل ملفات XPS، يمكن لواجهة برمجة التطبيقات إضافة الصفحات أو إزالتها، والتعامل مع عناصر اللوحات (canvases) والمسارات (paths) والرموز (glyphs)، وإنشاء أشكال رسومات متجهة وسلاسل نصية، وتحويل عناصر مخطط XPS، والمزيد.

يتيح لك حل واجهة برمجة التطبيقات لـ Java هنا تحويل ملفات تنسيقات PDL مثل PS وEPS وXPS برمجياً، ولكن قد تجد من المفيد رؤية وتجربة الأدوات متعددة المنصات المطورة على واجهات برمجة التطبيقات الأصلية هذه.

تحويل PostScript إلى PDF عبر Java.

لتحويل ملفات PostScript PS و Encapsulated PostScript EPS إلى PDF عبر Java API، تحتاج إلى اتباع الخطوات التالية:

  1. تحميل ملف PS أو EPS باستخدام PsDocument Class .
  2. ضبط خيارات حفظ PDF باستخدام PdfSaveOptions Class .
  3. استخدام FileStream Class لملف PDF الناتج.
  4. استخدام PdfDevice Class مع كائن FileOutputStream كمعامل.
  5. استدعاء PsDocument.Save لحفظ الملف بتنسيق PDF.
كود Java لتحويل PS EPS إلى PDF
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Initialize PS document with PostScript file
PsDocument document = new PsDocument(dataDir + "input.ps");

// If you want to convert Postscript file despite of minor errors set this flag
boolean suppressErrors = true;

//Initialize options object with necessary parameters.
PdfSaveOptions options = new PdfSaveOptions(suppressErrors);
// Default page size is 595x842 and it is not mandatory to set it in PdfDevice
// But if you need to specify size and image format use following line
// PdfSaveOptions options = new PdfSaveOptions(suppressErrors, new Dimension(595, 842));
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
//options.setAdditionalFontsFolders(new String [] {"FONTS_FOLDER"});

// Save PS document to PDF file
document.saveAsPdf(dataDir + "PStoPDF.pdf", options);

//Review errors
if (suppressErrors) {
    for (Exception ex : pdfOptions.getExceptions()) {
            System.out.println(ex.getMessage());
        }
}
 

تحويل PostScript إلى صور عبر Java.

لأيي تطبيق محول من EPS/PS PostScript إلى صور، يعمل كود Java التالي بشكل جيد، لذا اتبع الخطوات التالية:

  1. تهيئة تيار الإدخال مع ملف مصدر PS.
  2. إنشاء كائن PsDocument مع تيار إدخال PS الذي تم إنشاؤه كمعامل.
  3. استخدام ImageSaveOptions لتحديد AdditionalFontsFolder و SuppressError وما إلى ذلك.
  4. استخدام كائن ImageDevice لتحديد نوع الصورة وحجمها إذا لزم الأمر.
  5. حفظ ملف PS/EPS كصورة مع خيارات حفظ الصورة كصفوف من صفوف البايتات. صف واحد من البايتات لكل صفحة من ملف الإدخال.
كود Java لتحويل PostScript إلى صور
// The path to the documents directory.
String dataDir = Utils.getDataDir();
 // Initialize PS document with PostScript file
PsDocument document = new PsDocument(dataDir + "input.ps");

// If you want to convert Postscript file despite of minor errors set this flag
boolean suppressErrors = true;

// Initialize options object with necessary parameters.
ImageSaveOptions options = new ImageSaveOptions(suppressErrors);
// Default image format is PNG and it is not mandatory to set it in ImageSaveOptions.
// But if you need to specify another format use following line 
// options.setImageFormat(ImageFormat.JPEG);
// Default image size is 595x842 and it is not mandatory to set it in ImageDevice
// But if you need to specify size and image format use constructor with parameters
// ImageSaveOptions options = new ImageSaveOptions(new Dimension(595, 842), ImageFormat.JPEG, suppressErrors);
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
// options.setAdditionalFontsFolders(new String [] {"FONTS_FOLDER"});        

// Save PS document as images bytes arrays, one bytes array for one page of the document.
byte[][] imagesBytes = document.saveAsImage(options);

int i = 0;

for (byte [] imageBytes : imagesBytes) {
    String imagePath = dataDir + "PSToImage" + i + "." + options.getImageFormat().toString().toLowerCase();
    FileOutputStream fs = new FileOutputStream(imagePath);

    try {
        fs.write(imageBytes, 0, imageBytes.length);
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    } finally {
        fs.close();
    }
    i++;
}

//Review errors
if (suppressErrors) {
    for (Exception ex : options.getExceptions()) {
        System.out.println(ex.getMessage());
    }
}
 

تحويل XPS إلى صور JPG وPNG وBMP عبر Java.

تتعامل واجهة برمجة تطبيقات Java مع تنسيق XPS المستخدم لتمثيل تخطيط الصفحة. في أي سيناريو، كلما كانت هناك حاجة لتحويل XPS إلى صور BMP وJPG وPNG وTIFF برمجياً، يمكن دمج الكود التالي بسهولة داخل تطبيق Java.

  1. استخدم XpsDocument class لتحميل مستند XPS.
  2. استخدم فئة خيارات الصور ذات الصلة مثل PngSaveOptions أو JpegSaveOptions أو BmpSaveOptions أو TiffSaveOptions للإعدادات الإضافية للصور.
  3. إنشاء مثيل لفئة image device .
  4. استدعاء XpsDocument.save لحفظ صورة JPEG المحولة في كائن ImageDevice ثم استخدام ImageDevice لحفظ الصورة كـ JPG.
كود Java لتحويل XPS إلى صور
// The path to the documents directory.
String pathDir = Utils.getDataDir();
// Load XPS document
XpsDocument document = new XpsDocument(dataDir + "input.xps");
// Initialize options object with necessary parameters.
com.aspose.xps.rendering.PngSaveOptions options = new com.aspose.xps.rendering.PngSaveOptions();
options.setSmoothingMode(com.aspose.xps.rendering.SmoothingMode.HighQuality);
options.setResolution(300);
options.setPageNumbers(new int[] { 1, 2, 6 });

// Save XPS document as images bytes array. One bytes array for one page of every parttion in the document
byte [][][] imagesBytes = document.saveAsImage(options);

// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < imagesBytes.length; i++) {
    // Iterate through partition pages
    for (int j = 0; j < imagesBytes[i].length; j++) {
        // Initialize image output stream
        FileOutputStream imageStream = new FileOutputStream(dataDir + "XPStoPNG" + "_" + (i + 1) + "_" + (j + 1) + ".png");
        // Write image
        imageStream.write(imagesBytes[i][j], 0, imagesBytes[i][j].length);

        imageStream.close();
    }
}



FAQ

1. هل يمكنني تحويل Postscript باستخدام حل API هذا؟

Aspose.Page لديه وظيفة تسمح لك بتحويل ملفات PS و XPS و EPS إلى تنسيقات أخرى عبر الإنترنت أو برمجيًا. إذا كنت بحاجة إلى تحويل ملفاتك فورًا عبر الإنترنت ، فقد ترغب في استخدام تطبيق متعدد الأنظمة الأساسية محول ملفات تنسيق لغة وصف الصفحة .

2. ما هي اللغات التي يدعمها المحول لوصف الصفحة؟

تدعم وظيفة التحويل هذه الملفات ذات الامتدادات .ps و .eps و. xps. يتم تمثيل PDLs الشهيرة مثل PDF و SVG كحلول منفصلة في Aspose.products

3. هل الوظيفة مجانية؟

تعد المحولات متعددة الأنظمة الأساسية مجانية ، عندما يمكنك الحصول على نسخة تجريبية مجانية من حل API ثم شراء المنتج إذا لزم الأمر.

 

تحويل XPS إلى PDF عبر Java.

عملية تحويل مستندات XPS إلى PDF برمجياً بسيطة مع نتائج عالية الدقة بين ملفات الإدخال والإخراج.

  1. تحميل الملف باستخدام فئة XpsDocument. تهيئة كائن PdfSaveOptions class .
  2. إنشاء كائن PdfDevice للرنين وفي النهاية حفظ مستند PDF الناتج.
كود Java لتحويل XPS إلى PDF
// Load input XPS document
XpsDocument document = new XpsDocument(dataDir + "input.xps");

// Initialize options object with necessary parameters
PdfSaveOptions options = new PdfSaveOptions();
options.setJpegQualityLevel(100);
options.setImageCompression(PdfImageCompression.Jpeg);
options.setTextCompression(PdfTextCompression.Flate);

// Save XPS document to output PDF file
document.saveAsPdf(dataDir + "XPStoPDF.pdf", options);
 
  

Support and Learning Resources