Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Created May 10, 2026 17:47
Show Gist options
  • Select an option

  • Save GroupDocsGists/9809fb763369222708591dec9aae9c93 to your computer and use it in GitHub Desktop.

Select an option

Save GroupDocsGists/9809fb763369222708591dec9aae9c93 to your computer and use it in GitHub Desktop.
Unlock, merge, and rotate passwords on PDF documents using GroupDocs.Merger for Java

Password-Protected PDF Merge and Rotation in Java

Password-protected PDF merge and rotation is a GroupDocs.Merger capability for Java that enables unlocking, joining, and re-protecting PDF files with custom credentials.

This gist demonstrates how to handle password-protected PDF documents using GroupDocs.Merger for Java (Java). It covers checking protection status, unlocking files, merging unlocked content, and rotating passwords on the final document.

What you get

This snippet shows four core operations: isDocumentProtected checks if a PDF is encrypted using isPasswordSet(), unlockAll decrypts multiple files and returns raw bytes, mergeAndProtect joins decrypted streams and applies a new password via AddPasswordOptions, and rotateUnifiedPassword updates the password on an existing protected file using updatePassword().

Files in this gist

  • README.md - this overview.
  • PasswordMergeDemo.java - the entire example, ~30 lines, heavy comments.

Install

  • Maven: mvn dependency:get -Dartifact=com.groupdocs:groupdocs-merger:24.6

Notes

  • The API supports PDF, DOCX, XLSX, PPTX, and other formats; this example focuses on PDF.
  • Input files may be password-protected or plain; the snippet handles both cases via LoadOptions.
  • This minimal form is ideal for batch processing pipelines; full projects may include logging, error handling, and UI.

GroupDocs.Merger for Java

GroupDocs.Merger for Java provides document manipulation features including merge, split, rotate, and password management for PDF, Office, and other formats in Java environments. This snippet exercises Merger, isPasswordSet(), removePassword(), join(), addPassword(), and updatePassword().

Related Resources

// Check if a PDF is password-protected
Merger merger;
if (password == null || password.isEmpty()) {
merger = new Merger(path);
} else {
merger = new Merger(path, new LoadOptions(password));
}
try {
return merger.isPasswordSet();
} finally {
merger.dispose();
}
// Unlock all password-protected PDFs and return raw bytes
List<byte[]> unlocked = new ArrayList<>();
for (Map.Entry<String, String> e : sources.entrySet()) {
String path = e.getKey();
String password = e.getValue();
System.out.println("Unlocking (credentials="
+ (password != null ? "yes" : "no")
+ "): " + path);
if (password == null || password.isEmpty()) {
unlocked.add(Files.readAllBytes(Paths.get(path)));
} else {
LoadOptions opts = new LoadOptions(password);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
Merger m = new Merger(path, opts);
try {
m.removePassword();
m.save(buf);
} finally {
m.dispose();
}
unlocked.add(buf.toByteArray());
}
}
return unlocked;
// Merge unlocked PDF byte arrays and protect the result
InputStream first =
new ByteArrayInputStream(unlockedPdfs.get(0));
Merger merger = new Merger(first);
try {
for (int i = 1; i < unlockedPdfs.size(); i++) {
merger.join(
new ByteArrayInputStream(unlockedPdfs.get(i)));
}
merger.addPassword(
new AddPasswordOptions(unifiedPassword));
merger.save(outputPath);
} finally {
merger.dispose();
}
System.out.println("Merged output: "
+ new File(outputPath).getAbsolutePath());
System.out.println("Unified password: "
+ unifiedPassword);
// Rotate the password on an already-protected merged PDF
Merger merger = new Merger(path, new LoadOptions(oldPassword));
try {
merger.updatePassword(new UpdatePasswordOptions(newPassword));
merger.save(outputPath);
} finally {
merger.dispose();
}
System.out.println("Rotated output: "
+ new File(outputPath).getAbsolutePath());
System.out.println("New password: " + newPassword);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment