We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

AsposePDF for Java PDF to Tiff Selected pages

My intent is to take an inbound PDF and split it into multiple outbound tiff files. Seems simple enough.

But I also want each output tiff file to start with the first page of the PDF. Which sounded simple enough.

Snipped of code:
OutputStream imageStream = null;
Document pdfDocument;

Resolution resolution = new Resolution(200);
	
TiffSettings tiffSettings = new TiffSettings();
tiffSettings.setCompression(CompressionType.CCITT4);
tiffSettings.setDepth(ColorDepth.Format4bpp);
tiffSettings.setSkipBlankPages(true);
	
TiffDevice tiffDevice;
tiffDevice = new TiffDevice(resolution, tiffSettings);
	
try {
                
    File inFile= new File(C:/Development/testdata/Brian.pdf");
    
    pdfDocument = new Document(inputFilePath);

    imageStream = new FileOutputStream("C:/Development/testdata/Brian_1.tiff");
    tiffDevice.process(pdfDocument, 1, 100, imageStream);
    imageStream.close();		
                
    imageStream = new FileOutputStream("C:/Development/testdata/Brian_2.tiff");
    tiffDevice.process(pdfDocument, 1, 1, imageStream);
    tiffDevice.process(pdfDocument, 101, 200, imageStream);
    imageStream.close();		
            
	} catch (FileNotFoundException e1) {
		e1.printStackTrace();
	}
	catch (IOException e) {
		e.printStackTrace();
	} catch (ParseException e) {
		e.printStackTrace();
	}

Seems simple enough. And does create two tiff files. The first file is just file, contains 1-100 viewable pages.
However, the second output shows only the first page. Even though the size of the output file is clearly large enough to include “page 1” of the PDF as well as “pages 101 thru 200”.

When I remove the writing of the first PDF page to the second file, the resulting tiff file is viewable as expected.

There is something not quite right about how I am attempting to write a page, and then more pages to the output tiff.

Looking for suggestions that are not “split the PDF into multiple PDF files and then process those”.

The inbound PDF file is 391 pages.

Thanks.

@bduanebcbsma

Thanks for contacting support.

Would you please share respective PDF document with us. We will test the scenario in our environment and address it accordingly.

SystemAdmin_T9.pdf (9.2 MB)

The issue does not appear to be related to the inbound file, as I tested with a different file and found the same results.

I have attached the second file I tested. It has 597 pages.

@bduanebcbsma

Thanks for sharing sample PDF document.

We were able to notice the incorrect output after appending PDF Pages to existing image stream. For the sake of investigation regarding your requirements, we have logged a ticket as PDFJAVA-37976 in our issue tracking system. We will further investigate the scenario in details and check the feasibility of the feature. As soon as some definite progress is made, we will let you know. Please spare us little time.

We are sorry for the inconvenience.

Good Morning,
I see that the ticket PDFJAVA-37976 has been marked as closed. I have not received any information on the resolution.

Please advise.

Thanks you
Brian

@bduanebcbsma

We apologize for the delay in notification.

We have investigated the logged ticket and found that function i.e. tiffDevice.process() writes a TIFF file in the stream and when it uses twice then it writes two tiff files in one stream (which is not right approach). Please, use the code snippet below for creating desired Brian_2.tiff.

tiffSettings.setSkipBlankPages(false);

Document pdfDocument2 = new Document();
pdfDocument2.getPages().add(pdfDocument.getPages().get_Item(1));

for (int i = 101; i <= 200; i++) {
  pdfDocument2.getPages().add(pdfDocument.getPages().get_Item(i));
}

imageStream = new FileOutputStream(dataDir + "Brian_2.tiff");
tiffDevice.process(pdfDocument2, 1, pdfDocument2.getPages().size(), imageStream);

Please try above code snippet with Aspose.PDF for Java 18.9 and in case you still face any issue, please feel free to let us know.