Convert Base64 pdf to TIFF using Java

  1. I am trying to convert Base64 image to TIFF but i believe i have convert it first to PDF document then from PDF document i can convert to TIFF. Or do you have way in Aspose.PDF to convert it directly to TIFF ?

  2. Also, the image is color in PDF but when i am converting it is converted as black and white . Why ?

Can you please share the sample code for both of them? I have attached the Base64 for your reference.
Thanks
Gagan

@gaganpandey

Thank you for contacting support.

You may convert Base64 image to JPG without using Aspose.PDF for Java API, as under, and then perform further conversions with the API as per your requirements.

String source = "data:image/png;base64,iVBORw0KGgoAAA...";
String base64ImageString = source.replace("data:image/png;base64,", "");
byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64ImageString);
FileOutputStream fos = new FileOutputStream(dataDir + "Aspose.jpg");
try {
    fos.write(imageBytes);
}
finally {
    fos.close();
} 

Moreover, it is likely that your code snippet includes CompressionType.CCITT4; which generates Black and White TIFF images, so you may try using other compression options like CompressionType.None.

Furthermore, we can not find any files attached so you may share the code snippet and respective data via Google Drive, Dropbox in case of any further assistance.

Thanks for the response Farhan. I am using below code to convert base64 to PDF and then converting PDF to TIFF.
Base64 i am getting is PDF so, i have to convert them to PDF first. Please let me know how i can attach the sample base64? Also, the resolution of the TIFF is not good and the file size is too big. That’s why i am getting out of memory exception.

Sample Code :
public static void convertToBase64() throws IOException {
String extension = “pdf”;
String pathIn = “C:\Office\RND\ConvertpdfApp\DTImages.txt”;
String base64Text = “”;

    try {
        base64Text = new String(Files.readAllBytes(Paths.get(pathIn)));

        } catch (IOException ex) 
        {
        ex.printStackTrace();
        }
    
    //convert base64 string to binary data
    byte[] data = DatatypeConverter.parseBase64Binary(base64Text);
    String pathOut = "C:\\Gagan\\Office\\RND\\ConvertpdfApp\\PDFImages." + extension;
    File fileOut = new File(pathOut);
    
    try {
        OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(fileOut));
        outputStream.write(data);
        
    } catch (IOException e) {
        e.printStackTrace();
    }


 } 

public static void convertAllPDPagesToTIFFImages()
{

Document pdfDocument = new Document(“C:\Office\RND\ConvertpdfApp\PDFImages.pdf”);
int pageCount = pdfDocument.getPages().size();
for (int iCount =0; iCount < pageCount;iCount++ )
{
String tiffFile = “C:\Gagan\Office\RND\ConvertpdfApp\PDFImages”+ “pg”+ Integer.toString(iCount)+".tif";

                 // Create Resolution object
                Resolution resolution = new Resolution(300);
                // instantiate TiffSettings object
                TiffSettings tiffSettings = new TiffSettings();
                // set the compression of resultant TIFF image
                tiffSettings.setCompression(CompressionType.None);
                // set the color depth for resultant image
                tiffSettings.setDepth(ColorDepth.Format8bpp);
                // skip blank pages while rendering PDF to TIFF
                tiffSettings.setSkipBlankPages(true);
                
                //set image brightness
                tiffSettings.setBrightness(.5f);
                //set IndexedConversion Type, default value is simple
                tiffSettings.setIndexedConversionType(IndexedConversionType.Pixelated);
                
                //Create TiffDevice object with particular resolution
                TiffDevice objTiffDevice = new TiffDevice(2480, 3508,resolution,tiffSettings);
                
                // Create TiffDevice object with particular resolution old code
                //TiffDevice  objTiffDevice = new TiffDevice(resolution, tiffSettings); 
                // Convert a all pages of PDF file to TIFF format
                objTiffDevice.process(pdfDocument, iCount+1, iCount+1, tiffFile);
            }

}

I commented the Aspose code which is converting PDF to TIFF and process 20 Base64 file to convert them to PDF and they all worked fine. That mean my convertToBase64() code is working fine.
I see the issue with convertAllPDPagesToTIFFImages() code when it is trying to convert the PDF to TIFF.
I get “java.lang.OutOfMemoryError: Java heap space” error. Let me know if there is issue with the conversion code or we need to increase the heap size for java ?

@gaganpandey

Would you please share source PDF documents which are not successfully converted to TIFF images so that we may investigate your scenario and assist you accordingly. Moreover, the code snippet is fine and can convert PDF to TIFF so the issue is probably document specific or needs more memory.

I do not find a way to upload file. But it is normal PDF document which has one colored image and the PDF document size is 1.26 MB and when i am using Aspose to convert into TIFF it is converting it to 35.6MB.
Please let me know how to reduce the size of this file as the Java is not able to handle it. Is there a way instead of using Document Object we can use streaming? Will it resolve the memory issue ?
objTiffDevice.process(pdfDocument, iCount+1, iCount+1, tiffFile);

Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferByte.(DataBufferByte.java:76)
at java.awt.image.Raster.createInterleavedRaster(Raster.java:266)
at java.awt.image.BufferedImage.(BufferedImage.java:391)
at com.aspose.pdf.internal.l80t.lj.ld(Unknown Source)
at com.aspose.pdf.internal.l80t.lj.lI(Unknown Source)
at com.aspose.pdf.internal.l67k.lf.lI(Unknown Source)
at com.aspose.pdf.internal.l67k.lf.(Unknown Source)
at com.aspose.pdf.internal.l6l.lf.lI(Unknown Source)
at com.aspose.pdf.internal.l10v.le.(Unknown Source)
at com.aspose.pdf.internal.l10if.l0l.lI(Unknown Source)
at com.aspose.pdf.internal.l2u.ld.(Unknown Source)
at com.aspose.pdf.internal.l6t.lf.lI(Unknown Source)
at com.aspose.pdf.internal.l2y.lu.lI(Unknown Source)
at com.aspose.pdf.devices.lI.lI(Unknown Source)
at com.aspose.pdf.devices.TiffDevice.processInternal(Unknown Source)
at com.aspose.pdf.devices.DocumentDevice.process(Unknown Source)
at convertpdfapp.ConvertpdfApp.convertAllPDFPagesToTIFFImages_New(ConvertpdfApp.java:156)
at convertpdfapp.ConvertpdfApp.main(ConvertpdfApp.java:95)

I think this has few good points. Can you provide the Java version of this ?

@gaganpandey

You may simply drag and drop ZIP or PDF file in post editor for attaching it with your post so that we may reproduce and investigate your scenario exactly. Moreover, below is the Java version of code related to Aspose.Imaging for Java, for reducing size of TIFF image, as explained in the other thread you have referred to.

Image image =  Image.load("D:\\imaging_files\\Tiff2Tiff.tiff");
TiffOptions saveOptions = new TiffOptions(TiffExpectedFormat.Default);
saveOptions.setCompression(TiffCompressions.Jpeg);
saveOptions.setPhotometric(TiffPhotometrics.Rgb);
image.save("D:\\imaging_files\\Tiff2Tiff_ouput1.tiff", saveOptions);

Hi , we bought the license only for Aspose.PDF we i do not think i can use Image object.
Please find the attached PDF we are trying to convert to TIFF.
AIC0550642_173.pdf (1.2 MB)

@gaganpandey

We have been able to generate TIFF file of 8.3 MB size while consuming more than half GB of memory with Aspose.PDF for Java 19.2. Would you please verify if latest version is being used on your end while mentioning your environment details so that we may investigate further and sort this out.

Moreover, please also specify how are you calculating memory consumption for the process.