static Rect RenderTableHeader(C1PdfDocument pdf, Font font, Rect rc, string[] fields) { // calculate cell width (same for all columns) Rect rcCell = rc; rcCell.Width = rc.Width / fields.Length; rcCell.Height = 0; // calculate cell height (max of all columns) foreach (string field in fields) { var height = pdf.MeasureString(field, font, rcCell.Width).Height; rcCell.Height = Math.Max(rcCell.Height, height); } rcCell.Height += 6; // add 6 point margin // render header cells var fmt = new StringFormat(); fmt.LineAlignment = VerticalAlignment.Center; foreach (string field in fields) { pdf.FillRectangle(Colors.Black, rcCell); pdf.DrawString(field, font, Colors.White, rcCell, fmt); rcCell = PdfUtils.Offset(rcCell, rcCell.Width, 0); } // update rectangle and return it return(PdfUtils.Offset(rc, 0, rcCell.Height)); }
// measure a paragraph, skip a page if it won't fit, render it into a rectangle, // and update the rectangle for the next paragraph. // // optionally mark the paragraph as an outline entry and as a link target. // // this routine will not break a paragraph across pages. for that, see the Text Flow sample. // public static Rect RenderParagraph(C1PdfDocument pdf, string text, Font font, Rect rcPage, Rect rc, bool outline, bool linkTarget) { // if it won't fit this page, do a page break rc.Height = pdf.MeasureString(text, font, rc.Width).Height; if (rc.Bottom > rcPage.Bottom) { pdf.NewPage(); rc.Y = rcPage.Top; } // draw the string pdf.DrawString(text, font, Colors.Black, rc); // show bounds (mainly to check word wrapping) //pdf.DrawRectangle(Pens.Sienna, rc); // add headings to outline if (outline) { pdf.DrawLine(new Pen(Colors.Black), rc.X, rc.Y, rc.Right, rc.Y); pdf.AddBookmark(text, 0, rc.Y); } // add link target if (linkTarget) { pdf.AddTarget(text, rc); } // update rectangle for next time rc.Y += rc.Height; //rc.Offset(0, rc.Height); return(rc); }
static Rect RenderTableRow(C1PdfDocument pdf, Font font, Font hdrFont, Rect rcPage, Rect rc, string[] fields, DataRow dr) { // calculate cell width (same for all columns) Rect rcCell = rc; rcCell.Width = rc.Width / fields.Length; rcCell.Height = 0; // calculate cell height (max of all columns) rcCell = PdfUtils.Inflate(rcCell, -4, 0); foreach (string field in fields) { string text = dr[field].ToString(); var height = pdf.MeasureString(text, font, rcCell.Width).Height; rcCell.Height = Math.Max(rcCell.Height, height); } rcCell = PdfUtils.Inflate(rcCell, 4, 0); // add 4 point margin rcCell.Height += 2; // break page if we have to if (rcCell.Bottom > rcPage.Bottom) { pdf.NewPage(); rc = RenderTableHeader(pdf, hdrFont, rcPage, fields); rcCell.Y = rc.Y; } // center vertically just to show how StringFormat fmt = new StringFormat(); fmt.LineAlignment = VerticalAlignment.Center; // render data cells foreach (string field in fields) { // get content string text = dr[field].ToString(); // set horizontal alignment double d; fmt.Alignment = (double.TryParse(text, NumberStyles.Any, CultureInfo.CurrentCulture, out d)) ? HorizontalAlignment.Right : HorizontalAlignment.Left; // render cell pdf.DrawRectangle(Colors.LightGray, rcCell); rcCell = PdfUtils.Inflate(rcCell, -4, 0); pdf.DrawString(text, font, Colors.Black, rcCell, fmt); rcCell = PdfUtils.Inflate(rcCell, 4, 0); rcCell = PdfUtils.Offset(rcCell, rcCell.Width, 0); } // update rectangle and return it return(PdfUtils.Offset(rc, 0, rcCell.Height)); }
/// <summary> /// Shows how to position and align text /// </summary> static void CreateDocumentText(C1PdfDocument pdf) { // use landscape for more impact pdf.Landscape = true; // measure and show some text var text = Strings.DocumentBasicText; var font = new Font("Segoe UI Light", 12, PdfFontStyle.Italic); // create StringFormat used to set text alignment and line spacing var fmt = new StringFormat(); fmt.LineSpacing = -1.5; // 1.5 char height fmt.Alignment = HorizontalAlignment.Center; // measure it var sz = pdf.MeasureString(text, font, 72 * 3, fmt); var rc = new Rect(pdf.PageRectangle.Width / 2, 72, sz.Width, sz.Height); rc = PdfUtils.Offset(rc, 110, 0); // draw a rounded frame rc = PdfUtils.Inflate(rc, 0, 0); pdf.FillRectangle(Colors.Teal, rc, new Size(0, 0)); //pdf.DrawRectangle(new Pen(Colors.DarkGray, 5), rc, new Size(0, 0)); rc = PdfUtils.Inflate(rc, -10, -10); // draw the text //pdf.RotateAngle = 90; pdf.DrawString(text, font, Colors.White, rc, fmt); //pdf.RotateAngle = 0; // point in center for rotate the text rc = pdf.PageRectangle; var pt = new Point(rc.X + rc.Width / 2, rc.Y + rc.Height / 2); // rotate the string in small increments var step = 6; text = Strings.DocumentBasicText2; for (int i = 0; i <= 360; i += step) { pdf.RotateAngle = i; var s = string.Format(text, i); font = new Font("Courier New", 8 + i / 30.0, PdfFontStyle.Bold); byte b = (byte)(255 * (1 - i / 360.0)); pdf.DrawString(s, font, Color.FromArgb(0xff, b, b, b), pt); } }
//--------------------------------------------------------------------------------- #region ** text static void CreateDocumentText(C1PdfDocument pdf) { // use landscape for more impact pdf.Landscape = true; // measure and show some text var text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; var font = new Font("Times New Roman", 9, PdfFontStyle.Italic); // create StringFormat used to set text alignment and line spacing var fmt = new StringFormat(); fmt.LineSpacing = -1.5; // 1.5 char height fmt.Alignment = HorizontalAlignment.Center; // measure it var sz = pdf.MeasureString(text, font, 72 * 3, fmt); var rc = new Rect(pdf.PageRectangle.Width / 2, 72, sz.Width, sz.Height); rc = PdfUtils.Offset(rc, 72, 0); // draw a rounded frame rc = PdfUtils.Inflate(rc, 10, 10); pdf.FillRectangle(Colors.Black, rc, new Size(30, 30)); pdf.DrawRectangle(new Pen(Colors.Red, 4), rc, new Size(30, 30)); rc = PdfUtils.Inflate(rc, -10, -10); // draw the text pdf.DrawString(text, font, Colors.White, rc, fmt); // point in center for rotate the text rc = pdf.PageRectangle; var pt = new Point(rc.Location.X + rc.Width / 2, rc.Location.Y + rc.Height / 2); // rotate the string in small increments var step = 6; text = "PDF works in WPF!"; for (int i = 0; i <= 360; i += step) { pdf.RotateAngle = i; font = new Font("Courier New", 8 + i / 30.0, PdfFontStyle.Bold); byte b = (byte)(255 * (1 - i / 360.0)); pdf.DrawString(text, font, Color.FromArgb(0xff, b, b, b), pt); } }
static void CreateDocumentTOC(C1PdfDocument pdf) { // create pdf document pdf.DocumentInfo.Title = Strings.TableOfContentsDocumentTitle; // add title Font titleFont = new Font("Tahoma", 24, PdfFontStyle.Bold); Rect rcPage = PdfUtils.PageRectangle(pdf); Rect rc = PdfUtils.RenderParagraph(pdf, pdf.DocumentInfo.Title, titleFont, rcPage, rcPage, false); rc.Y += 12; // create nonsense document var bkmk = new List <string[]>(); Font headerFont = new Font("Arial", 14, PdfFontStyle.Bold); Font bodyFont = new Font("Times New Roman", 11); for (int i = 0; i < 30; i++) { // create ith header (as a link target and outline entry) string header = string.Format("{0}. {1}", i + 1, BuildRandomTitle()); rc = PdfUtils.RenderParagraph(pdf, header, headerFont, rcPage, rc, true, true); // save bookmark to build TOC later int pageNumber = pdf.CurrentPage + 1; bkmk.Add(new string[] { pageNumber.ToString(), header }); // create some text rc.X += 36; rc.Width -= 36; for (int j = 0; j < 3 + _rnd.Next(20); j++) { string text = BuildRandomParagraph(); rc = PdfUtils.RenderParagraph(pdf, text, bodyFont, rcPage, rc); rc.Y += 6; } rc.X -= 36; rc.Width += 36; rc.Y += 20; } // start Table of Contents pdf.NewPage(); // start TOC on a new page int tocPage = pdf.CurrentPage; // save page index (to move TOC later) rc = PdfUtils.RenderParagraph(pdf, Strings.TableOfContentsDocumentTitle, titleFont, rcPage, rcPage, true); rc.Y += 12; rc.X += 30; rc.Width -= 40; // render Table of Contents Pen dottedPen = new Pen(Colors.Gray, 1.5f); dottedPen.DashStyle = C1.Xaml.Pdf.DashStyle.Dot; StringFormat sfRight = new StringFormat(); sfRight.Alignment = HorizontalAlignment.Right; rc.Height = bodyFont.Size * 1.2; foreach (string[] entry in bkmk) { // get bookmark info string page = entry[0]; string header = entry[1]; // render header name and page number pdf.DrawString(header, bodyFont, Colors.Black, rc); pdf.DrawString(page, bodyFont, Colors.Black, rc, sfRight); #if true // connect the two with some dots (looks better than a dotted line) string dots = ". "; var wid = pdf.MeasureString(dots, bodyFont).Width; var x1 = rc.X + pdf.MeasureString(header, bodyFont).Width + 8; var x2 = rc.Right - pdf.MeasureString(page, bodyFont).Width - 8; var x = rc.X; for (rc.X = x1; rc.X < x2; rc.X += wid) { pdf.DrawString(dots, bodyFont, Colors.Gray, rc); } rc.X = x; #else // connect with a dotted line (another option) var x1 = rc.X + pdf.MeasureString(header, bodyFont).Width + 5; var x2 = rc.Right - pdf.MeasureString(page, bodyFont).Width - 5; var y = rc.Top + bodyFont.Size; pdf.DrawLine(dottedPen, x1, y, x2, y); #endif // add local hyperlink to entry pdf.AddLink(Strings.PoundSign + header, rc); // move on to next entry rc = PdfUtils.Offset(rc, 0, rc.Height); if (rc.Bottom > rcPage.Bottom) { pdf.NewPage(); rc.Y = rcPage.Y; } } // move table of contents to start of document PdfPage[] arr = new PdfPage[pdf.Pages.Count - tocPage]; pdf.Pages.CopyTo(tocPage, arr, 0, arr.Length); pdf.Pages.RemoveRange(tocPage, arr.Length); pdf.Pages.InsertRange(0, arr); }
static Rect RenderTableRow(C1PdfDocument pdf, Font font, Font hdrFont, Rect rcPage, Rect rc, string[] fields, DataRow dr) { // calculate cell width (same for all columns) Rect rcCell = rc; rcCell.Width = rc.Width / fields.Length; rcCell.Height = 0; // calculate cell height (max of all columns) rcCell = PdfUtils.Inflate(rcCell, -4, 0); foreach (string field in fields) { string text = dr[field].ToString(); var height = pdf.MeasureString(text, font, rcCell.Width).Height; rcCell.Height = Math.Max(rcCell.Height, height); } rcCell = PdfUtils.Inflate(rcCell, 4, 0); // add 4 point margin rcCell.Height += 2; // break page if we have to if (rcCell.Bottom > rcPage.Bottom) { pdf.NewPage(); rc = RenderTableHeader(pdf, hdrFont, rcPage, fields); rcCell.Y = rc.Y; } // center vertically just to show how StringFormat fmt = new StringFormat(); fmt.LineAlignment = VerticalAlignment.Center; // render data cells foreach (string field in fields) { // get content string text = dr[field].ToString(); // set horizontal alignment double d; fmt.Alignment = (double.TryParse(text, NumberStyles.Any, CultureInfo.CurrentCulture, out d)) ? HorizontalAlignment.Right : HorizontalAlignment.Left; // render cell pdf.DrawRectangle(Colors.LightGray, rcCell); rcCell = PdfUtils.Inflate(rcCell, -4, 0); pdf.DrawString(text, font, Colors.Black, rcCell, fmt); rcCell = PdfUtils.Inflate(rcCell, 4, 0); rcCell = PdfUtils.Offset(rcCell, rcCell.Width, 0); } // update rectangle and return it return PdfUtils.Offset(rc, 0, rcCell.Height); }
static Rect RenderTableHeader(C1PdfDocument pdf, Font font, Rect rc, string[] fields) { // calculate cell width (same for all columns) Rect rcCell = rc; rcCell.Width = rc.Width / fields.Length; rcCell.Height = 0; // calculate cell height (max of all columns) foreach (string field in fields) { var height = pdf.MeasureString(field, font, rcCell.Width).Height; rcCell.Height = Math.Max(rcCell.Height, height); } rcCell.Height += 6; // add 6 point margin // render header cells var fmt = new StringFormat(); fmt.LineAlignment = VerticalAlignment.Center; foreach (string field in fields) { pdf.FillRectangle(Colors.Black, rcCell); pdf.DrawString(field, font, Colors.White, rcCell, fmt); rcCell = PdfUtils.Offset(rcCell, rcCell.Width, 0); } // update rectangle and return it return PdfUtils.Offset(rc, 0, rcCell.Height); }
static void CreateDocumentTOC(C1PdfDocument pdf) { // create pdf document pdf.DocumentInfo.Title = "Document with Table of Contents"; // add title Font titleFont = new Font("Tahoma", 24, PdfFontStyle.Bold); Rect rcPage = PdfUtils.PageRectangle(pdf); Rect rc = PdfUtils.RenderParagraph(pdf, pdf.DocumentInfo.Title, titleFont, rcPage, rcPage, false); rc.Y += 12; // create nonsense document var bkmk = new List<string[]>(); Font headerFont = new Font("Arial", 14, PdfFontStyle.Bold); Font bodyFont = new Font("Times New Roman", 11); for (int i = 0; i < 30; i++) { // create ith header (as a link target and outline entry) string header = string.Format("{0}. {1}", i + 1, BuildRandomTitle()); rc = PdfUtils.RenderParagraph(pdf, header, headerFont, rcPage, rc, true, true); // save bookmark to build TOC later int pageNumber = pdf.CurrentPage + 1; bkmk.Add(new string[] { pageNumber.ToString(), header }); // create some text rc.X += 36; rc.Width -= 36; for (int j = 0; j < 3 + _rnd.Next(20); j++) { string text = BuildRandomParagraph(); rc = PdfUtils.RenderParagraph(pdf, text, bodyFont, rcPage, rc); rc.Y += 6; } rc.X -= 36; rc.Width += 36; rc.Y += 20; } // start Table of Contents pdf.NewPage(); // start TOC on a new page int tocPage = pdf.CurrentPage; // save page index (to move TOC later) rc = PdfUtils.RenderParagraph(pdf, "Table of Contents", titleFont, rcPage, rcPage, true); rc.Y += 12; rc.X += 30; rc.Width -= 40; // render Table of Contents Pen dottedPen = new Pen(Colors.Gray, 1.5f); dottedPen.DashStyle = DashStyle.Dot; StringFormat sfRight = new StringFormat(); sfRight.Alignment = HorizontalAlignment.Right; rc.Height = bodyFont.Size * 1.2; foreach (string[] entry in bkmk) { // get bookmark info string page = entry[0]; string header = entry[1]; // render header name and page number pdf.DrawString(header, bodyFont, Colors.Black, rc); pdf.DrawString(page, bodyFont, Colors.Black, rc, sfRight); #if true // connect the two with some dots (looks better than a dotted line) string dots = ". "; var wid = pdf.MeasureString(dots, bodyFont).Width; var x1 = rc.X + pdf.MeasureString(header, bodyFont).Width + 8; var x2 = rc.Right - pdf.MeasureString(page, bodyFont).Width - 8; var x = rc.X; for (rc.X = x1; rc.X < x2; rc.X += wid) { pdf.DrawString(dots, bodyFont, Colors.Gray, rc); } rc.X = x; #else // connect with a dotted line (another option) var x1 = rc.X + pdf.MeasureString(header, bodyFont).Width + 5; var x2 = rc.Right - pdf.MeasureString(page, bodyFont).Width - 5; var y = rc.Top + bodyFont.Size; pdf.DrawLine(dottedPen, x1, y, x2, y); #endif // add local hyperlink to entry pdf.AddLink("#" + header, rc); // move on to next entry rc = PdfUtils.Offset(rc, 0, rc.Height); if (rc.Bottom > rcPage.Bottom) { pdf.NewPage(); rc.Y = rcPage.Y; } } // move table of contents to start of document PdfPage[] arr = new PdfPage[pdf.Pages.Count - tocPage]; pdf.Pages.CopyTo(tocPage, arr, 0, arr.Length); pdf.Pages.RemoveRange(tocPage, arr.Length); pdf.Pages.InsertRange(0, arr); }
static void CreateDocumentText(C1PdfDocument pdf) { // use landscape for more impact pdf.Landscape = true; // measure and show some text var text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; var font = new Font("Times New Roman", 9, PdfFontStyle.Italic); // create StringFormat used to set text alignment and line spacing var fmt = new StringFormat(); fmt.LineSpacing = -1.5; // 1.5 char height fmt.Alignment = HorizontalAlignment.Center; // measure it var sz = pdf.MeasureString(text, font, 72 * 3, fmt); var rc = new Rect(pdf.PageRectangle.Width / 2, 72, sz.Width, sz.Height); rc = PdfUtils.Offset(rc, 72, 0); // draw a rounded frame rc = PdfUtils.Inflate(rc, 10, 10); pdf.FillRectangle(Colors.Black, rc, new Size(30, 30)); pdf.DrawRectangle(new Pen(Colors.Red, 4), rc, new Size(30, 30)); rc = PdfUtils.Inflate(rc, -10, -10); // draw the text pdf.DrawString(text, font, Colors.White, rc, fmt); // now draw some text rotating about the center of the page rc = pdf.PageRectangle; rc = PdfUtils.Offset(rc, rc.Width / 2, rc.Height / 2); // build StringFormat used to rotate the text fmt = new StringFormat(); // rotate the string in small increments var step = 6; text = "PDF works in WPF!"; for (int i = 0; i <= 360; i += step) { fmt.Angle = i; font = new Font("Courier New", 8 + i / 30.0, PdfFontStyle.Bold); byte b = (byte)(255 * (1 - i / 360.0)); pdf.DrawString(text, font, Color.FromArgb(0xff, b, b, b), rc, fmt); } }