Skip to content

feat(PdfViewer): add PageIndex parameter#506

Merged
ArgoZhang merged 4 commits intomasterfrom
feat-pdf
Jul 30, 2025
Merged

feat(PdfViewer): add PageIndex parameter#506
ArgoZhang merged 4 commits intomasterfrom
feat-pdf

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented Jul 30, 2025

Link issues

fixes #505

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Introduce a new PageIndex parameter, update URL generation to include the page anchor, and simplify reload logic by removing redundant state fields in PdfViewer.

New Features:

  • Add PageIndex parameter to PdfViewer component to allow specifying the initial page index of the PDF.

Enhancements:

  • Streamline OnAfterRenderAsync by removing URL and UseGoogleDocs state tracking and always invoking loadPdf on updates.

@bb-auto bb-auto Bot added the enhancement New feature or request label Jul 30, 2025
@bb-auto bb-auto Bot added this to the v9.2.0 milestone Jul 30, 2025
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Jul 30, 2025

Reviewer's Guide

Introduces a PageIndex parameter to PdfViewer and refactors rendering logic by removing internal state caching, ensuring the PDF reloads correctly with the specified page index.

Sequence diagram for PDF reload with PageIndex parameter

sequenceDiagram
    actor User
    participant PdfViewer
    participant JSInterop

    User->>PdfViewer: Set Url or PageIndex
    PdfViewer->>PdfViewer: OnAfterRenderAsync(false)
    PdfViewer->>JSInterop: InvokeVoidAsync("loadPdf", Id, GetAbsoluteUri(Url))
    JSInterop-->>PdfViewer: PDF loaded at specified page
Loading

Class diagram for updated PdfViewer component

classDiagram
    class PdfViewer {
        +string? Url
        +int PageIndex
        +string? Height
        +bool UseGoogleDocs
        +string? UseGoogleDocsString
        +Task OnAfterRenderAsync(bool firstRender)
        +string GetAbsoluteUri(string? url)
    }
Loading

File-Level Changes

Change Details Files
Add PageIndex parameter and integrate it into URL generation
  • Declared new PageIndex parameter with XML summary and Parameter attribute
  • Updated GetAbsoluteUri to append '#page={PageIndex}' fragment to the URL when not using Google Docs
src/components/BootstrapBlazor.PdfViewer/PdfViewer.razor.cs
Simplify OnAfterRenderAsync by removing manual state tracking
  • Removed private fields _url and _useGoogleDocs
  • Eliminated manual comparisons for Url and UseGoogleDocs
  • Always invoke loadPdf with the current Url on non-first renders
src/components/BootstrapBlazor.PdfViewer/PdfViewer.razor.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#505 Add a PageIndex parameter to the PdfViewer component.
#505 Ensure the PageIndex parameter is used to control the displayed page in the PDF viewer.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@ArgoZhang ArgoZhang merged commit 2b9a285 into master Jul 30, 2025
1 check passed
@ArgoZhang ArgoZhang deleted the feat-pdf branch July 30, 2025 02:56
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey @ArgoZhang - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `src/components/BootstrapBlazor.PdfViewer/PdfViewer.razor.cs:24` </location>
<code_context>
+    /// Gets or sets the page index of the PDF file.
+    /// </summary>
+    [Parameter]
+    public int PageIndex { get; set; }
+
     /// <summary>
</code_context>

<issue_to_address>
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.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
    [Parameter]
    public int PageIndex { get; set; }
=======
    [Parameter]
    public int PageIndex { get; set; } = 1;
>>>>>>> REPLACE

</suggested_fix>

### Comment 2
<location> `src/components/BootstrapBlazor.PdfViewer/PdfViewer.razor.cs:111` </location>
<code_context>
         url ??= string.Empty;
         if (string.IsNullOrEmpty(url) || !UseGoogleDocs)
         {
-            return url;
+            return $"{url}#page={PageIndex}";
         }
         var uri = NavigationManager.ToAbsoluteUri(url);
</code_context>

<issue_to_address>
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:

```csharp
        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.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +23 to +24
[Parameter]
public int PageIndex { get; set; }
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;

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(PdfViewer): add PageIndex parameter

1 participant