autodesk-forgeautodesk-viewerautodesk

How to scale the PDF in the Autodesk viewer using a custom extension or if there is any inbuilt method to do so?


I have added the extension for scaling the pdf using paper sizes from A0 to A10. But I'm unable to attach this code the show the exact size of the pdf on the Viewer.

// *******************************************
// PDF Scaling Extension
// *******************************************
class PdfScalingExtension extends Autodesk.Viewing.Extension {
  constructor(viewer, options) {
    super(viewer, options);
    this.paperSizes = {
      A0: { width: 841, height: 1189, text: "A0 - 841mm x 1189mm" },
      A1: { width: 594, height: 841, text: "A1 - 594mm x 841mm" },
      A2: { width: 420, height: 594, text: "A2 - 420mm x 594mm" },
      A3: { width: 297, height: 420, text: "A3 - 297mm x 420mm" },
      A4: { width: 210, height: 297, text: "A4 - 210mm x 297mm" },
      A5: { width: 148, height: 210, text: "A5 - 148mm x 210mm" },
      A6: { width: 105, height: 148, text: "A6 - 105mm x 148mm" },
      A7: { width: 74, height: 105, text: "A7 - 74mm x 105mm" },
      A8: { width: 52, height: 74, text: "A8 - 52mm x 74mm" },
      A9: { width: 37, height: 52, text: "A9 - 37mm x 52mm" },
      A10: { width: 26, height: 37, text: "A10 - 26mm x 37mm" },
    };
  }

  async load() {
    // Load the extension
    return true;
  }

  unload() {
    // Unload the extension
    return true;
  }

  onToolbarCreated() {
    // Create a new toolbar group
    this._group = this.viewer.toolbar.getControl("PdfScalingExtensionsToolbar");
    if (!this._group) {
      this._group = new Autodesk.Viewing.UI.ControlGroup("PdfScalingExtensionsToolbar");
      this.viewer.toolbar.addControl(this._group);
    }

    // Paper Size Dropdown
    let paperSizeDropdown = new Autodesk.Viewing.UI.ComboButton("PaperSizeDropdown");
    paperSizeDropdown.setToolTip("Select Paper Size");
    paperSizeDropdown.icon.classList.add("fas", "fa-file-alt");
    paperSizeDropdown.container.style.color = "white"; // Set the color of dropdown text to white
    this._group.addControl(paperSizeDropdown);

    // Add paper size options to dropdown
    for (let size in this.paperSizes) {
      let option = new Autodesk.Viewing.UI.Button(size);
      option.setToolTip(this.paperSizes[size].text);
      option.text = this.paperSizes[size].text; // Set the text of the button to the paper size name
      option.onClick = (ev) => {
        this.scalePdfToSize(this.paperSizes[size]);
      };
      paperSizeDropdown.addControl(option);
    }
  }

  scalePdfToSize(size) {
    // Logic to scale PDF to the specified size
    // Example:
    // - Determine current PDF size
    // - Calculate scaling factor
    // - Apply scaling transformation
    console.log(`Scaling PDF to size: ${size.width}mm x ${size.height}mm`);
  }
}

Autodesk.Viewing.theExtensionManager.registerExtension("PdfScalingExtension", PdfScalingExtension);

This is the code from which I'm creating the extension. Can you please give me some idea about how I can write and attach the scalePdfToSize this function to the forge viewer.

enter image description here

As you can see I'm getting the value of the paper size in the console. I just want that value to get attach to the pdf so that it might get scaled.

This is the GitHub link I have used for creating extension


Solution

  • The viewer doesn't scale the model in pixels. The initial size depends on the viewer container size. If you just want to zoom in and zoom out progrmatically , please refer to this answer.

    In case you want it in printable format, use viewer.getScreenShot() function, for more on this, please refer to this blog.