我使用itext sharp来动态创建PDF。我正在使用ColumnText,因为我正在处理从右到左的内容。通过搜索网络,我理解column.SetSimpleColumn接受4个args(x,y,w,h)对吗?我的第一个问题是我不知道列的宽度和高度,我需要它是文本内容的动态。任何人都知道如何根据他的内容设置列宽和高度?
try
{
for (int j = 0; j < project.Slides[i].Labels.Count; j++)
{
string pageContext = project.Slides[i].Labels[j].Text;
int fontNumber = Convert.ToInt32(project.Slides[i].Labels[j].Font);
int fontSize = Convert.ToInt32(project.Slides[i].Labels[j].Size);
float fontSizePoints = (float)fontSize * (float)2.3;
string fontColor = project.Slides[i].Labels[j].Color;
float fontX = (float)project.Slides[i].Labels[j].X;
float fontY = (float)project.Slides[i].Labels[j].Y;
var color = System.Drawing.ColorTranslator.FromHtml(fontColor);
baseFont = GetBaseFont(fontNumber);
float w = baseFont.GetWidthPoint(pageContext, fontSizePoints);
Font dFont = new Font(baseFont, fontSizePoints);
dFont.Color = new BaseColor(color);
ColumnText column = new ColumnText(writer.DirectContent);
column.SetSimpleColumn(fontX, fontY, w, 100);
column.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
column.AddElement(new Paragraph(pageContext, dFont));
column.Go();
}
}
catch (Exception)
{
}
我附上图片来描述问题。文档中可以有一个或20个文本列。我不知道字体的大小和这个文本有多长。它可以是2个单词或30个单词。我唯一的信息是左边的x,y底部坐标(以及字体系列,大小,颜色......)我的问题是如何创建TextColumn(由于RTL支持需要使用它)并将其放置在特定的coords位置。
答案 0 :(得分:0)
@mkl感谢您的回复。我找到的最佳解决方案是。在绝对位置+ RTL支持下工作得很好。
try
{
for (int j = 0; j < project.Slides[i].Labels.Count; j++)
{
string pageContext = project.Slides[i].Labels[j].Text;
int fontNumber = Convert.ToInt32(project.Slides[i].Labels[j].Font);
int fontSize = Convert.ToInt32(project.Slides[i].Labels[j].Size);
float fontSizePoints = (float)fontSize * (float)2.1;
string fontColor = project.Slides[i].Labels[j].Color;
float X = (float)project.Slides[i].Labels[j].X;
float Y = pHeightPoints - (float)project.Slides[i].Labels[j].Y;
float width = (float)project.Slides[i].Labels[j].Widht;
float height = (float)project.Slides[i].Labels[j].Heigh;
float llx = X;
float lly = Y + height;
float urx = X + width;
float ury = Y;
var color = System.Drawing.ColorTranslator.FromHtml(fontColor);
baseFont = GetBaseFont(fontNumber);
float w = baseFont.GetWidthPoint(pageContext, fontSizePoints);
Font dFont = new Font(baseFont, fontSizePoints);
dFont.Color = new BaseColor(color);
PlaceText(writer.DirectContent, pageContext,
new Font(baseFont, fontSizePoints,
Font.NORMAL,
new BaseColor(color)), llx, lly, urx, ury, 14,
Element.ALIGN_RIGHT);
}
}
catch (Exception)
{
}
和...
protected static void PlaceText(PdfContentByte pdfContentByte
, string text
, iTextSharp.text.Font font
, float lowerLeftx
, float lowerLefty
, float upperRightx
, float upperRighty
, float leading
, int alignment)
{
ColumnText ct = new ColumnText(pdfContentByte);
ct.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
ct.SetSimpleColumn(new Phrase(text, font), lowerLeftx, lowerLefty, upperRightx, upperRighty, leading, alignment);
ct.Go();
}
由于