【问题标题】:Is there a way to create a Stamp annotation using a SVG as ImageData有没有办法使用 SVG 作为 ImageData 创建 Stamp 注释
【发布时间】:2020-09-27 17:22:02
【问题描述】:

我想在 PDFTron 中使用 SVG 创建自定义图章注释。

我在某个谷歌论坛上看到有人有同样的问题,一位 PDFTron 开发人员实际上做出了回应,这就是我目前所拥有的......

const svgElement = '<svg>my svg</svg>'

const CustomStamp = function() {
   Annotations.StampAnnotation.call(this);
   this.ImageData = encodeURI('data:image/svg+xml,' + svgElement);
}

CustomStamp.prototype = new Annotations.StampAnnotation();

const CustomCreateTool = function(docViewer) {
            Tools.GenericAnnotationCreateTool.call(this, docViewer, CustomStamp);
        };

CustomCreateTool.prototype = new Tools.GenericAnnotationCreateTool();

CustomCreateTool.prototype.mouseLeftDown = function() {
            Tools.AnnotationSelectTool.prototype.mouseLeftDown.apply(this, arguments);
        };

CustomCreateTool.prototype.mouseMove = function() {
            Tools.AnnotationSelectTool.prototype.mouseMove.apply(this, arguments);
        };

CustomCreateTool.prototype.mouseLeftUp = function(e) {
            var annotation;
            Tools.GenericAnnotationCreateTool.prototype.mouseLeftDown.call(this, e);
            if (this.annotation) {
              this.annotation.Width = 50;
              this.annotation.Height = 50;
              this.annotation.X -= this.annotation.Width/2;
              this.annotation.Y -= this.annotation.Height/2;
              this.annotation.NoResize = true;
              annotation = this.annotation;
            }
            Tools.GenericAnnotationCreateTool.prototype.mouseLeftUp.call(this, e);
            if (annotation) {
              annotManager.redrawAnnotation(annotation);
            }

但是,它似乎只绘制了一个空框...任何帮助将不胜感激!

【问题讨论】:

    标签: svg uri pdftron


    【解决方案1】:

    我查看了您的代码,它似乎是正确的。自己尝试一下,我相信问题出在您的测试 SVG 上;看来,由于缺少 xmlns 标记,它会在调用 encodeURI 时导致 HTMLImage 元素无效。

    你能用下面的 SVG 试试你的代码吗?我用它来调试,戳记按预期工作。

    const svgElement = '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" /></svg>'
    

    【讨论】:

    • 嗨,Armando,非常感谢您的回答!确实,这似乎是我的 SVG 的问题,但我似乎不明白为什么……我正在使用来自 FontAwesome 的 SVG,这似乎是问题所在。另外,有没有办法使图章/SVG的颜色适应用户在工具箱中选择的颜色?
    • (edit) 我发现了问题,似乎是因为我的SVG中没有宽度和高度属性,所以它不起作用......现在我只是需要弄清楚如何制作它,以便 SVG 采用适当的颜色或填充...非常感谢 Armando!
    猜你喜欢
    • 2021-09-24
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 2021-06-15
    相关资源
    最近更新 更多