Skip to content

Commit

Permalink
Updated viewer/[net|java]/how-to-check-if-file-is-encrypted topic
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-litvinchik committed May 14, 2024
1 parent 2eaa766 commit 5bfd8d9
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,23 @@ hideChildren: False

The encryption protects data and allows only authorized people to open the file. So, only those who have an encryption key (such as a password) can open and view a file.

If you want to check if a file is encrypted, use the `getFileInfo()` method that returns the file type and flag that indicates if the file is encrypted. The following code snippet shows how to check if a file is encrypted.
## Check if a file is encrypted online

1. Navigate to the [GroupDocs.Viewer App](https://products.groupdocs.app/viewer/total).

![GroupDocs.Viewer App](/viewer/java/images/developer-guide/retrieving-document-information/how-to-check-if-file-is-encrypted/viewe-app-upload-file.png)

2. Upload your file. If you are prompted to enter a password to open the file, it is encrypted.

![GroupDocs.Viewer App password prompt](/viewer/java/images/developer-guide/retrieving-document-information/how-to-check-if-file-is-encrypted/viewe-app-password-prompt.png)

{{< alert style="warning" >}}
It's important to be cautious about uploading sensitive files to online services. For critical files, we recommend using a programmatic method.
{{< /alert >}}

## Programmatically check for file encryption

Use the _getFileInfo()_ method that returns the file type and flag that indicates if the file is encrypted. The following code snippet shows how to check if a file is encrypted.

{{< tabs "example1">}}
{{< tab "Java" >}}
Expand All @@ -20,19 +36,24 @@ import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.results.FileInfo;
// ...

try (Viewer viewer = new Viewer("encrypted.pdf")) {
try (Viewer viewer = new Viewer("protected.pdf")) {
FileInfo fileInfo = viewer.getFileInfo();

System.out.println("File type is: " + fileInfo.getFileType());
System.out.println("File encrypted: " + fileInfo.isEncrypted());
}

```
{{< /tab >}}
{{< /tabs >}}

{{< alert style="tip" >}}

Download a [sample application](/viewer/java/sample-apps/developer-guide/retrieving-document-information/how-to-check-if-file-is-encrypted/how-to-check-if-file-is-encrypted.zip) written in Java that uses this code snippet. You can run the application locally using the `mvn exec:java` command. Ensure you have the [Java](https://www.oracle.com/java/technologies/downloads/) and [Maven](https://maven.apache.org/download.cgi) installed beforehand.

{{< /alert >}}

The following image shows a sample console output:

![](/viewer/java/images/how-to-check-if-file-is-encrypted.png)

To learn how to open an encrypted file, please refer to [Load password-protected document]({{< ref "/viewer/java/developer-guide/loading-documents/load-password-protected-document.md" >}}) page.
To learn how to open an encrypted file, please refer to [Load password-protected document]({{< ref "/viewer/java/developer-guide/loading-documents/load-password-protected-document.md" >}}) documentation topic.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,33 @@ hideChildren: False

The encryption protects data and allows only authorized people to open the file. So, only those who have an encryption key (such as a password) can open and view a file.

If you want to check if a file is encrypted, use the _GetFileInfo()_ method that returns the file type and flag that indicates if the file is encrypted. The following code snippet shows how to check if a file is encrypted. Also, you can find this example [on GitHub](https://github.com/groupdocs-viewer/GroupDocs.Viewer-for-.NET/blob/master/Examples/GroupDocs.Viewer.Examples.CSharp/BasicUsage/CheckFileIsEncrypted.cs).
## Check if a file is encrypted online

1. Navigate to the [GroupDocs.Viewer App](https://products.groupdocs.app/viewer/total).

![GroupDocs.Viewer App](/viewer/net/images/developer-guide/retrieving-document-information/how-to-check-if-file-is-encrypted/viewe-app-upload-file.png)

2. Upload your file. If you are prompted to enter a password to open the file, it is encrypted.

![GroupDocs.Viewer App password prompt](/viewer/net/images/developer-guide/retrieving-document-information/how-to-check-if-file-is-encrypted/viewe-app-password-prompt.png)

{{< alert style="warning" >}}
It's important to be cautious about uploading sensitive files to online services. For critical files, we recommend using a programmatic method.
{{< /alert >}}

## Programmatically check for file encryption

Use the _GetFileInfo()_ method that returns the file type and flag that indicates if the file is encrypted. The following code snippet shows how to check if a file is encrypted.

{{< tabs "example1">}}
{{< tab "C#" >}}
```csharp
using System;
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
using GroupDocs.Viewer.Results;
// ...
using (Viewer viewer = new Viewer("encrypted.pdf"))
using (Viewer viewer = new Viewer("protected.pdf"))
{
// Get file information.
FileInfo fileInfo = viewer.GetFileInfo();
Expand All @@ -37,13 +52,12 @@ using (Viewer viewer = new Viewer("encrypted.pdf"))
```vb
Imports System
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
Imports GroupDocs.Viewer.Results
' ...

Module Program
Sub Main(args As String())
Using viewer As Viewer = New Viewer("encrypted.pdf")
Using viewer As Viewer = New Viewer("protected.pdf")
' Get file information.
Dim fileInfo As FileInfo = viewer.GetFileInfo()

Expand All @@ -57,8 +71,14 @@ End Module
{{< /tab >}}
{{< /tabs >}}

{{< alert style="tip" >}}

Download a [sample application](/viewer/net/sample-apps/developer-guide/retrieving-document-information/how-to-check-if-file-is-encrypted/how-to-check-if-file-is-encrypted.zip) written in C# that uses this code snippet. You can run the application locally using the `dotnet run` command. Ensure you have the [.NET SDK](https://dotnet.microsoft.com/download) installed beforehand.

{{< /alert >}}

The following image shows a sample console output:

![](/viewer/net/images/how-to-check-if-file-is-encrypted.png)

To learn how to open an encrypted file, please refer to [Load password-protected document]({{< ref "/viewer/net/developer-guide/loading-documents/load-password-protected-document.md" >}}) page.
To learn how to open an encrypted file, please refer to [Load password-protected document]({{< ref "/viewer/net/developer-guide/loading-documents/load-password-protected-document.md" >}}) documentation topic.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.

0 comments on commit 5bfd8d9

Please sign in to comment.