private float DrawPageTitle(PdfPageBase page, float y) { PdfBrush brush1 = PdfBrushes.MidnightBlue; PdfBrush brush2 = PdfBrushes.Red; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Bold)); String title = "Your Account Information(* = Required)"; SizeF size = font1.MeasureString(title); float x = (page.Canvas.ClientSize.Width - size.Width) / 2; page.Canvas.DrawString("Your Account Information(", font1, brush1, x, y); size = font1.MeasureString("Your Account Information("); x = x + size.Width; page.Canvas.DrawString("* = Required", font1, brush2, x, y); size = font1.MeasureString("* = Required"); x = x + size.Width; page.Canvas.DrawString(")", font1, brush1, x, y); y = y + size.Height; y = y + 3; PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Italic)); String p = "Your information is not public, shared in anyway, or displayed on this site."; page.Canvas.DrawString(p, font2, brush1, 0, y); return(y + font2.Height); }
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 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 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); }
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); }
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); }
public bool IntroPdf(PdfSection sec, string name, string surname, string email, string note, string pathOfImage, int numberOfNewLine, bool argument) { float intro = 10; try { page = sec.Pages.Add(); float width = 0; float height = 0; if (argument) { PdfImage image = PdfImage.FromFile(pathOfImage); if (image.Width < 150 || image.Height < 150) { return(false); } else { width = 150; height = 150; float x = (page.Canvas.ClientSize.Width - width) / 2; page.Canvas.DrawImage(image, x, intro, width, height); } } intro += height; page.Canvas.DrawString("Browser Report", font1, brush1, page.Canvas.ClientSize.Width / 2, intro, format1); intro += fontInfo.MeasureString("Country List", format1).Height; intro += 20; page.Canvas.DrawString("Name : " + name, fontInfo, brush1, 0, intro, leftAlignment); intro += 20; page.Canvas.DrawString("Surname : " + surname, fontInfo, brush1, 0, intro, leftAlignment); intro += 20; page.Canvas.DrawString("E-Mail : " + email, fontInfo, brush1, 0, intro, leftAlignment); if (numberOfNewLine <= 1) { intro += (20 * numberOfNewLine); } else { intro += (20 * numberOfNewLine) - fontInfo.MeasureString("Country List", format1).Height; } if (note.Length != 0) { page.Canvas.DrawString("Notes : " + note, fontInfo, brush1, 0, intro, leftAlignment); } intro += 20; page.Canvas.DrawString("Date : " + DateTime.Now, fontInfo, brush1, 0, intro, leftAlignment); return(true); } catch { Exception.ThrowExc.Instance.ErrorMessage(Const.Constants.ERROR_WHILE_CREATING); return(false); } }
private void button1_Click(object sender, EventArgs e) { //Load Pdf from disk PdfDocument doc = new PdfDocument(); doc.LoadFromFile("../../../../../../../Data/PDFTemplate_HF.pdf"); //Get the first page PdfPageBase page = doc.Pages[0]; string text1 = "Spire.Pdf is a robust component by"; string text2 = "Technology Co., Ltd."; PdfImage image = PdfImage.FromFile("../../../../../../../Data/Top-logo.png"); //Define font and bursh PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Impact", 10f)); PdfBrush bursh = PdfBrushes.DarkGray; //Get the size of text SizeF s1 = font.MeasureString(text1); SizeF s2 = font.MeasureString(text2); float x = 10; float y = 10; //Define image size SizeF imgSize = new SizeF(image.Width / 2, image.Height / 2); //Define rectangle and string format SizeF size = new SizeF(s1.Width, imgSize.Width); RectangleF rect1 = new RectangleF(new PointF(x, y), size); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); //Draw the text1 page.Canvas.DrawString(text1, font, bursh, rect1, format); //Draw the image x += s1.Width; page.Canvas.DrawImage(image, new PointF(x, y), imgSize); //Draw the text2 x += imgSize.Width; size = new SizeF(s2.Width, imgSize.Height); rect1 = new RectangleF(new PointF(x, y), size); page.Canvas.DrawString(text2, font, bursh, rect1, format); //Save the document string output = "ImageandPageNumberinHeaderFootersectionInline_out.pdf"; doc.SaveToFile(output); doc.Close(); //Launch the Pdf file PDFDocumentViewer(output); }
public byte[] ProForma(InvoicePdfModel model) { PdfLayoutResult layoutResult; string data; float y = 30; float x = 0; var document = new PdfDocument(); document.PageSettings.Margins.All = marginSize; var page = document.Pages.Add(); data = $"Faktura PRO FORMA nr {model.Number}"; layoutResult = new PdfTextElement(data, fontBig, brushBlack).Draw(page, new PointF(x, y)); data = $"Data wystawienia: {model.ExposureDate.ToString("yyyy-MM-dd")}"; y = layoutResult.Bounds.Bottom; x = page.Size.Width - fontNormal.MeasureString(data).Width - marginSize * 2 - 4; layoutResult = new PdfTextElement("data wystawienia:", fontNormalItalic, brushBlack).Draw(page, new PointF(x, y)); x += layoutResult.Bounds.Width + 4; layoutResult = new PdfTextElement(model.ExposureDate.ToString("yyyy-MM-dd"), fontNormalBold, brushBlack).Draw(page, new PointF(x, y)); data = $"Miejsce wystawienia: {model.InvoiceConfig.ExposurePlace}"; y = layoutResult.Bounds.Bottom; x = page.Size.Width - fontNormal.MeasureString(data).Width - marginSize * 2 - 4; layoutResult = new PdfTextElement("miejsce wystawienia:", fontNormalItalic, brushBlack).Draw(page, new PointF(x, y)); x += layoutResult.Bounds.Width + 4; layoutResult = new PdfTextElement(model.InvoiceConfig.ExposurePlace, fontNormalBold, brushBlack).Draw(page, new PointF(x, y)); float section = layoutResult.Bounds.Bottom + 32; DrawBuyer(model, page, page.Size.Width / 2, section); section = DrawSeller(model, page, 0, section); section = section + 48; section = DrawProducts(model, page, section); section = section + 48; section = DrawFooter(model, page, 0, section); using (var memoryStream = new MemoryStream()) { document.Save(memoryStream); return(memoryStream.ToArray()); } }
private PdfPageTemplateElement CreateFooterTemplate(PdfDocument doc, PdfMargins margins, SizeF pageSize) { //create a PdfPageTemplateElement object which works as footer space PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Bottom); footerSpace.Foreground = false; //declare two float variables float x = margins.Left; float y = 0; //draw line in footer space PdfPen pen = new PdfPen(PdfBrushes.Gray, 1); footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y); //draw text in footer space y = y + 5; PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f), true); //draw dynamic field in footer space PdfPageNumberField number = new PdfPageNumberField(); PdfPageCountField count = new PdfPageCountField(); PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "Page {0} of {1}", number, count); compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Top); SizeF size = font.MeasureString(compositeField.Text); compositeField.Bounds = new RectangleF(x, y, size.Width, size.Height); compositeField.Draw(footerSpace.Graphics); //return footerSpace return(footerSpace); }
private PdfLayoutResult DrawOrderItems(PdfPageBase page, DataTable orderItems, String title, float y) { //draw title PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Bold)); page.Canvas.DrawString(title, font1, PdfBrushes.Black, 0, y); y = y + font1.MeasureString(title).Height + 1; PdfTable table = new PdfTable(); table.Style.CellPadding = 2; table.Style.BorderPen = new PdfPen(PdfBrushes.Black, 0.75f); table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.MediumTurquoise; table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 8f)); table.Style.AlternateStyle = new PdfCellStyle(); table.Style.AlternateStyle.BackgroundBrush = PdfBrushes.PaleTurquoise; table.Style.AlternateStyle.Font = new PdfTrueTypeFont(new Font("Arial", 8f)); table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions; table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.Teal; table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Bold)); table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center); table.Style.ShowHeader = true; table.DataSource = orderItems; for (int i = 2; i < table.Columns.Count; i++) { table.Columns[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Right); } PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat(); tableLayout.Break = PdfLayoutBreakType.FitPage; tableLayout.Layout = PdfLayoutType.Paginate; return(table.Draw(page, new PointF(0, y), tableLayout)); }
/// <summary> /// Draws footer to the document. /// </summary> private PdfPageTemplateElement AddFooter(float width, string footerText) { RectangleF rect = new RectangleF(0, 0, width, 50); //Create a new instance of PdfPageTemplateElement class. PdfPageTemplateElement footer = new PdfPageTemplateElement(rect); PdfGraphics g = footer.Graphics; // Draw footer text. PdfSolidBrush brush = new PdfSolidBrush(Color.Gray); PdfFont font = new PdfTrueTypeFont(new Font("Helvetica", 6, FontStyle.Bold), true); float x = (width / 2) - (font.MeasureString(footerText).Width) / 2; g.DrawString(footerText, font, brush, new RectangleF(x, g.ClientSize.Height - 10, font.MeasureString(footerText).Width, font.Height)); //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(g, new PointF(470, 40)); return(footer); }
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 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 static void DrawToPage(PdfPageBase page, string text, PdfTrueTypeFont font, PdfBrush brushColor, PdfStringFormat formatLeft, RectangleF area, float y, out float yout, bool ignoreOut) { page.Canvas.DrawString(text, font, brushColor, area, formatLeft); SizeF size = font.MeasureString(text, area.Size, formatLeft); yout = (ignoreOut) ? y : y + size.Height + 2; }
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 static void AddDocumentLinkAnnotation(PdfDocument pdf, int AddPage, int DestinationPage, float y) { //Define a font PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f)); //Set the string format PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left); //Text string String prompt = "Local document Link: "; //Draw text string on page that pdf.Pages[AddPage].Canvas.DrawString(prompt, font, PdfBrushes.DodgerBlue, 0, y); //Use MeasureString to get the width of string float x = font.MeasureString(prompt, format).Width; //Create a PdfDestination with specific page PdfDestination dest = new PdfDestination(pdf.Pages[DestinationPage]); //Set the location of destination dest.Location = new PointF(0, y); //Set 50% zoom factor dest.Zoom = 0.5f; //Label string String label = "Click here to link the second page."; //Use MeasureString to get the SizeF of string SizeF size = font.MeasureString(label); //Create a rectangle RectangleF bounds = new RectangleF(x, y, size.Width, size.Height); //Draw label string pdf.Pages[AddPage].Canvas.DrawString(label, font, PdfBrushes.OrangeRed, x, y); //Create PdfDocumentLinkAnnotation on the rectangle and link to the destination PdfDocumentLinkAnnotation annotation = new PdfDocumentLinkAnnotation(bounds, dest); //Set color for annotation annotation.Color = Color.Blue; //Add annotation to the page (pdf.Pages[AddPage] as PdfNewPage).Annotations.Add(annotation); }
private static void DrawToPageWithHeader(PdfPageBase page, string header, string text, PdfTrueTypeFont fontHeader, PdfTrueTypeFont font, PdfBrush brushColor, PdfStringFormat formatLeft, float x, float y, out float yout, bool ignoreOut) { page.Canvas.DrawString(header, fontHeader, brushColor, x, y, formatLeft); SizeF size = font.MeasureString(header, formatLeft); page.Canvas.DrawString(text, font, brushColor, x + size.Width + 1, y, formatLeft); yout = (ignoreOut) ? y : y + size.Height + 2; }
private float AddFreeTextAnnotation(PdfPageBase page, float y) { PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f)); PdfStringFormat format = new PdfStringFormat(); format.MeasureTrailingSpaces = true; String prompt = "Text Markup: "; SizeF size = font.MeasureString(prompt); page.Canvas.DrawString(prompt, font, PdfBrushes.DodgerBlue, 0, y); float x = font.MeasureString(prompt, format).Width; String label = @"I'm a text box, not a TV"; size = font.MeasureString(label); RectangleF bounds = new RectangleF(x, y, size.Width, size.Height); page.Canvas.DrawRectangle(new PdfPen(Color.Blue, 0.1f), bounds); page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, x, y); PointF location = new PointF(bounds.Right + 16, bounds.Top - 16); RectangleF annotaionBounds = new RectangleF(location, new SizeF(80, 32)); PdfFreeTextAnnotation annotation = new PdfFreeTextAnnotation(annotaionBounds); annotation.AnnotationIntent = PdfAnnotationIntent.FreeTextCallout; annotation.Border = new PdfAnnotationBorder(0.5f); annotation.BorderColor = Color.Red; location = new PointF(bounds.Right + 105, page.ActualSize.Height - bounds.Top - 80); annotation.CalloutLines = new PointF[] { new PointF(bounds.Right + 85, page.ActualSize.Height - bounds.Top - 85), new PointF(bounds.Right + 105, page.ActualSize.Height - bounds.Top - 80), location }; annotation.Color = Color.Yellow; annotation.Flags = PdfAnnotationFlags.Locked; annotation.Font = font; annotation.LineEndingStyle = PdfLineEndingStyle.OpenArrow; annotation.MarkupText = "Just a joke."; annotation.Opacity = 0.75f; annotation.TextMarkupColor = Color.Green; (page as PdfNewPage).Annotations.Add(annotation); y = bounds.Bottom; return(y); }
static 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, 170); headerSpace.Foreground = false; //declare two float variables float x = margins.Left; float y = 0; //draw image in header space PdfImage headerImage = PdfImage.FromFile("1.png"); float width = headerImage.Width / 2; float height = headerImage.Height / 2; headerSpace.Graphics.DrawImage(headerImage, x, margins.Top - height + 130, width, height); /* //draw line in header space * PdfPen pen = new PdfPen(PdfBrushes.Gray, 1); * 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("Impact", 25f, FontStyle.Bold)); * PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left); * String headerText = "HEADER TEXT"; * SizeF size = font.MeasureString(headerText, format); * headerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Gray, pageSize.Width - x - size.Width - 2, margins.Top - (size.Height + 5), format); */ PdfImage headerImage2 = PdfImage.FromFile("2.png"); float width2 = headerImage.Width / 2; float height2 = headerImage.Height / 5; headerSpace.Graphics.DrawImage(headerImage2, x + 230, margins.Top - height2 + 50, width2, height2); PdfImage headerImage3 = PdfImage.FromFile("3.png"); float width3 = headerImage.Width / 4; float height3 = headerImage.Height / 5; headerSpace.Graphics.DrawImage(headerImage3, x + 510, margins.Top - height3 + 50, width3, height3); PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 28.5f, FontStyle.Bold)); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left); String headerText = "Invoice"; SizeF size = font.MeasureString(headerText, format); headerSpace.Graphics.DrawString(headerText, font, PdfBrushes.Black, pageSize.Width - x - size.Width - 40, margins.Top - (size.Height - 130), format); //return headerSpace return(headerSpace); }
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(3f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; //Create one page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); //Title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 13f, FontStyle.Bold | FontStyle.Italic), true); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Left); float y = 50; string s = "The sample demonstrates how to add annotations in PDF document."; page.Canvas.DrawString(s, font1, brush1, 0, y - 5, format1); y = y + font1.MeasureString(s, format1).Height; y = y + 15; y = AddDocumentLinkAnnotation(page, y); y = y + 6; y = AddFileLinkAnnotation(page, y); y = y + 6; y = AddFreeTextAnnotation(page, y); y = y + 6; y = AddLineAnnotation(page, y); y = y + 6; y = AddTextMarkupAnnotation(page, y); y = y + 6; y = AddPopupAnnotation(page, y); y = y + 6; y = AddRubberStampAnnotation(page, y); //Save pdf file doc.SaveToFile("Annotation.pdf"); doc.Close(); //Launch the Pdf file PDFDocumentViewer("Annotation.pdf"); }
private void button1_Click(object sender, EventArgs e) { PdfDocument doc = new PdfDocument(); // Creates a page PdfPageBase page = doc.Pages.Add(); //Create text String text = "Welcome to evaluate Spire.PDF for .NET !"; PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left); PdfSolidBrush brush = new PdfSolidBrush(Color.Black); // Defines a font PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Calibri", 15f, FontStyle.Regular)); float x = 50; float y = 50; // Draw text layer page.Canvas.DrawString(text, font, brush, new PointF(x, y), format); SizeF size = font.MeasureString("Welcome to evaluate", format); SizeF size2 = font.MeasureString("Spire.PDF for .NET", format); // Loads an image PdfImage image = PdfImage.FromFile("..\\..\\..\\..\\..\\..\\Data\\MultilayerImage.png"); // Draw image layer page.Canvas.DrawImage(image, new PointF(x + size.Width, y), size2); String result = "CreateMultilayerPDF_out.pdf"; //Save the document doc.SaveToFile(result); //Launch the Pdf file PDFDocumentViewer(result); }
public static void pdfsettings() { path = @"C:\Users\Public\Documents\RadianLabs\ReliefText\TeacherID-" + al5[relindex] + "_Date-" + date + "_Relief.pdf"; PdfUnitConvertor uc = new PdfUnitConvertor();; PdfMargins marg = new PdfMargins();; PdfPageBase page; PdfDocument pdfd; float y = 10; PdfBrush bru; PdfTrueTypeFont fon; PdfStringFormat format; pdfd = new PdfDocument(); marg.Top = uc.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); marg.Bottom = marg.Top; marg.Left = uc.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); marg.Right = marg.Left; page = pdfd.Pages.Add(PdfPageSize.A4, marg); bru = PdfBrushes.Black; fon = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold)); format = new PdfStringFormat(PdfTextAlignment.Center); page.Canvas.DrawString(textname, fon, bru, page.Canvas.ClientSize.Width / 2, y, format); y = y + fon.MeasureString(textname, format).Height; y = y + 5; String[] pdfdata = { "Period;Class", "One;" + relprinter[0] + "", "Two;" + relprinter[1] + "", "Three;" + relprinter[2] + "", "Four;" + relprinter[3] + "", "Five;" + relprinter[4] + "", "Six;" + relprinter[5] + "", "Seven;" + relprinter[6] + "", "Eight;" + relprinter[7] + "" }; String[][] dataSource = new String[pdfdata.Length][]; for (i = 0; i < pdfdata.Length; i++) { dataSource[i] = pdfdata[i].Split(';'); } PdfTable pdftable = new PdfTable(); pdftable.Style.CellPadding = 2; pdftable.Style.HeaderSource = PdfHeaderSource.Rows; pdftable.Style.HeaderRowCount = 1; pdftable.Style.ShowHeader = true; pdftable.DataSource = dataSource; PdfLayoutResult pdfresult = pdftable.Draw(page, new PointF(0, y)); PdfBrush bru2 = PdfBrushes.Gray; PdfTrueTypeFont fon2 = new PdfTrueTypeFont(new Font("Arial", 9f)); page.Canvas.DrawString(String.Format("{0}", pdfdata.Length - 1), fon2, bru2, 5, y); try { pdfd.SaveToFile(path); } catch (Exception ex) { Console.WriteLine(ex); } pdfd.Close(); }
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 an image PdfImage image = PdfImage.FromFile(@"..\..\..\..\..\..\Data\ChartImage.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 + 15; PdfBrush brush = new PdfSolidBrush(Color.OrangeRed); PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 16f, 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 + 25; 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 + 40; 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 + 40; } page.Canvas.Restore(); } //Save the document doc.SaveToFile("Transparency.pdf"); doc.Close(); //Launch the Pdf file PDFDocumentViewer("Transparency.pdf"); }
/// <summary> /// Draw a large string to the page at the specified x-position. /// </summary> /// <param name="text">The text to draw.</param> /// <param name="x">The x-position to start drawing the string at.</param> /// <param name="updateCurrentY">Should this method update the current y-position variable after drawing?</param> private static void DrawStringLarge(string text, float x, bool updateCurrentY = true) { SizeF stringSize = headerFont.MeasureString(text); float newX = x; if (x + stringSize.Width >= GetRightPage()) { newX = GetRightPage() - stringSize.Width; } PdfStringFormat layout = new PdfStringFormat { LineLimit = false, WordWrap = PdfWordWrapType.Word }; currentPage.Canvas.DrawString(text, headerFont, PdfBrushes.Black, newX, currentY, layout); if (updateCurrentY) { UpdateCurrentY(text, false, true); } }
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"); }
/// <summary> /// Generate pdf file with data from ticket model /// </summary> /// <param name="model">instance of Ticket class</param> public static void GenerateTicket(Ticket model) { using (PdfDocument document = new PdfDocument()) { // Set the page size. document.PageSettings.Size = new SizeF(500, 250); document.PageSettings.Orientation = PdfPageOrientation.Landscape; //Add a page to the document PdfPage page = document.Pages.Add(); document.PageSettings.Margins.All = 0; //Create PDF graphics for a page PdfGraphics graphics = page.Graphics; //Create new PDF brush. PdfBrush brushSilver = new PdfSolidBrush(Color.FromArgb(212, 212, 220)); PdfBrush brushDarkGrey = new PdfSolidBrush(Color.FromArgb(57, 63, 77)); PdfBrush brushYellow = new PdfSolidBrush(Color.FromArgb(254, 218, 106)); //Draw rectangle. graphics.DrawRectangle(brushDarkGrey, new RectangleF(0, 0, 500, 250)); //Set the standard font PdfFont fontTitle = new PdfTrueTypeFont(new Font("Broadway", 40)); PdfFont fontMovieTitle = new PdfTrueTypeFont(new Font("Letter Gothic Std", 25)); PdfFont font = new PdfTrueTypeFont(new Font("Letter Gothic Std", 15)); string title = "Kino Neptun"; string movieTitle = model.MovieTitle; string rowNumber = "Rząd " + model.RowNumber.ToString(); string seatNumber = "Miejsce " + model.SeatNumber.ToString(); string showingDate = model.ShowingDateTime.ToString("dd MMM yyyy"); string showingTime = model.ShowingDateTime.ToString("HH:mm"); SizeF sizeTitle = fontTitle.MeasureString(title); SizeF sizeMovieTitle = fontMovieTitle.MeasureString(movieTitle); //Draw the text graphics.DrawString(title, fontTitle, brushSilver, new RectangleF(new PointF(20, 20), sizeTitle)); graphics.DrawString(movieTitle, fontMovieTitle, brushYellow, new RectangleF(new PointF(20, 80), sizeMovieTitle)); graphics.DrawString(rowNumber, font, brushSilver, new PointF(300, 120)); graphics.DrawString(seatNumber, font, brushSilver, new PointF(300, 140)); graphics.DrawString(showingDate, font, brushSilver, new PointF(20, 120)); graphics.DrawString(showingTime, font, brushSilver, new PointF(20, 140)); //Save the document string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string filepath = path + @"/Tickets/ticket_" + model.Id + ".pdf"; document.Save(filepath); } }
private void button1_Click(object sender, EventArgs e) { //Create a pdf document PdfDocument doc = new PdfDocument(); //Create PdfUnitConvertor to convert the unit PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); //Setting for page margin PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; //Add one page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); //Define a PdfBrush PdfBrush brush1 = PdfBrushes.Black; //Define a font PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 13f, FontStyle.Bold), true); //Set the string format PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Left); //Set the position for drawing float x = 0; float y = 50; //Text string string specification = "The sample demonstrates how to create a file link in PDF document."; //Draw text string on page canvas page.Canvas.DrawString(specification, font1, brush1, x, y, format1); //Use MeasureString to get the height of string y = y + font1.MeasureString(specification, format1).Height + 10; //Add file link annotation AddFileLinkAnnotation(page, y); String result = "FileLinkAnnotation_out.pdf"; //Save the document doc.SaveToFile(result); //Launch the Pdf file PDFDocumentViewer(result); }
private static void AddFileLinkAnnotation(PdfPageBase page, float y) { //Define a font PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f)); //Set the string format PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left); //Text string String prompt = "Launch a File: "; //Draw text string on page canvas page.Canvas.DrawString(prompt, font, PdfBrushes.DodgerBlue, 0, y); //Use MeasureString to get the width of string float x = font.MeasureString(prompt, format).Width; //String of file name String label = "Sample.pdf"; //Use MeasureString to get the SizeF of string SizeF size = font.MeasureString(label); //Create a rectangle RectangleF bounds = new RectangleF(x, y, size.Width, size.Height); //Draw label string page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, x, y); //Create PdfFileLinkAnnotation on the rectangle and link file "Sample.pdf" PdfFileLinkAnnotation annotation = new PdfFileLinkAnnotation(bounds, @"..\..\..\..\..\..\Data\Sample.pdf"); //Set color for annotation annotation.Color = Color.Blue; //Add annotation to the page (page as PdfNewPage).Annotations.Add(annotation); }
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 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; } }
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 float DrawPageTitle(PdfPageBase page, float y) { PdfBrush brush1 = PdfBrushes.MidnightBlue; PdfBrush brush2 = PdfBrushes.Red; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Bold)); String title = "Your Account Information(* = Required)"; SizeF size = font1.MeasureString(title); float x = (page.Canvas.ClientSize.Width - size.Width) / 2; page.Canvas.DrawString("Your Account Information(", font1, brush1, x, y); size = font1.MeasureString("Your Account Information("); x = x + size.Width; page.Canvas.DrawString("* = Required", font1, brush2, x, y); size = font1.MeasureString("* = Required"); x = x + size.Width; page.Canvas.DrawString(")", font1, brush1, x, y); y = y + size.Height; y = y + 3; PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Italic)); String p = "Your information is not public, shared in anyway, or displayed on this site."; page.Canvas.DrawString(p, font2, brush1, 0, y); return y + font2.Height; }
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 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"); }
private PdfLayoutResult DrawPart(PdfPageBase page, DataTable parts, int index, String title, float y) { //draw title PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold)); DataRow row = parts.Rows[index]; page.Canvas.DrawString(title, font1, PdfBrushes.Black, 0, y); y = y + font1.MeasureString(title).Height + 1; //draw table Object[][] data = new Object[2][]; data[0] = new String[parts.Columns.Count]; for (int i = 0; i < parts.Columns.Count; i++) { data[0][i] = parts.Columns[i].ColumnName; } data[1] = row.ItemArray; PdfTable table = new PdfTable(); table.Style.CellPadding = 2; table.Style.BorderPen = new PdfPen(PdfBrushes.Black, 0.75f); table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.GreenYellow; table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 9f)); table.Style.HeaderSource = PdfHeaderSource.Rows; table.Style.HeaderRowCount = 1; table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.ForestGreen; table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Bold)); table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center); table.Style.ShowHeader = true; table.DataSource = data; float width = page.Canvas.ClientSize.Width - (table.Columns.Count + 1) * table.Style.BorderPen.Width; for (int i = 0; i < table.Columns.Count; i++) { table.Columns[i].Width = i == 1 ? width * 0.35f : width * 0.13f; } PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat(); tableLayout.Break = PdfLayoutBreakType.FitPage; tableLayout.Layout = PdfLayoutType.Paginate; return table.Draw(page, new PointF(0, y), tableLayout); }
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 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, PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Landscape); float y = 10; float x1 = page.Canvas.ClientSize.Width; //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("Vendor List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1); y = y + font1.MeasureString("Vendor List", format1).Height; y = y + 5; String[] data = { "VendorName;Address1;City;State;Country", "Cacor Corporation;161 Southfield Rd;Southfield;OH;U.S.A.", "Underwater;50 N 3rd Street;Indianapolis;IN;U.S.A.", "J.W. Luscher Mfg.;65 Addams Street;Berkely;MA;U.S.A.", "Scuba Professionals;3105 East Brace;Rancho Dominguez;CA;U.S.A.", "Divers' Supply Shop;5208 University Dr;Macon;GA;U.S.A.", "Techniques;52 Dolphin Drive;Redwood City;CA;U.S.A.", "Perry Scuba;3443 James Ave;Hapeville;GA;U.S.A.", "Beauchat, Inc.;45900 SW 2nd Ave;Ft Lauderdale;FL;U.S.A.", "Amor Aqua;42 West 29th Street;New York;NY;U.S.A.", "Aqua Research Corp.;P.O. Box 998;Cornish;NH;U.S.A.", "B&K Undersea Photo;116 W 7th Street;New York;NY;U.S.A.", "Diving International Unlimited;1148 David Drive;San Diego;DA;U.S.A.", "Nautical Compressors;65 NW 167 Street;Miami;FL;U.S.A.", "Glen Specialties, Inc.;17663 Campbell Lane;Huntington Beach;CA;U.S.A.", "Dive Time;20 Miramar Ave;Long Beach;CA;U.S.A.", "Undersea Systems, Inc.;18112 Gotham Street;Huntington Beach;CA;U.S.A.", "Felix Diving;310 S Michigan Ave;Chicago;IL;U.S.A.", "Central Valley Skin Divers;160 Jameston Ave;Jamaica;NY;U.S.A.", "Parkway Dive Shop;241 Kelly Street;South Amboy;NJ;U.S.A.", "Marine Camera & Dive;117 South Valley Rd;San Diego;CA;U.S.A.", "Dive Canada;275 W Ninth Ave;Vancouver;British Columbia;Canada", "Dive & Surf;P.O. Box 20210;Indianapolis;IN;U.S.A.", "Fish Research Labs;29 Wilkins Rd Dept. SD;Los Banos;CA;U.S.A." }; PdfGrid grid = new PdfGrid(); grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1); String[] header = data[0].Split(';'); grid.Columns.Add(header.Length); 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; PdfGridRow headerRow = grid.Headers.Add(1)[0]; headerRow.Style.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold), true); headerRow.Style.BackgroundBrush = new PdfLinearGradientBrush(new PointF(0, 0), new PointF(x1, 0), Color.Red, Color.Blue); for (int i = 0; i < header.Length; i++) { headerRow.Cells[i].Value = header[i]; headerRow.Cells[i].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); if (i == 0) { headerRow.Cells[i].Style.BackgroundBrush = PdfBrushes.Gray; } } Random random = new Random(); Dictionary<String, int> groupByCountry = new Dictionary<String, int>(); for (int r = 1; r < data.Length; r++) { PdfGridRow row = grid.Rows.Add(); row.Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f), true); byte[] buffer = new byte[6]; random.NextBytes(buffer); PdfRGBColor color1 = new PdfRGBColor(buffer[0], buffer[1], buffer[2]); PdfRGBColor color2 = new PdfRGBColor(buffer[3], buffer[4], buffer[5]); row.Style.BackgroundBrush = new PdfLinearGradientBrush(new PointF(0, 0), new PointF(x1, 0), color1, color2); String[] rowData = data[r].Split(';'); for (int c = 0; c < rowData.Length; c++) { row.Cells[c].Value = rowData[c]; if (c == 0) { row.Cells[c].Style.BackgroundBrush = PdfBrushes.Gray; } if(c < 3) { row.Cells[c].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle); } else { row.Cells[c].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); } if (c == 4) { if (groupByCountry.ContainsKey(rowData[c])) { groupByCountry[rowData[c]] = groupByCountry[rowData[c]] + 1; } else { groupByCountry[rowData[c]] = 1; } } } } StringBuilder totalAmount = new StringBuilder(); foreach (KeyValuePair<String, int> country in groupByCountry) { totalAmount.AppendFormat("{0}:\t{1}", country.Key, country.Value); totalAmount.AppendLine(); } PdfGridRow totalAmountRow = grid.Rows.Add(); totalAmountRow.Style.BackgroundBrush = PdfBrushes.Plum; totalAmountRow.Cells[0].Value = "Total Amount"; totalAmountRow.Cells[0].Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, 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 PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold | FontStyle.Italic), true); totalAmountRow.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle); //append product list PdfGrid productList = new PdfGrid(); productList.Style.Font = new PdfTrueTypeFont(new Font("Arial", 8f), 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 p.Description " + " from vendors v " + " inner join parts p " + " on v.VendorNo = p.VendorNo " + " where v.VendorName = 'Cacor Corporation'"; command.Connection = conn; using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command)) { DataTable dataTable = new DataTable(); dataAdapter.Fill(dataTable); productList.DataSource = dataTable; } } productList.Headers[0].Cells[0].Value = "Cacor Corporation"; productList.Headers[0].Cells[0].Style.Font = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Bold), true); productList.Headers[0].Cells[0].Style.Borders.All = new PdfPen(new PdfTilingBrush(new SizeF(1, 1)), 0); grid.Rows[0].Cells[0].Value = productList; grid.Rows[0].Cells[0].StringFormat.Alignment = PdfTextAlignment.Left; PdfLayoutResult result = grid.Draw(page, new PointF(0, y)); y = y + result.Bounds.Height + 5; PdfBrush brush2 = PdfBrushes.Gray; PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f)); result.Page.Canvas.DrawString(String.Format("* All {0} vendors in the list", grid.Rows.Count - 1), font2, brush2, 5, y); //Save pdf file. doc.SaveToFile("Grid.pdf"); doc.Close(); //Launching the Pdf file. PDFDocumentViewer("Grid.pdf"); }
private void DrawCover(PdfSection section, PdfMargins margin) { section.PageNumber = new PdfPageNumber(); section.PageNumber.NumberStyle = PdfNumberStyle.LowerRoman; section.PageNumber.Prefix = "cover "; section.PageSettings.Size = PdfPageSize.A4; section.PageSettings.Margins.All = 0; PdfPageBase page = section.Pages.Add(); DrawPageHeaderAndFooter(page, margin, true); //refenrence content PdfBrush brush1 = PdfBrushes.LightGray; PdfBrush brush2 = PdfBrushes.Blue; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 8f)); PdfStringFormat format = new PdfStringFormat(); format.MeasureTrailingSpaces = true; String text1 = "(All text and picture from "; String text2 = "Wikipedia"; String text3 = ", the free encyclopedia)"; float x = 0, y = 10; x = x + margin.Left; y = y + margin.Top; page.Canvas.DrawString(text1, font1, brush1, x, y, format); x = x + font1.MeasureString(text1, format).Width; page.Canvas.DrawString(text2, font1, brush2, x, y, format); x = x + font1.MeasureString(text2, format).Width; page.Canvas.DrawString(text3, font1, brush1, x, y, format); //cover PdfBrush brush3 = PdfBrushes.Black; PdfBrush brush4 = new PdfSolidBrush(new PdfRGBColor(0xf9, 0xf9, 0xf9)); PdfImage image = PdfImage.FromFile(@"..\..\..\..\..\..\Data\SciencePersonificationBoston.jpg"); String text = Pagination.Properties.Resources.ImageDescription; float r = image.PhysicalDimension.Height / image.Height; PdfPen pen = new PdfPen(brush1, r); SizeF size = font1.MeasureString(text, image.PhysicalDimension.Width - 2); PdfTemplate template = new PdfTemplate(image.PhysicalDimension.Width + 4 * r + 4, image.PhysicalDimension.Height + 4 * r + 7 + size.Height); template.Graphics.DrawRectangle(pen, brush4, 0, 0, template.Width, template.Height); x = y = r + 2; template.Graphics.DrawRectangle(brush1, x, y, image.PhysicalDimension.Width + 2 * r, image.PhysicalDimension.Height + 2 * r); x = y = x + r; template.Graphics.DrawImage(image, x, y); x = x - 1; y = y + image.PhysicalDimension.Height + r + 2; template.Graphics.DrawString(text, font1, brush3, new RectangleF(new PointF(x, y), size)); float x1 = (page.Canvas.ClientSize.Width - template.Width) / 2; float y1 = (page.Canvas.ClientSize.Height - margin.Top - margin.Bottom) * (1 - 0.618f) - template.Height / 2 + margin.Top; template.Draw(page.Canvas, x1, y1); //title format.Alignment = PdfTextAlignment.Center; PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 24f, FontStyle.Bold)); x = page.Canvas.ClientSize.Width / 2; y = y1 + template.Height + 10; page.Canvas.DrawString(Pagination.Properties.Resources.Title, font2, brush3, x, y, format); }
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; 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", "Chile;Santiago;South America;756943;13200000", "Colombia;Bagota;South America;1138907;33000000", "Cuba;Havana;North America;114524;10600000", "Ecuador;Quito;South America;455502;10600000", "El Salvador;San Salvador;North America;20865;5300000", "Guyana;Georgetown;South America;214969;800000", "Jamaica;Kingston;North America;11424;2500000", "Mexico;Mexico City;North America;1967180;88600000", "Nicaragua;Managua;North America;139000;3900000", "Paraguay;Asuncion;South America;406576;4660000", "Peru;Lima;South America;1285215;21600000", "United States of America;Washington;North America;9363130;249200000", "Uruguay;Montevideo;South America;176140;3002000", "Venezuela;Caracas;South America;912047;19700000" }; 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(); //Launching the Pdf file. PDFDocumentViewer("SimpleTable.pdf"); }
private PdfLayoutResult DrawOrderItems(PdfPageBase page, DataTable orderItems, String title, float y) { //draw title PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Bold)); page.Canvas.DrawString(title, font1, PdfBrushes.Black, 0, y); y = y + font1.MeasureString(title).Height + 1; PdfTable table = new PdfTable(); table.Style.CellPadding = 2; table.Style.BorderPen = new PdfPen(PdfBrushes.Black, 0.75f); table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.MediumTurquoise; table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 8f)); table.Style.AlternateStyle = new PdfCellStyle(); table.Style.AlternateStyle.BackgroundBrush = PdfBrushes.PaleTurquoise; table.Style.AlternateStyle.Font = new PdfTrueTypeFont(new Font("Arial", 8f)); table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions; table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.Teal; table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Bold)); table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center); table.Style.ShowHeader = true; table.DataSource = orderItems; for (int i = 2; i < table.Columns.Count; i++) { table.Columns[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Right); } PdfTableLayoutFormat tableLayout = new PdfTableLayoutFormat(); tableLayout.Break = PdfLayoutBreakType.FitPage; tableLayout.Layout = PdfLayoutType.Paginate; return table.Draw(page, new PointF(0, y), tableLayout); }
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 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 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; //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 + 2; //table top PdfDestination tableTopDest = new PdfDestination(page); tableTopDest.Location = new PointF(0, y); tableTopDest.Mode = PdfDestinationMode.Location; tableTopDest.Zoom = 1f; //Draw table PdfTrueTypeFont buttonFont = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold)); float buttonWidth = 70; float buttonHeight = buttonFont.Height * 1.5f; float tableTop = y; PdfLayoutResult tableLayoutResult = DrawTable(page, y + buttonHeight + 5); //table bottom PdfDestination tableBottomDest = new PdfDestination(tableLayoutResult.Page); tableBottomDest.Location = new PointF(0, tableLayoutResult.Bounds.Bottom); tableBottomDest.Mode = PdfDestinationMode.Location; tableBottomDest.Zoom = 1f; //go to table bottom float x = page.Canvas.ClientSize.Width - buttonWidth; PdfStringFormat format2 = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); RectangleF buttonBounds = new RectangleF(x, tableTop, buttonWidth, buttonHeight); page.Canvas.DrawRectangle(PdfBrushes.DarkGray, buttonBounds); page.Canvas.DrawString("To Bottom", buttonFont, PdfBrushes.CadetBlue, buttonBounds, format2); PdfGoToAction action1 = new PdfGoToAction(tableBottomDest); PdfActionAnnotation annotation1 = new PdfActionAnnotation(buttonBounds, action1); annotation1.Border = new PdfAnnotationBorder(0.75f); annotation1.Color = Color.LightGray; (page as PdfNewPage).Annotations.Add(annotation1); //go to table top float tableBottom = tableLayoutResult.Bounds.Bottom + 5; buttonBounds = new RectangleF(x, tableBottom, buttonWidth, buttonHeight); tableLayoutResult.Page.Canvas.DrawRectangle(PdfBrushes.DarkGray, buttonBounds); tableLayoutResult.Page.Canvas.DrawString("To Top", buttonFont, PdfBrushes.CadetBlue, buttonBounds, format2); PdfGoToAction action2 = new PdfGoToAction(tableTopDest); PdfActionAnnotation annotation2 = new PdfActionAnnotation(buttonBounds, action2); annotation2.Border = new PdfAnnotationBorder(0.75f); annotation2.Color = Color.LightGray; (tableLayoutResult.Page as PdfNewPage).Annotations.Add(annotation2); //goto last page PdfNamedAction action3 = new PdfNamedAction(PdfActionDestination.LastPage); doc.AfterOpenAction = action3; //script String script = "app.alert({" + " cMsg: \"Oh no, you want to leave me.\"," + " nIcon: 3," + " cTitle: \"JavaScript Action\"" + "});"; PdfJavaScriptAction action4 = new PdfJavaScriptAction(script); doc.BeforeCloseAction = action4; //Save pdf file. doc.SaveToFile("Action.pdf"); doc.Close(); //Launching the Pdf file. PDFDocumentViewer("Action.pdf"); }
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 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 = document.Pages.Add(PdfPageSize.A4, margin, PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Landscape); float y = 0; //title PdfBrush brush1 = PdfBrushes.Black; PdfTrueTypeFont font1 = new PdfTrueTypeFont(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 PdfTrueTypeFont(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 PdfTrueTypeFont(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 PdfTrueTypeFont(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 PdfTrueTypeFont(new Font("Arial", 10f, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic), true); // = new Font("Arial", 10f, System.Drawing.FontStyle.Bold); 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 PdfTrueTypeFont(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"); }
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 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 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; }
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 DrawContent(PdfSection section, PdfMargins margin) { section.PageNumber = new PdfPageNumber(); section.PageNumber.NumberStyle = PdfNumberStyle.Numeric; section.PageNumber.Prefix = "page "; section.PageSettings.Size = PdfPageSize.A4; section.PageSettings.Margins.All = 0; PdfPageBase page = section.Pages.Add(); DrawPageHeaderAndFooter(page, margin, false); float x = margin.Left; float y = margin.Top + 8; float width = page.Canvas.ClientSize.Width - margin.Left - margin.Right; PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f), true); PdfBrush brush1 = PdfBrushes.Black; PdfPen pen1 = new PdfPen(brush1, 0.75f); page.Canvas.DrawString(Pagination.Properties.Resources.Title, font1, brush1, x, y); y = y + font1.MeasureString(Pagination.Properties.Resources.Title).Height + 6; page.Canvas.DrawLine(pen1, x, y, page.Canvas.ClientSize.Width - margin.Right, y); y = y + 1.75f; String content = Pagination.Properties.Resources.Content; String[] lines = content.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Italic), true); PdfStringFormat format1 = new PdfStringFormat(); format1.MeasureTrailingSpaces = true; format1.LineSpacing = font2.Height * 1.5f; format1.ParagraphIndent = font2.MeasureString("\t", format1).Width; y = y + font2.Height * 0.5f; SizeF size = font2.MeasureString(lines[0], width, format1); page.Canvas.DrawString(lines[0], font2, brush1, new RectangleF(new PointF(x, y), size), format1); y = y + size.Height; PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f), true); PdfStringFormat format2 = new PdfStringFormat(); format2.LineSpacing = font3.Height * 1.4f; format2.MeasureTrailingSpaces = true; size = font3.MeasureString(lines[1], width, format2); page.Canvas.DrawString(lines[1], font3, brush1, new RectangleF(new PointF(x, y), size), format2); y = y + size.Height; y = y + font3.Height * 0.75f; float indent = font3.MeasureString("\t\t", format2).Width; float x1 = x + indent; size = font3.MeasureString(lines[2], width - indent, format2); page.Canvas.DrawString(lines[2], font3, brush1, new RectangleF(new PointF(x1, y), size), format2); y = y + size.Height + font3.Height * 0.75f; StringBuilder buff = new StringBuilder(); for (int i = 3; i < lines.Length; i++) { buff.AppendLine(lines[i]); } content = buff.ToString(); PdfStringLayouter textLayouter = new PdfStringLayouter(); PdfStringLayoutResult result = textLayouter.Layout(content, font3, format2, new SizeF(width, float.MaxValue)); foreach (LineInfo line in result.Lines) { if ((line.LineType & LineType.FirstParagraphLine) == LineType.FirstParagraphLine) { y = y + font3.Height * 0.75f; } if (y > (page.Canvas.ClientSize.Height - margin.Bottom - result.LineHeight)) { page = section.Pages.Add(); DrawPageHeaderAndFooter(page, margin, false); y = margin.Top; } page.Canvas.DrawString(line.Text, font3, brush1, x, y, format2); y = y + result.LineHeight; } }
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"); }
private PdfLayoutResult DrawVendor(PdfPageBase page, DataTable vendors, int index, String title, float y) { //draw title PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold)); DataRow row = vendors.Rows[index]; page.Canvas.DrawString(title, font1, PdfBrushes.Black, 0, y); y = y + font1.MeasureString(title).Height + 1; //draw table Object[][] data = new Object[vendors.Columns.Count][]; for (int i = 0; i < vendors.Columns.Count; i++) { data[i] = new Object[2]; data[i][0] = vendors.Columns[i].ColumnName; data[i][1] = vendors.Rows[index].ItemArray[i]; } PdfGrid grid = new PdfGrid(); grid.Style.CellPadding = new PdfPaddings(2, 2, 1, 1); grid.DataSource = data; float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1) * 0.75f; grid.Columns[0].Width = width * 0.20f; grid.Columns[1].Width = width * 0.80f; PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold)); PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f)); for (int i = 0; i < grid.Rows.Count; i++) { grid.Rows[i].Style.Font = font2; grid.Rows[i].Cells[0].Style.BackgroundBrush = PdfBrushes.CadetBlue; grid.Rows[i].Cells[1].Style.BackgroundBrush = PdfBrushes.SkyBlue; } PdfGridLayoutFormat layout = new PdfGridLayoutFormat(); layout.Break = PdfLayoutBreakType.FitPage; layout.Layout = PdfLayoutType.Paginate; return grid.Draw(page, new PointF(0, y), layout); }