我正在尝试设置一个简单的2列页面,写入第一列,然后写入第二列。但是,下面的代码将两个段落放在第二列中。当前列跟踪似乎是正确的(先0,然后1)
任何想法我做错了什么?
MultiColumnText columns = new MultiColumnText();
columns.AddSimpleColumn(0, 200);
columns.AddSimpleColumn(200, 400);
Paragraph para1 = new Paragraph("Para1");
columns.AddElement(para1);
Response.Write(columns.CurrentColumn);//traces 0
columns.NextColumn();
Response.Write(columns.CurrentColumn);//traces 1
Paragraph para2 = new Paragraph("Para2");
columns.AddElement(para2);
doc.Add(columns);
非常感谢
奥利弗
答案 0 :(得分:2)
我无法让NextColumn()
与MultiColumnText
对象一起工作,但我找不到任何样本(在.NET中)。
MultiColumnText
使得在文档中创建列相对容易,但作为交换,您放弃了对布局的大量控制。您可以使用ColumnText
对象,它可以对列布局进行大量控制,但需要更多代码。
以下是使用ColumnText
尝试执行的操作的简单但完整的示例:
private void TestColumnText() {
using (FileStream fs = new FileStream("ColumnTest.pdf", FileMode.Create)) {
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
PdfContentByte cb = writer.DirectContent;
ColumnText ct = new ColumnText(cb);
float columnWidth = 200f;
float[] left1 = { doc.Left + 90f, doc.Top - 80f, doc.Left + 90f, doc.Top - 170f, doc.Left, doc.Top - 170f, doc.Left, doc.Bottom };
float[] right1 = { doc.Left + columnWidth, doc.Top - 80f, doc.Left + columnWidth, doc.Bottom };
float[] left2 = { doc.Right - columnWidth, doc.Top - 80f, doc.Right - columnWidth, doc.Bottom };
float[] right2 = { doc.Right, doc.Top - 80f, doc.Right, doc.Bottom };
// Add content for left column.
ct.SetColumns(left1, right1);
ct.AddText(new Paragraph("Para 1"));
ct.Go();
// Add content for right column.
ct.SetColumns(left2, right2);
ct.AddText(new Paragraph("Para 2"));
ct.Go();
doc.Close();
}
}
警告:正如我所提到的,这是一个简单的示例,甚至不会作为您尝试做的事情的起点。以下网站上的样本(特别是第一个)将帮助您:
http://www.mikesdotnetting.com/Article/89/iTextSharp-Page-Layout-with-Columns http://www.devshed.com/c/a/Java/Adding-Columns-With-iTextSharp
答案 1 :(得分:1)
当我发现iTextSharp的最新版本不包括MultiColumnText
类时,我创建了我自己的一类。
Public Class SimpleColumnText
Inherits ColumnText
Dim workingDocument As Document
Dim columns As New List(Of Rectangle)
Dim currentColumn As Integer = 0
Public Sub New(content As PdfContentByte, columnCount As Integer, columnSpacing As Single, document As Document)
MyBase.New(content)
workingDocument = document
CalculateColumnBoundries(columnCount, columnSpacing)
End Sub
Private Sub CalculateColumnBoundries(columnCount As Integer, columnSpacing As Single)
Dim columnWidth As Single
Dim columnHeight As Single
With workingDocument
columnWidth = ((.PageSize.Width - .LeftMargin - .RightMargin) - (columnSpacing * (columnCount - 1))) / columnCount
columnHeight = (.PageSize.Height - .TopMargin - .BottomMargin)
End With
For x = 0 To columnCount - 1
Dim llx As Single = ((columnWidth + columnSpacing) * x) + workingDocument.LeftMargin
Dim lly As Single = workingDocument.BottomMargin
Dim urx As Single = llx + columnWidth
Dim ury As Single = columnHeight
Dim newRectangle As New Rectangle(llx, lly, urx, ury)
columns.Add(newRectangle)
Next
End Sub
Public Shadows Sub AddElement(element As IElement)
MyBase.AddElement(element)
'we have to see if there is a column on the page before we begin
Dim status As Integer
If currentColumn = 0 Then
status = ColumnText.NO_MORE_COLUMN
End If
Do
If status = ColumnText.NO_MORE_COLUMN Then
If currentColumn = columns.Count Then
'we need a new page
workingDocument.NewPage()
'reset column count
currentColumn = 0
End If
MyBase.SetSimpleColumn(columns(currentColumn))
currentColumn += 1
End If
status = MyBase.Go()
Loop While ColumnText.HasMoreText(status)
End Sub
End Class
这可以很容易地扩展到Shadow
中的其他功能ColumnText
。
答案 2 :(得分:0)
谢谢cjbarth,这很有用。我在C#中做了类似的版本,如果这有助于任何人。
public class ColumnTextSimple : ColumnText
{
Document document;
Rectangle[] columns;
public ColumnTextSimple(PdfContentByte writer, Document workingDocument, int columnCount, float columnSpacing) : base(writer)
{
document = workingDocument;
CalculateColumns(columnCount, columnSpacing);
}
private void CalculateColumns(int columnCount, float columnSpacing)
{
float columnWidth;
float columnHeight;
columnWidth = ((document.PageSize.Width - document.LeftMargin - document.RightMargin) - (columnSpacing * (columnCount - 1))) / columnCount;
columnHeight = (document.PageSize.Height - document.TopMargin - document.BottomMargin);
columns = new Rectangle[columnCount];
for (int c = 0; c < columnCount; c++)
{
float llx = ((columnWidth + columnSpacing) * c) + document.LeftMargin;
float lly = document.BottomMargin;
float urx = llx + columnWidth;
float ury = columnHeight;
columns[c] = new Rectangle(llx, lly, urx, ury);
}
}
public void produceColumns()
{
int column = 0;
int status = 0;
while (ColumnText.HasMoreText(status)) // same as while(status != 1)
{
if (column >= columns.Length)
{
column = 0;
document.NewPage();
}
this.SetSimpleColumn(columns[column]);
status = this.Go();
column++;
}
}
}
我为ProduceColumns添加了一个单独的方法,因为这允许多次调用AddElement。一旦添加了所有内容,就必须调用ProduceColumns。