private void Button_Click(object sender, RoutedEventArgs e) { var dlg = new Microsoft.Win32.SaveFileDialog(); dlg.Filter = "PDF files (*.pdf)|*.pdf"; //var t = dlg.ShowDialog(); if (dlg.ShowDialog().Value) { // create pdf document var pdf = new C1PdfDocument(); pdf.Landscape = true; pdf.Compression = CompressionLevel.NoCompression; // render all grids into pdf document var options = new PdfExportOptions(); options.ScaleMode = ScaleMode.ActualSize; GridExport.RenderGrid(pdf, _flex1, options); pdf.NewPage(); GridExport.RenderGrid(pdf, _flex2, options); pdf.NewPage(); GridExport.RenderGrid(pdf, _flex3, options); pdf.NewPage(); GridExport.RenderGrid(pdf, _flex4, options); // save document using (var stream = dlg.OpenFile()) { pdf.Save(stream); } } }
// 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)); }
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)) //{ //} }
//--------------------------------------------------------------------------------- #region ** tables static void CreateDocumentTables(C1PdfDocument pdf) { // get the data var ds = DataAccess.GetDataSet(); // calculate page rect (discounting margins) Rect rcPage = PdfUtils.PageRectangle(pdf); Rect rc = rcPage; // add title Font titleFont = new Font("Tahoma", 24, PdfFontStyle.Bold); rc = PdfUtils.RenderParagraph(pdf, pdf.DocumentInfo.Title, titleFont, rcPage, rc, false); // render some tables RenderTable(pdf, rc, rcPage, ds.Tables["Customers"], new string[] { "CompanyName", "ContactName", "Country", "Address", "Phone" }); pdf.NewPage(); rc = rcPage; RenderTable(pdf, rc, rcPage, ds.Tables["Products"], new string[] { "ProductName", "QuantityPerUnit", "UnitPrice", "UnitsInStock", "UnitsOnOrder" }); pdf.NewPage(); rc = rcPage; RenderTable(pdf, rc, rcPage, ds.Tables["Employees"], new string[] { "FirstName", "LastName", "Country", "Notes" }); }
private void ConvertToPdf(List <Image> images) { RectangleF rect = imagePdfDocument.PageRectangle; bool firstPage = true; foreach (var selectedimg in images) { if (!firstPage) { imagePdfDocument.NewPage(); } firstPage = false; rect.Inflate(-72, -72); imagePdfDocument.DrawImage(selectedimg, rect); } }
private RectangleF RenderMultiPageImage(ref C1PdfDocument c1pdf, RectangleF rcPage, RectangleF rc, string fileName, Fax fax, bool addMetaText) { //Image img = Image.FromFile(fileName); MemoryStream ms = new MemoryStream(File.ReadAllBytes(fileName)); Image img = Image.FromStream(ms); FrameDimension oDimension = new FrameDimension(img.FrameDimensionsList[0]); int FrameCount = img.GetFrameCount(oDimension); for (int i = 0; i < FrameCount; i++) { // calculate image height // based on image size and page size rc.Height = Math.Min(img.Height / 96f * 72, rcPage.Height); // skip page if necessary if (rc.Bottom > rcPage.Bottom) { c1pdf.NewPage(); rc.Y = rcPage.Y; } // draw solid background (mainly to see transparency) rc.Inflate(+2, +2); c1pdf.FillRectangle(Brushes.White, rc); rc.Inflate(-2, -2); // draw image (keep aspect ratio) img.SelectActiveFrame(oDimension, i); string fn = System.IO.Path.GetFileName(fileName); fn = _workingFolder + fn.Substring(0, fn.Length - 4) + "-" + i + ".tif"; img.Save(fn); //Image img1 = Image.FromFile(fn); MemoryStream ms1 = new MemoryStream(File.ReadAllBytes(fn)); Image img1 = Image.FromStream(ms1); c1pdf.DrawImage(img1, rc, ContentAlignment.MiddleCenter, ImageSizeModeEnum.Scale); // update rectangle rc.Y = rc.Bottom + 20; if ((i == 0) && (addMetaText == true)) { AddMetaText(c1pdf, fax); } } return(rc); }
private void newPagePDFBorder(C1PdfDocument pdf, Boolean loopfor) { int gapLine = 20, gapX = 40, gapY = 135, xCol2 = 130, xCol1 = 20, xCol3 = 300, xCol4 = 390, xCol5 = 1030; Size size = new Size(); //if(pdf.Pages.Count>1) pdf.NewPage(); //if ((loopfor) && (pdf.CurrentPage != 0)) pdf.NewPage(); if ((loopfor)) { pdf.NewPage(); } RectangleF rcHdr = new RectangleF(); rcHdr.Width = 542; rcHdr.Height = 500; rcHdr.X = gapX; rcHdr.Y = gapY; //rcHdr.Location pdf.DrawRectangle(Pens.Black, rcHdr); // ตารางใหญ่ pdf.DrawLine(Pens.Black, rcHdr.X, rcHdr.Y + 50, rcHdr.X + rcHdr.Width, rcHdr.Y + 50); pdf.DrawLine(Pens.Black, rcHdr.X, rcHdr.Y + 80, rcHdr.X + rcHdr.Width, rcHdr.Y + 80); pdf.DrawLine(Pens.Black, rcHdr.X + rcHdr.Width - 90, rcHdr.Y + 50, rcHdr.X + rcHdr.Width - 90, rcHdr.Y + rcHdr.Height); //เส้นตั้ง จำนวนเงิน pdf.DrawLine(Pens.Black, rcHdr.X + 260, rcHdr.Y, rcHdr.X + 260, rcHdr.Y + 50); // เส้นตั้ง ชื่อ - นามสกุล float xxx = rcHdr.X + rcHdr.Width - 90; float yyy = rcHdr.Y + rcHdr.Height; gapY += 510; rcHdr.Width = rcHdr.X + rcHdr.Width - 230; rcHdr.Height = 30; rcHdr.X = gapX; rcHdr.Y = gapY; pdf.DrawRectangle(Pens.Black, rcHdr); // ตารางจำนวนเงิน ตัวอักษร pdf.DrawLine(Pens.Black, rcHdr.X + 452, rcHdr.Y + 15, rcHdr.X + 542, rcHdr.Y + 15); //เส้นแบ่ง รวมเงิน pdf.DrawLine(Pens.Black, rcHdr.X + 452, rcHdr.Y + 40, rcHdr.X + 542, rcHdr.Y + 40); //เส้นแบ่ง รวมเงิน rcHdr.Width = 500 - xxx + gapX + 42; rcHdr.Height = 75; rcHdr.X = xxx; rcHdr.Y = yyy; pdf.DrawRectangle(Pens.Black, rcHdr); // ตารางรวมเงิน ด้านล่าง }
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); } }
void DrawPage(PreviewPageInfo[] pages, int index) { // skip to next page if (index > 0) { _pdf.NewPage(); } // get preview page info var pi = pages[index]; // adjust page size var ps = pi.PhysicalSize; _pdf.PageSize = new SizeF(ps.Width / 100f * 72, ps.Height / 100f * 72); // draw image var img = pi.Image; _pdf.DrawImage(img, _pdf.PageRectangle); }
private void ConvertToPdf(List <Image> images, C1PdfDocument imageToPdf) { RectangleF rect = imageToPdf.PageRectangle; bool firstPage = true; foreach (var selectedimg in images) { if (!firstPage) { imageToPdf.NewPage(); } firstPage = false; rect.Inflate(-72, -72); try { imageToPdf.DrawImage(selectedimg, rect); } catch (Exception e) { MessageBox.Show(e.Message, Properties.Resources.dialog_error); } } }
private void ConvertToPdf(List <Image> images) { //Coversion to pdf. try { RectangleF rect = imagePdfDocument.PageRectangle; bool firstPage = true; foreach (var selectedimg in images) { if (!firstPage) { imagePdfDocument.NewPage(); } firstPage = false; rect.Inflate(-72, -72); imagePdfDocument.DrawImage(selectedimg, rect); } } catch (Exception excp) { MessageBox.Show("Exception occured: " + excp); } }
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); }
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; } } }
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; } } }
private bool CreatePDF(Fax fax, string fileName) { bool result = false; try { C1PdfDocument c1pdf = new C1PdfDocument(); // create pdf document c1pdf.Clear(); c1pdf.DocumentInfo.Title = "Fax: " + fax.FaxFilename; // calculate document name if (true == File.Exists(fileName)) { // pdf file already exists - so don't recreate it. Trace.WriteLine("PDF File already exists: " + fileName); result = true; } else { bool newPage = false; RectangleF rcPage = RectangleF.Empty; RectangleF rc1 = RectangleF.Empty; bool addMetaText = true; foreach (Attachment attachment in fax.Attachments) { if (newPage) { c1pdf.NewPage(); rc1.Y = rcPage.Y; } rcPage = c1pdf.PageRectangle; //rcPage.Inflate(-72, -72); rc1 = rcPage; rc1.Inflate(-10, 0); rc1 = RenderMultiPageImage(ref c1pdf, rcPage, rc1, attachment.FileName, fax, addMetaText); addMetaText = false; Trace.WriteLine("Rendered page: " + attachment.FileName + " to pdf: " + fileName); newPage = true; } c1pdf.Save(fileName); Trace.WriteLine("Saved (" + fileName + ") successfully."); result = true; } } catch (IOException fileEx) { Trace.WriteLine("IOException occurred in method CreatePDF with: " + fileEx.Message, "exceptions"); result = false; } catch (Exception ex) { Trace.WriteLine("General Exception occurred in method CreatePDF with: " + ex.Message, "exceptions"); result = false; } return(result); }
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 CreateDocumentTables(C1PdfDocument pdf) { // get the data var ds = DataAccess.GetDataSet(); // calculate page rect (discounting margins) Rect rcPage = PdfUtils.PageRectangle(pdf); Rect rc = rcPage; // add title Font titleFont = new Font("Tahoma", 24, PdfFontStyle.Bold); rc = PdfUtils.RenderParagraph(pdf, pdf.DocumentInfo.Title, titleFont, rcPage, rc, false); // render some tables RenderTable(pdf, rc, rcPage, ds.Tables["Customers"], new string[] { "CompanyName", "ContactName", "Country", "Address", "Phone" }); pdf.NewPage(); rc = rcPage; RenderTable(pdf, rc, rcPage, ds.Tables["Products"], new string[] { "ProductName", "QuantityPerUnit", "UnitPrice", "UnitsInStock", "UnitsOnOrder" }); pdf.NewPage(); rc = rcPage; RenderTable(pdf, rc, rcPage, ds.Tables["Employees"], new string[] { "FirstName", "LastName", "Country", "Notes" }); }
public static void RenderGrid(C1PdfDocument pdf, C1FlexGrid flex, PdfExportOptions options) { // get rendering options if (options == null) { options = new PdfExportOptions(); } // get root element to lay out the PDF pages Panel root = null; for (var parent = flex.Parent as FrameworkElement; parent != null; parent = parent.Parent as FrameworkElement) { if (parent is Panel) { root = parent as Panel; } } // get page size var rc = pdf.PageRectangle; // create panel to hold elements while they render var pageTemplate = new PageTemplate(); pageTemplate.Width = rc.Width; pageTemplate.Height = rc.Height; pageTemplate.SetPageMargin(options.Margin); root.Children.Add(pageTemplate); // render grid into PDF document var m = options.Margin; var sz = new Size(rc.Width - m.Left - m.Right, rc.Height - m.Top - m.Bottom); var pages = flex.GetPageImages(options.ScaleMode, sz, 100); for (int i = 0; i < pages.Count; i++) { // skip a page when necessary if (i > 0) { pdf.NewPage(); } // set content pageTemplate.PageContent.Child = pages[i]; pageTemplate.PageContent.Stretch = options.ScaleMode == ScaleMode.ActualSize ? System.Windows.Media.Stretch.None : System.Windows.Media.Stretch.Uniform; // set header/footer text pageTemplate.HeaderLeft.Text = options.DocumentTitle; if (options.KnownPageCount) { pageTemplate.FooterRight.Text = string.Format("Page {0} of {1}", pdf.CurrentPage + 1, pages.Count); } else { pageTemplate.FooterRight.Text = string.Format("Page {0}", pdf.CurrentPage + 1); } // measure page element pageTemplate.UpdateLayout(); pageTemplate.Arrange(new Rect(0, 0, rc.Width, rc.Height)); // add to PDF pdf.DrawElement(pageTemplate, rc); } // done with template root.Children.Remove(pageTemplate); }
// export the grid to a PDF file void SavePdf(Stream s) { // get root element to lay out the PDF pages Panel root = null; for (var parent = _flex.Parent as FrameworkElement; parent != null; parent = parent.Parent as FrameworkElement) { if (parent is Panel) { root = parent as Panel; } } // create pdf document var pdf = new C1PdfDocument(PaperKind.Letter, false); // get page size var rc = pdf.PageRectangle; var m = new Thickness(96, 96, 96 / 2, 96 / 2); var scaleMode = ScaleMode.ActualSize; // create panel to hold elements while they render var pageTemplate = new PageTemplate(); pageTemplate.Width = rc.Width; pageTemplate.Height = rc.Height; pageTemplate.SetPageMargin(m); root.Children.Add(pageTemplate); // render grid into PDF document var sz = new Size(rc.Width - m.Left - m.Right, rc.Height - m.Top - m.Bottom); var pages = _flex.GetPageImages(scaleMode, sz, 100); for (int i = 0; i < pages.Count; i++) { // skip a page when necessary if (i > 0) { pdf.NewPage(); } // set content pageTemplate.PageContent.Child = pages[i]; pageTemplate.PageContent.Stretch = System.Windows.Media.Stretch.Uniform; // set footer text pageTemplate.FooterRight.Text = string.Format("Page {0} of {1}", i + 1, pages.Count); // measure page element pageTemplate.Measure(new Size(rc.Width, rc.Height)); pageTemplate.UpdateLayout(); // add page element to PDF pdf.DrawElement(pageTemplate, rc); } // done with template root.Children.Remove(pageTemplate); // save the PDF document pdf.Save(s); s.Close(); }
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); }
public static void CreateDocument(C1PdfDocument pdf) { // create pdf document pdf.Clear(); //pdf.Compression = CompressionLevel.NoCompression; pdf.ConformanceLevel = PdfAConformanceLevel.PdfA2b; pdf.DocumentInfo.Title = "PDF Acroform"; // calculate page rect (discounting margins) var rcPage = GetPageRect(pdf); var rc = rcPage; // add title Font titleFont = new Font("Tahoma", 24, PdfFontStyle.Bold); rc = RenderParagraph(pdf, pdf.DocumentInfo.Title, titleFont, rcPage, rc, false); // render acroforms rc = rcPage; Font fieldFont = new Font("Arial", 14, PdfFontStyle.Regular); // text box field rc = new Rect(rc.X, rc.Y + rc.Height / 10, rc.Width / 3, rc.Height / 30); PdfTextBox textBox1 = RenderTextBox(pdf, "TextBox Sample", fieldFont, rc); textBox1.BorderWidth = FieldBorderWidth.Thick; textBox1.BorderStyle = FieldBorderStyle.Inset; textBox1.BorderColor = Colors.Green; //textBox1.BackColor = Colors.Yellow; // first check box field rc = new Rect(rc.X, rc.Y + 2 * rc.Height, rc.Width, rc.Height); RenderCheckBox(pdf, true, "CheckBox 1 Sample", fieldFont, rc); // first radio button group rc = new Rect(rc.X, rc.Y + 2 * rc.Height, rc.Width, rc.Height); RenderRadioButton(pdf, false, "RadioGroup1", "RadioButton 1 Sample Group 1", fieldFont, rc); rc = new Rect(rc.X, rc.Y + 2 * rc.Height, rc.Width, rc.Height); RenderRadioButton(pdf, true, "RadioGroup1", "RadioButton 2 Sample Group 1", fieldFont, rc); rc = new Rect(rc.X, rc.Y + 2 * rc.Height, rc.Width, rc.Height); RenderRadioButton(pdf, false, "RadioGroup1", "RadioButton 3 Sample Group 1", fieldFont, rc); // second check box field rc = new Rect(rc.X, rc.Y + 2 * rc.Height, rc.Width, rc.Height); RenderCheckBox(pdf, false, "CheckBox 2 Sample", fieldFont, rc); // second radio button group rc = new Rect(rc.X, rc.Y + 2 * rc.Height, rc.Width, rc.Height); RenderRadioButton(pdf, true, "RadioGroup2", "RadioButton 1 Sample Group 2", fieldFont, rc); rc = new Rect(rc.X, rc.Y + 2 * rc.Height, rc.Width, rc.Height); RenderRadioButton(pdf, false, "RadioGroup2", "RadioButton 2 Sample Group 2", fieldFont, rc); // first combo box field rc = new Rect(rc.X, rc.Y + 2 * rc.Height, rc.Width, rc.Height); PdfComboBox comboBox1 = RenderComboBox(pdf, new string[] { "First", "Second", "Third" }, 2, fieldFont, rc); // first list box field var rclb = new Rect(rc.X, rc.Y + 2 * rc.Height, rc.Width, 3 * rc.Height); RenderListBox(pdf, new string[] { "First", "Second", "Third", "Fourth", "Fifth" }, 5, fieldFont, rclb); // load first icon Image icon = null; //using (Stream stream = GetManifestResource("phoenix.png")) //{ // icon = Image.FromStream(stream); //} // first push putton field rc = new Rect(rc.X, rc.Y + 6 * rc.Height, rc.Width, rc.Height); PdfPushButton button1 = RenderPushButton(pdf, "Submit", fieldFont, rc, icon, ButtonLayout.ImageLeftTextRight); button1.Actions.Released.Add(new PdfPushButton.Action(ButtonAction.CallMenu, "FullScreen")); button1.Actions.GotFocus.Add(new PdfPushButton.Action(ButtonAction.OpenFile, @"..\..\Program.cs")); button1.Actions.LostFocus.Add(new PdfPushButton.Action(ButtonAction.GotoPage, "2")); button1.Actions.Released.Add(new PdfPushButton.Action(ButtonAction.OpenUrl, "https://www.grapecity.com/en/componentone")); //// load second icon //using (Stream stream = GetManifestResource("download.png")) //{ // icon = Image.FromStream(stream); //} // second push putton field rc = new Rect(rc.X, rc.Y + 2 * rc.Height, rc.Width, 2 * rc.Height); PdfPushButton button2 = RenderPushButton(pdf, "Cancel", fieldFont, rc, icon, ButtonLayout.TextTopImageBottom); button2.Actions.Pressed.Add(new PdfPushButton.Action(ButtonAction.ClearFields)); button2.Actions.Released.Add(new PdfPushButton.Action(ButtonAction.CallMenu, "Quit")); //// load second icon //using (Stream stream = GetManifestResource("top100.png")) //{ // icon = Image.FromStream(stream); //} // push putton only icon field rc = new Rect(rc.X + 1.5f * rc.Width, rc.Y, rc.Width / 2, rc.Height); PdfPushButton button3 = RenderPushButton(pdf, "", fieldFont, rc, icon, ButtonLayout.ImageOnly); button3.Actions.MouseEnter.Add(new PdfPushButton.Action(ButtonAction.HideField, button1.Name)); button3.Actions.MouseLeave.Add(new PdfPushButton.Action(ButtonAction.ShowField, button1.Name)); button3.Actions.Released.Add(new PdfPushButton.Action(ButtonAction.CallMenu, "ShowGrid")); button3.BorderWidth = FieldBorderWidth.Medium; button3.BorderStyle = FieldBorderStyle.Beveled; button3.BorderColor = Colors.Gray; // next page pdf.NewPage(); // text for next page rc = rcPage; RenderParagraph(pdf, "Second page as bookmark", titleFont, rcPage, rc, false); // text box field //rc = rcPage; //rc = new Rect(rc.X, rc.Y + rc.Height / 10, rc.Width / 3, rc.Height / 30); //PdfTextBox textBox2 = RenderTextBox("TextSample 2", fieldFont, rc, Color.Yellow, "In 2 page"); // second pass to number pages AddFooters(pdf); }
// export the grid to a PDF file void SavePdf(Stream s, string documentName) { #if false // get root element to lay out the PDF pages Panel root = null; for (var parent = _flex.Parent as FrameworkElement; parent != null; parent = parent.Parent as FrameworkElement) { if (parent is Panel) { root = parent as Panel; } } // create pdf document var pdf = new C1PdfDocument(PaperKind.Letter, false); // get page size var rc = pdf.PageRectangle; var m = new Thickness(96, 96, 96 / 2, 96 / 2); var scaleMode = ScaleMode.ActualSize; // create panel to hold elements while they render var pageTemplate = new PageTemplate(); pageTemplate.Width = rc.Width; pageTemplate.Height = rc.Height; pageTemplate.SetPageMargin(m); root.Children.Add(pageTemplate); // render grid into PDF document var sz = new Size(rc.Width - m.Left - m.Right, rc.Height - m.Top - m.Bottom); var pages = _flex.GetPageImages(scaleMode, sz, 100); for (int i = 0; i < pages.Count; i++) { // skip a page when necessary if (i > 0) { pdf.NewPage(); } // set content pageTemplate.PageContent.Child = pages[i]; pageTemplate.PageContent.Stretch = System.Windows.Media.Stretch.Uniform; // set header/footer text pageTemplate.HeaderLeft.Text = documentName; pageTemplate.FooterRight.Text = string.Format("Page {0} of {1}", i + 1, pages.Count); // measure page element pageTemplate.Measure(new Size(rc.Width, rc.Height)); pageTemplate.UpdateLayout(); // add page element to PDF pdf.DrawElement(pageTemplate, rc); } // done with template root.Children.Remove(pageTemplate); // save the PDF document pdf.Save(s); s.Close(); #endif }
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 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); } }