private void makeDocument() { // start a new document C1PdfDocument pdf = new C1PdfDocument(); string name = Thread.CurrentThread.Name; // with two fonts Font font1 = new Font("Tahoma", 10); Font font2 = new Font("Tahoma", 10, FontStyle.Italic); // create document pdf.Clear(); RectangleF rc = pdf.PageRectangle; rc.Inflate(-72, -72); while (rc.Y < pdf.PageRectangle.Bottom - 72) { pdf.DrawString("Hello buddy. This is thread " + name, font1, Brushes.Red, rc); rc.Y += 12; pdf.DrawString("Hello again. Same thread " + name, font2, Brushes.Blue, rc); rc.Y += 12; pdf.DrawLine(Pens.ForestGreen, rc.X, rc.Y, rc.Right, rc.Y); // let other threads work Thread.Sleep(0); } // save document pdf.Save(string.Format(@"c:\temp\test{0}.pdf", name)); }
/// <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); } }
async Task CreateDocumentImages(C1PdfDocument pdf) { // calculate page rect (discounting margins) var rcPage = PdfUtils.PageRectangle(pdf); InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream(); // title Font font = new Font("Segoe UI Light", 16, PdfFontStyle.Italic); pdf.DrawString(Strings.ImagesDocumentTitle, font, Colors.Black, new Rect(72, 72, 400, 100)); // load image into writeable bitmap WriteableBitmap wb = new WriteableBitmap(880, 660); var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///PdfSamplesLib/Assets/pic.jpg")); wb.SetSource(await file.OpenReadAsync()); // simple draw image var rcPic = new Rect(72, 100, wb.PixelWidth / 5.0, wb.PixelHeight / 5.0); pdf.DrawImage(wb, rcPic); // draw on page preserving aspect ratio var delta = 100.0; rcPic = new Rect(new Point(delta, delta), new Point(rcPage.Width - delta, rcPage.Height - delta)); pdf.DrawImage(wb, rcPic, ContentAlignment.MiddleCenter, Stretch.Uniform); // translucent rectangle var clr = Color.FromArgb(50, 0, 255, 0); pdf.FillRectangle(clr, new Rect(200, 200, 300, 400)); }
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)); }
private void Button_Click(object sender, RoutedEventArgs e) { // get stream to save to var dlg = new SaveFileDialog(); dlg.DefaultExt = ".pdf"; var dr = dlg.ShowDialog(); if (!dr.HasValue || !dr.Value) { return; } // get sender button var btn = sender as Button; // create document var pdf = new C1PdfDocument(PaperKind.Letter); pdf.Clear(); // set document info var di = pdf.DocumentInfo; di.Author = "ComponentOne"; di.Subject = "C1.WPF.Pdf demo."; di.Title = "Experimental VisualTree Exporter for PDF"; // walk visual tree CreateDocumentVisualTree(pdf, content); // render footers // this reopens each page and adds content to them (now we know the page count). var font = new Font("Arial", 8, PdfFontStyle.Bold); var fmt = new StringFormat(); fmt.Alignment = HorizontalAlignment.Right; fmt.LineAlignment = VerticalAlignment.Bottom; for (int page = 0; page < pdf.Pages.Count; page++) { pdf.CurrentPage = page; var text = string.Format("C1.WPF.Pdf: {0}, page {1} of {2}", di.Title, page + 1, pdf.Pages.Count); pdf.DrawString( text, font, Colors.DarkGray, PdfUtils.Inflate(pdf.PageRectangle, -72, -36), fmt); } // save document using (var stream = dlg.OpenFile()) { pdf.Save(stream); } MessageBox.Show("Pdf Document saved to " + dlg.SafeFileName); }
// add radio button box field for fields of the PDF document // with common parameters and default names. // public static PdfRadioButton RenderRadioButton(C1PdfDocument pdf, bool value, string group, string text, Font font, Rect rc, Color back, string toolTip) { // create string name = string.IsNullOrEmpty(group) ? "ACFRGR" : group; PdfRadioButton radioButton = new PdfRadioButton(); // parameters radioButton.Name = name; radioButton.DefaultValue = value; radioButton.Value = value; radioButton.ToolTip = string.IsNullOrEmpty(toolTip) ? string.Format("{0} ({1})", text, name) : toolTip; if (back != Colors.Transparent) { radioButton.BackColor = back; } // add var radioSize = font.Size; var radioTop = rc.Top + (rc.Height - radioSize) / 2; pdf.AddField(radioButton, new Rect(rc.Left, radioTop, radioSize, radioSize)); _radioButtonCount++; // text for radio button field var x = rc.Left + radioSize + 1.0f; var y = rc.Top + (rc.Height - radioSize - 1.0f) / 2; pdf.DrawString(text, new Font(font.Name, radioSize, font.Style), Colors.Black, new Point(x, y)); // done return(radioButton); }
// 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); }
public static void SetDocumentInfo(this C1PdfDocument pdf, string title) { // set document info var di = pdf.DocumentInfo; di.Author = Strings.DocumentAuthor; di.Subject = Strings.DocumentSubject; di.Title = title; // render footers // this reopens each page and adds content to them (now we know the page count). var font = new Font("Arial", 8, PdfFontStyle.Bold); var fmt = new StringFormat(); fmt.Alignment = HorizontalAlignment.Right; fmt.LineAlignment = VerticalAlignment.Bottom; for (int page = 0; page < pdf.Pages.Count; page++) { pdf.CurrentPage = page; var text = string.Format(Strings.Documentfooter, di.Title, page + 1, pdf.Pages.Count); pdf.DrawString( text, font, Colors.DarkGray, PdfUtils.Inflate(pdf.PageRectangle, -72, -36), fmt); } }
private void AddTextToPage(string text, string key, C1PdfDocument doc) { int x = 0; int y = 0; int fontsize = 0; string description = ""; GetTextAttributes(key, out x, out y, out fontsize, out description); text = description + text; // Add the text. PointF startPoint = new PointF(x, y); Font fnt; string fontname = "Ariel"; fontname = ConfigurationManager.AppSettings["fontname"]; if (fontname == null) { fontname = "Ariel"; } fnt = new Font(fontname, fontsize); using (fnt) { doc.DrawString(text, fnt, Brushes.Blue, startPoint); } }
//--------------------------------------------------------------------------------- #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); } }
//--------------------------------------------------------------------------------- #region ** graphics static void CreateDocumentGraphics(C1PdfDocument pdf) { // set up to draw Rect rc = new Rect(0, 0, 300, 200); string text = "Hello world of .NET Graphics and PDF.\r\nNice to meet you."; Font font = new Font("Times New Roman", 12, PdfFontStyle.Italic | PdfFontStyle.Underline); // draw to pdf document int penWidth = 0; byte penRGB = 0; pdf.FillPie(Colors.Red, rc, 0, 20f); pdf.FillPie(Colors.Green, rc, 20f, 30f); pdf.FillPie(Colors.Blue, rc, 60f, 12f); pdf.FillPie(Colors.Orange, rc, -80f, -20f); for (float startAngle = 0; startAngle < 360; startAngle += 40) { Color penColor = Color.FromArgb(0xff, penRGB, penRGB, penRGB); Pen pen = new Pen(penColor, penWidth++); penRGB = (byte)(penRGB + 20); pdf.DrawArc(pen, rc, startAngle, 40f); } pdf.DrawRectangle(Colors.Red, rc); pdf.DrawString(text, font, Colors.Black, rc); // show a Bezier curve var pts = new Point[] { new Point(400, 100), new Point(420, 30), new Point(500, 140), new Point(530, 20), }; // draw Bezier pdf.DrawBezier(new Pen(Colors.Blue, 4), pts[0], pts[1], pts[2], pts[3]); // show Bezier control points pdf.DrawLines(Colors.Gray, pts); foreach (Point pt in pts) { pdf.FillRectangle(Colors.Red, pt.X - 2, pt.Y - 2, 4, 4); } // title pdf.DrawString("Simple Bezier", font, Colors.Black, new Rect(500, 150, 100, 100)); }
//================================================================================ // add page footers to a document // // this method is called by all samples in this project. it scans the document // and adds a 'page n of m' footer to each page. the footers are rendered as // vertical text along the right edge of the document. // // adding content to an existing page is easy: just set the CurrentPage property // to point to an existing page and write into it as usual. // public static void AddFooters(C1PdfDocument pdf) { Font fontHorz = new Font("Tahoma", 7, PdfFontStyle.Bold); Font fontVert = new Font("Viner Hand ITC", 14, PdfFontStyle.Bold); StringFormat sfRight = new StringFormat(); sfRight.Alignment = HorizontalAlignment.Right; StringFormat sfVert = new StringFormat(); sfVert.FormatFlags |= StringFormatFlags.DirectionVertical; sfVert.Alignment = HorizontalAlignment.Center; for (int page = 0; page < pdf.Pages.Count; page++) { // select page we want (could change PageSize) pdf.CurrentPage = page; // build rectangles for rendering text var rcPage = GetPageRect(pdf); var rcFooter = rcPage; rcFooter.Y = rcFooter.Bottom + 6; rcFooter.Height = 12; var rcVert = rcPage; rcVert.X = rcPage.Right + 6; // add left-aligned footer string text = pdf.DocumentInfo.Title; pdf.DrawString(text, fontHorz, Colors.Gray, rcFooter); // add right-aligned footer text = string.Format("Page {0} of {1}", page + 1, pdf.Pages.Count); pdf.DrawString(text, fontHorz, Colors.Gray, rcFooter, sfRight); // add vertical text text = pdf.DocumentInfo.Title + " (document created using the C1Pdf component)"; pdf.DrawString(text, fontVert, Colors.LightGray, rcVert, sfVert); // draw lines on bottom and right of the page var pen = new Pen(Colors.Gray, 1.0); pdf.DrawLine(pen, rcPage.Left, rcPage.Bottom, rcPage.Right, rcPage.Bottom); pdf.DrawLine(pen, rcPage.Right, rcPage.Top, rcPage.Right, rcPage.Bottom); } }
void CreateDocumentVisualTree(C1PdfDocument pdf, FrameworkElement targetElement) { // set up to render var font = new Font("Courier", 14); var img = new WriteableBitmap(CreateBitmap(targetElement)); // go render bool firstPage = true; foreach (Stretch stretch in new Stretch[] { Stretch.Fill, Stretch.None, Stretch.Uniform, Stretch.UniformToFill }) { // add page break if (!firstPage) { pdf.NewPage(); } firstPage = false; // set up to render var alignment = ContentAlignment.TopLeft; var rc = PdfUtils.Inflate(pdf.PageRectangle, -72, -72); rc.Height /= 2; // render element as image pdf.DrawString("Element as Image, Stretch: " + stretch.ToString(), font, Colors.Black, rc); rc = PdfUtils.Inflate(rc, -20, -20); pdf.DrawImage(img, rc, alignment, stretch); pdf.DrawRectangle(Colors.Green, rc); rc = PdfUtils.Inflate(rc, +20, +20); pdf.DrawRectangle(Colors.Green, rc); // move to bottom of the page rc = PdfUtils.Offset(rc, 0, rc.Height + 20); // render element pdf.DrawString("Element as VisualTree, Stretch: " + stretch.ToString(), font, Colors.Black, rc); rc = PdfUtils.Inflate(rc, -20, -20); pdf.DrawElement(targetElement, rc, alignment, stretch); pdf.DrawRectangle(Colors.Green, rc); rc = PdfUtils.Inflate(rc, +20, +20); pdf.DrawRectangle(Colors.Green, 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)); }
static void CreateDocumentPaperSizes(C1PdfDocument pdf) { // add title Font titleFont = new Font("Tahoma", 24, PdfFontStyle.Bold); Rect rc = PdfUtils.PageRectangle(pdf); PdfUtils.RenderParagraph(pdf, pdf.DocumentInfo.Title, titleFont, rc, rc, false); // create constant font and StringFormat objects Font font = new Font("Tahoma", 18); StringFormat sf = new StringFormat(); sf.Alignment = HorizontalAlignment.Center; sf.LineAlignment = VerticalAlignment.Center; // create one page with each paper size bool firstPage = true; foreach (PaperKind pk in Enum.GetValues(typeof(PaperKind))) { // Silverlight doesn't have Enum.GetValues //PaperKind pk = fi; // skip custom size if (pk == PaperKind.Custom) { continue; } // add new page for every page after the first one if (!firstPage) { pdf.NewPage(); } firstPage = false; // set paper kind and orientation pdf.PaperKind = pk; pdf.Landscape = !pdf.Landscape; // draw some content on the page rc = PdfUtils.PageRectangle(pdf); rc = PdfUtils.Inflate(rc, -6, -6); string text = string.Format(Strings.StringFormatTwoArg, pdf.PaperKind, pdf.Landscape); pdf.DrawString(text, font, Colors.Black, rc, sf); pdf.DrawRectangle(Colors.Black, rc); } //foreach (var fi in typeof(PaperKind).GetTypeInfo().GetFields(BindingFlags.Static | BindingFlags.Public)) //{ //} }
// add check box field for fields of the PDF document // with common parameters and default names. // static PdfCheckBox RenderCheckBox(C1PdfDocument pdf, bool value, string text, Font font, Rect rc, Color back, string toolTip) { // create string name = string.Format("ACFCB{0}", _checkBoxCount + 1); PdfCheckBox checkBox = new PdfCheckBox(); // default border checkBox.BorderWidth = FieldBorderWidth.Thin; checkBox.BorderStyle = FieldBorderStyle.Solid; checkBox.BorderColor = Colors.DarkGray; // parameters checkBox.Name = name; checkBox.DefaultValue = value; checkBox.Value = value; checkBox.ToolTip = string.IsNullOrEmpty(toolTip) ? string.Format("{0} ({1})", text, name) : toolTip; if (back != Colors.Transparent) { checkBox.BackColor = back; } // add var checkBoxSize = font.Size; var checkBoxTop = rc.Top + (rc.Height - checkBoxSize) / 2; pdf.AddField(checkBox, new Rect(rc.Left, checkBoxTop, checkBoxSize, checkBoxSize)); _checkBoxCount++; // text for check box field var x = rc.Left + checkBoxSize + 1.0f; var y = rc.Top + (rc.Height - checkBoxSize - 1.0f) / 2; pdf.DrawString(text, new Font(font.Name, checkBoxSize, font.Style), Colors.Black, new Point(x, y)); // done return(checkBox); }
private void c1Button1_Click(object sender, EventArgs e) { String statusOPD = "", vsDate = "", vn = "", an = "", anDate = "", hn = "", preno = "", anyr = "", vn1 = "", pathFolder = "", datetick = ""; C1PdfDocument pdf = new C1PdfDocument(); pdf.Clear(); //get page rectangle, discount margins RectangleF rcPage = pdf.PageRectangle; rcPage.Inflate(-72, -92); //loop through selected categories int page = 0; //add page break, update page counter if (page > 0) { pdf.NewPage(); } page++; pdf.DrawString("ใบงบหน้าสรุป", _fontTitle, Brushes.Blue, rcPage); datetick = DateTime.Now.Ticks.ToString(); pathFolder = "D:\\" + datetick; if (!Directory.Exists(pathFolder)) { Directory.CreateDirectory(pathFolder); } pdf.Save(pathFolder + "\\_summary.pdf"); System.Diagnostics.Process.Start("explorer.exe", pathFolder); }
private void BtnPrint_Click(object sender, EventArgs e) { int gapLine = 40, gapX = 40, gapY = 20, xCol2 = 130, xCol1 = 20, xCol3 = 300, xCol4 = 390, xCol5 = 1030; Size size = new Size(); String statusOPD = "", vsDate = "", vn = "", an = "", anDate = "", doccd = "", docno = "", anyr = "", vn1 = "", pathFolder = "", datetick = "", filename = "", docyr = "", amt2 = ""; String doc = "", datestart = ""; //DataTable dt = new DataTable(); DataTable dt = new DataTable(); dt = bc.bcDB.pttscDB.SelectByDoc(txtPaidType.Text.Trim()); DateTime dtstart = new DateTime(); DateTime.TryParse(txtDateStart.Text, out dtstart); if (bc.iniC.windows.Equals("windosxp")) { if (dtstart.Year > 2500) { dtstart = dtstart.AddYears(-543); } else if (dtstart.Year < 2000) { dtstart = dtstart.AddYears(543); } } datestart = dtstart.ToString("dd-MM-yyyy", new CultureInfo("en-US")); //throw new NotImplementedException(); C1PdfDocument pdf = new C1PdfDocument(); C1PdfDocumentSource pds = new C1PdfDocumentSource(); StringFormat _sfRight, _sfRightCenter; //Font _fontTitle = new Font("Tahoma", 15, FontStyle.Bold); _sfRight = new StringFormat(); _sfRight.Alignment = StringAlignment.Far; _sfRightCenter = new StringFormat(); _sfRightCenter.Alignment = StringAlignment.Far; _sfRightCenter.LineAlignment = StringAlignment.Center; //RectangleF rc = GetPageRect(pdf); Font titleFont = new Font(bc.iniC.pdfFontName, 18, FontStyle.Bold); Font hdrFont = new Font(bc.iniC.pdfFontName, 14, FontStyle.Regular); Font hdrFontB = new Font(bc.iniC.pdfFontName, 16, FontStyle.Bold); Font ftrFont = new Font(bc.iniC.pdfFontName, 8); Font txtFont = new Font(bc.iniC.pdfFontName, 10, FontStyle.Regular); //pdf.Clear(); pdf.FontType = FontTypeEnum.Embedded; //newPagePDFSummaryBorder(pdf, dt, titleFont, hdrFont, ftrFont, txtFont, false); //get page rectangle, discount margins RectangleF rcPage = pdf.PageRectangle; rcPage = RectangleF.Empty; rcPage.Inflate(-72, -92); rcPage.Location = new PointF(rcPage.X, rcPage.Y + titleFont.SizeInPoints + 10); rcPage.Size = new SizeF(0, titleFont.SizeInPoints + 3); rcPage.Width = 610; rcPage.Height = gapLine; gapY += gapLine; gapY += gapLine; String space2 = " ", space3 = " ", space4 = " ", space5 = " ", space = ""; int rowline = 0, i = 0; rowline = 205; String txt = "ใบนำส่งรายชื่อ ผู้มาตรวจ COVID " + (bc.iniC.branchId.Equals("001") ? "บางนา 1" : bc.iniC.branchId.Equals("002") ? "บางนา 2" : "ไม่ระบุ"); pdf.DrawString(txt, titleFont, Brushes.Black, rcPage); gapY += gapLine; rcPage.Location = new PointF(rcPage.X, rcPage.Y + titleFont.SizeInPoints + 10); rcPage.Size = new SizeF(0, titleFont.SizeInPoints + 3); rcPage.Width = 610; txt = " ประจำวันที่ " + datestart + " จำนวนผู้มาตรวจ " + dt.Rows.Count; pdf.DrawString(txt, titleFont, Brushes.Black, rcPage); gapY += gapLine; rcPage.Location = new PointF(rcPage.X, rcPage.Y + titleFont.SizeInPoints + 10); rcPage.Size = new SizeF(0, titleFont.SizeInPoints + 3); rcPage.Width = 610; txt = " เลขที่ " + txtPaidType.Text.Trim(); pdf.DrawString(txt, titleFont, Brushes.Black, rcPage); datetick = DateTime.Now.Ticks.ToString(); if (!Directory.Exists("report")) { Directory.CreateDirectory("report"); } filename = "report\\" + datetick + ".pdf"; pdf.Save(filename); pdf.Clear(); pdf.Dispose(); if (File.Exists(filename)) { //bool isExists = System.IO.File.Exists(filename); //if (isExists) System.Diagnostics.Process.Start(filename); } }
static void CreateDocumentGraphics(C1PdfDocument pdf) { // set up to draw Rect rc = new Rect(0, 0, 300, 200); string text = "Hello world of .NET Graphics and PDF.\r\nNice to meet you."; Font font = new Font("Times New Roman", 12, PdfFontStyle.Italic | PdfFontStyle.Underline); // draw to pdf document int penWidth = 0; byte penRGB = 0; pdf.FillPie(Colors.Red, rc, 0, 20f); pdf.FillPie(Colors.Green, rc, 20f, 30f); pdf.FillPie(Colors.Blue, rc, 60f, 12f); pdf.FillPie(Colors.Orange, rc, -80f, -20f); for (float startAngle = 0; startAngle < 360; startAngle += 40) { Color penColor = Color.FromArgb(0xff, penRGB, penRGB, penRGB); Pen pen = new Pen(penColor, penWidth++); penRGB = (byte)(penRGB + 20); pdf.DrawArc(pen, rc, startAngle, 40f); } pdf.DrawRectangle(Colors.Red, rc); pdf.DrawString(text, font, Colors.Black, rc); // show a Bezier curve var pts = new Point[] { new Point(400, 100), new Point(420, 30), new Point(500, 140), new Point(530, 20), }; // draw Bezier pdf.DrawBezier(new Pen(Colors.Blue, 4), pts[0], pts[1], pts[2], pts[3]); // show Bezier control points pdf.DrawLines(Colors.Gray, pts); foreach (Point pt in pts) { pdf.FillRectangle(Colors.Red, pt.X - 2, pt.Y - 2, 4, 4); } // title pdf.DrawString("Simple Bezier", font, Colors.Black, new Rect(500, 150, 100, 100)); }
static void CreateDocumentPaperSizes(C1PdfDocument pdf) { // add title Font titleFont = new Font("Tahoma", 24, PdfFontStyle.Bold); Rect rc = PdfUtils.PageRectangle(pdf); PdfUtils.RenderParagraph(pdf, pdf.DocumentInfo.Title, titleFont, rc, rc, false); // create constant font and StringFormat objects Font font = new Font("Tahoma", 18); StringFormat sf = new StringFormat(); sf.Alignment = HorizontalAlignment.Center; sf.LineAlignment = VerticalAlignment.Center; // create one page with each paper size bool firstPage = true; foreach (var fi in typeof(PaperKind).GetFields(BindingFlags.Static | BindingFlags.Public)) { // Silverlight/Phone doesn't have Enum.GetValues PaperKind pk = (PaperKind)fi.GetValue(null); // skip custom size if (pk == PaperKind.Custom) continue; // add new page for every page after the first one if (!firstPage) pdf.NewPage(); firstPage = false; // set paper kind and orientation pdf.PaperKind = pk; pdf.Landscape = !pdf.Landscape; // draw some content on the page rc = PdfUtils.PageRectangle(pdf); rc = PdfUtils.Inflate(rc, -6, -6); string text = string.Format("PaperKind: [{0}];\r\nLandscape: [{1}];\r\nFont: [Tahoma 18pt]", pdf.PaperKind, pdf.Landscape); pdf.DrawString(text, font, Colors.Black, rc, sf); pdf.DrawRectangle(Colors.Black, rc); } }
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 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); } }
private void printReserveVaccinePDF() { String pathFolder = "", filename = "", datetick = ""; int gapLine = 20, gapLine1 = 15, gapX = 40, gapY = 20, xCol2 = 200, xCol1 = 160, xCol3 = 300, xCol4 = 390, xCol5 = 500; Size size = new Size(); C1PdfDocument pdf = new C1PdfDocument(); C1PdfDocumentSource pds = new C1PdfDocumentSource(); StringFormat _sfRight, _sfRightCenter; //Font _fontTitle = new Font("Tahoma", 15, FontStyle.Bold); _sfRight = new StringFormat(); _sfRight.Alignment = StringAlignment.Far; _sfRightCenter = new StringFormat(); _sfRightCenter.Alignment = StringAlignment.Far; _sfRightCenter.LineAlignment = StringAlignment.Center; Font titleFont = new Font(bc.iniC.pdfFontName, bc.pdfFontSizetitleFont, FontStyle.Bold); Font hdrFont = new Font(bc.iniC.pdfFontName, bc.pdfFontSizehdrFont, FontStyle.Regular); Font hdrFontB = new Font(bc.iniC.pdfFontName, 16, FontStyle.Bold); Font ftrFont = new Font(bc.iniC.pdfFontName, 8); Font txtFont = new Font(bc.iniC.pdfFontName, bc.pdfFontSizetxtFont, FontStyle.Regular); pdf.FontType = FontTypeEnum.Embedded; RectangleF rcPage = pdf.PageRectangle; rcPage = RectangleF.Empty; rcPage.Inflate(-72, -92); rcPage.Location = new PointF(rcPage.X, rcPage.Y + titleFont.SizeInPoints + 10); rcPage.Size = new SizeF(0, titleFont.SizeInPoints + 3); rcPage.Width = 110; Image loadedImage; loadedImage = Resources.LOGO_BW_tran; float newWidth = loadedImage.Width * 100 / loadedImage.HorizontalResolution; float newHeight = loadedImage.Height * 100 / loadedImage.VerticalResolution; float widthFactor = 4.8F; float heightFactor = 4.8F; if (widthFactor > 1 | heightFactor > 1) { if (widthFactor > heightFactor) { widthFactor = 1; newWidth = newWidth / widthFactor; newHeight = newHeight / widthFactor; //newWidth = newWidth / 1.2; //newHeight = newHeight / 1.2; } else { newWidth = newWidth / heightFactor; newHeight = newHeight / heightFactor; } } RectangleF recf = new RectangleF(15, 15, (int)newWidth, (int)newHeight); pdf.DrawImage(loadedImage, recf); rcPage.X = gapX + recf.Width - 10; rcPage.Y = gapY; RectangleF rc = rcPage; string[] filePaths = Directory.GetFiles(System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\slip\\", txtID.Text.Trim() + "*.jpg"); if (filePaths.Length > 0) { int i = 1, xx = 40; foreach (String filename1 in filePaths) { Image loadedImage1, resizedImage1 = null; loadedImage1 = Image.FromFile(filename1); int originalWidth = 0; originalWidth = loadedImage1.Width; int newWidth1 = 200; resizedImage1 = loadedImage1.GetThumbnailImage(newWidth1, (newWidth1 * loadedImage1.Height) / originalWidth, null, IntPtr.Zero); RectangleF recf1 = new RectangleF(i == 1?xx: newWidth1 + 10, 380, (int)newWidth1, (int)resizedImage1.Height); pdf.DrawImage(loadedImage1, recf1); i++; } } Image qrcode = c1BarCode1.Image; RectangleF recf2 = new RectangleF(350, 500, qrcode.Width, qrcode.Height); pdf.DrawImage(qrcode, recf2); size = bc.MeasureString(bc.iniC.hostname, titleFont); rcPage.Width = size.Width; pdf.DrawString(bc.iniC.hostname, titleFont, Brushes.Black, rcPage); gapY += gapLine; rcPage.Y = gapY; size = bc.MeasureString(bc.iniC.hostaddresst, hdrFont); rcPage.Width = size.Width; pdf.DrawString(bc.iniC.hostaddresst, hdrFont, Brushes.Black, rcPage); String dose = "", amt11 = ""; int amt111 = 0; dose = txtDose.Text.Replace("จอง", "").Replace("เข็ม", "").Replace("3,300", "").Replace("1,650", "").Replace("4,950", "").Trim().Replace("6,600", "").Trim().Replace("8,250", "") .Replace("8,250", "").Replace("9,900", "").Replace("11,500", "").Replace("13,200", "").Replace("14,850", "").Replace("16,500", "").Replace("18,150", "").Replace("19,800", "").Trim(); int.TryParse(dose, out amt111); amt111 *= 1650; gapY += gapLine; gapY += gapLine; gapY += gapLine; gapX = xCol1; rc.X = (pdf.PageSize.Width / 2) - 15; rc.Y = gapY; pdf.DrawString("ใบจองวัคซีน", titleFont, Brushes.Black, rc); gapY += gapLine; gapY += gapLine; gapX = 30; rc.X = gapX; rc.Y = gapY; pdf.DrawString("ข้าพเจ้า ชื่อ-นามสกุล", txtFont, Brushes.Black, rc); rc.X = 110; rc.Y = rc.Y - 4; pdf.DrawString(txtName.Text, hdrFont, Brushes.Black, rc); pdf.DrawLine(Pens.Gray, 100, gapY + 15, 380, gapY + 15); rc.X = 400; rc.Y = gapY; pdf.DrawString("เลขที่ประชาชน", txtFont, Brushes.Black, rc); rc.X = 470; rc.Y = rc.Y - 4; pdf.DrawString(txtPID.Text, hdrFont, Brushes.Black, rc); pdf.DrawLine(Pens.Gray, 455, gapY + 15, 590, gapY + 15); gapY += gapLine; gapX = 30; rc.X = gapX; rc.Y = gapY; pdf.DrawString("ที่อยู่ปัจจุบัน", txtFont, Brushes.Black, rc); rc.X = 75; rc.Y = rc.Y - 4; pdf.DrawString(txtAddress.Text, hdrFont, Brushes.Black, rc); pdf.DrawLine(Pens.Gray, 70, gapY + 15, 400, gapY + 15); rc.X = 420; rc.Y = gapY; pdf.DrawString("เบอร์ที่ติดต่อได้", txtFont, Brushes.Black, rc); rc.X = 480; rc.Y = rc.Y - 4; pdf.DrawString(txtMobile.Text, hdrFont, Brushes.Black, rc); pdf.DrawLine(Pens.Gray, 470, gapY + 15, 590, gapY + 15); gapY += gapLine; gapX = 30; rc.X = gapX; rc.Y = gapY; rc.Width = 610; pdf.DrawString("ขอจองวัคซีนทางเลือก MODERNA และชำระเงิน จำนวน โดส ราคา 1,650 บาทต่อโดส", txtFont, Brushes.Black, rc); rc.X = 19; rc.Y = rc.Y - 4; pdf.DrawString(dose, hdrFont, Brushes.Black, rc); pdf.DrawLine(Pens.Gray, 215, gapY + 15, 220, gapY + 15); gapY += gapLine; gapX = 30; rc.X = gapX; rc.Y = gapY; pdf.DrawString("ราคาดังกล่าวเป็นราคาที่ รวมค่าวัคซีน ค่าประกัน ค่าบริการสำหรับการฉีด ไม่รวมค่าแพทย์ ถ้าต้องการพบแพทย์ โดยชำระเงินเต็มจำนวน", txtFont, Brushes.Black, rc); gapY += gapLine; gapX = 30; rc.X = gapX; rc.Y = gapY; pdf.DrawString("โดสละ 1,650 รวม โดส เป็นเงิน บาท ตามใบเจองเลขที่ ", txtFont, Brushes.Black, rc); rc.X = 100; rc.Y = rc.Y - 4; pdf.DrawString(dose, hdrFont, Brushes.Black, rc); rc.X = 175; rc.Y = rc.Y; pdf.DrawString(amt111.ToString("#,###.00"), hdrFont, Brushes.Black, rc); rc.X = 310; rc.Y = rc.Y; pdf.DrawString(txtID.Text.Trim(), hdrFont, Brushes.Black, rc); pdf.DrawLine(Pens.Gray, 95, gapY + 15, 100, gapY + 15); pdf.DrawLine(Pens.Gray, 165, gapY + 15, 220, gapY + 15); pdf.DrawLine(Pens.Gray, 340, gapY + 15, 470, gapY + 15); gapY += gapLine; gapX = 30; rc.X = gapX; rc.Y = gapY; pdf.DrawString("หมายเหตุ 1.ทางโรงพยาบาลจะนัดรับวัคซีนหลังจาก โรงพยาบาลได้รับการจัดสรรจากหน่วยงานภาครัฐ ตามที่อยู่และเบอร์โทรที่ได้ให้ไว้", txtFont, Brushes.Black, rc); gapY += gapLine; gapX = 30; rc.X = 67; rc.Y = gapY; pdf.DrawString("2.กรณีได้รับจัดสรรวัคซีนมาไม่เพียงพอต่อการจองที่โรงพยาบาลได้รับจองตามที่ได้รับจัดสรร ทางโรงพยาบาลจะเรียงลำดับการเข้ารับวัคซีนตามลำดดับการจองก่อน-หลัง", txtFont, Brushes.Black, rc); gapY += gapLine; gapX = 30; rc.X = 67; rc.Y = gapY; pdf.DrawString("และจะคืนเงินมัดจำให้เต็มจำนวน กรณีจองแล้วไม่ได้", txtFont, Brushes.Black, rc); gapY += gapLine; gapX = 30; rc.X = 67; rc.Y = gapY; pdf.DrawString("3.ทางโรงพยาบาลสงวนสิทธิ์ยกเลิกการของโดยไม่คืนเงินกรณีผู้จองไม่มารับวัคซีนตามช่วงเวลาที่กำหนด", txtFont, Brushes.Black, rc); gapY += gapLine; gapX = 30; rc.X = 67; rc.Y = gapY; pdf.DrawString("4.ห้ามนำวัคซีนไปขายต่อเพราะเป็นสินค้าควบคุมราคา", txtFont, Brushes.Black, rc); gapY += gapLine; gapX = 30; rc.X = 67; rc.Y = gapY; pdf.DrawString("5.ต้องมารับบริการฉีดวัคซีน ที่โรงพยาบาล บางนา5 เท่านั้น", txtFont, Brushes.Black, rc); gapY += gapLine; gapY += gapLine; gapY += gapLine; gapY += gapLine; gapX = 30; rc.X = 370; rc.Y = gapY; pdf.DrawString("ผู้จอง", txtFont, Brushes.Black, rc); pdf.DrawLine(Pens.Gray, 400, gapY + 15, 500, gapY + 15); gapY += gapLine; gapX = 30; rc.X = 370; rc.Y = gapY; pdf.DrawString("ผู้รับจอง on line", txtFont, Brushes.Black, rc); pdf.DrawLine(Pens.Gray, 400, gapY + 15, 500, gapY + 15); String txt = ""; if (txtDate.Text.Trim().Length > 9) { txt = bc.datetoShow(txtDate.Text.Trim()) + " " + txtDate.Text.Substring(10).Trim(); } else { txt = txtDate.Text.Trim(); } gapY += gapLine; gapX = 30; rc.X = 370; rc.Y = gapY; pdf.DrawString("วันที่จอง " + txt, txtFont, Brushes.Black, rc); pdf.DrawLine(Pens.Gray, 400, gapY + 15, 500, gapY + 15); gapY += gapLine; gapY += gapLine; gapY += gapLine; gapY += gapLine; gapY += gapLine; gapY += gapLine; gapY += gapLine; gapY += gapLine; gapY += gapLine; gapY += gapLine; gapY += gapLine; gapY += gapLine; gapY += gapLine; gapX = 30; rc.X = gapX; rc.Y = gapY; pdf.DrawString("สอบถามเพิ่มเติมโทร 02 138 1155-60 ต่อ 143 ", txtFont, Brushes.Black, rc); gapY += gapLine; gapX = 30; rc.X = gapX; rc.Y = gapY; pdf.DrawString("วันจันทร์ ถึง วันศุกร์ เวลา 8.00 - 16.00 น. (ปิดพักเที่ยง) ", txtFont, Brushes.Black, rc); gapY += gapLine; gapX = 30; rc.X = gapX; rc.Y = gapY; pdf.DrawString("line @657bkkyq", txtFont, Brushes.Black, rc); RectangleF rcHdr = new RectangleF(); rcHdr.Width = pdf.PageSize.Width - 20; rcHdr.Height = pdf.PageSize.Height - 20; rcHdr.X = 10; rcHdr.Y = 10; String PathName = "medical", fileName = ""; datetick = DateTime.Now.Ticks.ToString(); if (!Directory.Exists("report")) { Directory.CreateDirectory("report"); } fileName = "report\\" + txtID.Text.Trim() + "_" + datetick + ".pdf"; if (File.Exists(fileName)) { File.Delete(fileName); System.Threading.Thread.Sleep(100); } pdf.DrawRectangle(Pens.Black, rcHdr); // ตาราง String path = Path.GetDirectoryName(Application.ExecutablePath); pdf.Save(path + "\\" + fileName); Process.Start(fileName); }
static void CreateDocumentTextFlow(C1PdfDocument pdf) { // load long string from resource file string text = "Resource not found..."; using (var sr = new StreamReader(DataAccess.GetStream("flow.txt"))) { text = sr.ReadToEnd(); } text = text.Replace("\t", " "); text = string.Format("{0}\r\n\r\n---oOoOoOo---\r\n\r\n{0}", text); // create pdf document pdf.DocumentInfo.Title = "Text Flow"; // add title Font titleFont = new Font("Tahoma", 24, PdfFontStyle.Bold); Font bodyFont = new Font("Tahoma", 9); Rect rcPage = PdfUtils.PageRectangle(pdf); Rect rc = PdfUtils.RenderParagraph(pdf, pdf.DocumentInfo.Title, titleFont, rcPage, rcPage, false); rc.Y += titleFont.Size + 6; rc.Height = rcPage.Height - rc.Y; // create two columns for the text Rect rcLeft = rc; rcLeft.Width = rcPage.Width / 2 - 12; rcLeft.Height = 300; rcLeft.Y = (rcPage.Y + rcPage.Height - rcLeft.Height) / 2; Rect rcRight = rcLeft; rcRight.X = rcPage.Right - rcRight.Width; // start with left column rc = rcLeft; // render string spanning columns and pages for (; ; ) { // render as much as will fit into the rectangle rc = PdfUtils.Inflate(rc, -3, -3); int nextChar = pdf.DrawString(text, bodyFont, Colors.Black, rc); rc = PdfUtils.Inflate(rc, +3, +3); pdf.DrawRectangle(Colors.LightGray, rc); // break when done if (nextChar >= text.Length) { break; } // get rid of the part that was rendered text = text.Substring(nextChar); // switch to right-side rectangle if (rc.Left == rcLeft.Left) { rc = rcRight; } else // switch to left-side rectangle on the next page { pdf.NewPage(); rc = rcLeft; } } }
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 void CreateDocumentTextFlow(C1PdfDocument pdf) { // load long string from resource file string text = Strings.ResourceNotFound; using (var sr = new StreamReader(typeof(BasicTextPage).GetTypeInfo().Assembly.GetManifestResourceStream("PdfSamples.Resources.flow.txt"))) { text = sr.ReadToEnd(); } text = text.Replace("\t", " "); text = string.Format("{0}\r\n\r\n---oOoOoOo---\r\n\r\n{0}", text); // create pdf document pdf.DocumentInfo.Title = Strings.TextFlowDocumentTitle; // add title Font titleFont = new Font("Tahoma", 24, PdfFontStyle.Bold); Font bodyFont = new Font("Tahoma", 9); Rect rcPage = PdfUtils.PageRectangle(pdf); Rect rc = PdfUtils.RenderParagraph(pdf, pdf.DocumentInfo.Title, titleFont, rcPage, rcPage, false); rc.Y += titleFont.Size + 6; rc.Height = rcPage.Height - rc.Y; // create two columns for the text Rect rcLeft = rc; rcLeft.Width = rcPage.Width / 2 - 12; rcLeft.Height = 300; rcLeft.Y = (rcPage.Y + rcPage.Height - rcLeft.Height) / 2; Rect rcRight = rcLeft; rcRight.X = rcPage.Right - rcRight.Width; // start with left column rc = rcLeft; // render string spanning columns and pages for (; ;) { // render as much as will fit into the rectangle rc = PdfUtils.Inflate(rc, -3, -3); int nextChar = pdf.DrawString(text, bodyFont, Colors.Black, rc); rc = PdfUtils.Inflate(rc, +3, +3); pdf.DrawRectangle(Colors.LightGray, rc); // break when done if (nextChar >= text.Length) { break; } // get rid of the part that was rendered text = text.Substring(nextChar); // switch to right-side rectangle if (rc.Left == rcLeft.Left) { rc = rcRight; } else // switch to left-side rectangle on the next page { pdf.NewPage(); rc = rcLeft; } } }
public static void HandleButtonClick(object sender, RoutedEventArgs e) { // get sender button var btn = sender as Button; // create document var pdf = new C1PdfDocument(PaperKind.Letter); pdf.Clear(); // set document info var di = pdf.DocumentInfo; di.Author = "ComponentOne"; di.Subject = "C1.WPF.Pdf demo."; di.Title = (string)btn.Content; //// add some security //if (false) //{ // var si = pdf.Security; // si.AllowPrint = false; // si.AllowEditAnnotations = false; // si.AllowEditContent = false; // si.AllowCopyContent = false; // //si.UserPassword = "******"; // //si.OwnerPassword = "******"; //} //// set viewer preferences //if (false) //{ // var vp = pdf.ViewerPreferences; // vp.CenterWindow = true; // vp.FitWindow = true; // vp.PageLayout = PageLayout.TwoColumnLeft; // vp.PageMode = PageMode.FullScreen; //} // create document switch (di.Title) { case "Quotes": CreateDocumentQuotes(pdf); break; case "Tables": CreateDocumentTables(pdf); break; case "Images": CreateDocumentImages(pdf); break; case "Paper Sizes": CreateDocumentPaperSizes(pdf); break; case "Table of Contents": CreateDocumentTOC(pdf); break; case "Text Flow": CreateDocumentTextFlow(pdf); break; case "Text": CreateDocumentText(pdf); break; case "Graphics": CreateDocumentGraphics(pdf); break; } // render footers // this reopens each page and adds content to them (now we know the page count). var font = new Font("Arial", 8, PdfFontStyle.Bold); var fmt = new StringFormat(); fmt.Alignment = HorizontalAlignment.Right; fmt.LineAlignment = VerticalAlignment.Bottom; for (int page = 0; page < pdf.Pages.Count; page++) { pdf.CurrentPage = page; var text = string.Format("C1.WPF.Pdf: {0}, page {1} of {2}", di.Title, page + 1, pdf.Pages.Count); pdf.DrawString( text, font, Colors.DarkGray, PdfUtils.Inflate(pdf.PageRectangle, -72, -36), fmt); } var w = new Preview(); w.PdfDocument = pdf; w.Show(); }
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 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 void CreateDocumentGraphics(C1PdfDocument pdf) { // set up to draw Rect rc = new Rect(50, 70, 300, 300); string text = Strings.DocumentGraphicsText; Font font = new Font("Segoe UI Light", 16, PdfFontStyle.Italic); // draw to pdf document int penWidth = 0; byte penRGB = 0; pdf.FillPie(Colors.DarkRed, rc, 0, 20f); pdf.FillPie(Colors.Green, rc, 20f, 30f); pdf.FillPie(Colors.Teal, rc, 60f, 12f); pdf.FillPie(Colors.Orange, rc, -80f, -20f); for (float startAngle = 0; startAngle < 360; startAngle += 40) { Color penColor = Color.FromArgb(0xff, penRGB, penRGB, penRGB); Pen pen = new Pen(penColor, penWidth++); penRGB = (byte)(penRGB + 20); pdf.DrawArc(pen, rc, startAngle, 40f); } //pdf.DrawRectangle(Colors.Red, rc); rc = new Rect(10, 20, 300, 50); pdf.DrawString(text, font, Colors.Black, rc); // show a Bezier curve var pts = new Point[] { new Point(400, 100), new Point(420, 30), new Point(500, 140), new Point(530, 20), }; // draw Bezier pdf.DrawBezier(new Pen(Colors.Green, 4), pts[0], pts[1], pts[2], pts[3]); // show Bezier control points pdf.DrawLines(Colors.Gray, pts); foreach (Point pt in pts) { pdf.FillRectangle(Colors.Orange, pt.X - 2, pt.Y - 2, 4, 4); } // title pdf.DrawString(Strings.Bezier, font, Colors.Black, new Rect(450, 180, 100, 100)); // figures Color clr = Color.FromArgb(255, 250, 250, 0); Pen linePen = new Pen(clr, 2); linePen.DashStyle = C1.Xaml.Pdf.DashStyle.DashDotDot; pdf.DrawLine(linePen, 120, 700, 550, 300); pts = new Point[] { new Point(200, 400), new Point(500, 300), new Point(500, 560), new Point(370, 660), new Point(250, 600), new Point(200, 400), }; clr = Color.FromArgb(120, 0, 255, 0); pdf.FillPolygon(clr, pts); rc = new Rect(120, 350, 300, 300); clr = Color.FromArgb(150, 0, 0, 255); pdf.FillEllipse(clr, rc); rc = new Rect(100, 400, 250, 250); clr = Color.FromArgb(100, 255, 0, 0); pdf.FillRectangle(clr, rc); }
public static void HandleButtonClick(object sender, RoutedEventArgs e) { // get stream to save to var dlg = new SaveFileDialog(); dlg.DefaultExt = ".pdf"; var dr = dlg.ShowDialog(); if (!dr.HasValue || !dr.Value) { return; } // get sender button var btn = sender as Button; // create document var pdf = new C1PdfDocument(PaperKind.Letter); pdf.Clear(); // set document info var di = pdf.DocumentInfo; di.Author = "ComponentOne"; di.Subject = "C1.WPF.Pdf demo."; di.Title = (string)btn.Content; //// add some security //if (false) //{ // var si = pdf.Security; // si.AllowPrint = false; // si.AllowEditAnnotations = false; // si.AllowEditContent = false; // si.AllowCopyContent = false; // //si.UserPassword = "******"; // //si.OwnerPassword = "******"; //} //// set viewer preferences //if (false) //{ // var vp = pdf.ViewerPreferences; // vp.CenterWindow = true; // vp.FitWindow = true; // vp.PageLayout = PageLayout.TwoColumnLeft; // vp.PageMode = PageMode.FullScreen; //} // create document switch (di.Title) { case "Quotes": CreateDocumentQuotes(pdf); break; case "Tables": CreateDocumentTables(pdf); break; case "Images": CreateDocumentImages(pdf); break; case "Paper Sizes": CreateDocumentPaperSizes(pdf); break; case "Table of Contents": CreateDocumentTOC(pdf); break; case "Text Flow": CreateDocumentTextFlow(pdf); break; case "Text": CreateDocumentText(pdf); break; case "Graphics": CreateDocumentGraphics(pdf); break; } // render footers // this reopens each page and adds content to them (now we know the page count). var font = new Font("Arial", 8, PdfFontStyle.Bold); var fmt = new StringFormat(); fmt.Alignment = HorizontalAlignment.Right; fmt.LineAlignment = VerticalAlignment.Bottom; for (int page = 0; page < pdf.Pages.Count; page++) { pdf.CurrentPage = page; var text = string.Format("C1.WPF.Pdf: {0}, page {1} of {2}", di.Title, page + 1, pdf.Pages.Count); pdf.DrawString( text, font, Colors.DarkGray, PdfUtils.Inflate(pdf.PageRectangle, -72, -36), fmt); } // save document using (var stream = dlg.OpenFile()) { pdf.Save(stream); } MessageBox.Show("Pdf Document saved to " + dlg.SafeFileName); }