2010-11-04 90 views
23

我試圖弄清楚如何讓我的文本在一個PdfPCell裏面顯示在中間。我嘗試了很多不同的選項,例如:iTextsharp,PdfPCell.VerticalAlignment和PdfPCell.Horizo​​ntalAlignment

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

回答

32

我想你遇到的基本問題是,您將文本添加到iTextSharp Paragraph對象,然後嘗試使用包含它的PdfPCell對象設置此文本的對齊方式。我不確定PdfPCell.VerticalAlignment屬性是否僅適用於PdfPCell的文本,或者如果對齊PdfPCell中的Paragraph對象沒有任何影響,您可以在測試中看到。

您還將myCell.HorizontalAlignmentmyCell.VerticalAlignment設置爲for循環中的索引值。我認爲你的意思是使用i的1次讀取。

無論如何,設置一個PdfPCell的HorizontalAlignmentVerticalAlignment屬性確實可行。下面是演示這一點的一個小方法。我根據你想要做的事情寫得非常鬆散;如果它足夠接近你想要做的事情,那麼你可以用它作爲你項目的起點。

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

謝謝,它的工作更好,當我開始使用短語,而不是段落 – Jimmy 2010-11-05 10:25:24

0

請使用給定的代碼,我希望我會是誰想要打印在單元格中的文本中保護無效的Page_Load(對象發件人,EventArgs的)中間和頂部對齊 { 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(); 
     } 

    }