private void button1_Click(object sender, EventArgs e) { //Create a pdf document. PdfDocument doc = new PdfDocument(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; // Create one page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); float y = 10; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold), true); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Categories List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Categories List", format1).Height; y = y + 5; RectangleF rctg = new RectangleF(new PointF(0, 0), page.Canvas.ClientSize); PdfLinearGradientBrush brush = new PdfLinearGradientBrush(rctg, Color.Navy, Color.OrangeRed, PdfLinearGradientMode.Vertical); PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold); String formatted = "Beverages\nCondiments\nConfections\nDairy Products\nGrains/Cereals\nMeat/Poultry\nProduce\nSeafood"; PdfList list = new PdfList(formatted); list.Font = font; list.Indent = 8; list.TextIndent = 5; list.Brush = brush; PdfLayoutResult result = list.Draw(page, 0, y); y = result.Bounds.Bottom; PdfSortedList sortedList = new PdfSortedList(formatted); sortedList.Font = font; sortedList.Indent = 8; sortedList.TextIndent = 5; sortedList.Brush = brush; sortedList.Draw(page, 0, y); //Save pdf file. doc.SaveToFile("SimpleList.pdf"); doc.Close(); //Launching the Pdf file. PDFDocumentViewer("SimpleList.pdf"); }
public PdfLayoutResult HeaderPoints(string text, float yPosition, PdfPage page) { float headerBulletsXposition = 35; PdfTextElement txtElement = new PdfTextElement("") { Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 13), Brush = new PdfSolidBrush(aliceBlue), StringFormat = new PdfStringFormat() }; txtElement.StringFormat.WordWrap = PdfWordWrapType.Word; txtElement.StringFormat.LineLimit = true; txtElement.Draw(page, new RectangleF(headerBulletsXposition, yPosition, 300, 100)); txtElement = new PdfTextElement(text) { Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 11), StringFormat = new PdfStringFormat() }; txtElement.StringFormat.WordWrap = PdfWordWrapType.Word; txtElement.StringFormat.LineLimit = true; PdfLayoutResult result = txtElement.Draw(page, new RectangleF(headerBulletsXposition + 20, yPosition, 320, 100)); return(result); }
public PdfLayoutResult BodyContent(string text, float yPosition, PdfPage page) { float headerBulletsXposition = 35; PdfTextElement txtElement = new PdfTextElement("") { Font = new PdfStandardFont(PdfFontFamily.ZapfDingbats, 16), StringFormat = new PdfStringFormat() }; txtElement.StringFormat.WordWrap = PdfWordWrapType.Word; txtElement.StringFormat.LineLimit = true; txtElement.Draw(page, new RectangleF(headerBulletsXposition, yPosition, 320, 100)); txtElement = new PdfTextElement(text) { Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 17), StringFormat = new PdfStringFormat() }; txtElement.StringFormat.WordWrap = PdfWordWrapType.Word; txtElement.StringFormat.LineLimit = true; PdfLayoutResult result = txtElement.Draw(page, new RectangleF(headerBulletsXposition + 25, yPosition, 450, 130)); return(result); }
public MemoryStream PdfBuktiBank(TranshView trans) { var buktibank = _context.CbTransHs.Include(p => p.CbTransDs).Where(x => x.DocNo == trans.DocNo).FirstOrDefaultAsync(); if (buktibank == null) { throw new ArgumentNullException("Transaksi cannot be null"); } using (PdfDocument pdfDocument = new PdfDocument()) { // int paragraphAfterSpacing = 8; // int cellMargin = 8; //Add page to the PDF document PdfPage page = pdfDocument.Pages.Add(); //Create a new font PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 16); //Create a text element to draw a text in PDF page PdfTextElement title = new PdfTextElement("BUKTI KAS/BANK", font, PdfBrushes.Black); PdfLayoutResult result = title.Draw(page, new PointF(0, 0)); using (MemoryStream stream = new MemoryStream()) { //Saving the PDF document into the stream pdfDocument.Save(stream); //Closing the PDF document pdfDocument.Close(true); return(stream); } } }
private void button1_Click(object sender, EventArgs e) { //Create a pdf document.<br> PdfDocument doc = new PdfDocument(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; // Create new page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); float y = 10; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Country List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Country List", format1).Height; y = y + 5; String[] data = { "Name;Capital;Continent;Area;Population", "Argentina;Buenos Aires;South America;2777815;32300003", "Bolivia;La Paz;South America;1098575;7300000", "Brazil;Brasilia;South America;8511196;150400000", "Canada;Ottawa;North America;9976147;26500000", }; String[][] dataSource = new String[data.Length][]; for (int i = 0; i < data.Length; i++) { dataSource[i] = data[i].Split(';'); } PdfTable table = new PdfTable(); table.Style.CellPadding = 2; table.Style.HeaderSource = PdfHeaderSource.Rows; table.Style.HeaderRowCount = 1; table.Style.ShowHeader = true; table.DataSource = dataSource; PdfLayoutResult result = table.Draw(page, new PointF(0, y)); y = y + result.Bounds.Height + 5; PdfBrush brush2 = PdfBrushes.Gray; PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f)); page.Canvas.DrawString(String.Format("* {0} countries in the list.", data.Length - 1), font2, brush2, 5, y); //Save pdf file. doc.SaveToFile("SimpleTable.pdf"); doc.Close(); System.Diagnostics.Process.Start("SimpleTable.pdf"); }
private void button1_Click(object sender, EventArgs e) { //Create a Pdf document PdfDocument doc = new PdfDocument(); //Add a Pdf page PdfPageBase page = doc.Pages.Add(); float y = 20; //Draw the table 1 string title1 = "Table 1"; PdfLayoutResult result = DrawPDFTable(title1, y, page, "parts"); //Get the current Y coordinate and page y = result.Bounds.Height + 10; page = result.Page; //Draw the table 2 string title2 = "Table 2"; result = DrawPDFTable(title2, y, page, "country"); //Save the Pdf document string output = "AddContinuousTables_out.pdf"; doc.SaveToFile(output); doc.Close(); //Launch the Pdf file PDFDocumentViewer(output); }
private void DrawLine(PdfPage page, PdfLayoutResult result) { PdfPen linePen = new PdfPen(new PdfColor(126, 151, 173), 0.70f); PointF startPoint = new PointF(0, result.Bounds.Bottom + 3); PointF endPoint = new PointF(page.Graphics.ClientSize.Width, result.Bounds.Bottom + 3); page.Graphics.DrawLine(linePen, startPoint, endPoint); }
//Export weather data to PDF document. public MemoryStream CreatePdf(WeatherForecast[] forecasts) { if (forecasts == null) { throw new ArgumentNullException("Forecast cannot be null"); } //Create a new PDF document using (PdfDocument pdfDocument = new PdfDocument()) { int paragraphAfterSpacing = 8; int cellMargin = 8; // pdfDocument.PageSettings.Size = PdfPageSize.A4; //Add page to the PDF document PdfPage page = pdfDocument.Pages.Add(); //Create a new font PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 16); //Create a text element to draw a text in PDF page PdfTextElement title = new PdfTextElement("Weather Forecast", font, PdfBrushes.Black); PdfLayoutResult result = title.Draw(page, new PointF(0, 0)); PdfStandardFont contentFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); PdfTextElement content = new PdfTextElement("This component demonstrates fetching data from a service and Exporting the data to PDF document using Syncfusion .NET PDF library.", contentFont, PdfBrushes.Black); PdfLayoutFormat format = new PdfLayoutFormat(); format.Layout = PdfLayoutType.Paginate; //Draw a text to the PDF document result = content.Draw(page, new RectangleF(0, result.Bounds.Bottom + paragraphAfterSpacing, page.GetClientSize().Width, page.GetClientSize().Height), format); //Create a PdfGrid PdfGrid pdfGrid = new PdfGrid(); pdfGrid.Style.CellPadding.Left = cellMargin; pdfGrid.Style.CellPadding.Right = cellMargin; //Applying built-in style to the PDF grid pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1); //Assign data source pdfGrid.DataSource = forecasts; pdfGrid.Style.Font = contentFont; //Draw PDF grid into the PDF page pdfGrid.Draw(page, new Syncfusion.Drawing.PointF(0, result.Bounds.Bottom + paragraphAfterSpacing)); using (MemoryStream stream = new MemoryStream()) { //Saving the PDF document into the stream pdfDocument.Save(stream); //Closing the PDF document pdfDocument.Close(true); return(stream); } } }
public void ExportToPdf(List <T> entities, string reportTitle, ReportRequest reportRequest, List <string> columns) { int paragraphAfterSpacing = 8; int cellMargin = 8; PdfDocument pdfDocument = new PdfDocument(); //Add Page to the PDF document. PdfPage page = pdfDocument.Pages.Add(); //Create a new font. PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 16); //Create a text element to draw a text in PDF page. PdfTextElement title = new PdfTextElement(reportTitle, font, PdfBrushes.Black); PdfLayoutResult result = title.Draw(page, new PointF(0, 0)); var contentInfo = CreateContentInfo(reportRequest, entities); PdfStandardFont contentFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); PdfTextElement content = new PdfTextElement(contentInfo, contentFont, PdfBrushes.Black); PdfLayoutFormat format = new PdfLayoutFormat(); format.Layout = PdfLayoutType.Paginate; //Draw a text to the PDF document. result = content.Draw(page, new RectangleF(0, result.Bounds.Bottom + paragraphAfterSpacing, page.GetClientSize().Width, page.GetClientSize().Height), format); //Create a PdfGrid. PdfGrid pdfGrid = new PdfGrid(); pdfGrid.Style.CellPadding.Left = cellMargin; pdfGrid.Style.CellPadding.Right = cellMargin; //Applying built-in style to the PDF grid pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1); //Create data to populate table DataTable table = CreateDataSourceTable(entities, columns); //Assign data source. pdfGrid.DataSource = table; pdfGrid.Style.Font = contentFont; //Draw PDF grid into the PDF page. pdfGrid.Draw(page, new Syncfusion.Drawing.PointF(0, result.Bounds.Bottom + paragraphAfterSpacing)); MemoryStream memoryStream = new MemoryStream(); // Save the PDF document. pdfDocument.Save(memoryStream); // Download the PDF document _jsRuntime.SaveAs(reportTitle + ".pdf", memoryStream.ToArray()); }
public MemoryStream CreatePdf(WeatherForecast[] weatherForecasts) { if (weatherForecasts == null) { throw new ArgumentException("Data cannot be null"); } //Create a new PDF document using (PdfDocument pdfDocument = new PdfDocument()) { int paragraphAfterSpacing = 8; int cellMargin = 8; //Add page to the pdf Document PdfPage page = pdfDocument.Pages.Add(); //create New Font PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 16); //Create a text elemet to draw a text in PDf Page PdfTextElement title = new PdfTextElement("Weather Forecast", font, PdfBrushes.Black); PdfLayoutResult result = title.Draw(page, new PointF(0, 0)); PdfStandardFont contentFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 12); PdfLayoutFormat format = new PdfLayoutFormat(); format.Layout = PdfLayoutType.Paginate; //Create PDF PdfGrid pdfGrid = new PdfGrid(); pdfGrid.Style.CellPadding.Left = cellMargin; pdfGrid.Style.CellPadding.Right = cellMargin; //Apply built in Styke to the PDF grid pdfGrid.ApplyBuiltinStyle(PdfGridBuiltinStyle.GridTable4Accent1); //Assing Data Source pdfGrid.DataSource = weatherForecasts.ToList(); pdfGrid.Style.Font = contentFont; //Draw PDf Grid into the pdf Page pdfGrid.Draw(page, new PointF(0, result.Bounds.Bottom + paragraphAfterSpacing)); using (MemoryStream stream = new MemoryStream()) { pdfDocument.Save(stream); pdfDocument.Close(true); return(stream); } } }
public static void pdfsettings() { path = @"C:\Users\Public\Documents\RadianLabs\ReliefText\TeacherID-" + al5[relindex] + "_Date-" + date + "_Relief.pdf"; PdfUnitConvertor uc = new PdfUnitConvertor();; PdfMargins marg = new PdfMargins();; PdfPageBase page; PdfDocument pdfd; float y = 10; PdfBrush bru; PdfTrueTypeFont fon; PdfStringFormat format; pdfd = new PdfDocument(); marg.Top = uc.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); marg.Bottom = marg.Top; marg.Left = uc.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); marg.Right = marg.Left; page = pdfd.Pages.Add(PdfPageSize.A4, marg); bru = PdfBrushes.Black; fon = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); format = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString(textname, fon, bru, page.Canvas.ClientSize.Width / 2, y, format); y = y + fon.MeasureString(textname, format).Height; y = y + 5; String[] pdfdata = { "Period;Class", "One;" + relprinter[0] + "", "Two;" + relprinter[1] + "", "Three;" + relprinter[2] + "", "Four;" + relprinter[3] + "", "Five;" + relprinter[4] + "", "Six;" + relprinter[5] + "", "Seven;" + relprinter[6] + "", "Eight;" + relprinter[7] + "" }; String[][] dataSource = new String[pdfdata.Length][]; for (i = 0; i < pdfdata.Length; i++) { dataSource[i] = pdfdata[i].Split(';'); } PdfTable pdftable = new PdfTable(); pdftable.Style.CellPadding = 2; pdftable.Style.HeaderSource = PdfHeaderSource.Rows; pdftable.Style.HeaderRowCount = 1; pdftable.Style.ShowHeader = true; pdftable.DataSource = dataSource; PdfLayoutResult pdfresult = pdftable.Draw(page, new PointF(0, y)); PdfBrush bru2 = PdfBrushes.Gray; PdfTrueTypeFont fon2 = new PdfTrueTypeFont(new Font("Arial", 9f)); page.Canvas.DrawString(String.Format("{0}", pdfdata.Length - 1), fon2, bru2, 5, y); try { pdfd.SaveToFile(path); } catch (Exception ex) { Console.WriteLine(ex); } pdfd.Close(); }
private void CreateInputField(PdfPage questionlistPage, PdfLayoutResult afterResult, PdfDocument document) { PdfTextBoxField field = new PdfTextBoxField(questionlistPage, "input") { Text = "Schrijf hier uw advies.", Location = new PointF(6, afterResult.Bounds.Bottom + 40), Size = new SizeF(500, 16), Font = timesRoman, ForeColor = new PdfColor(System.Drawing.Color.White), BackColor = new PdfColor(126, 155, 203) }; document.Form.Fields.Add(field); }
private PdfLayoutResult BodyContent(string text, PdfFont font, Color color, PdfTextAlignment alignment, PdfVerticalAlignment hAlignment, PdfPage page, RectangleF rectangleF) { PdfTextElement txtElement = new PdfTextElement(text); txtElement.Font = font; txtElement.Brush = new PdfSolidBrush(color); txtElement.StringFormat = new PdfStringFormat(); txtElement.StringFormat.WordWrap = PdfWordWrapType.Word; txtElement.StringFormat.LineLimit = true; txtElement.StringFormat.Alignment = alignment; txtElement.StringFormat.LineAlignment = hAlignment; PdfLayoutResult result = txtElement.Draw(page, rectangleF); return(result); }
public PdfLayoutResult AddRectangleText(string leftText, string rightText, float y, float height, PdfBrush rectangleBrush = null, PdfBrush textBrush = null, PdfFont font = null) { rectangleBrush = rectangleBrush ?? AccentBrush; textBrush = textBrush ?? PdfBrushes.White; font = font ?? SubHeadingFont; DrawRectangle(0, y, PageWidth, height, rectangleBrush); y += 5; PdfLayoutResult result = AddText(leftText, 10, y, font, textBrush); AddText(rightText, 10, result.Bounds.Y, font, textBrush, true); return(result); }
private PdfLayoutResult OutputSingleLine(PdfPage page, PdfLayoutResult result, PdfTextElement element, string question, string answer) { try { element = new PdfTextElement(question, timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(126, 155, 203)); result = element.Draw(page, new PointF(10, result.Bounds.Bottom + _lineSpacing)); element = new PdfTextElement(answer, timesRoman); element.Brush = new PdfSolidBrush(new PdfColor(52, 173, 152)); } catch (Exception e) { } return(result = element.Draw(page, new PointF(10, result.Bounds.Bottom + _lineSpacing))); }
public PdfLayoutResult AddText(string text, float x, float y, PdfFont font = null, PdfBrush brush = null, bool xIsRightOffset = false, float?forceTextSize = null) { font = font ?? NormalFont; brush = brush ?? AccentBrush; if (xIsRightOffset) { SizeF textSize = font.MeasureString(text); float textWidth = forceTextSize ?? textSize.Width; x = PageWidth - textWidth - x; } PdfTextElement element = new PdfTextElement(text, font, brush); PdfLayoutResult result = element.Draw(CurrentPage, new PointF(x, y)); return(result); }
private void CreateChart(PdfPage page, PdfLayoutResult result, PdfTextElement element, RapportVragenlijstVM answerlist) { List <RapportVragenlijstStap> chartList = new List <RapportVragenlijstStap>(); foreach (var x in answerlist.Stappen) { if (x.TypeNaam.Equals("Getal")) { chartList.Add(x); } } CartesianChart Chart = new CartesianChart { DisableAnimations = true, Width = 600, Height = 400, Series = new SeriesCollection { new ColumnSeries { Values = new ChartValues <double> { 4, 3, 2, 5, 10 } } } }; Chart.AxisY.Add(new Axis { MinValue = 0, MaxValue = 10, Separator = new LiveCharts.Wpf.Separator { Step = 1, IsEnabled = true } }); string path = "chart.png"; Chart.Update(true, true); SaveToPng(Chart, path); }
private void InsertChart(PdfPage questionlistPage, PdfLayoutResult afterResult, PdfDocument document, PdfTextElement textelement, PdfGraphics graphics, RapportVragenlijstVM answerlist) { try { //Chart CreateChart(questionlistPage, afterResult, textelement, answerlist); string path = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\chart.png"; PdfBitmap image = new PdfBitmap(path); graphics.DrawImage(image, new RectangleF(6, afterResult.Bounds.Bottom + 100, 500, 80)); //PdfBitmap image = new PdfBitmap(File.Open("chart.png")); //image.Draw(page, 10, result.Bounds.Bottom + 60); } catch (Exception e) { MessageBox.Show("Er ging iets fout bij het genereren van de diagram.", "Waarschuwing"); } }
public static byte[] Print(Article a, string filename) { filename = Folder + filename; PdfDocument doc = new PdfDocument(); PdfPage page = doc.Pages.Add(); //create a new PDF string format PdfStringFormat drawFormat = new PdfStringFormat(); drawFormat.WordWrap = PdfWordWrapType.Word; drawFormat.Alignment = PdfTextAlignment.Justify; drawFormat.LineAlignment = PdfVerticalAlignment.Top; //Set the font. PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 40f); //Create a brush. PdfBrush brush = PdfBrushes.Red; //bounds RectangleF bounds = new RectangleF(new PointF(10, 10), new SizeF(page.Graphics.ClientSize.Width - 30, page.Graphics.ClientSize.Height - 20)); //Create a new text elememt PdfTextElement element = new PdfTextElement(a.Name, font, brush); //Set the string format element.StringFormat = drawFormat; //Draw the text element PdfLayoutResult result = element.Draw(page, bounds); FileStream s = new FileStream(filename, FileMode.OpenOrCreate); doc.Save(s); s.Flush(); s.Close(); s.Dispose(); return(File.ReadAllBytes(filename)); }
private PdfLayoutResult ReadFilledForm(PdfPage page, PdfLayoutResult result, PdfTextElement element, RapportVragenlijstVM answerlist) { foreach (var q in answerlist.Stappen) { if (q.TypeNaam.Equals("Foto") || q.TypeNaam.Equals("Enkel") || q.TypeNaam.Equals("Getal") || q.TypeNaam.Equals("Open")) { result = OutputSingleLine(page, result, element, q.Omschrijving, q.Antwoord); } else { string multiple = ""; foreach (var m in q.MeerkeuzeAntwoord) { multiple = multiple + m + ", "; } result = OutputSingleLine(page, result, element, q.Omschrijving, multiple); } DrawLine(page, result); } return(result); }
private PdfLayoutResult BodyContent(string text, float yPosition) { float headerBulletsXposition = 35; PdfTextElement txtElement = new PdfTextElement("3"); txtElement.Font = new PdfStandardFont(PdfFontFamily.ZapfDingbats, 16); txtElement.Brush = new PdfSolidBrush(violet); txtElement.StringFormat = new PdfStringFormat(); txtElement.StringFormat.WordWrap = PdfWordWrapType.Word; txtElement.StringFormat.LineLimit = true; txtElement.Draw(page, new RectangleF(headerBulletsXposition, yPosition, 320, 100)); txtElement = new PdfTextElement(text); txtElement.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 17); txtElement.Brush = new PdfSolidBrush(white); txtElement.StringFormat = new PdfStringFormat(); txtElement.StringFormat.WordWrap = PdfWordWrapType.Word; txtElement.StringFormat.LineLimit = true; PdfLayoutResult result = txtElement.Draw(page, new RectangleF(headerBulletsXposition + 25, yPosition, 450, 130)); return(result); }
private PdfLayoutResult DrawPDFTable(string title, float y, PdfPageBase page, string dataName) { //Draw Title PdfBrush brush = PdfBrushes.Black; PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center); string title1 = title; page.Canvas.DrawString(title1, font, brush, page.Canvas.ClientSize.Width / 2, y, format); y = y + font.MeasureString(title1, format).Height; y = y + 10; //Create PDF table and define table style PdfTable table = new PdfTable(); table.Style.CellPadding = 3; table.Style.BorderPen = new PdfPen(brush, 0.75f); table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.SkyBlue; table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f)); table.Style.DefaultStyle.StringFormat = format; table.Style.AlternateStyle = new PdfCellStyle(); table.Style.AlternateStyle.BackgroundBrush = PdfBrushes.LightYellow; table.Style.AlternateStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f)); table.Style.AlternateStyle.StringFormat = format; table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions; table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue; table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 14f, FontStyle.Bold)); table.Style.HeaderStyle.StringFormat = format; table.Style.ShowHeader = true; //Fill data in table table.DataSource = GetData(dataName); //Draw the table on Pdf page PdfLayoutResult result = table.Draw(page, new PointF(0, y)); return(result); }
private static void DrawTable(DataTable dt) { PdfTable table = new PdfTable { DataSource = dt }; PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat { Break = PdfLayoutBreakType.FitPage, Layout = PdfLayoutType.Paginate }; table.Style.CellPadding = 2; table.Style.BorderPen = new PdfPen(Color.Transparent); table.Style.DefaultStyle.Font = pageFont; table.Style.AlternateStyle.Font = pageFont; table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions; table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.Transparent; table.Style.HeaderStyle.Font = pageFontBold; table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center); table.Style.ShowHeader = true; table.BeginRowLayout += new BeginRowLayoutEventHandler(table_BeginRowLayout); //ensures cells are transparent as well //Draw table try { currentTableLayout = table.Draw(currentPage, new PointF(GetLeftPage(), currentY), tableLayout); currentY += currentTableLayout.Bounds.Height; } catch (PdfTableException) { currentPage = document.Pages.Add(); DrawTable(dt); } }