iTextSharp How include GenericTag using XML Parsing

☆樱花仙子☆ 提交于 2019-12-25 01:27:34

问题


I'm using iTextSharp library, specifically to view .xml documents and transform them into .pdf files as follows:

var docWriter = PdfWriter.GetInstance(document, new FileStream("DOC.pdf", FileMode.Create));

ITextHandler xmlHandler = new ITextHandler(document);

xmlHandler.Parse("MLDOC.xml");

I need to know if there is any way to include any attribute within the tags in my .xml file to indicate that they are GenericTag, or otherwise how I can identify a paragraph which I include a PdfAnnotation for example. I've tried to subscribe to the event OnGenericTag, but using the XML parser: xmlHandler.Parse("MLDOC.xml");, this method is never executed

Thanks!


回答1:


You're using an Old version of iText. I strongly recommend you get a more recent version... A post-5.0 release if you're okay with the AGPL, something prior (4.2 IIRC) if you'd rather stick with LGPL/MPL.

There ISN'T an iTextHandler class anymore.

You need to add the generic tags to the iText elements as they're created before iText will call your OnGenericTag handler.

The only way I can think to do this would be to Change The Source. For HTMLWorker, you'd have to change FactoryProperties so it'd look for some magic attributes you'd introduce for the generic tag's value.

In the 5.0.6 iTextSharp source, it looks like you'd want to modify ElementFactory.CreateChunk(), and insert something like:

if (chain.HasProperty(MY_MAGIC_PROPERTY)) {
  ck.setGenericTag( chain[MY_MAGIC_PROPERTY] );
}

Issue: ChainedProperties propagate to their child nodes. If you tagged a table with a generic tag, EVER CHUNK IN THAT TABLE would have that tag. Probably not what you want. }



来源:https://stackoverflow.com/questions/5745696/itextsharp-how-include-generictag-using-xml-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!