使用iTextSharp文本的合理性如何? [英] How justify text using iTextSharp?

查看:165
本文介绍了使用iTextSharp文本的合理性如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拥有如下代码:

 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")

并且有一个问题 - 如何使pdf合理化?有什么方法可以快速做到吗?

And have one problem-how make that pdf justified? Is any method or something which quickly do that?

推荐答案

啊,我明白了,你的意思是对齐而不是调整 。我更新了你的问题。这实际上非常简单。基本上,它取决于您要添加的内容类型以及该内容是否首先支持此概念。假设你有基本的段落,你可以在你的主循环中添加 $ b

Ah, I get it, you mean "justified" instead of "adjusted". I updated your question. It's actually pretty easy. Basically it depends on the type of content that you're adding and whether that content supports this concept in the first place. Assuming that you have basic paragraphs you can set the Alignment property on them before adding them in 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);
}

有两种类型的理由, Element.ALIGN_JUSTIFIED Element.ALIGN_JUSTIFIED_ALL 。第二个与第一个相同,只是它也证明了你可能想要或不想做的最后一行文本。

There's two types of justification, 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 which you may or may not want to do.

这篇关于使用iTextSharp文本的合理性如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆