2012-01-12 55 views
3

我不知道文本塊中文本的寬度,我希望文本塊像這樣對齊到右邊,同時還具有左對齊各行:使用iTextSharp,如何對齊一個文本容器(但保持單獨的行不對齊)

       Mr. Petersen  | 
           Elmstreet 9  | 
           888 Fantastic City| 

(| donotes文檔的右邊緣)

它應該是簡單,但我不能弄明白。

我試圖把所有的文本放在一個段落中,並設置paragraph.Alignment = Element.ALIGN_RIGHT,但是這將右對齊各條線。

我試過把段落放在一個表格中的單元格中,然後右對齊單元格,但單元格只佔用表格的整個寬度。

如果我可以只創建一個只需要寬度的容器,我可以簡單地對這個容器進行右對齊,所以也許這就是我的問題。

+0

根據你的頭銜,我實際上並不瞭解你想要做什麼。你不希望正常的段落對齊(左,右,對齊)?你是否嘗試對齊多個段落的邊緣? – 2012-01-12 14:24:37

回答

10

只需設置Paragraph.Align屬性:

using (Document document = new Document()) { 
    PdfWriter.GetInstance(
    document, STREAM 
); 
    document.Open(); 
    for (int i = 1; i < 11; ++i) { 
    Paragraph p = new Paragraph(string.Format(
     "Paragraph {0}", i 
    )); 
    p.Alignment = Element.ALIGN_RIGHT; 
    document.Add(p); 
    } 
} 

它甚至有長長的一串像這樣工作的:

string longString = @" 
iText ® is a library that allows you to create and manipulate PDF documents. It enables developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation. 
"; 
Paragraph pLong = new Paragraph(longString); 
pLong.Alignment = Element.ALIGN_RIGHT; 
document.Add(pLong); 

enter image description here

編輯: 在看「照片後「你畫了...

它與標題不符。 只有方式,你可以對齊個人Paragraph對象像你的圖片是如果「段落」不是超過Document對象的「內容」框(缺乏一個更好的術語)。換句話說,如果文本的數量超過合適的一行,則將無法獲得該類型的對齊方式。

隨着中說,如果你想要的類型定位的需要:

  1. 從您要使用的字符串的集合計算最大價值。
  2. 使用該值爲Paragraph設置共同的左縮進值。

事情是這樣的:

using (Document document = new Document()) { 
    PdfWriter.GetInstance(
    document, STREAM 
); 
    document.Open(); 
    List<Chunk> chunks = new List<Chunk>(); 
    float widest = 0f; 
    for (int i = 1; i < 5; ++i) { 
    Chunk c = new Chunk(string.Format(
     "Paragraph {0}", Math.Pow(i, 24) 
    )); 
    float w = c.GetWidthPoint(); 
    if (w > widest) widest = w; 
    chunks.Add(c); 
    } 
    float indentation = document.PageSize.Width 
    - document.RightMargin 
    - document.LeftMargin 
    - widest 
    ; 
    foreach (Chunk c in chunks) { 
    Paragraph p = new Paragraph(c); 
    p.IndentationLeft = indentation; 
    document.Add(p); 
    } 
} 

enter image description here

更新2

閱讀您的更新問題後,這裏的另一個選項,你可以添加文字的左側「容器」:

string textBlock = @" 
Mr. Petersen 
Elmstreet 9 
888 Fantastic City 
".Trim(); 
// get the longest line to calcuate the container width 
var widest = textBlock.Split(
    new string[] {Environment.NewLine} 
    , StringSplitOptions.None 
) 
    .Aggregate(
    "", (x, y) => x.Length > y.Length ? x : y 
) 
; 
// throw-away Chunk; used to set the width of the PdfPCell containing 
// the aligned text block 
float w = new Chunk(widest).GetWidthPoint(); 
PdfPTable t = new PdfPTable(2); 
float pageWidth = document.PageSize.Width 
    - document.LeftMargin 
    - document.RightMargin 
; 
t.SetTotalWidth(new float[]{ pageWidth - w, w }); 
t.LockedWidth = true; 
t.DefaultCell.Padding = 0; 
// you can add text in the left PdfPCell if needed 
t.AddCell(""); 
t.AddCell(textBlock); 
document.Add(t); 
+0

感謝您的回答。我不敢相信這一定很難。無論如何,你的建議來衡量的寬度,然後縮進爲我工作。 – 2012-01-17 13:23:57

+1

不幸的是,由於您事先不知道文本塊的寬度,所以必須計算最長的行**。你可以對一個寬度進行硬編碼,並把文本塊放在一個'PdfPTable'的第二列中,但是你可能會面臨[1]右邊空間太大,或者[2]太小空間和一行溢出' PdfPCell'寬度。在閱讀完編輯後的問題後,我用另一個選項更新了我的答案,更符合創建「容器」的要求。與更新的代碼不同的是,您可以將文本添加到文本塊的左側。 – kuujinbo 2012-01-17 15:13:42