private async void btnRender_Click(object sender, RoutedEventArgs e) { await Task.Delay(TimeSpan.FromMilliseconds(100)); pdf.Clear(); progressRing.IsActive = true; panel.Arrange(pdf.PageRectangle); //1. Export UI as an image and then draw this image in pdf document. var renderTargetBitmap = new RenderTargetBitmap(); await renderTargetBitmap.RenderAsync(panel); var wb = new WriteableBitmap(renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight); (await renderTargetBitmap.GetPixelsAsync()).CopyTo(wb.PixelBuffer); var rect = new Rect(0, 0, renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight); pdf.DrawImage(wb, rect); //2. Draw every UI elements inside the panel in pdf document. //await pdf.DrawElement(panel, pdf.PageRectangle); PdfUtils.SetDocumentInfo(pdf, Strings.RenderUIDocumentTitle); await pdfDocSource.LoadFromStreamAsync(PdfUtils.SaveToStream(pdf).AsRandomAccessStream()); progressRing.IsActive = false; }
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); }
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)); }
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); }
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); }
//----------------------------------------------------------- #region ** object model public bool RenderDocument(PrintDocument doc, bool showProgressDialog) { // save reference to document _doc = doc; // initialize pdf document _pdf.Clear(); _pdf.Landscape = false; // prepare to render var savePC = _doc.PrintController; _previewController = new PreviewPrintController(); _doc.PrintController = showProgressDialog ? new PrintControllerWithStatusDialog(_previewController) : (PrintController)_previewController; _pageCount = 0; _cancel = false; // render try { _doc.PrintPage += _doc_PrintPage; _doc.EndPrint += _doc_EndPrint; _doc.Print(); } finally { _doc.PrintPage -= _doc_PrintPage; _doc.EndPrint -= _doc_EndPrint; _doc.PrintController = savePC; } // done return(!_cancel); }
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); }
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); } }
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); }
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(); }
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); }