[英]PDFTron : How to add an input field
我嘗試使用 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();
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.