Error while trying to convert a TIF file to a PDF file

6 views (last 30 days)
Everytime I try to run 1 of the following options:
imageObject = PDImageXObject.createFromByteArray(document, image, 'TIF');
or
imageObject = LosslessFactory(document, image);
it gives me the error: No method 'org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.createFromByteArray'
with matching signature found.
And if I try
PDImageXObject.createFromFile(inputImage,document);
it gives me the error: java.io.IOException: First image in tiff is not CCITT T4 or T6.
The problem with this alternative is that I want the pdf colored, so inputImage has to be colored.
function ConvertToPDF(inputImage)%Image '.tif' or '.tiff'
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.graphics.image.*;
import java.io.*
import javax.imageio.ImageIO.*
image = imread(inputImage);
numPages=length(imfinfo(inputImage));
document = PDDocument();
for page = 1:numPages
image = image(:, :, :, page);
imageObject = PDImageXObject.createFromByteArray(document, image, 'TIF');%Error here
pdfPage = PDPage();
imageSize = size(image);
pdfPage.setMediaBox(PDRectangle(imageSize(2), imageSize(1)));
contentStream = PDPageContentStream(document, pdfPage);
contentStream.drawImage(imageObject, 0, 0, imageSize(2), imageSize(1));
contentStream.close();
document.addPage(pdfPage);
end
%Save
outputFile = 'output.pdf';
document.save(outputFile);
document.close();
end

Answers (1)

Mann Baidi
Mann Baidi on 21 Aug 2023
Edited: Mann Baidi on 3 Nov 2023
Hi Pablo,
I understand that you are facing issue in converting the TIF files into PDF files.
You can consider using the "print" function. For reference, please follow the sample code attached below:
ConvertToPDF('myfile.tif')
function ConvertToPDF(inputImage)%Image '.tif' or '.tiff'
image = imread(inputImage);
fig = figure;
imshow(image)
print(fig,'MySavedPlot','-dpdf');
%Save
end
Using the above attached sample code, you should be able to convert the TIF file into PDF.
For more details, you can refer to the "print" documentation using the following link.
I hope this helps.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!