【译】在Asp.Net中操作PDF - iTextSharp - 利用列进行排版
通常情况下.当你使用列时你都会喜欢使用多个列对文字进行排版。就像报纸那样。iTextSharp提供了MultiColumn对象使得实现多列排版变得非常简单。你仅仅需要告诉MultiColumn对象第一列的X轴坐标,第二列的X轴坐标,第二列结束的X轴坐标,列和列之间的距离,以及你需要多少列,下面代码在当前文档中加入两列,并在其内加入8次同样的段落(paragraph):
string pdfpath = Server.MapPath("PDFs");
string imagepath = Server.MapPath("Columns");
Document doc = new Document();
try
{
PdfWriter.GetInstance(doc, new FileStream(pdfpath + "/Columns.pdf", FileMode.Create));
doc.Open();
Paragraph heading = new Paragraph("Page Heading", new Font(Font.HELVETICA, 28f, Font.BOLD));
heading.SpacingAfter = 18f;
doc.Add(heading);
string text = @"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse blandit blandit turpis. Nam in lectus ut dolor consectetuer bibendum. Morbi neque ipsum, laoreet id; dignissim et, viverra id, mauris. Nulla mauris elit, consectetuer sit amet, accumsan eget, congue ac, libero. Vivamus suscipit. Nunc dignissim consectetuer lectus. Fusce elit nisi; commodo non, facilisis quis, hendrerit eu, dolor? Suspendisse eleifend nisi ut magna. Phasellus id lectus! Vivamus laoreet enim et dolor. Integer arcu mauris, ultricies vel, porta quis, venenatis at, libero. Donec nibh est, adipiscing et, ullamcorper vitae, placerat at, diam. Integer ac turpis vel ligula rutrum auctor! Morbi egestas erat sit amet diam. Ut ut ipsum? Aliquam non sem. Nulla risus eros, mollis quis, blandit ut; luctus eget, urna. Vestibulum vestibulum dapibus erat. Proin egestas leo a metus?";
MultiColumnText columns = new MultiColumnText();
//float left, float right, float gutterwidth, int numcolumns
columns.AddRegularColumns(36f, doc.PageSize.Width-36f, 24f, 2);
Paragraph para = new Paragraph(text, new Font(Font.HELVETICA, 8f));
para.SpacingAfter = 9f;
para.Alignment = Element.ALIGN_JUSTIFIED;
for (int i = 0; i < 8; i++)
{
columns.AddElement(para);
}
doc.Add(columns);
}
catch (Exception ex)
{
//Log(ex.Message);
}
finally
{
doc.Close();
}
下面结果展示了当文本从一个列中溢出后额外的文本自动填充到第二列:
AddRegularColumns()方易做图将其设置的每一列设置成相同的宽度,而AddSimpleColumn()方法允许你对单个列设置宽度,也就是你可以添加非常规列:
columns.AddSimpleColumn(36f, 170f);
columns.AddSimpleColumn(194f, doc.PageSize.Width - 36f);
上面的代码取代了在前一个代码段中的AddRegularColumn()方法来创建两个列.第一列距左36像素,2英尺宽(170 - 36 = 144),第二列在第一列的基础上向右24像素(3分之一英尺),结束于距离右边距36像素:
在列中加入其他比如图片和表格的元素也是非常简单:
string pdfpath = Server.MapPath("PDFs");
string imagepath = Server.MapPath("Images");
Document doc = new Document();
try
{
PdfWriter.GetInstance(doc, new FileStream(pdfpath + "/Columns.pdf", FileMode.Create));
doc.Open();
Paragraph heading = new Paragraph("Page Heading", new Font(Font.HELVETICA, 28f, Font.BOLD));
heading.SpacingAfter = 18f;
doc.Add(heading);
string text = @"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse blandit blandit turpis. Nam in lectus ut dolor consectetuer bibendum. Morbi neque ipsum, laoreet id; dignissim et, viverra id, mauris. Nulla mauris elit, consectetuer sit amet, accumsan eget, congue ac, libero. Vivamus suscipit. Nunc dignissim consectetuer lectus. Fusce elit nisi; commodo non, facilisis quis, hendrerit eu, dolor? Suspendisse eleifend nisi ut magna. Phasellus id lectus! Vivamus laoreet enim et dolor. Integer arcu mauris, ultricies vel, porta quis, venenatis at, libero. Donec nibh est, adipiscing et, ullamcorper vitae, placerat at, diam. Integer ac turpis vel ligula rutrum auctor! Morbi egestas erat sit amet diam. Ut ut ipsum? Aliquam non sem. Nulla risus eros, mollis quis, blandit ut; luctus eget, urna. Vestibulum vestibulum dapibus erat. Proin egestas leo a metus?";
MultiColumnText columns = new MultiColumnText();
columns.AddSimpleColumn(36f, 336f);
columns.AddSimpleColumn(360f, doc.PageSize.Width - 36f);
Paragraph para = new Paragraph(text, new Font(Font.HELVETICA, 8f));
para.SpacingAfter = 9f;
para.Alignment = Element.ALIGN_JUSTIFIED;
PdfPTable table = new PdfPTable(3);
float[] widths = new float[] { 1f, 1f, 1f };
table.TotalWidth = 300f;
table.LockedWidth = true;
table.SetWidths(widths);
PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.Colspan = 3;
cell.HorizontalAlignment = 0;
table.AddCell(cell);
table.AddCell("Col 1 Row 1");
table.AddCell("Col 2 Row 1");
table.AddCell("Col 3 Row 1");
table.AddCell("Col 1 Row 2");
table.AddCell("Col 2 Row 2");
table.AddCell("Col 3 Row 2");
Image jpg = Image.GetInstance(imagepath + "/Sunset.jpg");
jpg.ScaleToFit(300f, 300f);
jpg.SpacingAfter = 12f;
jpg.SpacingBefore = 12f;
columns.AddElement(para);
columns.AddElement(table);
columns.AddElement(jpg);
columns.AddElement(para);
<
补充:Web开发 , ASP.Net ,