如何使用自定义扩展在 Autodesk 查看器中缩放 PDF,或者是否有任何内置方法可以执行此操作?

问题描述 投票:0回答:1

我添加了使用 A0 到 A10 纸张尺寸缩放 pdf 的扩展。但我无法附加此代码以在查看器上显示 pdf 的确切大小。

// *******************************************
// 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);

这是我创建扩展的代码。 您能给我一些关于如何编写scalePdfToSize这个函数并将其附加到forge查看器的想法吗?

如您所见,我正在控制台中获取纸张尺寸的值。我只是希望将该值附加到 pdf,以便它可以缩放。

这是我用于创建扩展的 GitHub 链接

autodesk-forge autodesk-viewer autodesk
1个回答
0
投票

查看器不会以像素为单位缩放模型。初始大小取决于查看器容器的大小。如果您只想以编程方式放大和缩小,请参阅这个答案

如果您想要可打印的格式,请使用viewer.getScreenShot()函数,有关更多信息,请参阅此博客

© www.soinside.com 2019 - 2024. All rights reserved.