【问题标题】:Generate new PDF with SVG Markups使用 SVG 标记生成新的 PDF
【发布时间】:2021-01-30 19:57:29
【问题描述】:

我正在尝试使用扩展名 Autodesk.PDF 创建的 SVG 生成一个新的 PDF

        Autodesk.Viewing.Initializer(options, function onInitialized() {
            viewer = new Autodesk.Viewing.GuiViewer3D(document.getElementById('MyViewerDiv'));
            viewer.setTheme("light-theme");
            viewer.start();

            viewer.loadExtension('Autodesk.PDF').then(() => {
                viewer.loadModel(path, viewer);
                viewer.loadExtension("Autodesk.Viewing.MarkupsCore")
                viewer.loadExtension("Autodesk.Viewing.MarkupsGui")
            });
        });

我可以加载 PDF,但之后我真的不知道该怎么做。

在控制台中玩,我发现我可以得到一个列表,我可以迭代它并保存在一个变量中。

我在后端使用 iTextSharp 来管理我的应用程序的 PDF,所以如果我可以使用它将 SVG 添加到 PDF 中会很好,但我愿意尝试其他库。

这就是我尝试添加 SVG 的方式:

        var reader = new PdfReader(new MemoryStream(File.ReadAllBytes(HttpContext.Current.Server.MapPath(path))));
        var pdfDocument = new PdfDocument(reader);
        var writer = new PdfDocument(new PdfWriter(HttpContext.Current.Server.MapPath("newdoc.pdf")).SetSmartMode(true));
        pdfDocument.CopyPagesTo(1, 1, writer);
        //var appendedHtml = "<html><body>";
        var appendedHtml = "<svg xmlns='http://www.w3.org/2000/svg' version='1.1' baseProfile='full' layer-order-id='markups-svg' style='position: absolute; left: 0px; top: 0px; transform: scale(1, -1); touch-action: none; user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);' width='1105' height='690' viewBox='4.3959721282740105 4.558617165575239 30.58327865600586 19.097249984760307' pointer-events='painted' cursor='crosshair'><metadata><markup_document xmlns='http://www.w3.org/1999/xhtml' data-model-version='4'></markup_document></metadata><g cursor='inherit' pointer-events='stroke' style='touch-action: manipulation; user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);'><metadata><markup_element xmlns='http://www.w3.org/1999/xhtml' stroke-width='2.7548209366391347' stroke-color='#ff0000' stroke-opacity='1' fill-color='#ff0000' fill-opacity='0' type='rectangle' position='22.89795796238312 13.271543227939889' size='14.605644607543944 10.309867361327829' rotation='0'></markup_element></metadata><path id='markup' d='M -5.925411835452405 -3.777523212344347 l 11.85082367090481 0 l 0 7.555046424688694 l -11.85082367090481 0 z' stroke-width='2.7548209366391347' stroke='rgba(255,0,0,1)' fill='none' transform='translate( 22.89795796238312 , 13.271543227939889 ) rotate( 0 )'/></g></svg>";
        byte[] bytes = Encoding.UTF8.GetBytes(appendedHtml);
        SvgConverter.DrawOnDocument(new MemoryStream(bytes), writer, 1);
        pdfDocument.Close();
        writer.Close();

新的 PDF 文件已正确生成,但没有 SVG...

编辑: 显然 style transform: scale(1, -1) 使我的 SVG 在 PDF 上消失了,没有它我可以看到 SVG,但它的位置错误,大小也错误。

左边是正确的,右边是通过我的代码之后

【问题讨论】:

标签: c# asp.net svg autodesk-forge itext7


【解决方案1】:

恐怕在 Forge 方面我们无能为力。 Forge Viewer 中的Autodesk.PDF 扩展仅支持阅读 PDF,不支持写入它们。您可以做的是将 Forge Viewer 中的当前视图输出为光栅图像,并将标记 SVG 绘制到其中(参见 How do I get a snapshot of the forgeviewer without markups)。

【讨论】:

    猜你喜欢
    • 2015-07-29
    • 2016-01-14
    • 2011-08-15
    • 2018-05-07
    • 2016-08-30
    • 1970-01-01
    • 2017-07-05
    • 2019-06-13
    • 2018-03-07
    相关资源
    最近更新 更多