private void button20_Click(object sender, System.EventArgs e) { C1.C1Pdf.C1PdfDocument pdf = new C1.C1Pdf.C1PdfDocument(); // set up to draw string text = "We all came down to Montreux, by the Lake Geneva shoreline."; Font font = new Font("Tahoma", 12); RectangleF rc = new RectangleF(100, 100, 0, 0); // measure text on a single line rc.Size = pdf.MeasureString(text, font); pdf.DrawString(text, font, Brushes.Black, rc); pdf.DrawRectangle(Pens.LightGray, rc); // update rectangle for next sample rc.Y = rc.Bottom + 12; rc.Width = 120; // measure text that wraps rc.Size = pdf.MeasureString(text, font, rc.Width); pdf.DrawString(text, font, Brushes.Black, rc); pdf.DrawRectangle(Pens.LightGray, rc); // save the document to a file string fileName = tempdir + "measure.pdf"; pdf.Save(fileName); System.Diagnostics.Process.Start(fileName); }
private RectangleF RenderTableHeader(Font font, RectangleF rc, string[] fields) { // calculate cell width (same for all columns) RectangleF rcCell = rc; rcCell.Width = rc.Width / fields.Length; rcCell.Height = 0; // calculate cell height (max of all columns) foreach (string field in fields) { float height = _c1pdf.MeasureString(field, font, rcCell.Width).Height; rcCell.Height = Math.Max(rcCell.Height, height); } // render header cells foreach (string field in fields) { _c1pdf.FillRectangle(Brushes.Black, rcCell); _c1pdf.DrawString(field, font, Brushes.White, rcCell); rcCell.Offset(rcCell.Width, 0); } // update rectangle and return it rc.Offset(0, rcCell.Height); return(rc); }
internal RectangleF RenderParagraph(string text, Font font, RectangleF rcPage, RectangleF rc, bool outline, bool linkTarget) { // if it won't fit this page, do a page break rc.Height = _c1pdf.MeasureString(text, font, rc.Width).Height; if (rc.Bottom > rcPage.Bottom) { _c1pdf.NewPage(); rc.Y = rcPage.Top; } // draw the string _c1pdf.DrawString(text, font, Brushes.Black, rc); // add headings to outline if (outline) { _c1pdf.DrawLine(Pens.Black, rc.X, rc.Y, rc.Right, rc.Y); _c1pdf.AddBookmark(text, 0, rc.Y); } // add link target if (linkTarget) { _c1pdf.AddTarget(text, rc); } // update rectangle for next time rc.Offset(0, rc.Height); return(rc); }
private void CreatePDF() { _c1pdf = new C1PdfDocument(); // initialize pdf generator _c1pdf.Clear(); _c1pdf.DocumentInfo.Title = "Pdf Document With Table of Contents"; TEMP_DIR = Server.MapPath("../Temp"); if (Directory.Exists(TEMP_DIR)) { } else { Directory.CreateDirectory(TEMP_DIR); } // add title Font titleFont = new Font("Tahoma", 24, FontStyle.Bold); RectangleF rcPage = GetPageRect(); RectangleF rc = RenderParagraph(_c1pdf.DocumentInfo.Title, titleFont, rcPage, rcPage, false); rc.Y += 12; // create nonsense document ArrayList bkmk = new ArrayList(); Font headerFont = new Font("Tahoma", 16, FontStyle.Bold); Font bodyFont = new Font("Tahoma", 10); 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 = RenderParagraph(header, headerFont, rcPage, rc, true, true); // save bookmark to build TOC later int pageNumber = _c1pdf.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(10); j++) { string text = BuildRandomParagraph(); rc = RenderParagraph(text, bodyFont, rcPage, rc); rc.Y += 6; } rc.X -= 36; rc.Width += 36; rc.Y += 20; } // number pages (before adding TOC) AddFooters(); // start Table of Contents _c1pdf.NewPage(); // start TOC on a new page int tocPage = _c1pdf.CurrentPage; // save page index (to move TOC later) rc = RenderParagraph("Table of Contents", titleFont, rcPage, rcPage, true); rc.Y += 12; rc.X += 30; rc.Width -= 40; // render Table of Contents Pen dottedPen = new Pen(Brushes.Gray, 1.5f); dottedPen.DashStyle = DashStyle.Dot; StringFormat sfRight = new StringFormat(); sfRight.Alignment = StringAlignment.Far; rc.Height = bodyFont.Height; foreach (string[] entry in bkmk) { // get bookmark info string page = entry[0]; string header = entry[1]; // render header name and page number _c1pdf.DrawString(header, bodyFont, Brushes.Black, rc); _c1pdf.DrawString(page, bodyFont, Brushes.Black, rc, sfRight); // connect the two with some dots (looks better than a dotted line) string dots = ". "; float wid = _c1pdf.MeasureString(dots, bodyFont).Width; float x1 = rc.X + _c1pdf.MeasureString(header, bodyFont).Width + 8; float x2 = rc.Right - _c1pdf.MeasureString(page, bodyFont).Width - 8; float x = rc.X; for (rc.X = x1; rc.X < x2; rc.X += wid) { _c1pdf.DrawString(dots, bodyFont, Brushes.Gray, rc); } rc.X = x; // add local hyperlink to entry _c1pdf.AddLink("#" + header, rc); // move on to next entry rc.Offset(0, rc.Height); if (rc.Bottom > rcPage.Bottom) { _c1pdf.NewPage(); rc.Y = rcPage.Y; } } // move table of contents to start of document PdfPage[] arr = new PdfPage[_c1pdf.Pages.Count - tocPage]; _c1pdf.Pages.CopyTo(tocPage, arr, 0, arr.Length); _c1pdf.Pages.RemoveRange(tocPage, arr.Length); _c1pdf.Pages.InsertRange(0, arr); // save pdf file string uid = System.Guid.NewGuid().ToString(); filename = Server.MapPath("~") + "\\Temp\\testpdf" + uid + ".pdf"; _c1pdf.Save(filename); // display it //webBrowser1.Navigate(filename); }
private void CreatePDF() { _c1pdf = new C1PdfDocument(); // initialize pdf generator _c1pdf.Clear(); _c1pdf.DocumentInfo.Title = "Pdf Document With Table of Contents"; TEMP_DIR = Server.MapPath("../Temp"); if (Directory.Exists(TEMP_DIR)) { } else { Directory.CreateDirectory(TEMP_DIR); } // add title Font titleFont = new Font("Tahoma", 24, FontStyle.Bold); RectangleF rcPage = GetPageRect(); RectangleF rc = RenderParagraph(_c1pdf.DocumentInfo.Title, titleFont, rcPage, rcPage, false); rc.Y += 12; // create nonsense document ArrayList bkmk = new ArrayList(); Font headerFont = new Font("Tahoma", 16, FontStyle.Bold); Font bodyFont = new Font("Tahoma", 10); 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 = RenderParagraph(header, headerFont, rcPage, rc, true, true); // save bookmark to build TOC later int pageNumber = _c1pdf.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(10); j++) { string text = BuildRandomParagraph(); rc = RenderParagraph(text, bodyFont, rcPage, rc); rc.Y += 6; } rc.X -= 36; rc.Width += 36; rc.Y += 20; } // number pages (before adding TOC) AddFooters(); // start Table of Contents _c1pdf.NewPage(); // start TOC on a new page int tocPage = _c1pdf.CurrentPage; // save page index (to move TOC later) rc = RenderParagraph("Table of Contents", titleFont, rcPage, rcPage, true); rc.Y += 12; rc.X += 30; rc.Width -= 40; // render Table of Contents Pen dottedPen = new Pen(Brushes.Gray, 1.5f); dottedPen.DashStyle = DashStyle.Dot; StringFormat sfRight = new StringFormat(); sfRight.Alignment = StringAlignment.Far; rc.Height = bodyFont.Height; foreach (string[] entry in bkmk) { // get bookmark info string page = entry[0]; string header = entry[1]; // render header name and page number _c1pdf.DrawString(header, bodyFont, Brushes.Black, rc); _c1pdf.DrawString(page, bodyFont, Brushes.Black, rc, sfRight); // connect the two with some dots (looks better than a dotted line) string dots = ". "; float wid = _c1pdf.MeasureString(dots, bodyFont).Width; float x1 = rc.X + _c1pdf.MeasureString(header, bodyFont).Width + 8; float x2 = rc.Right - _c1pdf.MeasureString(page, bodyFont).Width - 8; float x = rc.X; for (rc.X = x1; rc.X < x2; rc.X += wid) _c1pdf.DrawString(dots, bodyFont, Brushes.Gray, rc); rc.X = x; // add local hyperlink to entry _c1pdf.AddLink("#" + header, rc); // move on to next entry rc.Offset(0, rc.Height); if (rc.Bottom > rcPage.Bottom) { _c1pdf.NewPage(); rc.Y = rcPage.Y; } } // move table of contents to start of document PdfPage[] arr = new PdfPage[_c1pdf.Pages.Count - tocPage]; _c1pdf.Pages.CopyTo(tocPage, arr, 0, arr.Length); _c1pdf.Pages.RemoveRange(tocPage, arr.Length); _c1pdf.Pages.InsertRange(0, arr); // save pdf file string uid = System.Guid.NewGuid().ToString(); filename = Server.MapPath("~") + "\\Temp\\testpdf" + uid + ".pdf"; _c1pdf.Save(filename); // display it //webBrowser1.Navigate(filename); }