简体   繁体   English

在 Java 中使用 Ghost4J 渲染大型 Post Script 文件

[英]Rendering big Post Script file with Ghost4J in Java

i made a Java application whose purpose is to offer a Print Preview for PS files.我制作了一个 Java 应用程序,其目的是为 PS 文件提供打印预览。

My program uses Ghostscript and Ghost4J to load the Post Script file and produces a list of Images (one for each page) using the SimpleRenderer.render method.我的程序使用 Ghostscript 和 Ghost4J 加载 Post Script 文件,并使用 SimpleRenderer.render 方法生成图像列表(每页一个)。 Then using a simple JList i show only the image corresponding to the page the user selected in JList.然后使用一个简单的 JList 我只显示与用户在 JList 中选择的页面对应的图像。

This worked fine until a really big PS file occurred, causing an OutOfMemoryError when executing the code这工作正常,直到出现一个非常大的 PS 文件,在执行代码时导致 OutOfMemoryError

PSDocument pdocument = new PSDocument(new File(filename));

I know that is possibile to read a file a little at a time using InputStreams, the problem is that i can't think of a way to connect the bytes that i read with the actual pages of the document.我知道使用 InputStreams 一次读取一个文件是可能的,问题是我想不出一种方法将我读取的字节与文档的实际页面连接起来。

Example, i tried to read from PS file 100 MB at a time例如,我尝试一次从 PS 文件中读取 100 MB

int buffer_size = 100000000;
byte[] buffer = new byte[buffer_size];

FileInputStream partial = new FileInputStream(filename);
partial.read(buffer, 0, buffer_size);
document.load(new ByteArrayInputStream(buffer));

SimpleRenderer renderer = new SimpleRenderer();

//how many pages do i have to read?
List<Image> images = renderer.render(document, firstpage ??, lastpage ??);

Am i missing some Ghost4J functionality to read partially a file?我是否缺少一些 Ghost4J 功能来部分读取文件? Or has someone other suggestions / approaches about how to solve this problem in different ways?或者有其他关于如何以不同方式解决此问题的建议/方法? I am really struggling我真的很挣扎

I found out I can use Ghost4J Core API to retrieve from a Post Script file a reduced set of pages as Images.我发现我可以使用 Ghost4J Core API 从 Post Script 文件中检索一组缩减的页面作为图像。

Ghostscript gs = Ghostscript.getInstance();

String[] gsArgs = new String[9];
        gsArgs[0] = "-dQUIET";
        gsArgs[1] = "-dNOPAUSE";
        gsArgs[2] = "-dBATCH";
        gsArgs[3] = "-dSAFER";
        gsArgs[4] = "-sDEVICE=display";
        gsArgs[5] = "-sDisplayHandle=0";
        gsArgs[6] = "-dDisplayFormat=16#804";
        gsArgs[7] = "-sPageList="+firstPage+"-"+lastPage;
        gsArgs[8] = "-f"+filename;
        
         //create display callback (capture display output pages as images)
  ImageWriterDisplayCallback displayCallback = new ImageWriterDisplayCallback();
 
        //set display callback
        gs.setDisplayCallback(displayCallback);
        
        //run PostScript (also works with PDF) and exit interpreter
        try {
 
            gs.initialize(gsArgs);
            gs.exit();
            Ghostscript.deleteInstance();
 
        } catch (GhostscriptException e) {
            System.out.println("ERROR: " + e.getMessage());
            e.printStackTrace();
        }

       return displayCallback.getImages(); //return List<Images>

This solve the problem of rendering page as images in the preview.这解决了在预览中将页面渲染为图像的问题。

However, i could not find a way to use Ghost4J to know total number of pages of PS file (in case the file is too big for opening it with Document.load()).但是,我找不到使用 Ghost4J 了解 PS 文件总页数的方法(以防文件太大而无法使用 Document.load() 打开)。

So, i am still here needing some help所以,我还在这里需要一些帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM