拖放到pdf文件后获取元素位置(Angular-Nodejs)

时间:2019-12-01 08:45:39

标签: node.js angular hummus.js

我使用角ng2-pdf-viewer和角材料cdkDrag拖动元素。 在后端,我使用hummusJS / hummusRecipe修改pdf文件。

我正在寻找一种方法来知道将元素放置在pdf文件中的位置,然后发出请求以坐标方式后端以知道该元素需要在pdf文件中的位置。

这是我在nodejs上的功能:

const pdfEditor = async () => {
  const pdfDoc = new HummusRecipe("./pdf/demo.pdf", "./pdf/output.pdf");
  pdfDoc
    // edit 1st page
    .editPage(1)
    .text("Add some texts to an existing pdf file", 150, 500, {
      color: "003240"
    })
    .image("./dest/signature.png", 100, 600, {
      width: 100,
      keepAspectRatio: true
    })
    .endPage()
    .endPDF();
}

我的客户Angular:

<div class="container">
<div class="otherContainer">
  <button (click)="getEditedPDF()" style="width: 100px;">Save image with signature</button>
  <pdf-viewer *ngIf="pdfSrc" [(page)]="pageVariable" [show-all]="true" [render-text]="true" [original-size]="false" [autoresize]="true" [src]="pdfSrc"> </pdf-viewer>
  <div class="signatureContainer">
    <signature-pad [options]="signaturePadOptions" (onBeginEvent)="drawStart()"></signature-pad>
    <div class="btn-grid">
      <button (click)="submitPad()">Submit</button>
      <button (click)="clearPad()">Clear Pad</button>
      <button (click)="openImage()">Open Image</button>
    </div>
  </div>
</div>
<div class="image">
  <img *ngIf="imageExpress" [src]="imageExpress | safeHtml" cdkDrag>
</div>
<pdf-viewer *ngIf="editedPDF" [(page)]="pageVariable" [show-all]="true" [render-text]="true" [original-size]="false" [autoresize]="true" [src]="editedPDF"> </pdf-viewer>
</div>

1 个答案:

答案 0 :(得分:0)

import { Component, OnInit, Input } from '@angular/core';
import { CdkDragEnd, CdkDragStart, CdkDragMove, CdkDragDrop } from '@angular/cdk/drag-drop';
@Component({
  selector: 'app-pdf-drag-drop',
  templateUrl: './pdf-drag-drop.component.html',
  styleUrls: ['./pdf-drag-drop.component.scss']
})
export class PdfDragDropComponent implements OnInit {
  dragDroppables: any[];
  state = '';
  position = '';

  @Input() props: [{ [key: string]: object | any }];

  constructor() { }

  ngOnInit(): void {
    this.dragDroppables = this.props;
  }

  public dragStarted(event: CdkDragStart) {
    this.state = 'dragStarted';
  }

  public dragEnded(event: CdkDragEnd) {
    this.state = 'dragEnded';
    console.log(`State: ${this.state} ${this.position}`);
  }

  public dragMoved(event: CdkDragMove) {
    this.position = ` Position X: ${event.pointerPosition.x} - Y: ${event.pointerPosition.y}`;
  }

}

..然后在模板中添加:

  cdkDrag
  (cdkDragStarted)="dragStarted($event)"
  (cdkDragEnded)="dragEnded($event)"
  (cdkDragMoved)="dragMoved($event)"

这对我有用。