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

7 visualizzazioni (ultimi 30 giorni)
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

Risposte (1)

Mann Baidi
Mann Baidi il 21 Ago 2023
Modificato: Mann Baidi il 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.

Categorie

Scopri di più su Convert Image Type in Help Center e File Exchange

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by