iText 7 – 单元格文本旋转

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

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 '/'?

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

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 '/'?

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

iText 7 – 单元格文本旋转

答案1

得分: 1

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

  1. var dateCell = new Cell().SetWidth(UnitValue.CreatePercentValue(1 * 5))
  2. .SetHeight(UnitValue.CreatePercentValue(30)) // 根据需要调整单元格高度
  3. .SetTextAlignment(TextAlignment.CENTER) // 将文本对齐方式设置为居中
  4. .SetPaddingBottom(10) // 根据需要调整底部填充
  5. .Add(new Paragraph("3/5/23")).SetFontSize(8);
  6. // 调整旋转角度使其稍微向右倾斜
  7. dateCell.SetRotationAngle(Math.PI / 4);
  8. // 从注释中的替代方法
  9. var paragraph = new Paragraph("3/5/23");
  10. // 调整旋转角度使其稍微向右倾斜
  11. paragraph.SetRotationAngle(Math.PI / 4);
  12. var dateCell = new Cell().SetWidth(UnitValue.CreatePercentValue(1 * 5))
  13. .SetHeight(UnitValue.CreatePercentValue(30)) // 根据需要调整单元格高度
  14. .SetTextAlignment(TextAlignment.CENTER) // 将文本对齐方式设置为居中
  15. .SetPaddingBottom(10) // 根据需要调整底部填充
  16. .Add(paragraph).SetFontSize(8);

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

英文:

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

  1. var dateCell = new Cell().SetWidth(UnitValue.CreatePercentValue(1 * 5))
  2. .SetHeight(UnitValue.CreatePercentValue(30)) // Adjust the cell height as needed
  3. .SetTextAlignment(TextAlignment.CENTER) // Set text alignment to center
  4. .SetPaddingBottom(10) // Adjust the bottom padding as needed
  5. .Add(new Paragraph("3/5/23")).SetFontSize(8);
  6. // Adjust rotation angle to make it fall slightly to the right
  7. dateCell.SetRotationAngle(Math.PI / 4);

Alternative from comment

  1. var paragraph = new Paragraph("3/5/23");
  2. // Adjust rotation angle to make it fall slightly to the right
  3. paragraph.SetRotationAngle(Math.PI / 4);
  4. var dateCell = new Cell().SetWidth(UnitValue.CreatePercentValue(1 * 5))
  5. .SetHeight(UnitValue.CreatePercentValue(30)) // Adjust the cell height as needed
  6. .SetTextAlignment(TextAlignment.CENTER) // Set text alignment to center
  7. .SetPaddingBottom(10) // Adjust the bottom padding as needed
  8. .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:

确定