iText 7 – 单元格文本旋转

huangapple go评论89阅读模式
英文:

iText 7 - cell text rotation

问题

I am using iText 7 C# to build a pdf.
Here is my issue.. I am trying to add a date to the last row. Since I do not have much space available I am trying to rotate it vertically.. but the text is wrapping to the next line. Also, I wanted the text to be exactly center of cell (vertically and horizontally after the rotation). Can I have the rotation slightly fall to the right like '/'?

// add signed date row
table.AddCell(new Cell().Add(new Paragraph("")).SetWidth(UnitValue.CreatePercentValue(1 * 5)).SetBorder(Border.NO_BORDER));
table.AddCell(new Cell().Add(new Paragraph("")).SetWidth(UnitValue.CreatePercentValue(7 * 5)).SetBorder(Border.NO_BORDER));
index = 0;
foreach (var month in months)
{
    var dateCell = new Cell().SetWidth(UnitValue.CreatePercentValue(1 * 5))
                            .Add(new Paragraph("3/5/23")).SetFontSize(8);
    dateCell.SetRotationAngle(Math.PI/2);
    table.AddCell(dateCell);
    index++;
 }

iText 7 – 单元格文本旋转

英文:

I am using iText 7 C# to build a pdf.
Here is my issue.. I am trying to add a date to the last row. Since I do not have much space available I am trying to rotate it vertically.. but the text is wrapping to the next line. Also, I wanted the text to be exactly center of cell (vertically and horizontally after the rotation). Can I have the rotation slightly fall to the right like '/'?

// add signed date row
table.AddCell(new Cell().Add(new Paragraph("")).SetWidth(UnitValue.CreatePercentValue(1 * 5)).SetBorder(Border.NO_BORDER));
table.AddCell(new Cell().Add(new Paragraph("")).SetWidth(UnitValue.CreatePercentValue(7 * 5)).SetBorder(Border.NO_BORDER));
index = 0;
foreach (var month in months)
{
    var dateCell = new Cell().SetWidth(UnitValue.CreatePercentValue(1 * 5))
                            .Add(new Paragraph("3/5/23")).SetFontSize(8);
    dateCell.SetRotationAngle(Math.PI/2);
    table.AddCell(dateCell);
    index++;
 }

iText 7 – 单元格文本旋转

答案1

得分: 1

这应该可以胜任工作,包括TextAlignment和防止换行的设置。

var dateCell = new Cell().SetWidth(UnitValue.CreatePercentValue(1 * 5))
        .SetHeight(UnitValue.CreatePercentValue(30)) // 根据需要调整单元格高度
        .SetTextAlignment(TextAlignment.CENTER) // 将文本对齐方式设置为居中
        .SetPaddingBottom(10) // 根据需要调整底部填充
        .Add(new Paragraph("3/5/23")).SetFontSize(8);

// 调整旋转角度使其稍微向右倾斜
dateCell.SetRotationAngle(Math.PI / 4);

// 从注释中的替代方法
var paragraph = new Paragraph("3/5/23");

// 调整旋转角度使其稍微向右倾斜
paragraph.SetRotationAngle(Math.PI / 4);

var dateCell = new Cell().SetWidth(UnitValue.CreatePercentValue(1 * 5))
        .SetHeight(UnitValue.CreatePercentValue(30)) // 根据需要调整单元格高度
        .SetTextAlignment(TextAlignment.CENTER) // 将文本对齐方式设置为居中
        .SetPaddingBottom(10) // 根据需要调整底部填充
        .Add(paragraph).SetFontSize(8);

希望这对你有所帮助!如果有任何其他问题,请随时提出。

英文:

This should do the job, including the TextAlignment and preventing of word wrap

var dateCell = new Cell().SetWidth(UnitValue.CreatePercentValue(1 * 5))
        .SetHeight(UnitValue.CreatePercentValue(30)) // Adjust the cell height as needed
        .SetTextAlignment(TextAlignment.CENTER) // Set text alignment to center
        .SetPaddingBottom(10) // Adjust the bottom padding as needed
        .Add(new Paragraph("3/5/23")).SetFontSize(8);

    // Adjust rotation angle to make it fall slightly to the right
    dateCell.SetRotationAngle(Math.PI / 4);

Alternative from comment

var paragraph = new Paragraph("3/5/23");

// Adjust rotation angle to make it fall slightly to the right    
paragraph.SetRotationAngle(Math.PI / 4);

var dateCell = new Cell().SetWidth(UnitValue.CreatePercentValue(1 * 5))
        .SetHeight(UnitValue.CreatePercentValue(30)) // Adjust the cell height as needed
        .SetTextAlignment(TextAlignment.CENTER) // Set text alignment to center
        .SetPaddingBottom(10) // Adjust the bottom padding as needed
        .Add(paragraph).SetFontSize(8);

huangapple
  • 本文由 发表于 2023年5月28日 17:54:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定