private void button21_Click(object sender, System.EventArgs e) { C1.C1Pdf.C1PdfDocument pdf = new C1.C1Pdf.C1PdfDocument(); // set up to draw Font font = new Font("Tahoma", 12); RectangleF rc = pdf.PageRectangle; rc.Inflate(-72, -72); // create document with 5 numbered pages for (int i = 0; i < 5; i++) { if (i > 0) { pdf.NewPage(); } pdf.DrawString("Page " + i.ToString(), font, Brushes.Black, rc); pdf.DrawRectangle(Pens.LightGray, rc); } // move the last page to the front of the document PdfPage last = pdf.Pages[pdf.Pages.Count - 1]; pdf.Pages.Remove(last); pdf.Pages.Insert(0, last); // save the document to a file string fileName = tempdir + "pages.pdf"; pdf.Save(fileName); System.Diagnostics.Process.Start(fileName); }
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 button7_Click(object sender, System.EventArgs e) { // initialize C1.C1Pdf.C1PdfDocument pdf = new C1.C1Pdf.C1PdfDocument(); Font font = new Font("Arial", 12); // create a 10 page document, make page 5 landscape for (int i = 0; i < 10; i++) { if (i > 0) { pdf.NewPage(); } pdf.Landscape = (i == 4); RectangleF rc = pdf.PageRectangle; rc.Inflate(-72, -72); pdf.DrawString("Hello", font, Brushes.Black, rc); pdf.DrawRectangle(Pens.Black, rc); } // save and show string fileName = tempdir + "landscape.pdf"; pdf.Save(fileName); System.Diagnostics.Process.Start(fileName); }
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 DrawPageImage(C1.C1Pdf.C1PdfDocument pdf, int index) { // get image bounds RectangleF rcBounds = pdf.PageRectangle; rcBounds.Inflate(-72, -72); // calculate zoom factor if (index == 0) { // get size of largest image SizeF szMax = Size.Empty; foreach (Image page in _images) { szMax.Height = Math.Max(szMax.Height, page.Height); szMax.Width = Math.Max(szMax.Width, page.Width); } // get size of page bounds SizeF szPage = rcBounds.Size; // calculate zoom so largest image doesn't overflow the page _zoom = 1; float zh = szPage.Width / szMax.Width; float zv = szPage.Height / szMax.Height; if (zh < 1 || zv < 1) { _zoom = Math.Min(zh, zv); } } // draw grid image var rc = rcBounds; Image img = _images[index] as Image; rc.Width = (int)(img.Width * _zoom); rc.Height = (int)(img.Height * _zoom); if (index > 0) { pdf.NewPage(); } pdf.DrawImage(img, 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 Create() { _c1pdf = new C1PdfDocument(); //create StringFormat for right-aligned fields _sfRight = new StringFormat(); _sfRight.Alignment = StringAlignment.Far; _sfRightCenter = new StringFormat(); _sfRightCenter.Alignment = StringAlignment.Far; _sfRightCenter.LineAlignment = StringAlignment.Center; //initialize pdf generator _c1pdf.Clear(); //get page rectangle, discount margins RectangleF rcPage = _c1pdf.PageRectangle; rcPage.Inflate(-72, -92); //loop through selected categories int page = 0; DataTable dt = GetCategories(); foreach (DataRow dr in dt.Rows) { //add page break, update page counter if (page > 0) { _c1pdf.NewPage(); } page++; //get current category name string catName = (string)dr["CategoryName"]; //add title to page _c1pdf.DrawString(catName, _fontTitle, Brushes.Blue, rcPage); //add outline entry _c1pdf.AddBookmark(catName, 0, 0); //build row template RectangleF[] rcRows = new RectangleF[6]; for (int i = 0; i < rcRows.Length; i++) { rcRows[i] = RectangleF.Empty; rcRows[i].Location = new PointF(rcPage.X, rcPage.Y + _fontHeader.SizeInPoints + 10); rcRows[i].Size = new SizeF(0, _fontBody.SizeInPoints + 3); } rcRows[0].Width = 110; // Product Name rcRows[1].Width = 60; // Unit Price rcRows[2].Width = 80; // Qty/Unit rcRows[3].Width = 60; // Stock Units rcRows[4].Width = 60; // Stock Value rcRows[5].Width = 60; // Reorder for (int i = 1; i < rcRows.Length; i++) { rcRows[i].X = rcRows[i - 1].X + rcRows[i - 1].Width + 8; } //add column headers _c1pdf.FillRectangle(Brushes.DarkGray, RectangleF.Union(rcRows[0], rcRows[5])); _c1pdf.DrawString("Product Name", _fontHeader, Brushes.White, rcRows[0]); _c1pdf.DrawString("Unit Price", _fontHeader, Brushes.White, rcRows[1], _sfRight); _c1pdf.DrawString("Qty/Unit", _fontHeader, Brushes.White, rcRows[2]); _c1pdf.DrawString("Stock Units", _fontHeader, Brushes.White, rcRows[3], _sfRight); _c1pdf.DrawString("Stock Value", _fontHeader, Brushes.White, rcRows[4], _sfRight); _c1pdf.DrawString("Reorder", _fontHeader, Brushes.White, rcRows[5]); //loop through products in this category DataRow[] products = dr.GetChildRows("Categories_Products"); foreach (DataRow product in products) { //move on to next row for (int i = 0; i < rcRows.Length; i++) { rcRows[i].Y += rcRows[i].Height; } //add row with some data try { _c1pdf.DrawString(product["ProductName"].ToString(), _fontBody, Brushes.Black, rcRows[0]); _c1pdf.DrawString(string.Format("{0:c}", product["UnitPrice"]), _fontBody, Brushes.Black, rcRows[1], _sfRight); _c1pdf.DrawString(string.Format("{0}", product["QuantityPerUnit"]), _fontBody, Brushes.Black, rcRows[2]); _c1pdf.DrawString(string.Format("{0}", product["UnitsInStock"]), _fontBody, Brushes.Black, rcRows[3], _sfRight); _c1pdf.DrawString(string.Format("{0:c}", product["ValueInStock"]), _fontBody, Brushes.Black, rcRows[4], _sfRight); if ((bool)product["OrderNow"]) { _c1pdf.DrawString("<<<", _fontBody, Brushes.Red, rcRows[5]); } } catch { // Debug.Assert(false); } } if (products.Length == 0) { rcRows[0].Y += rcRows[0].Height; _c1pdf.DrawString("No products in this category.", _fontBody, Brushes.Black, RectangleF.Union(rcRows[0], rcRows[5])); } } //add page headers AddPageHeaders(rcPage); }
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 RectangleF RenderTableRow(Font font, Font hdrFont, RectangleF rcPage, RectangleF rc, DataRow dr) { _rowCount += 1; RectangleF rcCell = rc; rcCell.Height = 100; // break page if we have to if (rcCell.Bottom > rcPage.Bottom) { _c1pdf.NewPage(); rc = RenderTableHeader(hdrFont, rcPage, new string[] { "TextBoxes", "RadioButtons", "CheckBoxes", "ComboBoxes", "ListBoxes" }); rcCell.Y = rc.Y; } // render data cells Pen pen = new Pen(Brushes.Gray, 0.1f); _c1pdf.DrawRectangle(pen, rcCell); rcCell.Inflate(-4, 0); // Render name float nameWidth = (float)(rc.Width * .5); float colWidth = rc.Width / 2; float x1 = rc.Location.X + 2; float x2 = rc.Location.X + rc.Width / 2; float y1 = rc.Location.Y + 2; float y2 = rc.Location.Y + 25; // Declare possible locations for row controls PointF r1c1 = new PointF(rc.Location.X + 2, rc.Location.Y + 2); PointF r2c1 = new PointF(rc.Location.X + 2, rc.Location.Y + 30); PointF r1c2 = new PointF(rc.Location.X + rc.Width / 2, rc.Location.Y + 2); PointF r2c2 = new PointF(rc.Location.X + rc.Width / 2, rc.Location.Y + 32); PointF r3c2 = new PointF(rc.Location.X + rc.Width / 2, rc.Location.Y + 62); // Render String string employeeName = dr["FirstName"].ToString() + " " + dr["LastName"].ToString(); _c1pdf.DrawString(employeeName, new Font(font.FontFamily, 20, FontStyle.Bold), Brushes.Black, new RectangleF(r1c1, new SizeF(nameWidth, 25))); // Render ComboBox _c1pdf.DrawString("Reports to:", font, Brushes.Black, new PointF(x1, y1 + 30)); int reportsTo = string.IsNullOrEmpty(dr["ReportsTo"].ToString()) ? 0 : (int)dr["ReportsTo"]; RenderComboBox(new string[] { "Nancy Davolio", "Andrew Fuller", "Janet Leverling", "Margaret Peacock", "Steven Buchanan", "Michael Suyama", "Robert King" }, reportsTo, font, new RectangleF(new PointF(x1 + 55, y1 + 29), new SizeF(colWidth - 70, 15))); // Render Radiobuttons _c1pdf.DrawString("Product Division:", font, Brushes.Black, new PointF(x1, y1 + 50)); RenderRadioButton(true, "GroupDivision" + _rowCount.ToString(), "One", font, new RectangleF(new PointF(x1 + 80, y1 + 50), new SizeF(colWidth - 85, 15))); RenderRadioButton(true, "GroupDivision" + _rowCount.ToString(), "Two", font, new RectangleF(new PointF(x1 + 120, y1 + 50), new SizeF(colWidth - 125, 15))); RenderRadioButton(true, "GroupDivision" + _rowCount.ToString(), "Three", font, new RectangleF(new PointF(x1 + 160, y1 + 50), new SizeF(colWidth - 165, 15))); // Render Checkbox RenderCheckBox(true, " Receives Email Notifications", font, new RectangleF(new PointF(x2 + 10, y1 + 5), new SizeF(colWidth - 5, 15))); _c1pdf.DrawString("Email Address:", font, Brushes.Black, new PointF(x2 + 10, y1 + 30)); // Render Textbox RenderTextBox(dr["Lastname"].ToString().ToLower() + "@nwind.com", font, new RectangleF(new PointF(x2 + 80, y1 + 29), new SizeF(colWidth - 95, 15)), Color.FromKnownColor(KnownColor.Info), "Enter Email Address", false); // Render Listbox _c1pdf.DrawString("Title:", font, Brushes.Black, new PointF(x2 + 10, y1 + 60)); RenderListBox(new string[] { "Sales Representative", "Sales Manager", "Vice President, Sales", "Inside Sales Coordinator" }, dr["Title"].ToString(), font, new RectangleF(new PointF(x2 + 50, y1 + 55), new SizeF(colWidth - 65, 30))); rcCell.Inflate(4, 0); pen.Dispose(); // update rectangle and return it rc.Offset(0, rcCell.Height); return(rc); }
private void Create() { _c1pdf = new C1PdfDocument(); //create StringFormat for right-aligned fields _sfRight = new StringFormat(); _sfRight.Alignment = StringAlignment.Far; _sfRightCenter = new StringFormat(); _sfRightCenter.Alignment = StringAlignment.Far; _sfRightCenter.LineAlignment = StringAlignment.Center; //initialize pdf generator _c1pdf.Clear(); //get page rectangle, discount margins RectangleF rcPage = _c1pdf.PageRectangle; rcPage.Inflate(-72, -92); //loop through selected categories int page = 0; DataTable dt = GetCategories(); foreach (DataRow dr in dt.Rows) { //add page break, update page counter if (page > 0) _c1pdf.NewPage(); page++; //get current category name string catName = (string)dr["CategoryName"]; //add title to page _c1pdf.DrawString(catName, _fontTitle, Brushes.Blue, rcPage); //add outline entry _c1pdf.AddBookmark(catName, 0, 0); //build row template RectangleF[] rcRows = new RectangleF[6]; for (int i = 0; i < rcRows.Length; i++) { rcRows[i] = RectangleF.Empty; rcRows[i].Location = new PointF(rcPage.X, rcPage.Y + _fontHeader.SizeInPoints + 10); rcRows[i].Size = new SizeF(0, _fontBody.SizeInPoints + 3); } rcRows[0].Width = 110; // Product Name rcRows[1].Width = 60; // Unit Price rcRows[2].Width = 80; // Qty/Unit rcRows[3].Width = 60; // Stock Units rcRows[4].Width = 60; // Stock Value rcRows[5].Width = 60; // Reorder for (int i = 1; i < rcRows.Length; i++) rcRows[i].X = rcRows[i - 1].X + rcRows[i - 1].Width + 8; //add column headers _c1pdf.FillRectangle(Brushes.DarkGray, RectangleF.Union(rcRows[0], rcRows[5])); _c1pdf.DrawString("Product Name", _fontHeader, Brushes.White, rcRows[0]); _c1pdf.DrawString("Unit Price", _fontHeader, Brushes.White, rcRows[1], _sfRight); _c1pdf.DrawString("Qty/Unit", _fontHeader, Brushes.White, rcRows[2]); _c1pdf.DrawString("Stock Units", _fontHeader, Brushes.White, rcRows[3], _sfRight); _c1pdf.DrawString("Stock Value", _fontHeader, Brushes.White, rcRows[4], _sfRight); _c1pdf.DrawString("Reorder", _fontHeader, Brushes.White, rcRows[5]); //loop through products in this category DataRow[] products = dr.GetChildRows("Categories_Products"); foreach (DataRow product in products) { //move on to next row for (int i = 0; i < rcRows.Length; i++) rcRows[i].Y += rcRows[i].Height; //add row with some data try { _c1pdf.DrawString(product["ProductName"].ToString(), _fontBody, Brushes.Black, rcRows[0]); _c1pdf.DrawString(string.Format("{0:c}", product["UnitPrice"]), _fontBody, Brushes.Black, rcRows[1], _sfRight); _c1pdf.DrawString(string.Format("{0}", product["QuantityPerUnit"]), _fontBody, Brushes.Black, rcRows[2]); _c1pdf.DrawString(string.Format("{0}", product["UnitsInStock"]), _fontBody, Brushes.Black, rcRows[3], _sfRight); _c1pdf.DrawString(string.Format("{0:c}", product["ValueInStock"]), _fontBody, Brushes.Black, rcRows[4], _sfRight); if ((bool)product["OrderNow"]) _c1pdf.DrawString("<<<", _fontBody, Brushes.Red, rcRows[5]); } catch { // Debug.Assert(false); } } if (products.Length == 0) { rcRows[0].Y += rcRows[0].Height; _c1pdf.DrawString("No products in this category.", _fontBody, Brushes.Black, RectangleF.Union(rcRows[0], rcRows[5])); } } //add page headers AddPageHeaders(rcPage); }