c# iTextsharp generated PDF with xmlworker is breaking Lists

人盡茶涼 提交于 2019-12-24 19:06:38

问题


I am using iTextsharp library with XmlWorker version 5.5.12.0, and facing problems with list is enclosed with DIV.

<body>
<span>
	<ul>
		<ul>
			<li>Project Management
				<ul>
					<li>
						<a class="jwiki-small" data-containerid="2544" data-containertype="14" data-objectid="14695" data-objecttype="102" href="https://SampleUrl.com/DOC-146">Sample Text</a>
					</li>
				</ul>
			</li>
		</ul>
	</ul>
</span>
</body>

and the pdf looks correct like the image below.

But Formatting problems start once List is enclosed in a Div at any level. List in pdf becomes inline.

<body>
<div>
	<span>
		<ul>
			<ul>
				<li>Project Management
					<ul>
						<li>
							<a class="jwiki-small" data-containerid="2544" data-containertype="14" data-objectid="14695" data-objecttype="102" href="https://SampleUrl.com/DOC-146">Sample Text</a>
						</li>
					</ul>
				</li>
			</ul>
		</ul>
	</span>
</div>
</body>
Is there anything i can do to handle this? Why is Div influencing the formating of list.

FYI, Here is CreatePDF method I am using.

  private void CreatePDF(string html)
        {
          

            var document = new Document(iTextSharp.text.PageSize.A4,20,20,20,20);
            var memoryStream = new MemoryStream();
			
            using (var pdfWriter = PdfWriter.GetInstance(document, memoryStream))
            {
                document.Open();
                var htmlContext = new HtmlPipelineContext(null);
                htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
                htmlContext.SetImageProvider(new CustomItextImageProvider());               
                htmlContext.CharSet(Encoding.UTF8);
               
               var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
                var pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, pdfWriter)));
               
                var xmlWorker = new XMLWorker(pipeline, true);
                
                var xmlParser = new XMLParser(true,xmlWorker);
              

                StringReader rdr = new StringReader((html));
                xmlParser.Parse(rdr);

                pdfWriter.CloseStream = false;

                document.AddCreator("iTextSharp");
                document.AddAuthor("ThreeWill");
                document.Close();

                string fileName = @"c:\temp\" + "test" + DateTime.Now.ToString("yyyy-mm-dd hh.mm.ss") + ".pdf";
                var outputFileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                memoryStream.Position = 0;
                memoryStream.WriteTo(outputFileStream);
               
                outputFileStream.Close();


            }

        }

回答1:


First this: your use of <span> is awkward. According to w3schools, the <span> tag is defined and used as follows:

The <span> tag is used to group inline-elements in a document.

The <span> tag provides no visual change by itself.

The <span> tag provides a way to add a hook to a part of a text or a part of a document.

When I look at the result you get, I see that the list is "flattened" to an inline element, instead of remaining the block element you want it to be. However, I understand why you would consider this an error, because a browser accepts badly written HTML and renders it as expected rather than as it probably should.

How to solve your problem?

You are using a maintenance release of a version of iText that is being phased out. Maintenance release means that this version is no longer supported for companies who aren't an iText customer. Only minor bugs are solved. Known problems, such as the one you are encountering now will not be fixed in iText 5!

Why won't we fix this in iText 5? Because this is already fixed in iText 7.1

I wrote the following code sample:

FileStream fs = new FileStream("list.pdf", FileMode.Create);
HtmlConverter.ConvertToPdf(htmlString, fs, props);

Where htmlString contains the HTML from your question.

This is the result I get:

So please stop complaining about errors in (a maintenance release of) an old iText version, and upgrade to iText 7 and pdfHTML! As explained in the introduction of the HTML to PDF tutorial, it will save you from a lot of frustration. It will also save me from a lot of frustration because I have been repeating this same message several times a day in the last couple of weeks.



来源:https://stackoverflow.com/questions/47830668/c-sharp-itextsharp-generated-pdf-with-xmlworker-is-breaking-lists

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