Craig
Craig

Reputation: 36816

iTextSharp SetCharacterSpacing broken when using Right Align

I have a table with two cells. The first cell is left aligned and the second cell is right aligned. I also want to change the character spacing of the text. I have found that changing the character spacing breaks the alignment so the right align text ends up outside the boundaries if the table.

I have created some test code below and the link to the output PDF is in this link

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

Are there any alternatives or workarounds to getting the character spacing working well with the right alignment?

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

Upvotes: 3

Views: 3657

Answers (1)

Craig
Craig

Reputation: 36816

I have managed to fix it by using chunks to set the character spacing. See amended code.

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

Upvotes: 4

Related Questions