Free Support Forum - aspose.com

How convert and merge 2 documents without file system

Hi,

My workflow is as follows

  • generate a word document
  • get pdf document
  • merge them in one tiff image.

I use Aspose Java Total family.
I know that there is an image load from an inputStream.
I known that there is a word document save into an outputStream
and a pdf process into an outputStream.
Is it possible

  1. to save a word & a pdf into outputStreams
  2. to load images from those outputStreams
  3. to merge them into one tiff
  4. to save it into outputStream

Thank you in advance,
Sergei

@SICRIF

As per our understanding, you want to extract the images from Word and PDF and save them into one TIFF file. In this case, we suggest you please read the following articles.
How to Extract Images from a Document using Aspose.Words
Extract Images from the PDF File using Aspose.PDF

Once you have images from both documents, insert the images into Aspose.Words.Document (word document) and save the final document to TIFF using Aspose.Words. Please read the following article.
Inserting an Image

Hope this helps you.

It does not help unfortunatly.

I’m not going to extract images from documents and insert them into another,.
I’m going to convert a document into an image and than convert a pdf into another image
and than to merge 2 images into one.
I know how to do it somehow…
The question is as follows.

How to make those operations using inputstreams and outputstreams without files.
That is the question…

Thank you in advance.

@SICRIF

In your case, we suggest you following solution.

  1. Save the Word document to PDF (stream) using Aspose.Words.
  2. Merge the PDF files using Aspose.PDF. You can import the PDF file from stream into Document using Aspose.PDF.
  3. Save the final PDF into TIFF.

Hope this helps you.

Could you please write down a line of code for step2.
How do you suggest to combine a stream from step1 with another stream.
Thank you in advance…

@SICRIF

The following basic code example shows how to save the Word document into the stream (PDF), merge the PDF files with another or new PDF (using stream), and create a TIFF file (stream) from the final PDF:

com.aspose.words.Document doc = new com.aspose.words.Document(dataDir + "Sample_21.1.docx");
ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
doc.save(dstStream, com.aspose.words.SaveFormat.PDF);

byte[] dataBytes = dstStream.toByteArray();
ByteArrayInputStream byteStream = new ByteArrayInputStream(dataBytes);
com.aspose.pdf.Document pdf1 = new com.aspose.pdf.Document(byteStream);

// You can initialize another existing PDF below using input stream as well
com.aspose.pdf.Document pdf2 = new com.aspose.pdf.Document();
pdf2.getPages().add();
pdf1.getPages().add(pdf2.getPages());
ByteArrayOutputStream pdfoutStream = new ByteArrayOutputStream();
pdf1.save(pdfoutStream);
        
byte[] pdfBytes = pdfoutStream.toByteArray();
ByteArrayInputStream pdfinStream = new ByteArrayInputStream(pdfBytes);

//Open document
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(pdfinStream);
ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
com.aspose.pdf.devices.Resolution resolution = new com.aspose.pdf.devices.Resolution(300);
com.aspose.pdf.devices.TiffSettings tiffSettings  = new com.aspose.pdf.devices.TiffSettings();
tiffSettings.setCompression(com.aspose.pdf.devices.CompressionType.CCITT4);
tiffSettings.setDepth(com.aspose.pdf.devices.ColorDepth.Format4bpp);
tiffSettings.setSkipBlankPages(true);
tiffSettings.setIndexedConversionType(com.aspose.pdf.devices.TiffSettings.IndexedConversionType.Pixelated);
com.aspose.pdf.devices.TiffDevice   tiffDevice = new com.aspose.pdf.devices.TiffDevice(2480, 3508,resolution,tiffSettings);
tiffDevice.process(pdfDocument,1,1,imageStream);

Thank a lot for code.
So my origional question was as follows.
Is it possible somehow connect outputstreams into inputstreams without terminated operations like

byte[] pdfBytes = pdfoutStream.toByteArray();

or

byte[] dataBytes = dstStream.toByteArray();

We are about to find an answer…
Just one step more.
Thank you in advance.

@SICRIF

You can convert output stream into input stream as shown above.

Moreover, please read the following Java article.
Java Convert OutputStream to InputStream Example

Thank you.
There is another way to connect input and output streams.
Using PipedOutputStream​ and PipedInputStream without byte arrays and files.
I tried this way in my task.
But it did not work.
That is whey I asked the question.
It seems that I did it not in a proper way.
So correct question is as follows
Is it possible to solve my task with PipedOutputStream​ and PipedInputStream?
Thank you in advance

@SICRIF

Please share a narrowed-down code snippet so that we may know how you are trying to use PipedInputStream and PipedOutputStream with Aspose APIs. We will test the scenario in our environment and share our feedback with you accordingly.

    Document doc = new Document(dataDir + "Hello.docx");
    PipedInputStream is = new PipedInputStream();
    PipedOutputStream os = new PipedOutputStream (is);
    CountDownLatch count = new CountDownLatch(1);
    new Thread (()->{
        System.out.println("doc.save start");
        try {doc.save(os,SaveFormat.TIFF);} catch (Exception e) {e.printStackTrace();}
        System.out.println("doc.save done");
        count.countDown();
    }).start();
    count.await();
    System.out.println("Image.load start");
    TiffImage tiff = (TiffImage) Image.load(is);
    System.out.println("Image.load done");
    tiff.save(dataDir+"Hello-stream.tiff");
    System.out.println("Done");

@SICRIF

It seems like you are using Aspose.Words in your code snippet to generate TIFF image. We are testing the scenario from this perspective and will get back to you in a while.

Yes.
I use Words.
I understand that I can convert doc to tiff in one call.
It is just an example to make it throuth steams…
Thank you in advacen

@SICRIF

Thanks for the confirmation. We are checking it and will share our feedback with you shortly.

@SICRIF

We used the below code snippet according to your original work flow as described in your first post:

com.aspose.words.Document doc = new com.aspose.words.Document(dataDir + "Sample_21.1.docx");
PipedInputStream is = new PipedInputStream();
PipedOutputStream os = new PipedOutputStream(is);
doc.save(os, com.aspose.words.SaveFormat.PDF);

com.aspose.pdf.Document pdf1 = new com.aspose.pdf.Document(is);
// You can initialize another existing PDF below using input stream as well
com.aspose.pdf.Document pdf2 = new com.aspose.pdf.Document();
pdf2.getPages().add();
pdf1.getPages().add(pdf2.getPages());
pdf1.save(os);

//Open document
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(is);
com.aspose.pdf.devices.Resolution resolution = new com.aspose.pdf.devices.Resolution(300);
com.aspose.pdf.devices.TiffSettings tiffSettings  = new com.aspose.pdf.devices.TiffSettings();
tiffSettings.setCompression(com.aspose.pdf.devices.CompressionType.CCITT4);
tiffSettings.setDepth(com.aspose.pdf.devices.ColorDepth.Format4bpp);
tiffSettings.setSkipBlankPages(true);
tiffSettings.setIndexedConversionType(com.aspose.pdf.devices.TiffSettings.IndexedConversionType.Pixelated);
com.aspose.pdf.devices.TiffDevice   tiffDevice = new com.aspose.pdf.devices.TiffDevice(2480, 3508,resolution,tiffSettings);
tiffDevice.process(pdfDocument,1,1,os);

We noticed that the code kept running for more than 10 minutes and did not generate any output. We need to further investigate the scenario and for the purpose, an investigation ticket as PDFJAVA-40205 has been logged in our issue tracking system. We will further look into its details and keep you posted with the status of its resolution. Please be patient and spare us some time.

We are sorry for the inconvenience.