.net - Tagging individual pages of a PDF with ItextSharp C# -
i working itextsharp 5.5.6.0
my goal add key each page , have persistent when read document again application. want able keep track of every page individually (the key unique, , comes source).
this import/write code:
using (pdfreader reader = new pdfreader(sourcepdfpath)) { using (document document = new document(reader.getpagesizewithrotation(pagenumber))) { pdfcopy pdfcopyprovider = new pdfcopy(document, new system.io.filestream(outputpdfpath, system.io.filemode.create)); pdfcopyprovider.settagged(); pdfcopyprovider.pdfversion = pdfwriter.version_1_7; pdfimportedpage importedpage = pdfcopyprovider.getimportedpage(reader, pagenumber, true); importedpage.setaccessibleattribute(pdfname.alt, new pdfstring("mykey")); pdfcopyprovider.addpage(importedpage); } }
this read code:
using (memorystream ms = new memorystream()) { document document = new document(); pdfcopy copy = new pdfcopy(document, ms); copy.settagged(); document.open(); (int = 0; < pdfs.count; ++i) { var pdf = file.readallbytes(pdfs[i]); pdfreader reader = new pdfreader(pdf); int n = reader.numberofpages; (int page = 0; page < n; ) { var importpage = copy.getimportedpage(reader, ++page, true); var mykey = importpage.getaccessibleattribute(pdfname.alt); if (mykey != null) //do key copy.addpage(importpage); } } document.close(); copy.close(); return ms.toarray(); }
i trying add accessibility alt text. currently, use attribute on images, , applications set leave attributes untouched.
the problem when add attribute way, save pdf file, , read on process, attribute no longer there.
i open other options, resolve problem of having primary key per page, can assign, read , remove
i trying avoid adding hidden field on each page.
i have little experience itext programming or c# i'm ideal answer question :)
first of all, if want mark page , afterwards find again, please do not use accessibility features in pdf. accessibility there assistive devices, abusing features isn't nice.
especially because - if understand correctly want - there no need so. if want mark page, should page dictionary, example:
pdfreader reader = new itextsharp.text.pdf.pdfreader(file_content); pdfdictionary pagedict = reader.getpagen(i);
copied from: http://goobbe.com/questions/8099416/how-to-get-the-userunit-property-from-a-pdffile-using-itextsharp-pdfreader
once have dict, can insert own private key in there:
public void put(pdfname key, pdfobject object);
the value assign you, if want follow rules, have use second class pdf name key. key consists of developer prefix - should registered unique , private part. example key like:
ficl:pagenumber
in case "ficl" developer prefix , "pagenumber" identification of data adding.
to register developer prefix, see adobe web site, example here: http://www.adobe.com/content/dam/adobe/en/devnet/acrobat/pdfs/pdfregistry_v3.pdf
hope helps.
ps: if here knows owns "ficl" prefix , letters come from, i'll buy beer :)
Comments
Post a Comment