Detect file which contains password

i’m converting the files(doc,docx,ppt,pptxpdf,xls,xlsx) to type(Pdf,Png,Jpeg)
if a file contains password it should skip the file & display it is password protected & continue with the process
can u give me the detailed example??

Hi Bharathkumar,

You can use the following code for Excel and Word documents

com.aspose.cells.FileFormatInfo excelInfo = com.aspose.cells.FileFormatUtil.detectFileFormat("PasswordProtected.xlsx");

boolean isExcelFileProtected = excelInfo.isEncrypted();

System.out.println(isExcelFileProtected);<?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" />

com.aspose.words.FileFormatInfo wordInfo = com.aspose.words.FileFormatUtil.detectFileFormat("PasswordProtected.docx");

boolean isWordFileProtected = wordInfo.isEncrypted();

System.out.println(isWordFileProtected);

For Aspose.Slides, there is no such feature and a request has been logged into our issue tracking system as SLIDESNET-35379. We will keep you updated on this issue in this thread. In the meantime you can use following workaround.

try{

PresentationEx pres = new PresentationEx("PasswordProtected.pptx");

} catch (com.aspose.slides.InvalidPasswordException e) {

// TODO Auto-generated catch block

System.out.println("Document is password protected");

}

Best Regards,

FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(fullpathoffile);
} catch (FileNotFoundException ex) {
convertedFileValues.put(“File inputstream error”, ex.getMessage());
}

BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
FileFormatInfo info = null;
try {
info = FileFormatUtil.detectFileFormat(bufferedInputStream);
} catch (Exception ex) {
logger.log(Level.SEVERE, ex.getMessage(), ex);
}
boolean encrypted;
// checks & sends the fomat

if ((this.EXTENSION.equalsIgnoreCase(“xls”)) || (this.EXTENSION.equalsIgnoreCase(“xlsx”))) {



encrypted = info.isEncrypted();
if (encrypted) {
System.out.println("---------------------------------");
logger.info(“The file is encrypted:”);
} else {


Workbook workbook = null;
try {
workbook = new Workbook(bufferedInputStream);
} catch (Exception ex) {
}
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();

int slidesCount = workbook.getWorksheets().getCount();
int countPages = slidesCount > 10 ? 5 : slidesCount;

timerstart
xlsConverter.convertToPDF(outPath, workbook);
timerend

timerstart
xlsConverter.convertToJpeg(outPath, workbook, countPages, imgOptions);
timerend

timerstart
xlsConverter.convertToPNG(outPath, workbook, countPages, imgOptions);
timerend

}
}

Hi Bharathkumar,

Yes, you are right. If we check encryption before conversion, it produces empty output files. Can you please share if you noticed this issue with other file types as well (other than Excel files)?

Best Regards,


it working instead of buffered input stream if we give path of the file directly

com.aspose.words.FileFormatInfo wordInfo = null;
try {
wordInfo = com.aspose.words.FileFormatUtil.detectFileFormat(path of file);
} catch (Exception ex) {

}

boolean isWordFileProtected = wordInfo.isEncrypted();
if(isWordFileProtected){
action-------------
}else{

i used this code

replace above code before else condition in my previous snippet

with help of hafeez i have done for pdf
i need for ppt&pptx


Hi Bharathkumar,

In fact, when you first read buffered input stream, it shifts the pointer at the end of the stream and when you try to convert the file, the pointer is at the end of stream so you see empty file.

There are two solutions to this issue.

1. Create new stream after checking if file is encrypted and before converting the file.

2. Use ByteArrayInputStream as you can see in the following code.

File file=new File("Sample.xlsx");

ByteArrayInputStream byteInputStream=new ByteArrayInputStream(FileUtils.readFileToByteArray(file));

com.aspose.cells.FileFormatInfo info = null;

try {

info = com.aspose.cells.FileFormatUtil.detectFileFormat(byteInputStream);

} catch (Exception ex) {

}

boolean encrypted;

encrypted = info.isEncrypted();

if (encrypted) {

System.out.println("---------------------------------");

} else {

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

Workbook workbook = null;

byteInputStream.reset();

try {

workbook = new Workbook(byteInputStream);

} catch (Exception ex) {

}

workbook.save("Out.pdf", com.aspose.cells.SaveFormat.PDF);

}

Best Regards,

The issues you have found earlier (filed as SLIDESNET-35379) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.
(8)
1 Like