public PDFline(int index, float fontSize, float lineSpacing, Color color, PdfTextAlignment textAlignment, bool isBold, string text) : base(index, fontSize, color, textAlignment) { Height = lineSpacing * fontSize; TextFormat = new PdfStringFormat(textAlignment); IsBold = isBold; Text = text; }
public static byte[] Text2Images(Stream inputFile, string filename) { // TODO Don't write file next time using (var fileStream = File.Create(filename)) { inputFile.Seek(0, SeekOrigin.Begin); inputFile.CopyTo(fileStream); } string text = File.ReadAllText(filename); PdfDocument doc = new PdfDocument(); PdfSection section = doc.Sections.Add(); PdfPageBase page = section.Pages.Add(); PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 11); PdfStringFormat format = new PdfStringFormat(); format.LineSpacing = 20f; PdfBrush brush = PdfBrushes.Black; PdfTextWidget textWidget = new PdfTextWidget(text, font, brush); float y = 0; PdfTextLayout textLayout = new PdfTextLayout(); textLayout.Break = PdfLayoutBreakType.FitPage; textLayout.Layout = PdfLayoutType.Paginate; RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize); textWidget.StringFormat = format; textWidget.Draw(page, bounds, textLayout); MemoryStream stream = new MemoryStream(); doc.SaveToStream(stream, Spire.Pdf.FileFormat.PDF); return(Pdf2Images(stream)); }
public void ConvertToPdf(string source, string destination) { try { var document = new PdfDocument(); var page = document.Pages.Add(); var graphics = page.Graphics; var font = new PdfStandardFont(PdfFontFamily.Helvetica, 12); var reader = new StreamReader(source, Encoding.ASCII); var text = reader.ReadToEnd(); reader.Close(); var format = new PdfStringFormat { Alignment = PdfTextAlignment.Justify, LineAlignment = PdfVerticalAlignment.Top, ParagraphIndent = 15f }; graphics.DrawString(text, font, PdfBrushes.Black, new RectangleF(new PointF(0, 0), page.GetClientSize()), format); document.Save(destination); document.Close(true); } catch (Exception ex) { FileLogger.SetLog(string.Format(ExceptionConstants.ConvertToPdf, source, ex.Message)); } }
private void addTitles() { PdfStringFormat stringFormat = new PdfStringFormat() { Alignment = PdfTextAlignment.Center }; RectangleF rectangleCenterPage = new Syncfusion.Drawing.RectangleF(0, currentY, _document.PageSettings.Size.Width, titleFont.Height); //title _document.Pages[0].Graphics.DrawString(Title, titleFont, brushBlack, rectangleCenterPage, stringFormat); currentY += spaceDefault; rectangleCenterPage.Y = currentY; //Laurea _document.Pages[0].Graphics.DrawString(TitleFacolta, subTitleFontBold, brushBlack, rectangleCenterPage, stringFormat); currentY += spaceDefault; rectangleCenterPage.Y = currentY; //Info = anno semestre _document.Pages[0].Graphics.DrawString(TitleInfo, subTitleFont, brushBlack, rectangleCenterPage, stringFormat); currentY += spaceDefault; // rectangleCenterPage.Y = currentY; //subTitleInfo //_document.Pages[0].Graphics.DrawString(SubtitleInfo, subTitleFont, brushBlack, rectangleCenterPage, stringFormat); //currentY += spaceDefault; }
private float AddTextMarkupAnnotation(PdfPageBase page, float y) { PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f)); PdfStringFormat format = new PdfStringFormat(); format.MeasureTrailingSpaces = true; String prompt = "Highlight incorrect spelling: "; SizeF size = font.MeasureString(prompt, format); page.Canvas.DrawString(prompt, font, PdfBrushes.DodgerBlue, 0, y); float x = size.Width; String label = "demo of anotation"; page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, x, y); size = font.MeasureString("demo of ", format); x = x + size.Width; PointF incorrectWordLocation = new PointF(x, y); String markupText = "Should be 'annotation'"; PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation(markupText, "anotation", new RectangleF(x, y, 100f, 100f), font); annotation.TextMarkupAnnotationType = PdfTextMarkupAnnotationType.Highlight; annotation.TextMarkupColor = Color.LightSkyBlue; (page as PdfNewPage).Annotations.Add(annotation); y = y + size.Height; return(y); }
static WatermarkBehavior() { watermarkFormat = new PdfStringFormat(); watermarkFormat.FormatFlags = PdfStringFormatFlags.NoWrap | PdfStringFormatFlags.NoClip; watermarkFormat.Alignment = PdfStringAlignment.Center; watermarkFormat.LineAlignment = PdfStringAlignment.Center; }
protected void GanttControlExporting_ServerPdfExporting(object sender, Syncfusion.JavaScript.Web.GanttEventArgs e) { PdfExport exp = new PdfExport(); GanttPdfExportSettings settings = new GanttPdfExportSettings(); settings.Theme = GanttExportTheme.FlatLime; settings.Locale = e.Arguments["locale"].ToString(); //Create footer template RectangleF bounds = new RectangleF(0, 0, 762, 25); PdfSolidBrush brush = new PdfSolidBrush(new PdfColor(51, 51, 51)); PdfPen pen = new PdfPen(new PdfColor(200, 200, 200)); PdfFont font = new PdfTrueTypeFont(new Font("Segoe UI", 10), true); PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Center; format.LineAlignment = PdfVerticalAlignment.Middle; PdfPageTemplateElement footer = new PdfPageTemplateElement(bounds); PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush); PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0}", pageNumber); compositeField.StringFormat = format; compositeField.Bounds = footer.Bounds; footer.Graphics.DrawRectangle(pen, bounds); compositeField.Draw(footer.Graphics, new PointF(0, 0)); PdfDocumentTemplate footerTemplate = new PdfDocumentTemplate(); footerTemplate.Bottom = footer; settings.PdfDocumentTemplate = footerTemplate; PdfDocument document = exp.Export(this.GanttControlExporting.Model, (IEnumerable)this.GanttControlExporting.DataSource, settings, false); document = exp.Export(this.GanttControlDesign.Model, (IEnumerable)this.GanttControlDesign.DataSource, settings, document, false); exp.Export(this.GanttControlImplementation.Model, (IEnumerable)this.GanttControlImplementation.DataSource, settings, "Gantt", document, true); }
private static PdfPageTemplateElement CreateHeaderTemplate(PdfDocument doc, PdfMargins margins, string headerString) { SizeF pageSize = doc.PageSettings.Size; PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Top + margins.Bottom) { Foreground = false }; float x = margins.Left; float y = 0; //Figure out what logo to use; either the application's logo or the owning company's logo Bitmap logo = null; //Resources.logo.ToBitmap(); //Draw image in header if we have one if (logo != null) { PdfImage headerImage = PdfImage.FromImage(Utility.ResizeImage(logo, 96, 96)); float width = headerImage.Width / 3; float height = headerImage.Height / 3; headerSpace.Graphics.DrawImage(headerImage, x, margins.Top - height - 2, width, height); } DrawLine(headerSpace.Graphics, x, y + margins.Top - 2, pageSize.Width - x, y + margins.Top - 2); //Draw string in header PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left); SizeF size = headerFontBold.MeasureString(headerString, format); headerSpace.Graphics.DrawString(headerString, headerFontBold, PdfBrushes.Black, (headerSpace.Width / 2) - (size.Width / 2), margins.Top - (size.Height + 5), format); headerSpace.Graphics.DrawString(DateTime.Now.Date.ToString("MM-dd-yyyy"), headerFontBold, PdfBrushes.Black, GetRightPage() - headerFontBold.MeasureString(DateTime.Now.Date.ToString("MM-dd-yyyy")).Width, margins.Top - (size.Height + 5), format); return(headerSpace); }
/// <summary> /// Adds header to the document /// </summary> /// <param name="width"></param> /// <param name="title"></param> /// <param name="description"></param> private PdfPageTemplateElement AddHeader(float width, string title, string description) { RectangleF rect = new RectangleF(0, 0, width, 50); //Create a new instance of PdfPageTemplateElement class. PdfPageTemplateElement header = new PdfPageTemplateElement(rect); PdfGraphics g = header.Graphics; //Draw title. PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold); PdfSolidBrush brush = new PdfSolidBrush(Color.FromArgb(44, 71, 120)); float x = (width / 2) - (font.MeasureString(title).Width) / 2; g.DrawString(title, font, brush, new RectangleF(x, (rect.Height / 4) + 3, font.MeasureString(title).Width, font.Height)); //Draw description brush = new PdfSolidBrush(Color.Gray); font = new PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Bottom); g.DrawString(description, font, brush, new RectangleF(0, 0, header.Width, header.Height - 8), format); //Draw some lines in the header PdfPen pen = new PdfPen(Color.DarkBlue, 0.7f); g.DrawLine(pen, 0, 0, header.Width, 0); pen = new PdfPen(Color.DarkBlue, 2f); g.DrawLine(pen, 0, 03, header.Width + 3, 03); pen = new PdfPen(Color.DarkBlue, 2f); g.DrawLine(pen, 0, header.Height - 3, header.Width, header.Height - 3); g.DrawLine(pen, 0, header.Height, header.Width, header.Height); return(header); }
private void button1_Click(object sender, EventArgs e) { //Create a pdf document. PdfDocument doc = new PdfDocument(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; // Create one page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); float y = 10; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold), true); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Categories List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Categories List", format1).Height; y = y + 5; RectangleF rctg = new RectangleF(new PointF(0, 0), page.Canvas.ClientSize); PdfLinearGradientBrush brush = new PdfLinearGradientBrush(rctg, Color.Navy, Color.OrangeRed, PdfLinearGradientMode.Vertical); PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold); String formatted = "Beverages\nCondiments\nConfections\nDairy Products\nGrains/Cereals\nMeat/Poultry\nProduce\nSeafood"; PdfList list = new PdfList(formatted); list.Font = font; list.Indent = 8; list.TextIndent = 5; list.Brush = brush; PdfLayoutResult result = list.Draw(page, 0, y); y = result.Bounds.Bottom; PdfSortedList sortedList = new PdfSortedList(formatted); sortedList.Font = font; sortedList.Indent = 8; sortedList.TextIndent = 5; sortedList.Brush = brush; sortedList.Draw(page, 0, y); //Save pdf file. doc.SaveToFile("SimpleList.pdf"); doc.Close(); //Launching the Pdf file. PDFDocumentViewer("SimpleList.pdf"); }
private float AddDocumentLinkAnnotation(PdfPageBase page, float y) { PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f)); PdfStringFormat format = new PdfStringFormat(); format.MeasureTrailingSpaces = true; String prompt = "Document Link: "; SizeF size = font.MeasureString(prompt); page.Canvas.DrawString(prompt, font, PdfBrushes.DodgerBlue, 0, y); float x = font.MeasureString(prompt, format).Width; PdfDestination dest = new PdfDestination(page); dest.Mode = PdfDestinationMode.Location; dest.Location = new PointF(0, y); dest.Zoom = 2f; String label = "Click me, Zoom 200%"; size = font.MeasureString(label); RectangleF bounds = new RectangleF(x, y, size.Width, size.Height); page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, x, y); PdfDocumentLinkAnnotation annotation = new PdfDocumentLinkAnnotation(bounds, dest); annotation.Color = Color.Blue; (page as PdfNewPage).Annotations.Add(annotation); y = bounds.Bottom; return(y); }
private float AddFileLinkAnnotation(PdfPageBase page, float y) { PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f)); PdfStringFormat format = new PdfStringFormat(); format.MeasureTrailingSpaces = true; String prompt = "Launch File: "; SizeF size = font.MeasureString(prompt); page.Canvas.DrawString(prompt, font, PdfBrushes.DodgerBlue, 0, y); float x = font.MeasureString(prompt, format).Width; String label = @"Launch Notepad.exe"; size = font.MeasureString(label); RectangleF bounds = new RectangleF(x, y, size.Width, size.Height); page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, x, y); PdfFileLinkAnnotation annotation = new PdfFileLinkAnnotation(bounds, @"C:\Windows\Notepad.exe"); annotation.Color = Color.Blue; (page as PdfNewPage).Annotations.Add(annotation); y = bounds.Bottom; return(y); }
private float AddLineAnnotation(PdfPageBase page, float y) { PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f)); PdfStringFormat format = new PdfStringFormat(); format.MeasureTrailingSpaces = true; String prompt = "Line Annotation: "; SizeF size = font.MeasureString(prompt); page.Canvas.DrawString(prompt, font, PdfBrushes.DodgerBlue, 0, y); float x = font.MeasureString(prompt, format).Width; String label = @"Line Anotation"; size = font.MeasureString(label); page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, x, y); RectangleF bounds = new RectangleF(x, y, size.Width, size.Height); int[] linePoints = new int[] { (int)bounds.Left, (int)bounds.Top, (int)bounds.Right, (int)bounds.Bottom }; PdfLineAnnotation annotation = new PdfLineAnnotation(linePoints, "Annotation"); annotation.BeginLineStyle = PdfLineEndingStyle.ClosedArrow; annotation.EndLineStyle = PdfLineEndingStyle.ClosedArrow; annotation.LineCaption = true; annotation.BackColor = Color.Black; annotation.CaptionType = PdfLineCaptionType.Inline; (page as PdfNewPage).Annotations.Add(annotation); y = bounds.Bottom; return(y); }
/// <summary> /// Add a text to PDF page and return the layout result. /// </summary> public PdfLayoutResult AddParagraph(PdfPage page, string text, RectangleF bounds, bool isTitle, bool mainTitle = false) { //Set string format, font size, style. PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Justify); PdfFontStyle style = PdfFontStyle.Regular; float size = 13; if (mainTitle) { format = new PdfStringFormat(PdfTextAlignment.Center); } if (isTitle) { size = 18; if (mainTitle) { size = 24; } } if (isTitle && !mainTitle) { style = PdfFontStyle.Bold; } //Create text element and draw it to PDF page. PdfTextElement element = new PdfTextElement(text, new PdfStandardFont(PdfFontFamily.Helvetica, size, style), PdfBrushes.Black); element.StringFormat = format; return(element.Draw(page, bounds)); }
public void DrawBandHead() { pPen.Width = 1; pdfGC.DrawLine(pPen, (float)BandTopLeftLocation.X, (float)BandTopLeftLocation.Y, (float)BandTopLeftLocation.X, (float)(BandTopLeftLocation.Y + BandHight)); pdfGC.DrawLine(pPen, (float)BandTopLeftLocation.X, (float)(BandTopLeftLocation.Y + BandHight), (float)(BandTopLeftLocation.X - BandHeadWidth), (float)(BandTopLeftLocation.Y + BandHight)); pdfGC.DrawLine(pPen, (float)(BandTopLeftLocation.X - BandHeadWidth), (float)(BandTopLeftLocation.Y + BandHight), (float)(BandTopLeftLocation.X - BandHeadWidth), (float)BandTopLeftLocation.Y); pdfGC.DrawLine(pPen, (float)(BandTopLeftLocation.X - BandHeadWidth), (float)BandTopLeftLocation.Y, (float)BandTopLeftLocation.X, (float)BandTopLeftLocation.Y); // RectangleF rF = new RectangleF((float)(BandTopLeftLocation.X - BandHeadWidth), (float)(BandTopLeftLocation.Y), (float)BandHeadWidth, (float)BandHight); RectangleF rF = new RectangleF(0, 0, (float)BandHight, (float)BandHeadWidth); PdfStringFormat psf = new PdfStringFormat(PdfStringFormat.GenericDefault); psf.Alignment = PdfStringAlignment.Center; psf.LineAlignment = PdfStringAlignment.Center; // pdfGC.DrawString(BandName, textfont, Brushes.Black, rF, psf); pdfGC.SaveGraphicsState(); // head box 的左下角 pdfGC.TranslateTransform((float)(BandTopLeftLocation.X - BandHeadWidth), (float)(BandTopLeftLocation.Y + BandHight)); pdfGC.RotateTransform(270); pdfGC.DrawString(BandName, headFont, new SolidBrush(Color.Black), rF, psf); pdfGC.RestoreGraphicsState(); }
private PdfPageTemplateElement CreateHeaderTemplate(PdfDocument doc, PdfMargins margins) { //get page size SizeF pageSize = doc.PageSettings.Size; //create a PdfPageTemplateElement object as header space PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Top); headerSpace.Foreground = false; //declare two float variables float x = margins.Left; float y = 0; //draw line in header space PdfPen pen = new PdfPen(PdfBrushes.Gray, 2); headerSpace.Graphics.DrawLine(pen, x, y + margins.Top - 2, pageSize.Width - x, y + margins.Top - 2); //draw text in header space PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Helvetica", 25f, FontStyle.Bold)); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center); String headerText = "SACRAMENT MEETING AGENDA"; SizeF size = font.MeasureString(headerText, format); headerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Gray, pageSize.Width - x - size.Width + 165, margins.Top - (size.Height + 5), format); //return headerSpace return(headerSpace); }
private void button1_Click(object sender, EventArgs e) { //Create a pdf document.<br> PdfDocument doc = new PdfDocument(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; // Create new page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); float y = 10; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Country List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Country List", format1).Height; y = y + 5; String[] data = { "Name;Capital;Continent;Area;Population", "Argentina;Buenos Aires;South America;2777815;32300003", "Bolivia;La Paz;South America;1098575;7300000", "Brazil;Brasilia;South America;8511196;150400000", "Canada;Ottawa;North America;9976147;26500000", }; String[][] dataSource = new String[data.Length][]; for (int i = 0; i < data.Length; i++) { dataSource[i] = data[i].Split(';'); } PdfTable table = new PdfTable(); table.Style.CellPadding = 2; table.Style.HeaderSource = PdfHeaderSource.Rows; table.Style.HeaderRowCount = 1; table.Style.ShowHeader = true; table.DataSource = dataSource; PdfLayoutResult result = table.Draw(page, new PointF(0, y)); y = y + result.Bounds.Height + 5; PdfBrush brush2 = PdfBrushes.Gray; PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f)); page.Canvas.DrawString(String.Format("* {0} countries in the list.", data.Length - 1), font2, brush2, 5, y); //Save pdf file. doc.SaveToFile("SimpleTable.pdf"); doc.Close(); System.Diagnostics.Process.Start("SimpleTable.pdf"); }
private void button3_Click(object sender, EventArgs e) { if (textBox2.Text != "") { PdfDocument doc = new PdfDocument(); PdfSection section = doc.Sections.Add(); PdfPageBase page = section.Pages.Add(); PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 11); PdfStringFormat format = new PdfStringFormat(); format.LineSpacing = 20f; PdfBrush brush = PdfBrushes.Black; PdfTextWidget textWidget = new PdfTextWidget(richTextBox1.Text, font, brush); float y = 0; PdfTextLayout textLayout = new PdfTextLayout(); textLayout.Break = PdfLayoutBreakType.FitPage; textLayout.Layout = PdfLayoutType.Paginate; RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize); textWidget.StringFormat = format; textWidget.Draw(page, bounds, textLayout); doc.SaveToFile("E:\\" + textBox2.Text + ".pdf", FileFormat.PDF); MessageBox.Show("Amal bajarildi", "Bajarildi", MessageBoxButtons.OK, MessageBoxIcon.Information); textBox2.Text = null; richTextBox1.Text = null; pictureBox1.Image = null; textBox1.Text = null; } }
/// <summary> /// Fired when Header and Footer are exported to PDF /// </summary> /// <param name="sender">PDF Export_HeaderAndFooterExporting sender</param> /// <param name="e">PDF Export_HeaderAndFooterExporting EventArgs e</param> private void PdfExport_HeaderAndFooterExporting(object sender, PdfHeaderFooterEventArgs e) { var width = e.PdfPage.GetClientSize().Width; PdfStandardFont font = null; PdfPageTemplateElement header = new PdfPageTemplateElement(width, 60); var assmbely = this.GetType().GetTypeInfo().Assembly; #if COMMONSB var imagestream = assmbely.GetManifestResourceStream("SampleBrowser.Icons.SyncfusionLogo.jpg"); #else var imagestream = assmbely.GetManifestResourceStream("SampleBrowser.SfDataGrid.Icons.SyncfusionLogo.jpg"); #endif PdfImage pdfImage = PdfImage.FromStream(imagestream); header.Graphics.DrawImage(pdfImage, new RectangleF(width - 148, 0, 148, 60)); if (Device.RuntimePlatform == Device.iOS) { font = new PdfStandardFont(PdfFontFamily.Helvetica, 18, PdfFontStyle.Bold); } else { font = new PdfStandardFont(PdfFontFamily.Helvetica, 13, PdfFontStyle.Bold); } PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left); header.Graphics.DrawString("Customer Details", font, PdfBrushes.Black, new RectangleF(0, 25, 200, 60), format); e.PdfDocumentTemplate.Top = header; }
//create the pdf footer void pdfConvertor_DrawPDFFooter(object sender, PDFHeaderFooterEventArgs e) { PdfPageTemplateElement footer = e.HeaderFooterTemplate; PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 8); PdfSolidBrush brush = new PdfSolidBrush(Color.Gray); PdfPen pen = new PdfPen(Color.DarkBlue, 3f); font = new PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Bold); PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Center; format.LineAlignment = PdfVerticalAlignment.Middle; footer.Graphics.DrawString("@Copyright 2008", font, brush, new RectangleF(0, footer.Height - 40, footer.Width, footer.Height), format); format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Right; format.LineAlignment = PdfVerticalAlignment.Bottom; //Create page number field PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush); //Create page count field PdfPageCountField count = new PdfPageCountField(font, brush); PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count); compositeField.Bounds = footer.Bounds; compositeField.Draw(footer.Graphics, new PointF(470, footer.Height - 10)); }
void GridPdfExportExtension_DrawPdfFooter(object sender, PdfHeaderFooterEventArgs e) { PdfPageTemplateElement footer = e.HeaderFooterTemplate; PdfSolidBrush brush = new PdfSolidBrush(new PdfColor(System.Drawing.Color.Gray)); PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 6, PdfFontStyle.Bold); PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Center; format.LineAlignment = PdfVerticalAlignment.Middle; footer.Graphics.DrawString("@Copyright 2020", font, brush, new System.Drawing.RectangleF(0, footer.Height - 40, footer.Width, footer.Height), format); format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Right; format.LineAlignment = PdfVerticalAlignment.Bottom; //Create page number field. PdfPageNumberField pageNumber = new PdfPageNumberField(font, brush); //Create page count field. PdfPageCountField count = new PdfPageCountField(font, brush); PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumber, count); compositeField.Bounds = footer.Bounds; compositeField.Draw(footer.Graphics, new System.Drawing.PointF(470, footer.Height - 10)); //header.Graphics.DrawImage(PdfImage.FromFile(@"pack:/application:,,,/syncfusion.gridcontroldemos.wpf;component/Assets/GridControl/Footer.png"), 0, 0, header.Width, header.Height); }
private static PdfPageTemplateElement CreateFooterTemplate(PdfDocument doc, PdfMargins margins) { SizeF pageSize = doc.PageSettings.Size; PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Bottom + margins.Top); footerSpace.Foreground = false; float x = margins.Left; float y = 0; DrawLine(footerSpace.Graphics, x, y, GetRightPage(), y); y += 5; PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left); string footerText = "This document was generated using computer software\nand is deemed to be as accurate as possible."; footerSpace.Graphics.DrawString(footerText, pageFont, PdfBrushes.Gray, x, y, format); PdfPageNumberField number = new PdfPageNumberField(); PdfPageCountField count = new PdfPageCountField(); PdfCompositeField compositeField = new PdfCompositeField(pageFont, PdfBrushes.Gray, "Page {0} of {1}", number, count); compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top); SizeF size = pageFont.MeasureString(compositeField.Text); compositeField.Bounds = new RectangleF(GetRightPage() - margins.Right - size.Width, y, size.Width, size.Height); compositeField.Draw(footerSpace.Graphics); return(footerSpace); }
private void generatePdf() { //Add a page _document.PageSettings.Margins.All = 0; _document.Pages.Add(); createHeader(); createFooter(); currentY = marginTop; addTitles(); //title & subtitle currentY += spaceLine; //testo PdfStringFormat stringFormat = new PdfStringFormat() { Alignment = PdfTextAlignment.Left }; PdfTextElement element = new PdfTextElement(Text, textFont, brushBlack); PdfLayoutFormat format = new PdfLayoutFormat(); format.Layout = PdfLayoutType.Paginate; format.PaginateBounds = new RectangleF(marginLeft, heightHeader + spaceDefault + spaceSubtitle, _document.PageSettings.Size.Width - marginRight - marginLeft, _document.PageSettings.Size.Height - marginTop * 3); element.Draw(_document.Pages [0], new RectangleF(marginLeft, currentY, _document.PageSettings.Width - marginRight - marginLeft, _document.PageSettings.Height - marginTop * 3)); }
private void SetSectionTemplate(PdfSection section, SizeF pageSize, PdfMargins margin, String label) { PdfPageTemplateElement leftSpace = new PdfPageTemplateElement(margin.Left, pageSize.Height); leftSpace.Foreground = true; section.Template.OddLeft = leftSpace; PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic)); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); float y = (pageSize.Height - margin.Top - margin.Bottom) * (1 - 0.618f); RectangleF bounds = new RectangleF(10, y, margin.Left - 20, font.Height + 6); leftSpace.Graphics.DrawRectangle(PdfBrushes.OrangeRed, bounds); leftSpace.Graphics.DrawString(label, font, PdfBrushes.White, bounds, format); PdfPageTemplateElement rightSpace = new PdfPageTemplateElement(margin.Right, pageSize.Height); rightSpace.Foreground = true; section.Template.EvenRight = rightSpace; bounds = new RectangleF(10, y, margin.Right - 20, font.Height + 6); rightSpace.Graphics.DrawRectangle(PdfBrushes.SaddleBrown, bounds); rightSpace.Graphics.DrawString(label, font, PdfBrushes.White, bounds, format); }
private float AddPopupAnnotation(PdfPageBase page, float y) { PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f)); PdfStringFormat format = new PdfStringFormat(); format.MeasureTrailingSpaces = true; String prompt = "Markup incorrect spelling: "; SizeF size = font.MeasureString(prompt, format); page.Canvas.DrawString(prompt, font, PdfBrushes.DodgerBlue, 0, y); float x = size.Width; String label = "demo of annotation"; page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, x, y); x = x + font.MeasureString(label, format).Width; String markupText = "All words were spelled correctly"; size = font.MeasureString(markupText); PdfPopupAnnotation annotation = new PdfPopupAnnotation(new RectangleF(new PointF(x, y), SizeF.Empty), markupText); annotation.Icon = PdfPopupIcon.Paragraph; annotation.Open = true; annotation.Color = Color.Yellow; (page as PdfNewPage).Annotations.Add(annotation); y = y + size.Height; return(y); }
private void button1_Click(object sender, EventArgs e) { //Load the Pdf from disk PdfDocument doc = new PdfDocument(); doc.LoadFromFile("../../../../../../../Data/MultipagePDF.pdf"); string header1 = "Header 1"; string header2 = "Header 2"; //Define style PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 15f, FontStyle.Bold)); PdfBrush brush = PdfBrushes.Red; RectangleF rect = new RectangleF(new PointF(0, 20), new SizeF(doc.PageSettings.Size.Width, 50f)); PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Center; doc.Pages[0].Canvas.DrawString(header1, font, brush, rect, format); font = new PdfTrueTypeFont(new Font("Aleo", 15f, FontStyle.Regular)); brush = PdfBrushes.Black; format.Alignment = PdfTextAlignment.Left; doc.Pages[1].Canvas.DrawString(header2, font, brush, rect, format); //Save the document string output = "AddingDifferentHeaders_result.pdf"; doc.SaveToFile(output, FileFormat.PDF); //Launch the Pdf PDFDocumentViewer(output); }
private void AlignTextInRectangle(PdfPageBase page) { //Draw the text - align in rectangle PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10f); PdfSolidBrush brush = new PdfSolidBrush(Color.Blue); RectangleF rctg1 = new RectangleF(0, 70, page.Canvas.ClientSize.Width / 2, 100); RectangleF rctg2 = new RectangleF(page.Canvas.ClientSize.Width / 2, 70, page.Canvas.ClientSize.Width / 2, 100); page.Canvas.DrawRectangle(new PdfSolidBrush(Color.LightBlue), rctg1); page.Canvas.DrawRectangle(new PdfSolidBrush(Color.LightSkyBlue), rctg2); PdfStringFormat leftAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Top); page.Canvas.DrawString("Left! Left!", font, brush, rctg1, leftAlignment); page.Canvas.DrawString("Left! Left!", font, brush, rctg2, leftAlignment); PdfStringFormat rightAlignment = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); page.Canvas.DrawString("Right! Right!", font, brush, rctg1, rightAlignment); page.Canvas.DrawString("Right! Right!", font, brush, rctg2, rightAlignment); PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Bottom); page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush, rctg1, centerAlignment); page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush, rctg2, centerAlignment); }
private void DrawPageHeaderAndFooter(PdfPageBase page, PdfMargins margin, bool isCover) { page.Canvas.SetTransparency(0.5f); PdfImage headerImage = PdfImage.FromFile(@"..\..\..\..\..\..\Data\Header.png"); PdfImage footerImage = PdfImage.FromFile(@"..\..\..\..\..\..\Data\Footer.png"); page.Canvas.DrawImage(headerImage, new PointF(0, 0)); page.Canvas.DrawImage(footerImage, new PointF(0, page.Canvas.ClientSize.Height - footerImage.PhysicalDimension.Height)); if (isCover) { page.Canvas.SetTransparency(1); return; } PdfBrush brush = PdfBrushes.Black; PdfPen pen = new PdfPen(brush, 0.75f); PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic), true); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right); format.MeasureTrailingSpaces = true; float space = font.Height * 0.75f; float x = margin.Left; float width = page.Canvas.ClientSize.Width - margin.Left - margin.Right; float y = margin.Top - space; page.Canvas.DrawLine(pen, x, y, x + width, y); y = y - 1 - font.Height; page.Canvas.DrawString("Demo of Spire.Pdf", font, brush, x + width, y, format); page.Canvas.SetTransparency(1); }
private static void GeneratePDF() { var text = File.ReadAllText("TestDocument.txt"); var doc = new PdfDocument(); var section = doc.Sections.Add(); var page = section.Pages.Add(); var font = new PdfFont(PdfFontFamily.Helvetica, 11); var format = new PdfStringFormat(); format.LineSpacing = 40f; var brush = PdfBrushes.Black; float y = 20; var textLayout = new PdfTextLayout(); textLayout.Break = PdfLayoutBreakType.FitPage; textLayout.Layout = PdfLayoutType.Paginate; var textWidget = new PdfTextWidget(text, font, brush); var bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize); textWidget.StringFormat = format; textWidget.Draw(page, bounds, textLayout); doc.SaveToFile("TxtToPDf.pdf", FileFormat.PDF); }
private float AddRubberStampAnnotation(PdfPageBase page, float y) { PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f)); PdfStringFormat format = new PdfStringFormat(); format.MeasureTrailingSpaces = true; String prompt = "Markup incorrect spelling: "; SizeF size = font.MeasureString(prompt, format); page.Canvas.DrawString(prompt, font, PdfBrushes.DodgerBlue, 0, y); float x = size.Width; String label = "demo of annotation"; page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, x, y); x = x + font.MeasureString(label, format).Width; String markupText = "Just a draft, not checked."; size = font.MeasureString(markupText); PdfRubberStampAnnotation annotation = new PdfRubberStampAnnotation(new RectangleF(x, y, font.Height, font.Height), markupText); annotation.Icon = PdfRubberStampAnnotationIcon.Draft; annotation.Color = Color.Plum; (page as PdfNewPage).Annotations.Add(annotation); y = y + size.Height; return(y); }
private void button1_Click(object sender, EventArgs e) { //Create a pdf document. PdfDocument doc = new PdfDocument(); // Create one section PdfSection section = doc.Sections.Add(); //Load image PdfImage image = PdfImage.FromFile(@"..\..\..\..\..\..\Data\SalesReportChart.png"); float imageWidth = image.PhysicalDimension.Width / 2; float imageHeight = image.PhysicalDimension.Height / 2; foreach (PdfBlendMode mode in Enum.GetValues(typeof(PdfBlendMode))) { PdfPageBase page = section.Pages.Add(); float pageWidth = page.Canvas.ClientSize.Width; float y = 0; //title y = y + 5; PdfBrush brush = new PdfSolidBrush(Color.OrangeRed); PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Bold)); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center); String text = String.Format("Transparency Blend Mode: {0}", mode); page.Canvas.DrawString(text, font, brush, pageWidth / 2, y, format); SizeF size = font.MeasureString(text, format); y = y + size.Height + 6; page.Canvas.DrawImage(image, 0, y, imageWidth, imageHeight); page.Canvas.Save(); float d = (page.Canvas.ClientSize.Width - imageWidth) / 5; float x = d; y = y + d / 2; for (int i = 0; i < 5; i++) { float alpha = 1.0f / 6 * (5 - i); page.Canvas.SetTransparency(alpha, alpha, mode); page.Canvas.DrawImage(image, x, y, imageWidth, imageHeight); x = x + d; y = y + d / 2; } page.Canvas.Restore(); } //Save pdf file. doc.SaveToFile("Transparency.pdf"); doc.Close(); //Launching the Pdf file. PDFDocumentViewer("Transparency.pdf"); }
private void DrawPage(PdfPageBase page) { float y = 10; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Category Sales by Year", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Category Sales by Year", format1).Height; y = y + 5; String[][] data = { new String[]{"Category Name", "1994 Sale Amount", "1995 Sale Amount", "1996 Sale Amount"}, new String[]{"Beverages", "38,487.20", "102,479.46", "126,901.53"}, new String[]{"Condiments", "16,402.95", "51,041.83", "38,602.31"}, new String[]{"Confections", "23,812.90", "79,752.25", "63,792.07"}, new String[]{"Dairy Products", "30,027.79", "116,495.45", "87,984.05"}, new String[]{"Grains/Cereals", "7,313.92", "53,823.48", "34,607.19"}, new String[]{"Meat/Poultry", "19,856.86", "77,164.75", "66,000.75"}, new String[]{"Produce", "10,694.96", "45,973.69", "43,315.93"}, new String[]{"Seafood", "16,247.77", "64,195.51", "50,818.46"} }; PdfTable table = new PdfTable(); table.Style.CellPadding = 2; table.Style.BorderPen = new PdfPen(brush1, 0.75f); table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.SkyBlue; table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f)); table.Style.HeaderSource = PdfHeaderSource.Rows; table.Style.HeaderRowCount = 1; table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue; table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold)); table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center); table.Style.ShowHeader = true; table.DataSource = data; PdfLayoutResult result = table.Draw(page, new PointF(0, y)); y = y + result.Bounds.Height + 5; PdfBrush brush2 = PdfBrushes.LightGray; PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f)); page.Canvas.DrawString("* All data from NorthWind", font2, brush2, 5, y); }
private void AlignText(PdfPageBase page) { //Draw the text - alignment PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 20f); PdfSolidBrush brush = new PdfSolidBrush(Color.Blue); PdfStringFormat leftAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); page.Canvas.DrawString("Left!", font, brush, 0, 20, leftAlignment); page.Canvas.DrawString("Left!", font, brush, 0, 50, leftAlignment); PdfStringFormat rightAlignment = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); page.Canvas.DrawString("Right!", font, brush, page.Canvas.ClientSize.Width, 30, rightAlignment); page.Canvas.DrawString("Right!", font, brush, page.Canvas.ClientSize.Width, 60, rightAlignment); PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush, page.Canvas.ClientSize.Width / 2, 40, centerAlignment); }
private void TransformText(PdfPageBase page) { //save graphics state PdfGraphicsState state = page.Canvas.Save(); //Draw the text - transform PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 18f); PdfSolidBrush brush1 = new PdfSolidBrush(Color.Blue); PdfSolidBrush brush2 = new PdfSolidBrush(Color.CadetBlue); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.TranslateTransform(page.Canvas.ClientSize.Width / 2, 20); page.Canvas.DrawString("Sales Report Chart", font, brush1, 0, 0, format); page.Canvas.ScaleTransform(1f, -0.8f); page.Canvas.DrawString("Sales Report Chart", font, brush2, 0, -2 * 18 * 1.2f, format); //restor graphics page.Canvas.Restore(state); }
private void DrawAutomaticField(PdfPageBase page) { float y = 0; //title PdfBrush brush1 = PdfBrushes.CadetBlue; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Automatic Field List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Automatic Field List", format1).Height; y = y + 5; String[] fieldList = new String[] { "DateTimeField", "CreationDateField", "DocumentAuthorField", "SectionNumberField", "SectionPageNumberField", "SectionPageCountField", "PageNumberField", "PageCountField", "DestinationPageNumberField", "CompositeField" }; PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f)); PdfStringFormat fieldNameFormat = new PdfStringFormat(); fieldNameFormat.MeasureTrailingSpaces = true; foreach (String fieldName in fieldList) { //draw field name String text = String.Format("{0}: ", fieldName); page.Canvas.DrawString(text, font, PdfBrushes.DodgerBlue, 0, y); float x = font.MeasureString(text, fieldNameFormat).Width; RectangleF bounds = new RectangleF(x, y, 200, font.Height); DrawAutomaticField(fieldName, page, bounds); y = y + font.Height + 3; } }
public static PdfDocument PrintCappReport(string username) { if (EntitiesHelper.GetSessionId(username) == -1) return null; var userData = UserHelper.GetApplicationUser(username); var courseData = CourseHelper.GetCappReport(username); var pdf = new PdfDocument(); var cappSection = pdf.Sections.Add(); var page = cappSection.Pages.Add(); var font = new PdfFont(PdfFontFamily.TimesRoman, 11); var format = new PdfStringFormat { LineSpacing = 20f }; var brush = PdfBrushes.Black; var stringData = "\n"; stringData += userData.Username + "\t" + userData.Major + "\n"; stringData = userData.Advisors.Aggregate(stringData, (current, adivsor) => current + (adivsor.Name + ": " + adivsor.Email + "\n")); stringData += "\n\n"; foreach (var reqSet in courseData.RequirementSets) { stringData += reqSet.Name + "\n"; stringData = reqSet.AppliedCourses.Aggregate(stringData, (current, course) => current + (course.DepartmentCode + "-" + course.CourseNumber + "\n")); stringData += "\n"; } // put user info at the top // aggragate through all of them // put requirementset name in the header // list all of the classes applied to it var textWidget = new PdfTextWidget(stringData, font, brush); var textLayout = new PdfTextLayout { Break = PdfLayoutBreakType.FitPage, Layout = PdfLayoutType.Paginate }; var bounds = new RectangleF(new Point(0, 0), page.Canvas.ClientSize); textWidget.StringFormat = format; textWidget.Draw(page, bounds, textLayout); return pdf; }
private void button1_Click(object sender, EventArgs e) { //Create a pdf document. PdfDocument doc = new PdfDocument(); // Create one page PdfPageBase page = doc.Pages.Add(); //Draw the text float l = page.Canvas.ClientSize.Width / 2; PointF center = new PointF(l, l); float r = (float)Math.Sqrt(2 * l * l); PdfRadialGradientBrush brush = new PdfRadialGradientBrush(center, 0f, center, r, Color.Blue, Color.Red); PdfFontFamily[] fontFamilies = (PdfFontFamily[])Enum.GetValues(typeof(PdfFontFamily)); float y = 10; for(int i = 0; i < fontFamilies.Length; i++) { String text = String.Format("Font Family: {0}", fontFamilies[i]); float x1 = 0; y = 10 + i * 16; PdfFont font1 = new PdfFont(PdfFontFamily.Courier, 14f); PdfFont font2 = new PdfFont(fontFamilies[i], 14f); float x2 = x1 + 10 + font1.MeasureString(text).Width; page.Canvas.DrawString(text, font1, brush, x1, y); page.Canvas.DrawString(text, font2, brush, x2, y); } //true type font - embedded. System.Drawing.Font font = new System.Drawing.Font("Arial", 14f, FontStyle.Bold); PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font); page.Canvas.DrawString("Font Family: Arial - Embedded", trueTypeFont, brush, 0, (y = y + 16f)); //right to left String arabicText = "\u0627\u0644\u0630\u0647\u0627\u0628\u0021\u0020" + "\u0628\u062F\u0648\u0631\u0647\u0020\u062D\u0648\u0644\u0647\u0627\u0021\u0020" + "\u0627\u0644\u0630\u0647\u0627\u0628\u0021\u0020" + "\u0627\u0644\u0630\u0647\u0627\u0628\u0021\u0020" + "\u0627\u0644\u0630\u0647\u0627\u0628\u0021"; trueTypeFont = new PdfTrueTypeFont(font, true); RectangleF rctg = new RectangleF(new PointF(0, (y = y + 16f)), page.Canvas.ClientSize); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right); format.RightToLeft = true; page.Canvas.DrawString(arabicText, trueTypeFont, brush, rctg, format); //true type font - not embedded font = new System.Drawing.Font("Batang", 14f, FontStyle.Italic); trueTypeFont = new PdfTrueTypeFont(font); page.Canvas.DrawString("Font Family: Batang - Not Embedded", trueTypeFont, brush, 0, (y = y + 16f)); //font file String fontFileName = @"..\..\..\..\..\..\Data\Hawaii_Killer.ttf"; trueTypeFont = new PdfTrueTypeFont(fontFileName, 20f); page.Canvas.DrawString("Hawaii Killer Font", trueTypeFont, brush, 0, (y = y + 16f)); page.Canvas.DrawString("Hawaii Killer Font, from http://www.1001freefonts.com", new PdfFont(PdfFontFamily.Helvetica, 8f), brush, 10, (y = y + 20f)); //cjk font PdfCjkStandardFont cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.MonotypeHeiMedium, 14f); page.Canvas.DrawString("How to say 'Font' in Chinese? \u5B57\u4F53", cjkFont, brush, 0, (y = y + 16f)); cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsGothicMedium, 14f); page.Canvas.DrawString("How to say 'Font' in Japanese? \u30D5\u30A9\u30F3\u30C8", cjkFont, brush, 0, (y = y + 16f)); cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsShinMyeongJoMedium, 14f); page.Canvas.DrawString("How to say 'Font' in Korean? \uAE00\uAF34", cjkFont, brush, 0, (y = y + 16f)); //Save pdf file. doc.SaveToFile("Font.pdf"); doc.Close(); //Launching the Pdf file. PDFDocumentViewer("Font.pdf"); }
private void button1_Click(object sender, EventArgs e) { //Create a pdf document. PdfDocument doc = new PdfDocument(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; // Create one page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); float y = 10; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold), true); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Categories List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Categories List", format1).Height; y = y + 5; RectangleF rctg = new RectangleF(new PointF(0, 0), page.Canvas.ClientSize); PdfLinearGradientBrush brush2 = new PdfLinearGradientBrush(rctg, Color.Navy, Color.OrangeRed, PdfLinearGradientMode.Vertical); PdfLinearGradientBrush brush3 = new PdfLinearGradientBrush(rctg, Color.OrangeRed, Color.Navy, PdfLinearGradientMode.Vertical); PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 10f), true); PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 8f), true); PdfOrderedMarker marker1 = new PdfOrderedMarker(PdfNumberStyle.LowerRoman, new PdfFont(PdfFontFamily.Helvetica, 10f)); PdfOrderedMarker marker2 = new PdfOrderedMarker(PdfNumberStyle.Numeric, new PdfFont(PdfFontFamily.Helvetica, 8f)); PdfSortedList vendorList = new PdfSortedList(font2); vendorList.Indent = 0; vendorList.TextIndent = 5; vendorList.Brush = brush2; vendorList.Marker = marker1; using (OleDbConnection conn = new OleDbConnection()) { conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\..\..\Data\demo.mdb"; OleDbCommand command = new OleDbCommand(); command.CommandText = " select VendorNo, VendorName from vendors "; command.Connection = conn; OleDbCommand command2 = new OleDbCommand(); command2.CommandText = " select Description from parts where VendorNo = @VendorNo"; command2.Connection = conn; OleDbParameter param = new OleDbParameter("@VendorNo", OleDbType.Double); command2.Parameters.Add(param); conn.Open(); OleDbDataReader reader = command.ExecuteReader(); while (reader.Read()) { double id = reader.GetDouble(0); PdfListItem item = vendorList.Items.Add(reader.GetString(1)); PdfSortedList subList = new PdfSortedList(font3); subList.Marker = marker2; subList.Brush = brush3; item.SubList = subList; subList.TextIndent = 20; command2.Parameters[0].Value = id; using (OleDbDataReader reader2 = command2.ExecuteReader()) { while (reader2.Read()) { subList.Items.Add(reader2.GetString(0)); } } String maxNumberLabel = Convert.ToString(subList.Items.Count); subList.Indent = 30 - font3.MeasureString(maxNumberLabel).Width; } } PdfTextLayout textLayout = new PdfTextLayout(); textLayout.Break = PdfLayoutBreakType.FitPage; textLayout.Layout = PdfLayoutType.Paginate; vendorList.Draw(page, new PointF(0, y), textLayout); //Save pdf file. doc.SaveToFile("List.pdf"); doc.Close(); //Launching the Pdf file. PDFDocumentViewer("List.pdf"); }
private float DrawFormSection(String label, PdfPageBase page, float y) { PdfBrush brush1 = PdfBrushes.LightYellow; PdfBrush brush2 = PdfBrushes.DarkSlateGray; PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Bold)); PdfStringFormat format = new PdfStringFormat(); float height = font.MeasureString(label).Height; page.Canvas.DrawRectangle(brush2, 0, y, page.Canvas.ClientSize.Width, height + 2); page.Canvas.DrawString(label, font, brush1, 2, y + 1); y = y + height + 2; PdfPen pen = new PdfPen(PdfBrushes.LightSkyBlue, 0.75f); page.Canvas.DrawLine(pen, 0, y, page.Canvas.ClientSize.Width, y); return y + 0.75f; }
private void RotateText(PdfPageBase page) { //save graphics state PdfGraphicsState state = page.Canvas.Save(); //Draw the text - transform PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10f); PdfSolidBrush brush = new PdfSolidBrush(Color.Blue); PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); float x = page.Canvas.ClientSize.Width / 2; float y = 380; page.Canvas.TranslateTransform(x, y); for (int i = 0; i < 12; i++) { page.Canvas.RotateTransform(30); page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush, 20, 0, centerAlignment); } //restor graphics page.Canvas.Restore(state); }
private float DrawFormField(XPathNavigator fieldNode, PdfForm form, PdfPageBase page, float y, int fieldIndex) { float width = page.Canvas.ClientSize.Width; float padding = 2; //measure field label String label = fieldNode.GetAttribute("label", ""); PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 9f)); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); float labelMaxWidth = width * 0.4f - 2 * padding; SizeF labelSize = font1.MeasureString(label, labelMaxWidth, format); //measure field height float fieldHeight = MeasureFieldHeight(fieldNode); float height = labelSize.Height > fieldHeight ? labelSize.Height : fieldHeight; height = height + 2; //draw background PdfBrush brush = PdfBrushes.SteelBlue; if (fieldIndex % 2 == 1) { brush = PdfBrushes.LightGreen; } page.Canvas.DrawRectangle(brush, 0, y, width, height); //draw field label PdfBrush brush1 = PdfBrushes.LightYellow; RectangleF labelBounds = new RectangleF(padding, y, labelMaxWidth, height); page.Canvas.DrawString(label, font1, brush1, labelBounds, format); //daw field float fieldMaxWidth = width * 0.57f - 2 * padding; float fieldX = labelBounds.Right + 2 * padding; float fieldY = y + (height - fieldHeight) / 2; String fieldType = fieldNode.GetAttribute("type", ""); String fieldId = fieldNode.GetAttribute("id", ""); bool required = "true" == fieldNode.GetAttribute("required", ""); switch (fieldType) { case "text": case "password": PdfTextBoxField textField = new PdfTextBoxField(page, fieldId); textField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight); textField.BorderWidth = 0.75f; textField.BorderStyle = PdfBorderStyle.Solid; textField.Required = required; if ("password" == fieldType) { textField.Password = true; } if ("true" == fieldNode.GetAttribute("multiple", "")) { textField.Multiline = true; textField.Scrollable = true; } form.Fields.Add(textField); break; case "checkbox": PdfCheckBoxField checkboxField = new PdfCheckBoxField(page, fieldId); float checkboxWidth = fieldHeight - 2 * padding; float checkboxHeight = checkboxWidth; checkboxField.Bounds = new RectangleF(fieldX, fieldY + padding, checkboxWidth, checkboxHeight); checkboxField.BorderWidth = 0.75f; checkboxField.Style = PdfCheckBoxStyle.Cross; checkboxField.Required = required; form.Fields.Add(checkboxField); break; case "list": XPathNodeIterator itemNodes = fieldNode.Select("item"); if ("true" == fieldNode.GetAttribute("multiple", "")) { PdfListBoxField listBoxField = new PdfListBoxField(page, fieldId); listBoxField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight); listBoxField.BorderWidth = 0.75f; listBoxField.MultiSelect = true; listBoxField.Font = new PdfFont(PdfFontFamily.Helvetica, 9f); listBoxField.Required = required; //add items into list box. foreach (XPathNavigator itemNode in itemNodes) { String text = itemNode.SelectSingleNode("text()").Value; listBoxField.Items.Add(new PdfListFieldItem(text, text)); } listBoxField.SelectedIndex = 0; form.Fields.Add(listBoxField); break; } if (itemNodes != null && itemNodes.Count <= 3) { PdfRadioButtonListField radioButtonListFile = new PdfRadioButtonListField(page, fieldId); radioButtonListFile.Required = required; //add items into radio button list. float fieldItemHeight = fieldHeight / itemNodes.Count; float radioButtonWidth = fieldItemHeight - 2 * padding; float radioButtonHeight = radioButtonWidth; foreach (XPathNavigator itemNode in itemNodes) { String text = itemNode.SelectSingleNode("text()").Value; PdfRadioButtonListItem fieldItem = new PdfRadioButtonListItem(text); fieldItem.BorderWidth = 0.75f; fieldItem.Bounds = new RectangleF(fieldX, fieldY + padding, radioButtonWidth, radioButtonHeight); radioButtonListFile.Items.Add(fieldItem); float fieldItemLabelX = fieldX + radioButtonWidth + padding; SizeF fieldItemLabelSize = font1.MeasureString(text); float fieldItemLabelY = fieldY + (fieldItemHeight - fieldItemLabelSize.Height) / 2; page.Canvas.DrawString(text, font1, brush1, fieldItemLabelX, fieldItemLabelY); fieldY = fieldY + fieldItemHeight; } form.Fields.Add(radioButtonListFile); break; } //combo box PdfComboBoxField comboBoxField = new PdfComboBoxField(page, fieldId); comboBoxField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight); comboBoxField.BorderWidth = 0.75f; comboBoxField.Font = new PdfFont(PdfFontFamily.Helvetica, 9f); comboBoxField.Required = required; //add items into combo box. foreach (XPathNavigator itemNode in itemNodes) { String text = itemNode.SelectSingleNode("text()").Value; comboBoxField.Items.Add(new PdfListFieldItem(text, text)); } form.Fields.Add(comboBoxField); break; } if (required) { //draw * float flagX = width * 0.97f + padding; PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold)); SizeF size = font3.MeasureString("*"); float flagY = y + (height - size.Height) / 2; page.Canvas.DrawString("*", font3, PdfBrushes.Red, flagX, flagY); } return y + height; }
private void DrawPage(PdfPageBase page) { float pageWidth = page.Canvas.ClientSize.Width; float y = 0; //page header PdfPen pen1 = new PdfPen(Color.LightGray, 1f); PdfBrush brush1 = new PdfSolidBrush(Color.LightGray); PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Italic)); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Right); String text = "Demo of Spire.Pdf"; page.Canvas.DrawString(text, font1, brush1, pageWidth, y, format1); SizeF size = font1.MeasureString(text, format1); y = y + size.Height + 1; page.Canvas.DrawLine(pen1, 0, y, pageWidth, y); //title y = y + 5; PdfBrush brush2 = new PdfSolidBrush(Color.Black); PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format2 = new PdfStringFormat(PdfTextAlignment.Center); format2.CharacterSpacing = 1f; text = "Summary of Science"; page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2); size = font2.MeasureString(text, format2); y = y + size.Height + 6; //icon PdfImage image = PdfImage.FromFile(@"..\..\..\..\..\..\Data\Wikipedia_Science.png"); page.Canvas.DrawImage(image, new PointF(pageWidth - image.PhysicalDimension.Width, y)); float imageLeftSpace = pageWidth - image.PhysicalDimension.Width - 2; float imageBottom = image.PhysicalDimension.Height + y; //refenrence content PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 9f)); PdfStringFormat format3 = new PdfStringFormat(); format3.ParagraphIndent = font3.Size * 2; format3.MeasureTrailingSpaces = true; format3.LineSpacing = font3.Size * 1.5f; String text1 = "(All text and picture from "; String text2 = "Wikipedia"; String text3 = ", the free encyclopedia)"; page.Canvas.DrawString(text1, font3, brush2, 0, y, format3); size = font3.MeasureString(text1, format3); float x1 = size.Width; format3.ParagraphIndent = 0; PdfTrueTypeFont font4 = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Underline)); PdfBrush brush3 = PdfBrushes.Blue; page.Canvas.DrawString(text2, font4, brush3, x1, y, format3); size = font4.MeasureString(text2, format3); x1 = x1 + size.Width; page.Canvas.DrawString(text3, font3, brush2, x1, y, format3); y = y + size.Height; //content PdfStringFormat format4 = new PdfStringFormat(); text = System.IO.File.ReadAllText(@"..\..\..\..\..\..\Data\Summary_of_Science.txt"); PdfTrueTypeFont font5 = new PdfTrueTypeFont(new Font("Arial", 10f)); format4.LineSpacing = font5.Size * 1.5f; PdfStringLayouter textLayouter = new PdfStringLayouter(); float imageLeftBlockHeight = imageBottom - y; PdfStringLayoutResult result = textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight)); if (result.ActualSize.Height < imageBottom - y) { imageLeftBlockHeight = imageLeftBlockHeight + result.LineHeight; result = textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight)); } foreach (LineInfo line in result.Lines) { page.Canvas.DrawString(line.Text, font5, brush2, 0, y, format4); y = y + result.LineHeight; } PdfTextWidget textWidget = new PdfTextWidget(result.Remainder, font5, brush2); PdfTextLayout textLayout = new PdfTextLayout(); textLayout.Break = PdfLayoutBreakType.FitPage; textLayout.Layout = PdfLayoutType.Paginate; RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize); textWidget.StringFormat = format4; textWidget.Draw(page, bounds, textLayout); }
private void btnRun_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(ofdSingleFile.SafeFileName)) { MessageBox.Show("Please select a pdf file"); return; } btnRun.Enabled = false; Task.Factory.StartNew(() => { var filePath = ofdSingleFile.FileName; var helper = new PdfHelper(); var pdf = new PdfDocument(filePath); var pdfTemp = new PdfDocument(); var docWillSave = new PdfDocument(); var totalPage = pdf.Pages.Count; pbSplitProgress.Maximum = totalPage; var curPage = 0; foreach (PdfPageBase itemPage in pdf.Pages) { curPage++; #region Lable one var pageOrigin = itemPage; var template = pageOrigin.CreateTemplate(); var pageTemp = pdfTemp.Pages.Add(PdfHelper.SizeF4x6, new PdfMargins(0)); pageTemp.Canvas.DrawTemplate(template, new PointF(38, 9)); var templateNew = pageTemp.CreateTemplate(); var page = docWillSave.Pages.Add(PdfHelper.SizeF4x6, new PdfMargins(0)); page.Canvas.DrawTemplate(templateNew, new PointF(-40, -9)); PdfGraphicsState state = page.Canvas.Save(); PdfStringFormat leftAlignment = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 20f); PdfSolidBrush brush = new PdfSolidBrush(Color.Blue); page.Canvas.RotateTransform(-90); page.Canvas.DrawString(txtText.Text, font, brush, new PointF(-250, 25), leftAlignment); // page.Canvas.Restore(state); pdfTemp.Pages.RemoveAt(0); #endregion #region Lable two var ListImage = itemPage.ExtractImages(); #region Second Page PdfPageBase page2 = docWillSave.Pages.Add(new SizeF(288, 432), new PdfMargins(0)); ListImage[1].RotateFlip(RotateFlipType.Rotate90FlipNone); #region cut image System.Drawing.Rectangle cropArea = new System.Drawing.Rectangle(0, 0, 800, 1204); var img = ListImage[1]; System.Drawing.Bitmap bmpImage = new System.Drawing.Bitmap(img); System.Drawing.Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat); #endregion PdfImage image = PdfImage.FromImage(bmpCrop); page2.Canvas.DrawImage(image, 0, 0, page2.Canvas.ClientSize.Width, page2.Canvas.ClientSize.Height); #endregion #endregion pbSplitProgress.Value = curPage ; } var newFile = filePath.Remove(filePath.LastIndexOf('.'), 4) + "_cut" + DateTime.Now.ToFileTime() + ".pdf"; docWillSave.SaveToFile(newFile); docWillSave.Close(); lblMsg.Text = "File has been save as " + newFile; System.Diagnostics.Process.Start(newFile); }); }
private void button1_Click(object sender, EventArgs e) { //Create a pdf document. PdfDocument doc = new PdfDocument(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; // Create one page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); float y = 10; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Part List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Part List", format1).Height; y = y + 5; //create data table PdfTable table = new PdfTable(); table.Style.CellPadding = 1; table.Style.BorderPen = new PdfPen(brush1, 0.75f); table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.SkyBlue; table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f), true); table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions; table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue; table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold), true); table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center); table.Style.ShowHeader = true; table.Style.RepeatHeader = true; using (OleDbConnection conn = new OleDbConnection()) { conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\..\..\Data\demo.mdb"; OleDbCommand command = new OleDbCommand(); command.CommandText = " select * from parts "; command.Connection = conn; using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command)) { DataTable dataTable = new DataTable(); dataAdapter.Fill(dataTable); dataTable.Columns.RemoveAt(1); table.DataSourceType = PdfTableDataSourceType.TableDirect; table.DataSource = dataTable; } } float width = page.Canvas.ClientSize.Width - (table.Columns.Count + 1) * table.Style.BorderPen.Width; for(int i = 0; i < table.Columns.Count; i++) { if (i == 1) { table.Columns[i].Width = width * 0.4f * width; table.Columns[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else { table.Columns[i].Width = width * 0.12f * width; table.Columns[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } } table.BeginRowLayout += new BeginRowLayoutEventHandler(table_BeginRowLayout); PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat(); tableLayout.Break = PdfLayoutBreakType.FitElement; tableLayout.Layout = PdfLayoutType.Paginate; PdfLayoutResult result = table.Draw(page, new PointF(0, y), tableLayout); y = result.Bounds.Bottom + 5; PdfBrush brush2 = PdfBrushes.Gray; PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f)); result.Page.Canvas.DrawString(String.Format("* All {0} parts in the list", table.Rows.Count), font2, brush2, 5, y); //Save pdf file. doc.SaveToFile("TableLayout.pdf"); doc.Close(); //Launching the Pdf file. PDFDocumentViewer("TableLayout.pdf"); }
private void button1_Click(object sender, EventArgs e) { //Create a pdf document. PdfDocument doc = new PdfDocument(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; // Create one page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); float y = 10; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Country List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Country List", format1).Height; y = y + 5; //create data table PdfTable table = new PdfTable(); table.Style.CellPadding = 2; table.Style.BorderPen = new PdfPen(brush1, 0.75f); table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.SkyBlue; table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f)); table.Style.AlternateStyle = new PdfCellStyle(); table.Style.AlternateStyle.BackgroundBrush = PdfBrushes.LightYellow; table.Style.AlternateStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f)); table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions; table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue; table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold)); table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center); table.Style.ShowHeader = true; using (OleDbConnection conn = new OleDbConnection()) { conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\..\..\Data\demo.mdb"; OleDbCommand command = new OleDbCommand(); command.CommandText = " select Name,Capital,Continent,Area,Population from country "; command.Connection = conn; using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command)) { DataTable dataTable = new DataTable(); dataAdapter.Fill(dataTable); table.DataSourceType = PdfTableDataSourceType.TableDirect; table.DataSource = dataTable; } } float width = page.Canvas.ClientSize.Width - (table.Columns.Count + 1) * table.Style.BorderPen.Width; table.Columns[0].Width = width * 0.24f * width; table.Columns[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); table.Columns[1].Width = width * 0.21f * width; table.Columns[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); table.Columns[2].Width = width * 0.24f * width; table.Columns[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); table.Columns[3].Width = width * 0.13f * width; table.Columns[3].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); table.Columns[4].Width = width * 0.18f * width; table.Columns[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); PdfLayoutResult result = table.Draw(page, new PointF(0, y)); y = y + result.Bounds.Height + 5; PdfBrush brush2 = PdfBrushes.Gray; PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f)); page.Canvas.DrawString(String.Format("* {0} countries in the list.", table.Rows.Count), font2, brush2, 5, y); //Save pdf file. doc.SaveToFile("DataSource.pdf"); doc.Close(); //Launching the Pdf file. PDFDocumentViewer("DataSource.pdf"); }
static void addressTable_BeginRowLayout(object sender, BeginRowLayoutEventArgs args) { PdfFont helv12 = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold); PdfFont helv11 = new PdfFont(PdfFontFamily.Helvetica, 11f); PdfStringFormat centered = new PdfStringFormat(PdfTextAlignment.Center); if (args.RowIndex == 0) { //header args.CellStyle.Font = helv12; args.CellStyle.StringFormat = centered; args.CellStyle.BackgroundBrush = PdfBrushes.LightGray; } else { args.CellStyle.Font = helv11; args.CellStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Left); args.CellStyle.BackgroundBrush = PdfBrushes.White; } }
public FileResult Download(int year, int id) { //Create a pdf document.<br> PdfDocument doc = new PdfDocument(); Participant data = TheRace.Historical(year).Participants.Where(p => p.Startnumber == id).First(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(1f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(1f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; // Create new page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); float y = 10; //title PdfBrush brush1 = PdfBrushes.Black; PdfFont font32b = new PdfFont(PdfFontFamily.Helvetica, 40f, PdfFontStyle.Bold); ; PdfFont font32 = new PdfFont(PdfFontFamily.TimesRoman, 32f, PdfFontStyle.Regular); PdfFont font20b = new PdfFont(PdfFontFamily.TimesRoman, 20f, PdfFontStyle.Bold); PdfFont font20 = new PdfFont(PdfFontFamily.TimesRoman, 18f, PdfFontStyle.Regular); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Left); format1.WordWrap = PdfWordWrapType.Word; //Draw the image PdfImage image = PdfImage.FromFile(Server.MapPath("~/Content/Images/price.jpg")); float width = image.Width; float height = image.Height; float x = page.Canvas.ClientSize.Width - width; page.Canvas.DrawImage(image, x, y, width, height); page.Canvas.DrawString(data.Splits(data.Classes[0].Id).Last().Position.ToString() + ".", font32b, brush1, x + 60, y + 50, format1); y = image.Height; page.Canvas.DrawString(data.Name, font32b, brush1, 0, y, format1); y = y + font32b.MeasureString(data.Name, format1).Height + 15; foreach (ParticipantClass c in data.Classes.Where(c => c.Official)) { StringBuilder sb = new StringBuilder(c.Name); sb.Append(" - "); var res = data.Leg(c.Id, 248); if (res != null) sb.Append(data.Leg(c.Id, 248).Position); sb.Append(".plass"); page.Canvas.DrawString(sb.ToString(), font20, brush1, 0, y, format1); y += font20.MeasureString(sb.ToString(), format1).Height + 15; } List<String[]> splits = data.Splits(data.Classes[0].Id).Select(p => new String[] { p.Leg, p.IsSuper ? "" : p.Name, p.Time }).ToList<String[]>(); if (splits.Count() > 0) { splits.Add(new String[] { "Totaltid", "", data.TotalTime }); y = y + 30; page.Canvas.DrawString("Etappetider", font20b, brush1, 0, y, format1); y += font20b.MeasureString("Etappetider", format1).Height + 5; PdfTable table = new PdfTable(); table.Style.BorderPen = new PdfPen(Color.Transparent); table.Style.DefaultStyle.TextBrush = brush1; table.Style.DefaultStyle.Font = font20; table.Style.DefaultStyle.BorderPen = new PdfPen(Color.Transparent); table.Style.CellPadding = 2; table.Style.HeaderSource = PdfHeaderSource.Rows; table.Style.HeaderRowCount = 0; table.Style.ShowHeader = false; table.Style.AlternateStyle = new PdfCellStyle(); table.Style.AlternateStyle.TextBrush = brush1; table.Style.AlternateStyle.Font = font20; table.Style.AlternateStyle.BackgroundBrush = PdfBrushes.LightGray; table.Style.AlternateStyle.BorderPen = new PdfPen(Color.Transparent); table.DataSource = splits.ToArray<String[]>(); PdfLayoutResult result = table.Draw(page, new PointF(0, y)); y = y + result.Bounds.Height + 5; } StringBuilder s = new StringBuilder("Start # "); s.Append(data.Startnumber.ToString()); s.Append(" Emit # "); s.Append(data.EmitID.ToString()); y += 50; page.Canvas.DrawString(s.ToString(), font20, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = page.Canvas.ClientSize.Height - 60; image = PdfImage.FromFile(Server.MapPath("~/Content/Images/kjeringi2016_logo-2.png")); width = image.Width; height = image.Height; x = page.Canvas.ClientSize.Width - width; page.Canvas.DrawImage(image, 0, y, width, height); image = PdfImage.FromFile(Server.MapPath("~/Content/Images/difi-logo.png")); width = image.Width; height = image.Height; x = page.Canvas.ClientSize.Width - width; page.Canvas.DrawImage(image, x, y, width, height); doc.SaveToFile(@"c:\temp\KjeringiOpen-" + year.ToString() + "_" + id.ToString() + ".pdf"); return File(@"c:\temp\KjeringiOpen-" + year.ToString() + "_" + id.ToString() + ".pdf", "application/pdf"); }
private void button1_Click(object sender, EventArgs e) { //Create a pdf document. PdfDocument doc = new PdfDocument(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; //create section PdfSection section = doc.Sections.Add(); section.PageSettings.Size = PdfPageSize.A4; section.PageSettings.Margins = margin; // Create one page PdfPageBase page = section.Pages.Add(); float y = 10; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Sales Report", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Sales Report", format1).Height; y = y + 5; PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold)); PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold)); using (OleDbConnection conn = new OleDbConnection()) { conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\..\..\Data\demo.mdb"; conn.Open(); OleDbCommand partQueryCommand = PreparePartQueryCommand(conn); OleDbCommand orderItemQueryCommand = PrepareOrderItemQueryCommand(conn); DataTable vendors = GetVendors(conn); for (int i = 0; i < vendors.Rows.Count; i++) { if (i > 0) { //next page page = section.Pages.Add(); y = 0; } //draw vendor String vendorTitle = String.Format("{0}. {1}", i + 1, vendors.Rows[i].ItemArray[1]); PdfLayoutResult drawVendorLayoutResult = DrawVendor(page, vendors, i, vendorTitle, y); //add vendor bookmark PdfDestination vendorBookmarkDest = new PdfDestination(page, new PointF(0, y)); PdfBookmark vendorBookmark = doc.Bookmarks.Add(vendorTitle); vendorBookmark.Color = Color.SaddleBrown; vendorBookmark.DisplayStyle = PdfTextStyle.Bold; vendorBookmark.Action = new PdfGoToAction(vendorBookmarkDest); y = drawVendorLayoutResult.Bounds.Bottom + 5; page = drawVendorLayoutResult.Page; //get parts of vendor DataTable parts = GetParts(partQueryCommand, (double)vendors.Rows[i].ItemArray[0]); for (int j = 0; j < parts.Rows.Count; j++) { if (j > 0) { //next page page = section.Pages.Add(); y = 0; } //draw part String partTitle = String.Format("{0}.{1}. {2}", i + 1, j + 1, parts.Rows[j].ItemArray[1]); PdfLayoutResult drawPartLayoutResult = DrawPart(page, parts, j, partTitle, y); //add part bookmark PdfDestination partBookmarkDest = new PdfDestination(page, new PointF(0, y)); PdfBookmark partBookmark = vendorBookmark.Add(partTitle); partBookmark.Color = Color.Coral; partBookmark.DisplayStyle = PdfTextStyle.Italic; partBookmark.Action = new PdfGoToAction(partBookmarkDest); y = drawPartLayoutResult.Bounds.Bottom + 5; page = drawPartLayoutResult.Page; //get order items String orderItemsTitle = String.Format("{0} - Order Items", parts.Rows[j].ItemArray[1]); DataTable orderItems = GetOrderItems(orderItemQueryCommand, (double)parts.Rows[j].ItemArray[0]); DrawOrderItems(page, orderItems, orderItemsTitle, y); } } } //Save pdf file. doc.SaveToFile("Bookmark.pdf"); doc.Close(); //Launching the Pdf file. PDFDocumentViewer("Bookmark.pdf"); }
private void crearFacturaPDF(String title) { //create a new pdf document PdfDocument pdfDoc = new PdfDocument(); //add one blank page PdfPageBase page = pdfDoc.Pages.Add(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; float y = 40; float infoMargin = 120; Cliente c = findCliente(fac_cliente.Text); //Encabezado PdfBrush brush1 = PdfBrushes.BlueViolet; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Calibri", 11f)); ; PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Left); page.Canvas.DrawString(c.cl_nombre, font1, brush1, infoMargin, y, format1); y = y + font1.MeasureString(title, format1).Height + 5; page.Canvas.DrawString(c.cl_ruc, font1, brush1, infoMargin, y, format1); y = y + font1.MeasureString(title, format1).Height + 5; page.Canvas.DrawString(c.cl_direccion, font1, brush1, infoMargin, y, format1); y = y + font1.MeasureString(title, format1).Height + 5; page.Canvas.DrawString(fac_fecha.Text, font1, brush1, infoMargin, y, format1); y = y + font1.MeasureString(title, format1).Height + 30; //Productos PdfBrush brush2 = PdfBrushes.Green; PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f)); float x = 30; float totalX = page.Canvas.ClientSize.Width - 2 * x; foreach (FacturaItem fi in _FacturaItemsCollection) { page.Canvas.DrawString(fi.fi_codigo, font2, brush2, x, y, format1); x = x + 0.15f * totalX; page.Canvas.DrawString(fi.fi_nombre, font2, brush2, x, y, format1); x = x + 0.55f * totalX; page.Canvas.DrawString(fi.fi_cantidad.ToString("n2"), font2, brush2, x, y, format1); x = x + 0.15f * totalX; page.Canvas.DrawString(fi.fi_importe.ToString("n2"), font2, brush2, x, y, format1); y = y + font2.MeasureString(fi.fi_nombre, format1).Height + 5; x = 30; } //Tail y = page.Canvas.ClientSize.Height - 60; infoMargin = x + 0.85f * totalX; page.Canvas.DrawString(fact_total.ToString("n2"), font1, brush1, infoMargin, y, format1); y = y - font1.MeasureString(fact_total.ToString(), format1).Height - 5; page.Canvas.DrawString(fact_iva.ToString("n2"), font1, brush1, infoMargin, y, format1); y = y - font1.MeasureString(fact_total.ToString(), format1).Height - 5; page.Canvas.DrawString(fact_subtotal.ToString("n2"), font1, brush1, infoMargin, y, format1); y = y - font1.MeasureString(fact_subtotal.ToString(), format1).Height - 5; //page.Canvas.DrawString(String.Format("* {0} productos en la base de datos.", dataSource.Length - 1), font2, brush2, 5, y); //save the pdf document String filename = title + ".pdf"; pdfDoc.SaveToFile(@filename); //launch the pdf document System.Diagnostics.Process.Start(@filename); }
private String crearPDFoculto(String title, String[][] dataSource, String[] extraInfo) { //create a new pdf document PdfDocument pdfDoc = new PdfDocument(); //add one blank page PdfPageBase page = pdfDoc.Pages.Add(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; float y = 10; //title PdfBrush brush1 = PdfBrushes.BlueViolet; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Calibri", 13f)); ; PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString(title, font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString(title, format1).Height; y = y + 5; PdfTable table = new PdfTable(); table.Style.CellPadding = 2; table.Style.HeaderSource = PdfHeaderSource.Rows; table.Style.HeaderRowCount = 1; table.Style.ShowHeader = true; table.DataSource = dataSource; PdfLayoutResult result = table.Draw(page, new PointF(0, y)); y = y + result.Bounds.Height + 5; if (extraInfo != null) { PdfBrush brush2 = PdfBrushes.Green; PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f)); foreach (String item in extraInfo) { page.Canvas.DrawString(item, font2, brush2, 5, y); y = y + font2.MeasureString(item, format1).Height; y = y + 5; } } //page.Canvas.DrawString(String.Format("* {0} productos en la base de datos.", dataSource.Length - 1), font2, brush2, 5, y); //save the pdf document String filename = "data/" + title + ".pdf"; pdfDoc.SaveToFile(@filename); return filename; //launch the pdf document //System.Diagnostics.Process.Start(@filename); }
private void button1_Click(object sender, EventArgs e) { //Create a pdf document. PdfDocument doc = new PdfDocument(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; // Create one page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); float y = 10; float x = 0; PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12)); String label = "Simple Link: "; PdfStringFormat format = new PdfStringFormat(); format.MeasureTrailingSpaces = true; page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format); x = font.MeasureString(label, format).Width; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 12, FontStyle.Underline)); String url1 = "http://www.e-iceblue.com"; page.Canvas.DrawString(url1, font1, PdfBrushes.Blue, x, y); y = y + font1.MeasureString(url1).Height; label = "Web Link: "; page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format); x = font.MeasureString(label, format).Width; String text = "e-iceblue"; PdfTextWebLink link2 = new PdfTextWebLink(); link2.Text = text; link2.Url = url1; link2.Font = font1; link2.Brush = PdfBrushes.Blue; link2.DrawTextWebLink(page.Canvas, new PointF(x, y)); y = y + font1.MeasureString(text).Height; label = "URI Annonationa: "; page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format); x = font.MeasureString(label, format).Width; text = "Google"; PointF location = new PointF(x, y); SizeF size = font1.MeasureString(text); RectangleF linkBounds = new RectangleF(location, size); PdfUriAnnotation link3 = new PdfUriAnnotation(linkBounds); link3.Border = new PdfAnnotationBorder(0); link3.Uri = "http://www.google.com"; (page as PdfNewPage).Annotations.Add(link3); page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y); y = y + size.Height; label = "URI Annonationa Action: "; page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format); x = font.MeasureString(label, format).Width; text = "JavaScript Action (Click Me)"; location = new PointF(x, y); size = font1.MeasureString(text); linkBounds = new RectangleF(location, size); PdfUriAnnotation link4 = new PdfUriAnnotation(linkBounds); link4.Border = new PdfAnnotationBorder(0.75f); link4.Color = Color.LightGray; //script String script = "app.alert({" + " cMsg: \"Hello.\"," + " nIcon: 3," + " cTitle: \"JavaScript Action\"" + "});"; PdfJavaScriptAction action = new PdfJavaScriptAction(script); link4.Action = action; (page as PdfNewPage).Annotations.Add(link4); page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y); y = y + size.Height; //Save pdf file. doc.SaveToFile("Link.pdf"); doc.Close(); //Launching the Pdf file. PDFDocumentViewer("Link.pdf"); }
protected void PDFQuote(Object source, EventArgs e) { //demos: http://www.e-iceblue.com/Tutorials/Spire.PDF/Demos.html //had to add permissions to IIS Express Folder for Network Users. Wont export more than 10 pages on free version. SaveQuote(); var filename = PdfFileName.Value; if (filename == "CANCELDONOTMAKE") return; PdfDocument pdf = new PdfDocument(); PdfPageBase page = pdf.Pages.Add(); float pageWidth = page.Canvas.ClientSize.Width; float y = 0; //formatting helpers PdfStringFormat centered = new PdfStringFormat(PdfTextAlignment.Center); PdfStringFormat rightAlign = new PdfStringFormat(PdfTextAlignment.Right); PdfFont helv24 = new PdfFont(PdfFontFamily.Helvetica, 24f, PdfFontStyle.Bold); PdfFont helv20 = new PdfFont(PdfFontFamily.Helvetica, 20f, PdfFontStyle.Bold); PdfFont helv16 = new PdfFont(PdfFontFamily.Helvetica, 16f, PdfFontStyle.Bold); PdfFont helv14 = new PdfFont(PdfFontFamily.Helvetica, 14f); PdfFont helv12 = new PdfFont(PdfFontFamily.Helvetica, 12f); PdfFont helv12Bold = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold); PdfFont helv11 = new PdfFont(PdfFontFamily.Helvetica, 11f); PdfFont helv9Ital = new PdfFont(PdfFontFamily.Helvetica, 9f, PdfFontStyle.Italic); PdfFont helv8 = new PdfFont(PdfFontFamily.Helvetica, 8f); PdfBrush black = new PdfSolidBrush(Color.Black); SizeF size; string brand = "Texas Digital Systems, Inc."; string address = "400 Technology Parkway College Station, TX 77845"; string contact = "(979) 693-9378 Voice (979) 764-8650 Fax"; string title = "Proposal Summary"; //HEADER page.Canvas.DrawString(brand, helv24, new PdfSolidBrush(Color.Black), pageWidth/2, y, centered); size = helv24.MeasureString(brand); y += size.Height + 1; page.Canvas.DrawString(address, helv12, black, pageWidth/2, y, centered); size = helv12.MeasureString(address); y += size.Height + 1; page.Canvas.DrawString(contact, helv12, black, pageWidth / 2, y, centered); size = helv12.MeasureString(contact); y += size.Height + 1; page.Canvas.DrawString("By: " + quote.Owner, helv12, black, 0, y); page.Canvas.DrawString(quote.Email, helv12, black, pageWidth, y, rightAlign); size = helv12.MeasureString(contact); y += size.Height + 1; page.Canvas.DrawString("Date: " + quote.Date, helv12, black, 0, y); page.Canvas.DrawString("PN: " + quote.PhoneNumber, helv12, black, pageWidth, y, rightAlign); size = helv12.MeasureString(quote.Owner); y += size.Height + 5; page.Canvas.DrawString(title, helv20, black, pageWidth / 2, y, centered); size = helv20.MeasureString(title); y += size.Height + 5; //ADDRESS TABLE PdfTable addressTable = new PdfTable(); addressTable.Style.CellPadding = 1; addressTable.Style.BorderPen = new PdfPen(PdfBrushes.Black, .5f); string[] addressData = { ";Customer Address;Billing Address;Shipping Address", "Contact;"+quote.Customer.Contact+";"+quote.Billing.Contact+";"+quote.Shipping.Contact, "Company;"+quote.Customer.Company+";"+quote.Billing.Company+";"+quote.Shipping.Company, "Address1;"+quote.Customer.Address1+";"+quote.Billing.Address1+";"+quote.Shipping.Address1, "Address2;"+quote.Customer.Address2+";"+quote.Billing.Address2+";"+quote.Shipping.Address2, "City/State/Zip;"+quote.Customer.CityState+";"+quote.Billing.CityState+";"+quote.Shipping.CityState, "Phone;"+quote.Customer.Phone+";"+quote.Billing.Phone+";"+quote.Shipping.Phone, "Fax;"+quote.Customer.Fax+";"+quote.Billing.Fax+";"+quote.Shipping.Fax, "Email;"+quote.Customer.Email+";"+quote.Billing.Email+";"+quote.Shipping.Email }; string[][] addressDataSource = new string[addressData.Length][]; for (int i = 0; i < addressData.Length; i++) addressDataSource[i] = addressData[i].Split(';'); addressTable.DataSource = addressDataSource; float width = page.Canvas.ClientSize.Width - (addressTable.Columns.Count + 1) * addressTable.Style.BorderPen.Width; for (int i = 0; i < addressTable.Columns.Count; i++) { if(i==0) addressTable.Columns[i].Width = width * .12f * width; else addressTable.Columns[i].Width = width * .2f * width; } addressTable.BeginRowLayout += new BeginRowLayoutEventHandler(addressTable_BeginRowLayout); PdfLayoutResult addressResult = addressTable.Draw(page, new PointF(0, y)); y += addressResult.Bounds.Height + 5; //QUOTE DETAILS PdfTable detailsTable = new PdfTable(); detailsTable.Style.CellPadding = 1; detailsTable.Style.BorderPen = new PdfPen(PdfBrushes.Black, .5f); detailsTable.Style.DefaultStyle.Font = helv11; detailsTable.Style.DefaultStyle.StringFormat = centered; width = page.Canvas.ClientSize.Width - (detailsTable.Columns.Count + 1) * detailsTable.Style.BorderPen.Width; //converting t/f to y/n string NewLocation, Dealer, TaxExempt; if (quote.NewLocation) NewLocation = "Yes"; else NewLocation = "No"; if (quote.Dealer) Dealer = "Yes"; else Dealer = "No"; if (quote.TaxExempt == "exempt") TaxExempt = "Yes"; else TaxExempt = "No"; string[] detailsData = { "Source: ;"+quote.Source+";Source Specific: ;"+quote.SpecificSource+";No. Of Locations: ;"+quote.LocationCount, "POS Provider: ;"+quote.POSProvidor+";Install Date: ;"+quote.InstallDate+";Business Unit: ;"+quote.BusinessUnit, "New Location: ;"+NewLocation+";Dealer: ;"+Dealer+";Tax Exempt: ;"+TaxExempt }; string[][] detailsDataSource = new string[detailsData.Length][]; for (int i = 0; i < detailsData.Length; i++) detailsDataSource[i] = detailsData[i].Split(';'); detailsTable.DataSource = detailsDataSource; for (int i = 0; i < detailsTable.Columns.Count; i++) { if (i %2 != 0) detailsTable.Columns[i].Width = width * .05f * width; else detailsTable.Columns[i].Width = width * .08f * width; } PdfLayoutResult detailsResult = detailsTable.Draw(page, new PointF(0, y)); y += detailsResult.Bounds.Height + 5; //QUOTE LINES if(quote.linesHW.Count > 0) { page.Canvas.DrawString("Hardware", helv16, black, pageWidth / 2, y, centered); size = helv14.MeasureString("Hardware"); y += size.Height + 2; y += (buildPdfLines(page, quote.linesHW,"Hardware", y)).Bounds.Height + 2; } if(quote.linesSW.Count > 0) { page.Canvas.DrawString("Software", helv16, black, pageWidth / 2, y, centered); size = helv16.MeasureString("Software & Maintenance"); y += size.Height + 2; y += (buildPdfLines(page, quote.linesSW,"Software", y)).Bounds.Height + 2; } if(quote.linesCC.Count > 0) { page.Canvas.DrawString("Content", helv16, black, pageWidth / 2, y, centered); size = helv16.MeasureString("Content"); y += size.Height + 2; y += (buildPdfLines(page, quote.linesCC,"Content", y)).Bounds.Height + 2; } if(quote.linesInst.Count > 0) { page.Canvas.DrawString("Installation", helv16, black, pageWidth / 2, y, centered); size = helv16.MeasureString("Installation"); y += size.Height + 2; y += (buildPdfLines(page, quote.linesInst, "Installation", y)).Bounds.Height + 2; } if (quote.linesRec.Count > 0) { page.Canvas.DrawString("Recurring", helv16, black, pageWidth / 2, y, centered); size = helv16.MeasureString("Recurring"); y += size.Height + 2; y += (buildPdfLines(page, quote.linesRec, "Recurring", y)).Bounds.Height + 2; } bool FreightExists = false; if (quote.Freight > 0) FreightExists = true; bool SalesTaxExists = false; if (quote.SalesTax > 0) SalesTaxExists = true; double GrandTotal = quote.GetGrandTotal(); //NOTES if (quote.ExternalNotes.Length > 0) { string notes = quote.ExternalNotes; PdfStringLayouter layouter = new PdfStringLayouter(); PdfStringFormat format = new PdfStringFormat(); format.LineSpacing = helv11.Size * 1.5f; PdfStringLayoutResult result = layouter.Layout(notes, helv11, format, new SizeF(pageWidth, y)); page.Canvas.DrawString("Notes", helv14, black, pageWidth / 2, y, centered); size = helv14.MeasureString("LULZ"); y += size.Height + 2; foreach (LineInfo line in result.Lines) { page.Canvas.DrawString(line.Text, helv11, black, 0, y, format); y = y + result.LineHeight; } } y += 5; page.Canvas.DrawLine(new PdfPen(PdfBrushes.Black, .5f), new PointF(0, y), new PointF(pageWidth, y)); y += 5; //TOTALS if(FreightExists || SalesTaxExists) { page.Canvas.DrawString("Subtotal: $" + GrandTotal.ToString(), helv12, black, 0, y); } if(FreightExists) { page.Canvas.DrawString("Freight: $" + quote.Freight.ToString(), helv12, black, pageWidth/4, y); GrandTotal += quote.Freight; } if (SalesTaxExists) { page.Canvas.DrawString("Sales Tax: $" + quote.SalesTax.ToString(), helv12, black, pageWidth / 2, y); GrandTotal += quote.SalesTax; } page.Canvas.DrawString("Total: $" + GrandTotal.ToString(), helv12Bold, black, pageWidth, y, rightAlign); size = helv12Bold.MeasureString("999999"); y += size.Height + 5; page.Canvas.DrawLine(new PdfPen(PdfBrushes.Black, .5f), new PointF(0, y), new PointF(pageWidth, y)); y += 5; //FINE PRINT page.Canvas.DrawString("Quote is good for: " + quote.QuoteLength + " days", helv8, black, 0, y); page.Canvas.DrawString("F.O.B. College Station, TX", helv8, black, pageWidth / 2, y, centered); page.Canvas.DrawString("Payment Terms: " + quote.PaymentTerms, helv8, black, pageWidth, y, rightAlign); size = helv8.MeasureString("THESE WORDS DON'T MATTER"); y += size.Height + 1; page.Canvas.DrawString("This is not an invoice and may not include freight and/or sales tax. An invoice will be sent upon receipt of the signed quote.", helv9Ital, black, pageWidth/2, y, centered); size = helv9Ital.MeasureString("ONLY DEVS WILL SEE THIS"); y += size.Height + 10; page.Canvas.DrawString("Please sign to accept this quotation: ", helv8, black, 0, y); size = helv8.MeasureString("I CAN SAY WHATEVER I WANT"); page.Canvas.DrawLine(new PdfPen(PdfBrushes.Black, .5f), new PointF(150, y+size.Height), new PointF(350, y+size.Height)); y += size.Height + 1; page.Canvas.DrawString("By signing I agree that I have read, understand and agree to be bound by the Texas Digital Standard Terms and Conditions Applicable to", helv8, black, 0, y); size = helv8.MeasureString("PAY UP GUY"); y += size.Height + 1; page.Canvas.DrawString("Quotes and Purchase Orders accessible at: ", helv8, black, 0, y); size = helv8.MeasureString("Quotes and Purchase Orders accessible at: "); page.Canvas.DrawString("http://www.ncr.com/wp-content/uploads/TXDigital_Terms_and_Conditions.pdf", helv8, PdfBrushes.DarkGreen, size.Width, y); y += size.Height + 1; page.Canvas.DrawString("After signing please fax to (979) 764-8650", helv8, black, 0, y); page.Canvas.DrawString("Delivery ARO: 45-60 days", helv8, black, pageWidth, y, rightAlign); size = helv8.MeasureString("THIS ISNT THE END LOL"); y += size.Height + 1; //pdf.SaveToFile(filename); pdf.SaveToHttpResponse(filename, Response, HttpReadType.Save); pdf.Close(); System.Diagnostics.Process.Start(filename); }
private PdfPageBase DrawPages(PdfDocument doc) { //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; // Create one page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); float y = 10; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Part List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Part List", format1).Height; y = y + 5; //create data table PdfTable table = new PdfTable(); table.Style.CellPadding = 2; table.Style.BorderPen = new PdfPen(brush1, 0.75f); table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.SkyBlue; table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f)); table.Style.AlternateStyle = new PdfCellStyle(); table.Style.AlternateStyle.BackgroundBrush = PdfBrushes.LightYellow; table.Style.AlternateStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f)); table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions; table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue; table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold)); table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center); table.Style.ShowHeader = true; using (OleDbConnection conn = new OleDbConnection()) { conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\..\..\Data\demo.mdb"; OleDbCommand command = new OleDbCommand(); command.CommandText = " select Description, OnHand, OnOrder, Cost, ListPrice from parts "; command.Connection = conn; using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command)) { DataTable dataTable = new DataTable(); dataAdapter.Fill(dataTable); table.DataSourceType = PdfTableDataSourceType.TableDirect; table.DataSource = dataTable; } } float width = page.Canvas.ClientSize.Width - (table.Columns.Count + 1) * table.Style.BorderPen.Width; for (int i = 0; i < table.Columns.Count; i++) { if (i == 0) { table.Columns[i].Width = width * 0.40f * width; table.Columns[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else { table.Columns[i].Width = width * 0.15f * width; table.Columns[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); } } PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat(); tableLayout.Break = PdfLayoutBreakType.FitElement; tableLayout.Layout = PdfLayoutType.Paginate; PdfLayoutResult result = table.Draw(page, new PointF(0, y), tableLayout); y = result.Bounds.Bottom + 3; PdfBrush brush2 = PdfBrushes.Gray; PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f)); result.Page.Canvas.DrawString(String.Format("* {0} parts in the list.", table.Rows.Count), font2, brush2, 5, y); return result.Page; }
static void LinesTable_BeginRowLayout(object sender, BeginRowLayoutEventArgs args) { PdfFont helv11 = new PdfFont(PdfFontFamily.Helvetica, 11f); PdfFont helv12Bold = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold); PdfStringFormat centered = new PdfStringFormat(PdfTextAlignment.Center); PdfBrush gray = new PdfSolidBrush(Color.LightGray); PdfBrush clear = new PdfSolidBrush(Color.Transparent); args.CellStyle.BorderPen = new PdfPen(Color.Transparent); args.CellStyle.BackgroundBrush = clear; if(args.RowIndex == 0) { //header args.CellStyle.Font = helv12Bold; } else args.CellStyle.Font = helv11; if(args.RowIndex % 2 != 0) { args.CellStyle.BackgroundBrush = gray; } }
private void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin) { PdfPageTemplateElement leftSpace = new PdfPageTemplateElement(margin.Left, pageSize.Height); doc.Template.Left = leftSpace; PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top); topSpace.Foreground = true; doc.Template.Top = topSpace; //draw header label PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic)); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right); String label = "Demo of Spire.Pdf"; SizeF size = font.MeasureString(label, format); float y = topSpace.Height - font.Height - 1; PdfPen pen = new PdfPen(Color.Black, 0.75f); topSpace.Graphics.SetTransparency(0.5f); topSpace.Graphics.DrawLine(pen, margin.Left, y, pageSize.Width - margin.Right, y); y = y - 1 - size.Height; topSpace.Graphics.DrawString(label, font, PdfBrushes.Black, pageSize.Width - margin.Right, y, format); PdfPageTemplateElement rightSpace = new PdfPageTemplateElement(margin.Right, pageSize.Height); doc.Template.Right = rightSpace; PdfPageTemplateElement bottomSpace = new PdfPageTemplateElement(pageSize.Width, margin.Bottom); bottomSpace.Foreground = true; doc.Template.Bottom = bottomSpace; //draw footer label y = font.Height + 1; bottomSpace.Graphics.SetTransparency(0.5f); bottomSpace.Graphics.DrawLine(pen, margin.Left, y, pageSize.Width - margin.Right, y); y = y + 1; PdfPageNumberField pageNumber = new PdfPageNumberField(); PdfPageCountField pageCount = new PdfPageCountField(); PdfCompositeField pageNumberLabel = new PdfCompositeField(); pageNumberLabel.AutomaticFields = new PdfAutomaticField[] { pageNumber, pageCount }; pageNumberLabel.Brush = PdfBrushes.Black; pageNumberLabel.Font = font; pageNumberLabel.StringFormat = format; pageNumberLabel.Text = "page {0} of {1}"; pageNumberLabel.Draw(bottomSpace.Graphics, pageSize.Width - margin.Right, y); PdfImage headerImage = PdfImage.FromFile(@"..\..\..\..\..\..\Data\Header.png"); PointF pageLeftTop = new PointF(-margin.Left, -margin.Top); PdfPageTemplateElement header = new PdfPageTemplateElement(pageLeftTop, headerImage.PhysicalDimension); header.Foreground = false; header.Graphics.SetTransparency(0.5f); header.Graphics.DrawImage(headerImage, 0, 0); doc.Template.Stamps.Add(header); PdfImage footerImage = PdfImage.FromFile(@"..\..\..\..\..\..\Data\Footer.png"); y = pageSize.Height - footerImage.PhysicalDimension.Height; PointF footerLocation = new PointF(-margin.Left, y); PdfPageTemplateElement footer = new PdfPageTemplateElement(footerLocation, footerImage.PhysicalDimension); footer.Foreground = false; footer.Graphics.SetTransparency(0.5f); footer.Graphics.DrawImage(footerImage, 0, 0); doc.Template.Stamps.Add(footer); }
private void buttonRun_Click(object sender, RoutedEventArgs e) { SaveFileDialog saveFiledialog = new SaveFileDialog(); saveFiledialog.Filter = "PDF Document (*.pdf)|*.pdf"; bool? result = saveFiledialog.ShowDialog(); if (!result.HasValue || !result.Value) { return; } //create a pdf document PdfDocument document = new PdfDocument(); //margin PdfMargins margin = new PdfMargins(); margin.Top = ConvertUnits(1.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; //create one page PdfPageBase page = document.Pages.Add(PdfPageSize.A4, margin, PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Landscape); float y = 0; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfInternalFont(new Font("Arial", 16f, System.Drawing.FontStyle.Bold), true); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Vendor List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Vendor List", format1).Height; y = y + 5; PdfGrid grid = new PdfGrid(); grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1); grid.Columns.Add(this.dataGridVendorList.Columns.Count); float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1); grid.Columns[0].Width = width * 0.25f; grid.Columns[1].Width = width * 0.25f; grid.Columns[2].Width = width * 0.25f; grid.Columns[3].Width = width * 0.15f; grid.Columns[4].Width = width * 0.10f; //header of grid PdfGridRow headerRow = grid.Headers.Add(1)[0]; headerRow.Style.Font = new PdfInternalFont(new Font("Arial", 11f, System.Drawing.FontStyle.Bold), true); String[] header = { "Vendor Name", "Address", "City", "State", "Country" }; for (int i = 0; i < header.Length; i++) { headerRow.Cells[i].Value = header[i]; headerRow.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); } headerRow.Style.BackgroundBrush = PdfBrushes.SeaGreen; //datarow of grid int rowIndex = 1; foreach (Vendor vendor in this.dataSource) { PdfGridRow row = grid.Rows.Add(); row.Style.Font = new PdfInternalFont(new Font("Arial", 10f), true); row.Cells[0].Value = vendor.VendorName; row.Cells[1].Value = vendor.Address; row.Cells[2].Value = vendor.City; row.Cells[3].Value = vendor.State; row.Cells[4].Value = vendor.Country; row.Cells[0].Style.BackgroundBrush = PdfBrushes.LightGray; row.Cells[0].StringFormat = row.Cells[1].StringFormat = row.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); row.Cells[3].StringFormat = row.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); if (rowIndex % 2 == 0) { row.Style.BackgroundBrush = PdfBrushes.YellowGreen; } else { row.Style.BackgroundBrush = PdfBrushes.Bisque; } rowIndex++; } StringBuilder totalAmount = new StringBuilder(); var groupByCountry = this.dataSource.GroupBy(v => v.Country) .Select(g => new { Name = g.Key, Count = g.Count() }); foreach (var item in groupByCountry) { totalAmount.AppendFormat("{0}:\t{1}", item.Name, item.Count); totalAmount.AppendLine(); } PdfGridRow totalAmountRow = grid.Rows.Add(); totalAmountRow.Style.BackgroundBrush = PdfBrushes.Plum; totalAmountRow.Cells[0].Value = "Total Amount"; totalAmountRow.Cells[0].Style.Font = new PdfInternalFont(new Font("Arial", 10f, System.Drawing.FontStyle.Bold), true); totalAmountRow.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); totalAmountRow.Cells[1].ColumnSpan = 4; totalAmountRow.Cells[1].Value = totalAmount.ToString(); totalAmountRow.Cells[1].Style.Font = new PdfInternalFont(new Font("Arial", 10f, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic), true); totalAmountRow.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); PdfLayoutResult resultl = grid.Draw(page, new PointF(0, y)); y = y + resultl.Bounds.Height + 5; PdfBrush brush2 = PdfBrushes.Gray; PdfTrueTypeFont font2 = new PdfInternalFont(new Font("Arial", 9f)); resultl.Page.Canvas.DrawString(String.Format("* All {0} vendors in the list", grid.Rows.Count - 1), font2, brush2, 5, y); //Save pdf file. using (Stream stream = saveFiledialog.OpenFile()) { document.SaveToStream(stream); } document.Close(); }
private void button1_Click(object sender, EventArgs e) { //Create a pdf document. PdfDocument doc = new PdfDocument(); //margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; //create section PdfSection section = doc.Sections.Add(); section.PageSettings.Size = PdfPageSize.A4; section.PageSettings.Margins = margin; // Create one page PdfPageBase page = section.Pages.Add(); float y = 10; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString("Attachment", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Attachment", format1).Height; y = y + 5; //attachment PdfAttachment attachment = new PdfAttachment("Header.png"); attachment.Data = File.ReadAllBytes(@"..\..\..\..\..\..\Data\Header.png"); attachment.Description = "Page header picture of demo."; attachment.MimeType = "image/png"; doc.Attachments.Add(attachment); attachment = new PdfAttachment("Footer.png"); attachment.Data = File.ReadAllBytes(@"..\..\..\..\..\..\Data\Footer.png"); attachment.Description = "Page footer picture of demo."; attachment.MimeType = "image/png"; doc.Attachments.Add(attachment); PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Bold)); PointF location = new PointF(0, y); String label = "Sales Report Chart"; byte[] data = File.ReadAllBytes(@"..\..\..\..\..\..\Data\SalesReportChart.png"); SizeF size = font2.MeasureString(label); RectangleF bounds = new RectangleF(location, size); page.Canvas.DrawString(label, font2, PdfBrushes.DarkOrange, bounds); bounds = new RectangleF(bounds.Right + 3, bounds.Top, font2.Height / 2, font2.Height); PdfAttachmentAnnotation annotation1 = new PdfAttachmentAnnotation(bounds, "SalesReportChart.png", data); annotation1.Color = Color.Teal; annotation1.Flags = PdfAnnotationFlags.ReadOnly; annotation1.Icon = PdfAttachmentIcon.Graph; annotation1.Text = "Sales Report Chart"; (page as PdfNewPage).Annotations.Add(annotation1); y = y + size.Height + 2; location = new PointF(0, y); label = "Science Personification Boston"; data = File.ReadAllBytes(@"..\..\..\..\..\..\Data\SciencePersonificationBoston.jpg"); size = font2.MeasureString(label); bounds = new RectangleF(location, size); page.Canvas.DrawString(label, font2, PdfBrushes.DarkOrange, bounds); bounds = new RectangleF(bounds.Right + 3, bounds.Top, font2.Height / 2, font2.Height); PdfAttachmentAnnotation annotation2 = new PdfAttachmentAnnotation(bounds, "SciencePersonificationBoston.jpg", data); annotation2.Color = Color.Orange; annotation2.Flags = PdfAnnotationFlags.NoZoom; annotation2.Icon = PdfAttachmentIcon.PushPin; annotation2.Text = "SciencePersonificationBoston.jpg, from Wikipedia, the free encyclopedia"; (page as PdfNewPage).Annotations.Add(annotation2); y = y + size.Height + 2; location = new PointF(0, y); label = "Picture of Science"; data = File.ReadAllBytes(@"..\..\..\..\..\..\Data\Wikipedia_Science.png"); size = font2.MeasureString(label); bounds = new RectangleF(location, size); page.Canvas.DrawString(label, font2, PdfBrushes.DarkOrange, bounds); bounds = new RectangleF(bounds.Right + 3, bounds.Top, font2.Height / 2, font2.Height); PdfAttachmentAnnotation annotation3 = new PdfAttachmentAnnotation(bounds, "Wikipedia_Science.png", data); annotation3.Color = Color.SaddleBrown; annotation3.Flags = PdfAnnotationFlags.Locked; annotation3.Icon = PdfAttachmentIcon.Tag; annotation3.Text = "Wikipedia_Science.png, from Wikipedia, the free encyclopedia"; (page as PdfNewPage).Annotations.Add(annotation3); y = y + size.Height + 2; location = new PointF(0, y); label = "Hawaii Killer Font"; data = File.ReadAllBytes(@"..\..\..\..\..\..\Data\Hawaii_Killer.ttf"); size = font2.MeasureString(label); bounds = new RectangleF(location, size); page.Canvas.DrawString(label, font2, PdfBrushes.DarkOrange, bounds); bounds = new RectangleF(bounds.Right + 3, bounds.Top, font2.Height / 2, font2.Height); PdfAttachmentAnnotation annotation4 = new PdfAttachmentAnnotation(bounds, "Hawaii_Killer.ttf", data); annotation4.Color = Color.CadetBlue; annotation4.Flags = PdfAnnotationFlags.NoRotate; annotation4.Icon = PdfAttachmentIcon.Paperclip; annotation4.Text = "Hawaii Killer Font, from http://www.1001freefonts.com"; (page as PdfNewPage).Annotations.Add(annotation4); y = y + size.Height + 2; //Save pdf file. doc.SaveToFile("Attachment.pdf"); doc.Close(); //Launching the Pdf file. PDFDocumentViewer("Attachment.pdf"); }