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

6 visualizaciones (últimos 30 días)
Pablo Collados
Pablo Collados el 15 de Jul. de 2023
Editada: Mann Baidi el 3 de Nov. de 2023
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

Respuestas (1)

Mann Baidi
Mann Baidi el 21 de Ag. de 2023
Editada: Mann Baidi el 3 de Nov. de 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.

Categorías

Más información sobre Convert Image Type en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by