我尝试使用PDFTron添加输入字段,但PDF中没有任何内容,

我遵循了这个文件:
https://www.pdftron.com/documentation/samples/js/InteractiveFormsTest

这是我尝试的:

 (() => {
    window.addEventListener('documentLoaded', async () => {
       await PDFNet.initialize();
       const doc = readerControl.docViewer.getDocument();
       const pdfDoc = await doc.getPDFDoc();
       await pdfDoc.requirePage(1);
       await PDFNet.runWithCleanup(async () => await main(pdfDoc));
       readerControl.docViewer.refreshAll();
       readerControl.docViewer.updateView();
  });

  async function main(pdfDoc) {
       ...
       const pageRect = await PDFNet.Rect.init(0, 0, 612, 794);
       let page = await pdfDoc.pageCreate(pageRect);
       const empFirstName = await pdfDoc.fieldCreateFromStrings('test', PDFNet.Field.Type.e_text, 'John', 'fg');
      const annot1 = await PDFNet.WidgetAnnot.create(pdfDoc, await PDFNet.Rect.init(50, 550, 350, 600), empFirstName);
      page.annotPushBack(annot1);


      pdfDoc.pagePushBack(page);
      pdfDoc.refreshFieldAppearances();
      ...
 };


结果:
  PDF上没有任何内容

任何想法?

最佳答案

我查看了您的代码并进行了一些修改-我不确定被截断的内容,但也许您错过了一些步骤。

让我知道这是否澄清了以编程方式添加字段的过程,以及是否还有其他问题。

window.addEventListener('documentLoaded', async() => {
const docViewer = readerControl.docViewer;
const annotManager = docViewer.getAnnotationManager();
await PDFNet.initialize();
const doc = readerControl.docViewer.getDocument();
const pdfDoc = await doc.getPDFDoc();
await pdfDoc.requirePage(1);
await PDFNet.runWithCleanup(async () => {
  const document = docViewer.getDocument();
  const pdfDoc = await document.getPDFDoc();

  const empFirstName = await pdfDoc.fieldCreateFromStrings('test', PDFNet.Field.Type.e_text, 'John', 'fg');
  const annot1 = await PDFNet.WidgetAnnot.create(pdfDoc, await PDFNet.Rect.init(50, 550, 350, 600), empFirstName);

  const rotation = docViewer.getCompleteRotation(1) * 90;
  annot1.setRotation(rotation);



  // draw the annotation the viewer
  const page = await pdfDoc.getPage(1);
  await page.annotPushBack(annot1);
  await pdfDoc.refreshFieldAppearances();

  // import newly created form fields
  const fdfDoc = await pdfDoc.fdfExtract(PDFNet.PDFDoc.ExtractFlag.e_both);
  const xfdf = await fdfDoc.saveAsXFDFAsString();

  await annotManager.importAnnotations(xfdf);

  // refresh viewer
  docViewer.refreshAll();
  docViewer.updateView();
  document.refreshTextData();
});


});

08-06 07:17