Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.0.3</Version>
<Version>9.0.5</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
35 changes: 9 additions & 26 deletions src/components/BootstrapBlazor.PdfViewer/PdfViewer.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public partial class PdfViewer
[Parameter]
public string? Url { get; set; }

/// <summary>
/// Gets or sets the page index of the PDF file.
/// </summary>
[Parameter]
public int PageIndex { get; set; }
Comment on lines +23 to +24
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider defaulting PageIndex to 1 to avoid invalid PDF page references.

Defaulting to 1 aligns with standard PDF viewer behavior and helps prevent errors from invalid page references.

Suggested change
[Parameter]
public int PageIndex { get; set; }
[Parameter]
public int PageIndex { get; set; } = 1;


/// <summary>
/// Gets or sets the viewer height. Default is null.
/// </summary>
Expand Down Expand Up @@ -52,9 +58,6 @@ public partial class PdfViewer
.AddClass($"--bb-pdf-viewer-height: {Height};", !string.IsNullOrEmpty(Height))
.Build();

private string? _url;
private bool _useGoogleDocs;

private string? UseGoogleDocsString => UseGoogleDocs ? "true" : null;

/// <summary>
Expand All @@ -66,29 +69,9 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);

if (firstRender)
{
_url = Url;
_useGoogleDocs = UseGoogleDocs;
return;
}

var rerender = false;
if (_url != Url)
{
_url = Url;
rerender = true;
}

if (_useGoogleDocs != UseGoogleDocs)
{
_useGoogleDocs = UseGoogleDocs;
rerender = true;
}

if (rerender)
if (!firstRender)
{
await InvokeVoidAsync("loadPdf", Id, GetAbsoluteUri(_url));
await InvokeVoidAsync("loadPdf", Id, GetAbsoluteUri(Url));
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Appending '#page={PageIndex}' to all URLs may break some PDF viewers or integrations.

Some viewers or contexts may not handle the '#page=' fragment correctly. Consider making this addition optional or context-aware.

Suggested implementation:

        url ??= string.Empty;
        // Add an optional parameter to control appending the page fragment
        return GetProcessedUrl(url, appendPageFragment: false);

    }

    private string GetProcessedUrl(string url, bool appendPageFragment = false)
    {
        if (string.IsNullOrEmpty(url) || !UseGoogleDocs)
        {
            if (appendPageFragment)
            {
                return $"{url}#page={PageIndex}";
            }
            return url;
        }
        var uri = NavigationManager.ToAbsoluteUri(url);
        if (appendPageFragment)
        {
            return $"{uri.AbsoluteUri}#page={PageIndex}";
        }
        return uri.AbsoluteUri;

You will need to update all usages of this code to call GetProcessedUrl(url, appendPageFragment: true) if you want to append the #page={PageIndex} fragment in specific contexts. By default, the fragment will not be appended, making the behavior context-aware and opt-in.

Expand All @@ -108,7 +91,7 @@ private string GetAbsoluteUri(string? url)
url ??= string.Empty;
if (string.IsNullOrEmpty(url) || !UseGoogleDocs)
{
return url;
return $"{url}#page={PageIndex}";
}
var uri = NavigationManager.ToAbsoluteUri(url);
return uri.AbsoluteUri;
Expand Down