2

I need to save a pdf document, generated by aspose.pdf for java library to memory (without using temporary file)

I was looking at the documentation and didn't find the save method with the appropriate signature. (I was looking for some kind of outputstream, or at least byte array).

Is it possible? If it is, how can I manage that?

Thanks

ppopoff
  • 658
  • 7
  • 17
  • maybe this can help you: http://www.aspose.com/community/forums/thread/326929/how-to-write-pdf-stream-to-pdf-file.aspx – ivan Dec 23 '15 at 00:08
  • also this may work too: http://www.aspose.com/docs/display/pdfjava/Create+a+Hello+World+PDF+document+through+API – ivan Dec 23 '15 at 00:13
  • Thanks, but the first link about C#, I'm using java. In the second they are using `pdf1.save("HelloWorld.pdf")`, but I'm looking for something like `pdf1.save(outputStream)` – ppopoff Dec 23 '15 at 00:29

2 Answers2

9

Aspose.Pdf for Java supports saving output to both file and stream. Please check following code snippet, It will help you to accomplish the task.

byte[] input = getBytesFromFile(new File("C:/data/HelloWorld.pdf"));
ByteArrayOutputStream output = new ByteArrayOutputStream();
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(new ByteArrayInputStream(input));
pdfDocument.save(output);
//If you want to read the result into a Document object again, in Java you need to get the
//data bytes and wrap into an input stream.
InputStream inputStream=new ByteArrayInputStream(output.toByteArray());

I am Tilal Ahmad, developer evangelist at Aspose.

Tilal Ahmad
  • 940
  • 5
  • 9
-1

I did similar thing.

Here is method to write data to byte:

public byte[] toBytes() {
        //create byte array output stream object
        ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
        //create new data output stream object
        DataOutputStream outStream = new DataOutputStream(byteOutStream);
        try {//write data to bytes stream
            if (data != null) {
            outStream.write(data);//write data
            }//return array of bytes
        return byteOutStream.toByteArray();
    }

Then you do something like

yourFileName.toBytes;
mer063
  • 47
  • 6
  • 1
    I was looking for the solution for the *specific* library in this case (`aspose.pdf for java`), which saves documents as files on a hard drive using the following method: `pdfFile.save("/name/of/the/file.pdf")`. I was expecting the solution with outputstream as the parameter. – ppopoff Dec 23 '15 at 00:33
  • Did you encounter any issue while using ByteArrayOutputStream object as shared by Tilal above. In case you are facing any problem, please share your resource files by posting query in Aspose.Pdf product support forum. – codewarior Dec 27 '15 at 16:13