iTextSharp的,PdfPCell.VerticalAlignment和PdfPCell.Horizo​​ntalAlignment [英] iTextsharp, PdfPCell.VerticalAlignment and PdfPCell.HorizontalAlignment

查看:1337
本文介绍了iTextSharp的,PdfPCell.VerticalAlignment和PdfPCell.Horizo​​ntalAlignment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去弄清楚如何获得PdfPCell里面我的文字在中间显示。我已经尝试了许多不同的选项,如:

 
myCell.VerticalAlignment = Element.ALIGN_MIDDLE;
myCell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
myCell.VerticalAlignment = Rectangle.ALIGN_MIDDLE;



这些都不为我工作。 VerticalAlignment接受一个int,所以我试图让一个循环,看看如果我能找到合适的数量,但一切都只是被甩在底部对齐。

 文档myDocument中=新的文件(PageSize.A4); 

PdfWriter myPdfWriter = PdfWriter.GetInstance(myDocument中,新的FileStream(strPDFFilePath,FileMode.Create));
myDocument.Open();

myDocument.NewPage();

PdfContentByte myPdfContentByte = myPdfWriter.DirectContent;

PdfPCell了myCell;
段myParagraph;

PdfPTable myTable的=新PdfPTable(4);
myTable.WidthPercentage = 100;
myTable.SetWidths(新INT [4] {25,25,25,25});

myTable.DefaultCell.BorderWidth = 1;
myTable.DefaultCell.BorderColor = BaseColor.RED;

的for(int i = -100; I< 100;我++)
{
myParagraph =新段(的String.Format(对齐方式:{0},我));
myParagraph.Font.SetFamily(宋体);
myParagraph.Font.SetColor(72,72,72);
myParagraph.Font.Size = 11;

=了myCell新PdfPCell();
myCell.AddElement(myParagraph);
myCell.Horizo​​ntalAlignment = I;
myCell.VerticalAlignment = I;
myTable.AddCell(了myCell);
}

myDocument.Add(myTable的);
myDocument.Add(新组块(的String.Empty));
myDocument.Close();


解决方案

我觉得您遇到的基本问题是:您要添加文本iTextSharp的段落对象,然后尝试使用包含它的 PdfPCell 对象来设置该文本的对齐。我不知道如果 PdfPCell.VerticalAlignment 属性是只对 PdfPCell 的文字,或者如果对准段落中的 PdfPCell 没有任何影响您可以在测试中看到。

$ b对象
$ b

你还设置 myCell.Horizo​​ntalAlignment myCell.VerticalAlignment 来索引在你的循环。我想你的意思是使用1 instread



总之,创下了PdfPCell的的Horizo​​ntalAlignment VerticalAlignment 性能做的工作,虽然。下面是说明了这一点小法。我写的很松散的基础上是你所试图做的;如果它足够近,你正在试图做的或许你可以用这个作为你的项目的出发点是什么。

 私人无效TestTableCreation(){使用(的FileStream FS =新的FileStream(TableTest.pdf,FileMode.Create)){
文档的文档=新的文件(PageSize.A4)
;
PdfWriter.GetInstance(DOC,FS);
doc.Open();

PdfPTable表=新PdfPTable(4);

的for(int i = -100; I< 100;我++){
PdfPCell电池=新PdfPCell(新乐句(的String.Format(对齐方式:{0},我)));
//给我们一些行高度,所以我们可以看到测试垂直对齐方式。
cell.FixedHeight = 30.0f;

// **试试吧**
//cell.Horizo​​ntalAlignment = Element.ALIGN_LEFT;
//cell.Horizo​​ntalAlignment = Element.ALIGN_CENTER;
cell.Horizo​​ntalAlignment = Element.ALIGN_RIGHT;

cell.VerticalAlignment = Element.ALIGN_TOP;
//cell.VerticalAlignment = Element.ALIGN_MIDDLE;
//cell.VerticalAlignment = Element.ALIGN_BOTTOM;

table.AddCell(单元);
}

doc.Add(表);
doc.Close();
}
}


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.ALIGN_MIDDLE;
myCell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
myCell.VerticalAlignment = Rectangle.ALIGN_MIDDLE;

None of that works for me. VerticalAlignment takes an int, so I tried to make a loop, to see if i could find the right number, but everything just get left bottom align..

   Document myDocument = new Document(PageSize.A4);

   PdfWriter myPdfWriter = PdfWriter.GetInstance(myDocument, new FileStream(strPDFFilePath, FileMode.Create));
   myDocument.Open();

   myDocument.NewPage();

   PdfContentByte myPdfContentByte = myPdfWriter.DirectContent;

   PdfPCell myCell;
   Paragraph myParagraph;

   PdfPTable myTable = new PdfPTable(4);
   myTable.WidthPercentage = 100;
   myTable.SetWidths(new int[4] { 25, 25, 25, 25 });

   myTable.DefaultCell.BorderWidth = 1;
   myTable.DefaultCell.BorderColor = BaseColor.RED;                

   for (int i = -100; i < 100; i++)
   {
       myParagraph = new Paragraph(String.Format("Alignment: {0}", i));
       myParagraph.Font.SetFamily("Verdana");
       myParagraph.Font.SetColor(72, 72, 72);
       myParagraph.Font.Size = 11;

       myCell = new PdfPCell();
       myCell.AddElement(myParagraph);
       myCell.HorizontalAlignment = i;
       myCell.VerticalAlignment = i;                    
       myTable.AddCell(myCell);
   }

   myDocument.Add(myTable);
   myDocument.Add(new Chunk(String.Empty));
   myDocument.Close();

解决方案

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

这篇关于iTextSharp的,PdfPCell.VerticalAlignment和PdfPCell.Horizo​​ntalAlignment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆