Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在同一行上左右对齐两段?_C#_Pdf_Itextsharp - Fatal编程技术网

C# 如何在同一行上左右对齐两段?

C# 如何在同一行上左右对齐两段?,c#,pdf,itextsharp,C#,Pdf,Itextsharp,我想在一行的左侧和右侧显示两段内容(可能是段落或文本)。我的输出应该是 Name:ABC date:2015-03-02 我如何才能做到这一点?请看一看示例。它为您的问题提供了两种不同的解决方案: 解决方案1:使用胶水 所谓粘合,我指的是一种特殊的块,它的作用类似于分隔两个(或多个)其他块对象的分隔符: Chunk glue = new Chunk(new Verti

我想在一行的左侧和右侧显示两段内容(可能是段落或文本)。我的输出应该是

 Name:ABC                                                               date:2015-03-02
我如何才能做到这一点?

请看一看示例。它为您的问题提供了两种不同的解决方案:

解决方案1:使用胶水

所谓粘合,我指的是一种特殊的
,它的作用类似于分隔两个(或多个)其他
对象的分隔符:

Chunk glue = new Chunk(new VerticalPositionMark());
Paragraph p = new Paragraph("Text to the left");
p.add(new Chunk(glue));
p.add("Text to the right");
document.add(p);
这样,您将在左侧显示
“文本到左侧”
,在右侧显示
“文本到右侧”

解决方案2:使用
PdfPTable

假设有一天,有人要求你把东西放在中间,那么使用<代码> PDFPTABE>代码>是最有前途的解决方案:

PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(100);
table.addCell(getCell("Text to the left", PdfPCell.ALIGN_LEFT));
table.addCell(getCell("Text in the middle", PdfPCell.ALIGN_CENTER));
table.addCell(getCell("Text to the right", PdfPCell.ALIGN_RIGHT));
document.add(table);
在您的例子中,您只需要左边和右边的部分,因此您需要创建一个只有两列的表:
table=newpdfptable(2)

如果您在
getCell()
方法中徘徊,它是这样的:

public PdfPCell getCell(String text, int alignment) {
    PdfPCell cell = new PdfPCell(new Phrase(text));
    cell.setPadding(0);
    cell.setHorizontalAlignment(alignment);
    cell.setBorder(PdfPCell.NO_BORDER);
    return cell;
}
解决方案3:对齐文本

这一点在对这一问题的答复中作了解释:


但是,一旦字符串中有空格,这将导致奇怪的结果。例如:如果您有
“Name:ABC”
,它将起作用。如果你有
“Name:Bruno Lowagie”
“Bruno”,
“Lowagie”
的话,它就不起作用了。如果你证明这条线正确的话,
“Lowagie”

我这样做是为了工作和它的工作

            Document document = new Document(PageSize.A4, 30, 30, 100, 150);
            document.SetPageSize(iTextSharp.text.PageSize.A4);
            PdfWriter writer = PdfWriter.GetInstance(document, fs);
            writer.PageEvent = new ITextEvents();
            document.Open();
            iTextSharp.text.Font fntHead2 = new iTextSharp.text.Font(bfntHead, 11, 1, BaseColor.BLACK);
            Paragraph para = new Paragraph();
            Chunk glue = new Chunk(new VerticalPositionMark());
            Phrase ph1 = new Phrase();

            Paragraph main = new Paragraph();
            ph1.Add(new Chunk("Left Side", fntHead2)); 
            ph1.Add(glue); // Here I add special chunk to the same phrase.    
            ph1.Add(new Chunk("Right Side", fntHead2)); 
            para.Add(ph1);
            document.Add(para);

@Jongware我认为问题不在于证明文本的合理性。我认为这是关于左右对齐的字符串。我已经提供了一个包含几个选项的答案,包括你提到的那个选项,并且我已经解释了为什么我认为你的指针不能回答这个问题。@BrunoLowagie:正确;OP随后澄清了他原来的帖子。@Jongware啊哈,我错过了那部分。我想知道他为什么不在左右弦中使用空格;-)我刚刚检查了原始问题,我很高兴看到坏代码被删除了。