C 設定或驗證PDF文字域格式的方法詳解

2022-09-20 18:12:12 字數 4641 閱讀 5860

目錄

pdf中的文字域可以通過設定不同格式,用於顯示數字、貨幣、日期、時間、郵政編碼、**號碼和社保號等等。adobe acrobat提供了許多固定的j**ascripts用來設定和驗證文字域的格式,如:afnumber_format(2, 0, 0, 0, "$", true)和afnumber_keystroke(2, 0, 0, 0, "$", true)。format字尾的script是用來設定文字域顯示的格式,而keystroke字尾的script是用來驗證輸入內容。

spire.pdf for .net提供了相應的方法來設定和驗證文字域格式。下面的**羅列了常用的格式和spire.pdf中對應的方法,可參考使用:

1.通過nuget安裝dll(2種方法)

1.1 可以在visual studio中開啟「解決方案資源管理器」,滑鼠右鍵點選「引用」,「管理nuget包」,然後搜尋「spire.pdf」,點選「安裝」。

1.2 將以下內容複製到pm控制台安裝。

install-package spire.pdf -version 7.12.1

2.手動新增dll引用

可通過手動**包,然後解壓,找到bin資料夾下的spire.pdf.dll。在visual studio中開啟「解決方案資源管理器」,滑鼠右鍵點選「引用」,「新增引用」將本地路徑bin資料夾下的dll檔案新增引用至程式。

c#using spire.pdf;

using spire.pdf.actions;

using spire.pdf.fields;

using system.drawing;

namespace settextformatintextboxfield}}

vb.net

imports spire.pdf

imports spire.pdf.actions

imports spire.pdf.fields

imports system.drawing

namespace settextformatintextboxfield

class program

private shared sub main(args as string())

'新建pdf文件,並新增空白頁

dim pdf as new pdfdocument()

dim page as pdfpagebase = pdf.pages.add()

'定義座標變數

dim x as single = 10

dim y as single = 10

dim width as single = 100

dim height as single = 20

'例項化乙個文字域物件,並設定它的位置和邊框樣式

dim textbox as new pdftextboxfield(page, "number-textbox")

textbox.bounds = new rectanglef(x, y, width, height)

textbox.borderwidth = 0.75f

textbox.borderstyle = pdfborderstyle.solid

'給文字域的鍵盤擊鍵事件設定乙個j**ascript動作用於驗證輸入內容是否符合要求

dim js as string = pdfj**ascript.getnumberkeystrokestring(2, 0, 0, 0, "$", true)

dim jsaction as new pdfj**ascriptaction(js)

textbox.actions.keypressed = jsaction

'設定文字域內容顯示為數字貨幣

js = pdfj**ascript.getnumberformatstring(2, 0, 0, 0, "$", true)

jsaction = new pdfj**ascriptaction(js)

textbox.actions.format = jsaction

'新增文字域到pdf中,並儲存文件

pdf.form.fields.add(textbox)

'新增文字框,設定文字內容顯示為日期格式

dim textbox1 as new pdftextboxfield(page, "dateformat-textbox")

textbox1.bounds = new rectanglef(x + 200, y, width, height)

twww.cppcns.comextbox1.borderwidth = 0.75f

textbox1.borderstyle = pdfborderstyle.solid

dim js1 as string = pdfj**ascript.getdatekeystrokestring("mm/dd/yyyy")

dim jsaction1 as new pdfj**ascriptaction(js1)

textbox1.actions.keypressed = jsaction1

js1 = pdfj**ascript.getdateformatstring("mm/dd/yyyy")

jsaction1 = new pdfj**ascriptaction(js1)

textbox1.actions.format = jsaction1

pdf.form.fields.add(textbox1)

'新增文字框,設定文字內容顯示為郵政編碼格式

dim textbox2 as new pdftextboxfield(page, "specialformat0-1-textbox")

textbox2.bounds = new rectanglef(x + 400, y, width, height)

textbox2.borderwidth = 0.75f

textbox2.borderstyle = pdfborderstyle.solid

'string js2 = pdfj**ascript.getspecialkeystrokestring(0);

dim js2 as string = pdfj**ascript.getspecialkeystrokestring(1)

dim jsaction2 as new pdfj**ascriptaction(js2)

textbox2.actions.keypressed = jsaction2

'js2 = pdfj**ascript.getspecialformatstring(0);

js2 = pdfj**ascript.getspecialformatstring(1)

jsaction2 = new pdfj**ascriptaction(js2)

textbox2.actions.format = jsaction2

pdf.form.fields.add(textbox2)

'新增文字框,設定文字內容顯示為百分數

dim textbox3 as new pdftextboxfield(page, "specialformat2-textbox")

textbox3.bounds = new rectanglef(x, y + 50, width, height)

textbox3.borderwidth = 0.75f

textbox3.borderstyle = pdfborderstyle.solid

dim js3 as string = pdfj**ascript.getpercentkeystrokestring(1, 0)

dim jsaction3 as new pdfj**ascriptaction(js3)

textbox3.actions.keypressed = jsaction3

js3 = pdfj**ascript.getpercentformatstring(1, 0)

jsaction3 = new pdfj**ascriptaction(js3)

textbox3.actions.format = jsaction3

pdf.form.fields.add(textbox3)

'新增文字框,設定資料驗證

dim textbox4 as new pdftextboxfield(page, "rangevalidate-textbox")

textbox4.bounds = new rectanglef(x + 200, y + 50, width, height)

textbox4.borderwidth = 0.75f

textbox4.borderstyle = pdfborderstyle.solid

dim js4 as string = pdfj**ascript.getrangevalidatestring(true, -18, true, 18)

dim jsaction4 as new pdfj**ascriptaction(js4)

textbox4.actions.format = jsaction4

pdf.form.fields.add(textbox4)

'儲存文件

pdf.s**etofile("formatfield.pdf", fileformat.pdf)

end sub

end class

end namespace

設定後的文字框域填寫效果如圖:

怎樣才能讓文字域的格式保持word的格式

在word裡面編輯好的格式文字貼上到文字框或者文字域,然後存入資料庫再讀取出來就沒有先前的格式了。怎麼才能讓資料保持在word中編排好的格式啊?我用的access資料庫,望高手幫助,謝謝,最好詳細點。百姓網用的什麼技術識別同一使用者?附個人分析 1.同樣的 裡的qq和手機,不同id會被識別為同一使用...

設定textarea標籤的文字域不可拉伸

textarea標籤 對於一般建立的textarea標籤,其建立乙個文字區,自帶有cols,rows,value,disable,readonly,id,name等屬性,設定cols和rows設定寬高。但是在文字區右下角有個小三角,可以任意拖動文字域的大小,在日常使用常不需要此功能,僅需固定其大小,...

用C 實現pdf檔案的完整性驗證

現在對檔案的完整性驗證,防止檔案被篡改的技術已經比較成熟,一般使用數字簽名,數字水印等,最近我在乙個專案中也遇到了防篡改的需求。該專案要求使用者將原始發票用專門的掃瞄程式掃瞄成pdf檔案,然後將該pdf檔案傳到伺服器上,在上傳的同時必須要驗證這個pdf是沒有被手工修改過的。我剛一接觸到這個需求想到的...