Extract a figure from a pdf file
    23 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
pdfFile = "example.pdf" % String
function extractImagePDF(pdfFile)
import java.io.*
import javax.imageio.ImageIO.*
import org.apache.pdfbox.*
filename = fullfile(pwd,pdfFile);
jFile = File(filename);
document = pdmodel.PDDocument.load(jFile);
catalog = document.getDocumentCatalog();
pages = catalog.getPages();
 iter = pages.iterator();
% look for image objects on each page of the PDF
while (iter.hasNext())  
    page = iter.next();
    resources = page.getResources();
    pageImages = resources.getXObjectNames;
    if ~isempty(pageImages)
        imageIter = pageImages.iterator();
        i = 1;
        % extract each image object from page and write to destination folder
        while (imageIter.hasNext())
            key = imageIter.next();
            if (resources.isImageXObject(key))
                xObject = resources.getXObject(key);            
                img = xObject.getImage();
                outputfile = File("Img_"+i +".jpg");
                write(img, "jpg", outputfile);
            end
            i = i+1;
        end
    end
end
document.close();
end
%% I want to extract Figure 1 (Fig. 1. Geometrical representation of the model), Page 5 from the attached pdf
%% The code I got from this forum but unable to adjust
2 Commenti
  Sachin Lodhi
      
 il 2 Mag 2024
				Hello Minati, please refer to the following Matlab answer post : https://www.mathworks.com/matlabcentral/answers/709623-how-can-i-convert-a-scanned-pdf-to-an-image-using-matlab 
  DGM
      
      
 il 2 Mag 2024
				I don't know why the java tools can't find it, but here it is.  I just used Inkscape to get the object. 

Risposte (0)
Vedere anche
Categorie
				Scopri di più su Creating, Deleting, and Querying Graphics Objects in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


