private void LoadPdf(byte[] bytes, ref PDDocument document) { try { InputStream ins = new ByteArrayInputStream(bytes); document = PDDocument.loadNonSeq(ins, null); } catch (Exception) { try { InputStream ins = new ByteArrayInputStream(bytes); document = PDDocument.load(ins, null, true); } catch (IOException e) { throw new PdfNotReadableException($"The pdf could not be loaded correctly. The fileformat might be corrupted.", e); } catch (Exception e) { throw new PdfReadException($"Could not load byte array of pdf", e); } } if (document.isEncrypted()) { try { document.decrypt(""); } catch (InvalidPasswordException e) { throw new PdfNotReadableException($"The pdf is password protected.'", e); } } }