i have an issue where i am using a grid which repeats across pages. See attached file that the grid borders are being placed on top of existing grid again and seem offset. First and last pages are fine.
- Code: Select all
var pSize = PdfPageSize.A3;
var pMargins = new PdfMargins(30, 30, 30, 30);
var pRotation = PdfPageRotateAngle.RotateAngle0;
var pOrientation = PdfPageOrientation.Landscape;
PdfDocument doc = new PdfDocument();
doc.PageSettings.Orientation = PdfPageOrientation.Landscape;
PdfPageBase page = doc.Pages.Add(pSize, pMargins, pRotation, pOrientation);
var fontBold = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Bold), true);
PdfFont fontPageNumbers = new PdfFont(PdfFontFamily.Helvetica, 8f);
//PdfFont wingDingsfont = new PdfFont(PdfFontFamily.ZapfDingbats, 11f);
PdfBrush brushBlack = PdfBrushes.Black;
PdfBrush brushWhite = PdfBrushes.White;
PdfBrush brushBlueBackground = new PdfSolidBrush(new PdfRGBColor(88, 103, 221));
PdfBrush brushYellowBackground = new PdfSolidBrush(new PdfRGBColor(255, 184, 34));
PdfBrush brushGreyBackground = new PdfSolidBrush(new PdfRGBColor(211, 218, 230));
PdfBrush brushRedBackground = new PdfSolidBrush(new PdfRGBColor(253, 57, 122));
PdfBrush brushGreenBackground = new PdfSolidBrush(new PdfRGBColor(29, 201, 183));
PdfStringFormat formatLeftAlign = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Top);
PdfStringFormat formatRightAlign = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top);
PdfStringFormat formatCenterAlign = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Top);
PdfBorders borderNone = new PdfBorders
{
All = new PdfPen(Color.Transparent)
};
PdfBorders borderRight = new PdfBorders
{
All = new PdfPen(Color.Transparent),
Right = new PdfPen(Color.Black, (float)0.5)
};
PdfBorders borderTop = new PdfBorders
{
All = new PdfPen(Color.Transparent),
Top = new PdfPen(Color.Black, (float)0.5)
};
PdfBorders borderAll = new PdfBorders
{
All = new PdfPen(Color.Black, (float)0.5)
};
PdfBrush titleBrush = new PdfSolidBrush(new PdfRGBColor(14, 123, 192));
PdfStringFormat titleFormat = new PdfStringFormat(PdfTextAlignment.Right);
PdfFont titleFont = new PdfFont(PdfFontFamily.Helvetica, 11f, PdfFontStyle.Bold);
PointF location = new PointF(20, 20);
float width = page.Canvas.ClientSize.Width / 2;
float totalWidth = page.Canvas.ClientSize.Width;
//List<PdfGrid> grid = new List<PdfGrid>();
PdfGrid parentGrid = new PdfGrid();
parentGrid.Columns.Add();
parentGrid.AllowCrossPages = true;
parentGrid.Style.CellPadding = new PdfPaddings(0, 0, 0, 0);
parentGrid.Columns[0].Width = totalWidth;
parentGrid.Style.CellSpacing = 0;
//allow to break across page for large tables
//newGrid.AllowCrossPages = true;
int colCount = 14;
var colWidths = new List<float> {
(float)0.05,//1
(float)0.03,//2
(float)0.23,//3
(float)0.08,//4
(float)0.08,//5
(float)0.01,//6
(float)0.08,//7
(float)0.06,//8
(float)0.06,//9
(float)0.05,//10
(float)0.05,//11
(float)0.05,//12
(float)0.12,//13
(float)0.05//14 13
};
var colNames = new List<string> {
"Tag",//1
"Qty Reqd",//2
"Description",//3
"Supplier",//4
"Reqd Del Date",//5
"",//6
"Status",//7
"Qty Ord",//8
"Qty Del",//9
"PO Del Date",//10
"Del Date",//11
"Received Date",//12
"PO No.",//13
"Note"//14
};
foreach (var item in req)
{
PdfGrid newGrid = new PdfGrid();
newGrid.Style.CellPadding = new PdfPaddings(4, 4, 4, 4);
newGrid.Style.CellSpacing = 0;
newGrid.Style.AllowHorizontalOverflow = false;
//allow to break across page for large tables
newGrid.Columns.Add(colCount);
//PdfGridRow headerRow = newGrid.Headers.Add(1)[0];
newGrid.AllowCrossPages = true;
newGrid.RepeatHeader = true;
for (int c = 0; c < colCount; c++)
{
newGrid.Columns[c].Format.Alignment = PdfTextAlignment.Center;
newGrid.Columns[c].Width = totalWidth * colWidths[c];
}
///Add grid headers
PdfGridRow headerRow00 = newGrid.Headers.Add(1)[0];
PdfGridRow headerRow0 = newGrid.Headers.Add(1)[1];
PdfGridRow headerRow1 = newGrid.Headers.Add(1)[2];
PdfGridRow headerRow2 = newGrid.Headers.Add(1)[3];
headerRow00.Height = 10;
headerRow00.Cells[0].ColumnSpan = colCount;
headerRow00.Cells[0].Style.Borders = borderNone;
headerRow0.Height = 25;
headerRow0.Cells[0].ColumnSpan = colCount;
headerRow0.Cells[0].Style.Borders = borderNone;
headerRow0.Cells[0].Style.Font = fontBold;
headerRow0.Cells[0].Value = "Bill of Materials - " + proj;
headerRow0.Cells[0].Style.StringFormat = formatLeftAlign;
headerRow1.Height = 6;
headerRow1.Cells[0].ColumnSpan = 5;
headerRow1.Cells[0].Style.BackgroundBrush = brushBlueBackground;
headerRow1.Cells[0].Style.Borders = borderNone;
headerRow1.Cells[5].Style.Borders = borderNone;
headerRow1.Cells[6].ColumnSpan = 8;
headerRow1.Cells[6].Style.BackgroundBrush = brushYellowBackground;
headerRow1.Cells[6].Style.Borders = borderNone;
for (int c = 0; c < colCount; c++)
{
headerRow2.Height = 35;
headerRow2.Cells[c].Value = colNames[c];
headerRow2.Cells[c].Style.Borders = borderNone;
headerRow2.Cells[c].Style.Font = fontBold;
}
////////////////
int rowIndex = 0;
foreach (var p in model)
{
newGrid.Rows.Add();
newGrid.Rows[rowIndex].Cells[0].Value = p.ItemRef;
//newGrid.Rows[rowIndex].Cells[1].Value = Math.Round(p.QtyReqd, 2).ToString();
newGrid.Rows[rowIndex].Cells[2].Value = p.LineDesc;
//newGrid.Rows[rowIndex].Cells[3].Value = p.SupplierName;
//newGrid.Rows[rowIndex].Cells[4].Value = p.DateReqd;
//newGrid.Rows[rowIndex].Cells[5].Value = "";
//newGrid.Rows[rowIndex].Cells[6].Value = p.StatusName;
//newGrid.Rows[rowIndex].Cells[7].Value = Math.Round(p.QtyOrd, 2).ToString();
//newGrid.Rows[rowIndex].Cells[8].Value = Math.Round(p.QtyReceived, 2).ToString();
//newGrid.Rows[rowIndex].Cells[9].Value = p.DateDel;
//newGrid.Rows[rowIndex].Cells[10].Value = p.DateDelConf;
//newGrid.Rows[rowIndex].Cells[11].Value = p.DateReceipt;
var poNos = "";
foreach (var pon in p.PONumbers)
{
poNos = poNos + "[" + pon + "] ";
}
//newGrid.Rows[rowIndex].Cells[12].Value = poNos;
//newGrid.Rows[rowIndex].Cells[13].Value = p.TopNote;
for (int c = 0; c < colCount; c++)
{
newGrid.Rows[rowIndex].Cells[c].Style.Borders = borderAll;
}
newGrid.Rows[rowIndex].Cells[5].Style.Borders = borderNone;
if (p.StatusName == "Not ordered")
{
newGrid.Rows[rowIndex].Cells[6].Style.BackgroundBrush = brushGreyBackground;
}
if (p.StatusName == "Partial ordered")
{
newGrid.Rows[rowIndex].Cells[6].Style.BackgroundBrush = brushGreyBackground;
}
if (p.StatusName == "Not delivered")
{
newGrid.Rows[rowIndex].Cells[6].Style.BackgroundBrush = brushRedBackground;
}
if (p.StatusName == "Delivered")
{
newGrid.Rows[rowIndex].Cells[6].Style.BackgroundBrush = brushGreenBackground;
}
rowIndex++;
//alpha++;
}
PdfGridRow nprm = parentGrid.Rows.Add();
nprm.Cells[0].Value = newGrid;
nprm.Cells[0].Style.Borders = borderNone;
}
}
float y = 15;
PdfLayoutResult result = parentGrid.Draw(page, new PointF(0, y));
y = y + result.Bounds.Height + 15;
//page numbers
for (i = 0; i < doc.Pages.Count; i++)
{
page = doc.Pages[i];
//location = new PointF(0, page.Canvas.ClientSize.Height - 40);
//if (docName != null)
//{
// page.Canvas.DrawString(docName, font, brushBlack, location, new PdfStringFormat(PdfTextAlignment.Left));
//}
//PdfPen pen = new PdfPen(Color.Black, 0.1f);
//page.Canvas.DrawLine(pen, 0, page.Canvas.ClientSize.Height - 25, page.Canvas.ClientSize.Width, page.Canvas.ClientSize.Height - 25);
location = new PointF(page.Canvas.ClientSize.Width, page.Canvas.ClientSize.Height - 15);
page.Canvas.DrawString(String.Format("Page {0} of {1}", i + 1, doc.Pages.Count), fontPageNumbers, brushBlack, location, new PdfStringFormat(PdfTextAlignment.Right));
//location = new PointF(page.Canvas.ClientSize.Width / 2, page.Canvas.ClientSize.Height - 20);
//page.Canvas.DrawString(String.Format("Copyright © " + DateTime.Now.Year.ToString() + " " + companyName), font, brushBlack, location, new PdfStringFormat(PdfTextAlignment.Center));
}
string virtualFolder = "~/temp/";
string physicalFolder = System.Web.HttpContext.Current.Server.MapPath(virtualFolder);
string FileID = Guid.NewGuid().ToString();
string NewFileName = physicalFolder + "\\temp_" + FileID + ".pdf";
//set compression level here
//////////////////////////////////////////////////
//newDoc.FileInfo.IncrementalUpdate = false;
//newDoc.CompressionLevel = PdfCompressionLevel.Best;
//////////////////////////////////////////////////
///
PaperSize paper = new PaperSize("A3", (int)PdfPageSize.A3.Width, (int)PdfPageSize.A3.Height);
paper.RawKind = (int)PaperKind.A3;
doc.PrintSettings.PaperSize = paper;
doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.FitSize);
doc.SaveToFile(NewFileName);
doc.SaveToHttpResponse(NewFileName, System.Web.HttpContext.Current.Response, HttpReadType.Open);
doc.Dispose();
return NewFileName;