using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aspose.Pdf;
using Aspose.Pdf.Text;
namespace PDFGen
{
class Program
{
static void Main(string[] args)
{
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
doc.Info.Author = "Company Name";
var page = doc.Pages.Add();
page.PageInfo.Margin = new MarginInfo(15, 50, 15, 50);
page.Header = new HeaderFooter();
page.Header.Margin = new MarginInfo(15, 0, 15, 0);
Aspose.Pdf.Table hdrTable = new Aspose.Pdf.Table();
int imageSize = 35;
int width2Use = (int)(!page.PageInfo.IsLandscape ? page.PageInfo.Width : page.PageInfo.Height);
int twentyPercent = (int)(width2Use / 5);
int thirtyPercent = (int)((width2Use / 10) * 3);
int fortyPercent = (int)((width2Use / 10) * 4);
int sixtyPercent = (int)(width2Use - (2 * twentyPercent));
hdrTable.ColumnWidths = string.Format("{0} {1} {2} {3}", imageSize, thirtyPercent - imageSize, fortyPercent, thirtyPercent);
hdrTable.DefaultCellPadding = new MarginInfo(0, 2, 0, 2);
hdrTable.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Bottom, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black));
Aspose.Pdf.Row hdrRow = hdrTable.Rows.Add();
hdrRow.DefaultCellTextState.Font = FontRepository.FindFont("TimesNewRoman");
hdrRow.DefaultCellTextState.FontSize = 11;
hdrRow.DefaultCellTextState.FontStyle = FontStyles.Bold;
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
image.File = "PrintLogo.bmp";
image.FixHeight = imageSize;
image.FixWidth = imageSize;
hdrRow.Cells.Add();
hdrRow.Cells[0].Paragraphs.Add(image);
hdrRow.Cells[0].RowSpan = 2;
hdrRow.Cells[0].Alignment = HorizontalAlignment.Left;
hdrRow.Cells.Add("Company Name");
hdrRow.Cells[1].Alignment = HorizontalAlignment.Left;
hdrRow.Cells[1].DefaultCellTextState.FontSize = 16;
hdrRow.Cells[1].DefaultCellTextState.Font = FontRepository.FindFont("Arial");
hdrRow.Cells[1].Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Bottom, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black));
hdrRow.Cells.Add("Report Title");
hdrRow.Cells[2].Alignment = HorizontalAlignment.Center;
hdrRow.Cells[2].RowSpan = 2;
hdrRow.Cells[2].VerticalAlignment = VerticalAlignment.Center;
hdrRow.Cells.Add("USERNAME");
hdrRow.Cells[3].Alignment = HorizontalAlignment.Right;
hdrRow.Cells[3].RowSpan = 2;
hdrRow.Cells[3].VerticalAlignment = VerticalAlignment.Center;
hdrRow = hdrTable.Rows.Add();
hdrRow.DefaultCellTextState.Font = FontRepository.FindFont("TimesNewRoman");
hdrRow.DefaultCellTextState.FontSize = 11;
hdrRow.DefaultCellTextState.FontStyle = FontStyles.Bold;
hdrRow.Cells.Add("Service Name");
hdrRow.Cells[0].Alignment = HorizontalAlignment.Left;
page.Header.Paragraphs.Add(hdrTable);
page.Footer = new HeaderFooter();
page.Footer.Margin = new MarginInfo(15, 0, 15, 0);
Aspose.Pdf.Table ftrTable = new Aspose.Pdf.Table();
ftrTable.ColumnWidths = string.Format("{0} {1} {0}", twentyPercent, sixtyPercent);
ftrTable.DefaultCellPadding = new MarginInfo(0, 1, 0, 1);
ftrTable.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Top, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black));
Aspose.Pdf.Row ftrRow = ftrTable.Rows.Add();
ftrRow.DefaultCellTextState.Font = FontRepository.FindFont("TimesNewRoman");
ftrRow.DefaultCellTextState.FontSize = 10;
ftrRow.DefaultCellTextState.FontStyle = FontStyles.Bold;
ftrRow.Cells.Add("Version : 9.99.9.999");
ftrRow.Cells[0].Alignment = HorizontalAlignment.Left;
ftrRow.Cells.Add("This report is confidential and for the intended recipient only.");
ftrRow.Cells[1].Alignment = HorizontalAlignment.Center;
ftrRow.Cells[1].DefaultCellTextState.FontSize = 9;
ftrRow.Cells.Add(System.DateTime.Now.ToString("dd/MM/yy HH:mm.ss"));
ftrRow.Cells[2].Alignment = HorizontalAlignment.Right;
ftrRow = ftrTable.Rows.Add();
ftrRow.DefaultCellTextState.Font = FontRepository.FindFont("TimesNewRoman");
ftrRow.DefaultCellTextState.FontSize = 10;
ftrRow.DefaultCellTextState.FontStyle = FontStyles.Bold;
ftrRow.Cells.Add(string.Empty);
ftrRow.Cells[0].Alignment = HorizontalAlignment.Left;
ftrRow.Cells.Add("If you are not the intended recipient please destroy this page immediately.");
ftrRow.Cells[1].Alignment = HorizontalAlignment.Center;
ftrRow.Cells[1].DefaultCellTextState.FontSize = 9;
ftrRow.Cells.Add("Page $p-$P");
ftrRow.Cells[2].Alignment = HorizontalAlignment.Right;
page.Footer.Paragraphs.Add(ftrTable);
Aspose.Pdf.Heading heading = new Aspose.Pdf.Heading(1);
heading.IsInList = false;
heading.Text = "Supplier Name";
heading.TextState.FontSize = 20;
heading.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
page.Paragraphs.Add(heading);
page.Paragraphs[0].Margin = new MarginInfo(0, 10, 0, 0);
TextFragment textFragment = new TextFragment("Services provided between dd/mm/yyyy and dd/mm/yyyy.");
textFragment.TextState.Font = FontRepository.FindFont("TimesNewRoman");
textFragment.TextState.FontSize = 12;
textFragment.TextState.FontStyle = FontStyles.Italic;
textFragment.TextState.FormattingOptions = new TextFormattingOptions();
textFragment.TextState.FormattingOptions.LineSpacing = TextFormattingOptions.LineSpacingMode.FullSize;
page.Paragraphs.Add(textFragment);
page.Paragraphs[1].Margin = new MarginInfo(0, 10, 0, 0);
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
table.Broken = TableBroken.Vertical;
table.CornerStyle = BorderCornerStyle.Round;
table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
table.DefaultCellTextState.Font = FontRepository.FindFont("Arial");
table.DefaultCellTextState.FontSize = 12;
table.DefaultCellPadding = new MarginInfo(5, 5, 5, 5);
table.RepeatingRowsCount = 1;
table.ColumnWidths = string.Format("200 100 200 200 100");
for (int row_count = 1; row_count <= 50; row_count++)
{
Aspose.Pdf.Row row = table.Rows.Add();
if (row_count == 1)
{
row.DefaultCellTextState.FontSize = 14;
row.DefaultCellTextState.FontStyle = FontStyles.Bold;
row.Cells.Add("Name");
row.Cells.Add("Age");
row.Cells.Add("Position");
row.Cells.Add("Car");
row.Cells.Add("Reg. No.");
row.Cells[1].Alignment = HorizontalAlignment.Center;
}
else
{
string name = string.Empty;
int age = 0;
string position = string.Empty;
string car = string.Empty;
string regNo = string.Empty;
switch (row_count % 3)
{
case 1:
name = "Director One";
age = 51;
position = "Director";
car = "Audi A6 S-Line Black Edition";
regNo = "XX99 XXX";
break;
case 2:
name = "Director Two";
age = 49;
position = "Director";
car = "Audi A4 Avant S-Line";
regNo = "XX99 XXX";
break;
default:
name = "Employee One";
age = 46;
position = "Employee";
car = "Audi A1 SE";
regNo = "XX99 XXX";
break;
}
row.Cells.Add(name);
row.Cells.Add(age.ToString());
row.Cells.Add(position);
row.Cells.Add(car);
row.Cells.Add(regNo);
row.Cells[1].Alignment = HorizontalAlignment.Center;
}
}
page.Paragraphs.Add(table);
TextFragment endOfReport = new TextFragment("*** End of Report ***");
endOfReport.TextState.Font = FontRepository.FindFont("TimesNewRoman");
endOfReport.TextState.FontSize = 12;
endOfReport.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
endOfReport.Margin = new MarginInfo(0, 0, 0, 10);
page.Paragraphs.Add(endOfReport);
doc.Save(".\\Output.pdf");
}
}
}
Output.pdf (361.2 KB)