How to justify text using iTextSharp?

Enter the code snippet as shown below:

var workStream = new MemoryStream(); var doc = new Document(PageSize.LETTER, 10, 10, 42, 35); PdfWriter.GetInstance(doc, workStream).CloseStream = false; doc.Open(); var builder = new StringBuilder(); builder.Append("MY LONG HTML TEXT"); var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(builder.ToString()), null); foreach (var htmlElement in parsedHtmlElements) doc.Add(htmlElement); doc.Close(); byte[] byteInfo = workStream.ToArray(); workStream.Write(byteInfo, 0, byteInfo.Length); workStream.Position = 0; return new FileStreamResult(workStream, "application/pdf") 

And there is one problem - how to make this pdf sound? Is there any method or something that quickly does this?

+6
source share
1 answer

And, I understand, you mean "justified" instead of "adjusted." I updated your question. It is actually quite easy. This mainly depends on the type of content being added and whether this content supports this concept in the first place. Assuming you have basic paragraphs, you can set the Alignment property for them before adding them to your main loop:

 foreach (var htmlElement in parsedHtmlElements){ //If the current element is a paragraph if (htmlElement is Paragraph){ //Set its alignment ((Paragraph)htmlElement).Alignment = Element.ALIGN_JUSTIFIED; } doc.Add(htmlElement); } 

There are two types of rationale: Element.ALIGN_JUSTIFIED and Element.ALIGN_JUSTIFIED_ALL . The second is the same as the first, except that it also justifies the last line of text that you may or may not want to do.

+10
source

Source: https://habr.com/ru/post/913153/


All Articles