Skip to content

Commit ee2eed9

Browse files
committed
feat: add password support for encrypted PDFs (Phase 5.2)
- Add Password property to PdfSource base class - Android: Use .password() configurator to unlock encrypted PDFs - iOS: Use PdfDocument.Unlock() and IsLocked property - Handle password errors gracefully on both platforms - Throw error if PDF is locked but no password provided - Throw error if provided password is incorrect - Cross-platform password-protected PDF support complete
1 parent aeaa7fe commit ee2eed9

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

src/MauiNativePdfView/Abstractions/PdfSource.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ namespace MauiNativePdfView.Abstractions;
55
/// </summary>
66
public abstract class PdfSource
77
{
8+
/// <summary>
9+
/// Gets or sets the password for encrypted PDF documents.
10+
/// Leave null or empty for non-encrypted PDFs.
11+
/// </summary>
12+
public string? Password { get; set; }
13+
814
/// <summary>
915
/// Creates a PDF source from a file path.
1016
/// </summary>

src/MauiNativePdfView/Platforms/Android/PdfViewAndroid.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ private void ConfigureAndLoad(PDFView.Configurator configurator, int pageToResto
271271
bool enablePageSnap = _displayMode == Abstractions.PdfDisplayMode.SinglePage;
272272
bool enablePageFling = _displayMode == Abstractions.PdfDisplayMode.SinglePage;
273273

274+
// Set password if provided
275+
if (!string.IsNullOrEmpty(_source?.Password))
276+
{
277+
configurator.Password(_source.Password);
278+
}
279+
274280
configurator
275281
.EnableSwipe(_enableSwipe)
276282
.EnableDoubleTap(_enableZoom)

src/MauiNativePdfView/Platforms/iOS/PdfViewiOS.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,25 @@ private void LoadDocument()
292292

293293
if (document != null)
294294
{
295+
// Check if document is locked and attempt to unlock with password
296+
if (document.IsLocked)
297+
{
298+
if (!string.IsNullOrEmpty(_source.Password))
299+
{
300+
bool unlocked = document.Unlock(_source.Password);
301+
if (!unlocked)
302+
{
303+
OnError(new PdfErrorEventArgs("Failed to unlock PDF: incorrect password"));
304+
return;
305+
}
306+
}
307+
else
308+
{
309+
OnError(new PdfErrorEventArgs("PDF is password-protected but no password was provided"));
310+
return;
311+
}
312+
}
313+
295314
_pdfView.Document = document;
296315

297316
// Get document metadata
@@ -355,6 +374,11 @@ private void OnPageChangedNotification(NSNotification notification)
355374
}
356375
}
357376

377+
private void OnError(PdfErrorEventArgs args)
378+
{
379+
Error?.Invoke(this, args);
380+
}
381+
358382
private void HandleTap(UITapGestureRecognizer recognizer)
359383
{
360384
var location = recognizer.LocationInView(_pdfView);

0 commit comments

Comments
 (0)