iTextsharp、PdfPCell.VerticalAlignment 和 PdfPCell.Horizo​​ntalAlignment

发布于 2024-09-30 14:11:53 字数 1430 浏览 9 评论 0原文

我试图弄清楚如何让我的文本在 PdfPCell 中显示在中间。我尝试了很多不同的选择,例如:

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

这些都不适合我。 VerticalAlignment 需要一个 int,所以我尝试创建一个循环,看看是否能找到正确的数字,但所有内容都左下对齐。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

四叶草在未来唯美盛开 2024-10-07 14:11:53

我认为您遇到的基本问题是您将文本添加到 iTextSharp Paragraph 对象,然后尝试使用包含它的 PdfPCell 对象设置该文本的对齐方式。我不确定 PdfPCell.VerticalAlignment 属性是否仅适用于 PdfPCell 的文本,或者是否对齐 Paragraph 对象内的 < code>PdfPCell 不会对您在测试中看到的任何影响。

您还将 myCell.Horizo​​ntalAlignmentmyCell.VerticalAlignment 设置为 for 循环中的索引值。我认为您打算使用 1 代替 i

无论如何,设置 PdfPCell 的 Horizo​​ntalAlignmentVerticalAlignment 属性确实有效。下面是一个演示这一点的小方法。我根据你想要做的事情非常宽松地写了它;如果它足够接近您想要做的事情,也许您可​​以使用它作为您项目的起点。

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

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();
    }
}
软糯酥胸 2024-10-07 14:11:53

当我添加以下内容时,Jay Riggs 解决方案也开始适用于垂直对齐:

cell.UseAscender = true;

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

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

年少掌心 2024-10-07 14:11:53

我尝试给出直接整数和这里提到的其他解决方案。但对我来说没有任何作用。这两者的结合在构图方法中对我很有用。

cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;

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;
你的呼吸 2024-10-07 14:11:53

请使用给定的代码,我希望我对那些想要在单元格中以中间和顶部对齐方式打印文本的人最有帮助
protected void Page_Load(对象发送者,EventArgs e)
{
获取表();
}

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

    }

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

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文