【问题标题】:iTextSharp SetCharacterSpacing broken when using Right Align使用右对齐时 iTextSharp SetCharacterSpacing 损坏
【发布时间】:2013-07-05 13:15:03
【问题描述】:

我有一个包含两个单元格的表格。第一个单元格左对齐,第二个单元格右对齐。我还想更改文本的字符间距。我发现更改字符间距会破坏对齐方式,因此如果表格右对齐文本最终会超出边界。

我在下面创建了一些测试代码,输出 PDF 的链接在这个链接中

https://skydrive.live.com/redir?resid=1ECE2061CFF9124B!190&authkey=!ANcB0z_BN3N4UFo

是否有任何替代方法或解决方法可以使字符间距在正确对齐的情况下正常工作?

public void CharacterSpacingTest()
{
    float margin = 50;

    using (var stream = new MemoryStream())
    {
        Document document = new Document();
        document.SetPageSize(new Rectangle(PageSize.A4.Width, PageSize.A4.Height));
        document.SetMargins(margin, margin, 30f, 100f);

        PdfWriter writer = PdfWriter.GetInstance(document, stream);
        writer.CloseStream = false;

        document.Open();

        PdfPTable table = new PdfPTable(new float[] { 100 });
        table.TotalWidth = PageSize.A4.Width - margin - margin;
        table.WidthPercentage = 100f;
        table.LockedWidth = true;

        // Create a row that is right aligned
        PdfPCell cell1 = new PdfPCell();
        cell1.AddElement(new Paragraph("Hello World") { Alignment = Element.ALIGN_RIGHT });
        cell1.BorderWidth = 1;
        table.AddCell(cell1);

        // Change the character spacing
        PdfContentByte cb = writer.DirectContent;
        cb.SetCharacterSpacing(1f);

        // Create a row that is left aligned
        PdfPCell cell2 = new PdfPCell();
        cell2.AddElement(new Paragraph("Hello World"));
        cell2.BorderWidth = 1;
        table.AddCell(cell2);

        document.Add(table);

        document.Close();

        Blobs.SaveToFile(Blobs.LoadFromStream(stream), @"c:\Dev\test.pdf");
    }
}

【问题讨论】:

  • 我已经在 iText 的付费支持票务系统中为此制作了一张票。请注意,当一个问题同时发布在不同的论坛上时,这很烦人。将来,请将其发布在 SO 或官方 iText 邮件列表上。如果您也在其他地方发布了这个问题,请告诉我,以便我可以指向 SO 上的 URL。
  • 仅在 SO 和 iText 上发布。
  • 好的,我将支持票分配给 Raf Hens。票的优先级较低。如果我查看他的议程,我希望他能够在大约一个半星期内查看它。

标签: c# .net itextsharp


【解决方案1】:

我已经设法通过使用块来设置字符间距来修复它。请参阅修改后的代码。

    public void CharacterSpacingTest()
    {
        float margin = 50;

        using (var stream = new MemoryStream())
        {
            Document document = new Document();
            document.SetPageSize(new Rectangle(PageSize.A4.Width, PageSize.A4.Height));
            document.SetMargins(margin, margin, 30f, 100f);

            PdfWriter writer = PdfWriter.GetInstance(document, stream);
            writer.CloseStream = false;

            document.Open();

            PdfPTable table = new PdfPTable(new float[] { 100 });
            table.TotalWidth = PageSize.A4.Width - margin - margin;
            table.WidthPercentage = 100f;
            table.LockedWidth = true;

            // Create a row that is right aligned
            PdfPCell cell1 = new PdfPCell();
            cell1.AddElement(new Paragraph(GetChunk("Hello World")) { Alignment = Element.ALIGN_RIGHT });
            cell1.BorderWidth = 1;
            table.AddCell(cell1);

            // Create a row that is left aligned
            PdfPCell cell2 = new PdfPCell();
            cell2.AddElement(new Paragraph(GetChunk("Hello World")));
            cell2.BorderWidth = 1;
            table.AddCell(cell2);

            document.Add(table);

            document.Close();

            Blobs.SaveToFile(Blobs.LoadFromStream(stream), @"c:\Dev\test.pdf");
        }
    }

    private Chunk GetChunk(string text)
    {
        Chunk chunk = new Chunk(text);
        chunk.SetCharacterSpacing(1);
        return chunk;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-02
    • 2015-08-24
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多