使用右对齐时,iTextSharp SetCharacterSpacing中断

时间:2013-07-05 13:15:03

标签: c# .net itextsharp

我有一张有两个牢房的桌子。第一个单元格左对齐,第二个单元格右对齐。我还想更改文本的字符间距。我发现更改字符间距会破坏对齐,因此右对齐文本最终会在表格之外的边界之外。

我在下面创建了一些测试代码,输出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");
    }
}

1 个答案:

答案 0 :(得分:4)

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

    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;
    }