Hi everyone,
The ability to set "fit" widths by default will be released next week, as well as pinch-to-zoom.
Regards,
Dimo
Progress Telerik
Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.
We still need this! A JavaScript workaround is possible, but complicated.
Some public functions, such as SetFitToWidth() or SetZoom(PdfViewerZoomLevelType? zoomLevelType = null), would suffice.
Some methods already exist in the "TelerikPdfViewer.cs" class, but all of them are private and therefore not easily accessible.
I have currently solved this using reflections on the private methods. I can use this quite easily directly in the code.
I created the following using reflection to allow you to change the zoom to "Fit to width" at any time, but you can also do "Fit to page" as well.
private async Task ZoomPdf()
{
if (_pdfViewerRef == null) return;var type = _pdfViewerRef.GetType();
var parameterTypes = new[] { typeof(PdfViewerZoomLevelDescriptor) };
var dynMethod = type.GetMethod("ZoomAsync", BindingFlags.NonPublic | BindingFlags.Instance, parameterTypes);
await (dynMethod?.Invoke(_pdfViewerRef, new object[] { GetZoomLevelDescriptor(_pdfViewerRef, type, "Fit to width") }) as Task ?? Task.CompletedTask);
}
private static PdfViewerZoomLevelDescriptor GetZoomLevelDescriptor(object? instance, IReflect type, string valueToFind)
{
// Use reflection to get the ZoomLevels property
var zoomLevelsProperty = type.GetProperty("ZoomLevels", BindingFlags.NonPublic | BindingFlags.Instance);
if (zoomLevelsProperty == null)
{
throw new InvalidOperationException("ZoomLevels property not found.");
}
// Get the value of the ZoomLevels property
var zoomLevelsValue = zoomLevelsProperty.GetValue(instance);
// Ensure the value is of the correct type (List<PdfViewerZoomLevelDescriptor>)
if (zoomLevelsValue is List<PdfViewerZoomLevelDescriptor> zoomLevels)
{
// Search for the descriptor with the specified value
var descriptor = zoomLevels.FirstOrDefault(z => z.Value == valueToFind);
return descriptor;
}
throw new InvalidOperationException("ZoomLevels property is not of the expected type.");
}
Hello Marc,
Thanks for your contribution. Indeed, a JavaScript workaround is possible. Here is a complete example for the Blazor PdfViewer.
Regards,
Dimo
Progress Telerik
A workaround for 'Fit to Width' can be accomplished by adding this JavaScript, but a regular setting would be better...
function setZoomOption(zoomOption) {
var combo = $(".k-combobox-clearable").find("input").last().getKendoComboBox();
if (combo) {
console.log("SetZoomOption: Zoomcombo found");
combo.select(zoomOption);
combo.trigger("change");
}
else {
console.log("SetZoomOption: Zoomcombo not found");
}
}
And call it from HTML:
<input type="button" value="Set ZoomOption" onclick="setZoomOption(2)" />
Or from Blazor: