Free Support Forum - aspose.com

Extract Images from Password protected pdf

Hi,


I’m using PdfExtractor.HasNextImage() to know if there are any images in the document. It works fine if the file is not password protected. But how do I pass the password while Binding the pdf through BindPdf(). I can pass an already opened pdfDoc but the Intellisense is just showing me a Input path or Stream as the input parameters where as the documentation I got with the Aspose PDF for .Net shows it accepts a PdfDoc object as well. This confused me.

Can someone please help in finding a way to use this pdfExtractor or any other way to know if a password protected pdf file contains images or not.

Thanks,
John.

Hi John,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for considering Aspose.Pdf for .NET.

I think you may try Facades.PdfFileSecurity to dcrypt the file first and then check / extract the images from the Pdf file.

Please see the following sample code and let us know if it fits your needs:

FileStream inputStream = new FileStream(@"D:\AP Data\Source_Files\input.pdf", FileMode.Open);

MemoryStream outputStream = new MemoryStream();

PdfFileSecurity fileSecurity = new PdfFileSecurity(inputStream, outputStream);

fileSecurity.DecryptFile("abc123");

//open input PDF

PdfExtractor pdfExtractor = new PdfExtractor();

pdfExtractor.BindPdf(outputStream);

//extract images

pdfExtractor.ExtractImage();

//get extracted images

while (pdfExtractor.HasNextImage())

{

//read image into memory stream

MemoryStream memoryStream = new MemoryStream();

pdfExtractor.GetNextImage(memoryStream);

//write to disk, if you like, or use it otherwise.

FileStream fileStream = new

FileStream(@"D:\AP Data\Source_Files\"+ DateTime.Now.Ticks.ToString() + ".jpg",

FileMode.Create);

memoryStream.WriteTo(fileStream);

fileStream.Close();

}

Thank You & Best Regards,

Thanks Nausherwan,


That worked!

As I mentioned, all I need is to find is if the document has any images, So is there a way to find it with out doing ExtractImage(since this method looks like it extracts each and every image in the file). Just wondering if there is another way to boost the performance a bit. Please Let me know.

Thanks,
John.

Hi John,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I think you may try to navigate through pages and check the Page.Resources.Images to get your desired result. Please see the following sample code in this regard:

FileStream inputStream = new FileStream(@"D:\AP Data\Source_Files\input.pdf", FileMode.Open);

MemoryStream outputStream = new MemoryStream();

PdfFileSecurity fileSecurity = new PdfFileSecurity(inputStream, outputStream);

fileSecurity.DecryptFile("abc123");

//open document

Document pdfDocument = new Document(outputStream);

//get the page where image needs to be added

foreach (Aspose.Pdf.Page page in pdfDocument.Pages)

{

//check image count in Images collection of Page Resources

if (page.Resources.Images.Count > 0)

{

//Your Code Here If Image Exists

}

}

If you need any further assistance, please feel free to contact support.

Thank You & Best Regards,