private void button8_Click(object sender, System.EventArgs e) { // initialize C1.C1Pdf.C1PdfDocument pdf = new C1.C1Pdf.C1PdfDocument(); // create a regular (external) hyperlink RectangleF rc = new RectangleF(50, 50, 200, 15); Font font = new Font("Arial", 10, FontStyle.Underline); pdf.AddLink("http://www.componentone.com", rc); pdf.DrawString("Visit ComponentOne", font, Brushes.Blue, rc); // create a link target pdf.AddTarget("#myLink", rc); // add a few pages for (int i = 0; i < 5; i++) { pdf.NewPage(); } // add a link to the target pdf.AddLink("#myLink", rc); pdf.FillRectangle(Brushes.BlanchedAlmond, rc); pdf.DrawString("Local link: back to page 1...", font, Brushes.Blue, rc); // save and show string fileName = tempdir + "links.pdf"; pdf.Save(fileName); System.Diagnostics.Process.Start(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); }
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); }