iTextSharp XmlWorker: right-to-left

元气小坏坏 提交于 2019-12-24 01:53:52

问题


After a long time of struggling with this not-so-friendly API, I am finally making progress, but now I've come to a really nasty issue.. I have placed "dir" attributes in various places in my html with the value being "rtl".. but the XMLWorker doesn't seem to respect that at all. Does anyone know of a workaround? Here's my method:

public static void Generate<TModel>(string templateFile, TModel model, string outputFile, IEnumerable<string> fonts)
        {
            string template = System.IO.File.ReadAllText(templateFile);
            string result = Razor.Parse(template, model);

            using (var fsOut = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
            using (var stringReader = new StringReader(result))
            {
                var document = new Document();
                var pdfWriter = PdfWriter.GetInstance(document, fsOut);
                pdfWriter.InitialLeading = 12.5f;
                document.Open();

                var xmlWorkerHelper = XMLWorkerHelper.GetInstance();

                var cssResolver = new StyleAttrCSSResolver();
                //cssResolver.AddCss(cssFile);

                var xmlWorkerFontProvider = new XMLWorkerFontProvider();

                foreach (string font in fonts)
                {
                    xmlWorkerFontProvider.Register(font);
                }

                var cssAppliers = new CssAppliersImpl(xmlWorkerFontProvider);
                var htmlContext = new HtmlPipelineContext(cssAppliers);
                htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());

                PdfWriterPipeline pdfWriterPipeline = new PdfWriterPipeline(document, pdfWriter);
                HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, pdfWriterPipeline);
                CssResolverPipeline cssResolverPipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
                XMLWorker xmlWorker = new XMLWorker(cssResolverPipeline, true);
                XMLParser xmlParser = new XMLParser(xmlWorker);
                xmlParser.Parse(stringReader);

                document.Close();
            }
        }

回答1:


I've created a sample to show how to parse and display RTL data using XMLWorker. Download it from here.



来源:https://stackoverflow.com/questions/10510786/itextsharp-xmlworker-right-to-left

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