แปลง PS, EPS และ XPS

โซลูชัน API ตัวแปลง PS, EPS และ XPS สำหรับ Java

 

เมื่อใดก็ตามที่มีความจำเป็นต้องแปลงไฟล์ PostScript PS และ Encapsulated PostScript EPS รวมถึงเอกสาร XPS ด้วยโปรแกรม Java API สามารถทำได้อย่างราบรื่นและแปลงไฟล์ได้หลายไฟล์ สำหรับ PS และ EPS นั้น API รองรับตัวดำเนินการ PostScript ระดับ 1-3 และความคิดเห็นส่วนหัว EPS ส่วนใหญ่ รวมถึงการแปลงเอกสาร PostScript โดยมีความสอดคล้องสูงสุด ยกเว้นในกรณีของฟอนต์บางกรณี ซึ่ง API จะจัดการฟอนต์ดังกล่าวเหมือนกับ Time New Roman

นอกจากนี้ สำหรับการแปลงไฟล์ XPS นั้น API สามารถเพิ่มหรือลบหน้า จัดการองค์ประกอบแคนวาส (canvases), เส้นทาง (paths) และไกลฟ์ (glyphs), สร้างรูปทรงกราฟิกแบบเวกเตอร์ และสตริงข้อความ, แปลงรายการเอาต์ไลน์ของ XPS และอื่นๆ

โซลูชัน API สำหรับ Java ที่นี่ช่วยให้คุณแปลงไฟล์รูปแบบ PDL เช่น PS, EPS และ XPS ด้วยโปรแกรมได้ แต่คุณอาจพบว่ามีประโยชน์ที่จะลองใช้เครื่องมือข้ามแพลตฟอร์มที่พัฒนาขึ้นบน API ดั้งเดิมเหล่านี้

การแปลง 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

สำหรับแอปพลิเคชันตัวแปลง PostScript EPS/PS เป็นรูปภาพใดๆ โค้ด 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 API จัดการรูปแบบ 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 เป็นรูปแบบอื่นทางออนไลน์หรือโดยทางโปรแกรม หากคุณต้องการแปลงไฟล์ออนไลน์ทันที คุณอาจต้องการใช้ Page Description Language format files Converter แอปพลิเคชันข้ามแพลตฟอร์ม

2. ตัวแปลงภาษาใดที่คำอธิบายเพจรองรับ?

ฟังก์ชันการแปลงนี้สนับสนุนไฟล์ที่มีนามสกุล .ps, .eps และ .xps PDL ที่มีชื่อเสียงเช่น 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);