iTextsharp, PdfPCell.VerticalAlignment and PdfPCell.HorizontalAlignment

前端 未结 4 610
再見小時候
再見小時候 2021-02-05 04:15

Im trying to figure out how to get my text inside a PdfPCell to show in the middle. I have tried many different options, like:

myCell.VerticalAlignment = Element.ALIG         


        
相关标签:
4条回答
  • 2021-02-05 04:29

    Jay Riggs solution started to work also for vertical alignment when I added:

    cell.UseAscender = true;

    http://www.afterlogic.com/mailbee-net/docs-itextsharp/html/0602b79e-ea9c-0c7d-c4b2-bc4b5f976f15.htm

    0 讨论(0)
  • 2021-02-05 04:34

    Please use given code, i hope i will be most helpful for those who want to print a text in a cell in middle and top alignment protected void Page_Load(object sender, EventArgs e) { gettable(); }

        void gettable()
        {
            using (FileStream fs = new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Vedic-Chart-Life-Report.pdf"), FileMode.Create, FileAccess.Write, FileShare.Read))
            {
    
                Document doc = new Document(PageSize.LETTER);
                PdfWriter writer = PdfWriter.GetInstance(doc, fs);
                doc.Open();
                doc.NewPage();
    
                BaseFont bf = BaseFont.CreateFont("C:/WINDOWS/Fonts/krdv010.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                Font f = new Font(bf, 16, Font.BOLD);
    
                PdfContentByte cb = writer.DirectContent;
    
                cb.MoveTo(0, doc.PageSize.Height - 20);
                cb.LineTo(doc.PageSize.Width, doc.PageSize.Height - 20);
                cb.Stroke();
                cb.ClosePathStroke();
    
                PdfPTable table = new PdfPTable(1);
                PdfPCell cell = new PdfPCell(new Phrase("eaxynks'k foospu", f));
    
                // Give our rows some height so we can see test vertical alignment.
                cell.FixedHeight = 15f;
                cell.HorizontalAlignment = 1;
                cell.VerticalAlignment = Element.ALIGN_TOP;
    
                table.AddCell(cell);
                doc.Add(table);
    
                //cb.RoundRectangle(10f, 550f, 592f, 200f, 20f);
                //cb.Stroke();
    
                //doc.Add(new Phrase("eaxynks'k foospu", f));
    
                doc.Close();
            }
    
        }
    
    0 讨论(0)
  • 2021-02-05 04:51

    I think the basic problem you're having is that you're adding text to iTextSharp Paragraph objects and then attempting to set this text's alignment using the PdfPCell object that contains it. I'm not sure if the PdfPCell.VerticalAlignment property is only for a PdfPCell's text, or if aligning the Paragraph object inside the PdfPCell doesn't have any affect you can see in your test.

    You're also setting myCell.HorizontalAlignment and myCell.VerticalAlignment to the index value in your for loop. I think you meant to use 1 instread of i.

    Anyway, setting a PdfPCell's HorizontalAlignment and VerticalAlignment properties do work though. Below is a small method that demonstrates this. I wrote it very loosely based on what you were trying to do; if it's close enough to what you're trying to do perhaps you can use this as a starting point in your project.

    private void TestTableCreation() {
        using (FileStream fs = new FileStream("TableTest.pdf", FileMode.Create)) {
            Document doc = new Document(PageSize.A4);
            PdfWriter.GetInstance(doc, fs);
            doc.Open();
    
            PdfPTable table = new PdfPTable(4);
    
            for (int i = -100; i < 100; i++) {
                PdfPCell cell = new PdfPCell(new Phrase(String.Format("Alignment: {0}", i)));
                // Give our rows some height so we can see test vertical alignment.
                cell.FixedHeight = 30.0f;
    
                // ** Try it **
                //cell.HorizontalAlignment = Element.ALIGN_LEFT;
                //cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.HorizontalAlignment = Element.ALIGN_RIGHT;
    
                cell.VerticalAlignment = Element.ALIGN_TOP;
                //cell.VerticalAlignment = Element.ALIGN_MIDDLE;
                //cell.VerticalAlignment = Element.ALIGN_BOTTOM;
    
                table.AddCell(cell);
            }
    
            doc.Add(table);
            doc.Close();
        }
    }
    
    0 讨论(0)
  • 2021-02-05 04:56

    I tried giving direct integers and other solutions mentioned here. But nothing worked for me. A combination of this worked for me in Composition method.

    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
    
    0 讨论(0)
提交回复
热议问题