public static void DrawRectangle(TextFrame frameContainer, System.Drawing.Color color, float xPos, float yPos, float width, float height) { MigraDoc.DocumentObjectModel.Color colorx = ConvertColor(color); TextFrame frameRect = new TextFrame(); frameRect.LineFormat.Color = colorx; frameRect.Height = Unit.FromInch(height / 100); frameRect.Width = Unit.FromInch(width / 100); TextFrame frame = new TextFrame(); frame.Elements.Add(frameRect); frame.RelativeVertical = RelativeVertical.Page; frame.RelativeHorizontal = RelativeHorizontal.Page; frame.MarginLeft = Unit.FromInch(xPos / 100); frame.MarginTop = Unit.FromInch(yPos / 100); frame.Top = TopPosition.Parse("0 in"); frame.Left = LeftPosition.Parse("0 in"); frame.Width = frameContainer.Width; Unit bottom = Unit.FromInch((yPos + height) / 100); if (frameContainer.Height < bottom) { frameContainer.Height = bottom; } frame.Height = frameContainer.Height; frameContainer.Elements.Add(frame); }
private void SetTableHeader(ref Table table, Color tableColor) { var columns = new Dictionary <string, Dictionary <string, ParagraphAlignment> >(); columns.Add("#", new Dictionary <string, ParagraphAlignment> { { "1cm", ParagraphAlignment.Center } }); columns.Add("Title", new Dictionary <string, ParagraphAlignment> { { "6cm", ParagraphAlignment.Left } }); var taxesEnabled = _taxSettings.TaxesEnabled; var columnWidth = taxesEnabled ? "2cm" : "3cm"; columns.Add("Qty", new Dictionary <string, ParagraphAlignment> { { columnWidth, ParagraphAlignment.Center } }); columns.Add("Unit Price" + (taxesEnabled ? " (ex TAX)" : ""), new Dictionary <string, ParagraphAlignment> { { columnWidth, ParagraphAlignment.Right } }); if (taxesEnabled) { columns.Add("Tax Rate", new Dictionary <string, ParagraphAlignment> { { columnWidth, ParagraphAlignment.Right } }); } columns.Add("Net Sub Total", new Dictionary <string, ParagraphAlignment> { { "3cm", ParagraphAlignment.Right } }); foreach (var item in columns) { Column column = table.AddColumn(item.Value.First().Key); column.Format.Alignment = item.Value.First().Value; } Row row = table.AddRow(); row.HeadingFormat = true; row.Format.Alignment = ParagraphAlignment.Center; row.Format.Font.Bold = true; row.Shading.Color = tableColor; row.TopPadding = 2; row.BottomPadding = 2; int rowId = 0; foreach (var item in columns) { row.Cells[rowId].AddParagraph(item.Key); row.Cells[rowId].Format.Alignment = ParagraphAlignment.Center; rowId++; } table.SetEdge(0, 0, 5, 1, Edge.Box, BorderStyle.Single, 0.75, Color.Empty); }
private void SetDocument(ref Document document, Order order) { var tableColor = new Color(0, 0, 0, 0); Section section = document.AddSection(); //HEADER SetHeader(ref section); //FOOTER SetFooter(ref section); //INFO SetInfo(order, ref section); //TABLE STYLE Table table = SetTableStyle(ref section, tableColor); //HEADERS SetTableHeader(ref table, tableColor); //ITEMS SetTableData(order, ref table); //SUMMARY SetTableSummary(order, ref table); }
private Table SetTableStyle(ref Section section, Color tableColor) { TextFrame frame = section.AddTextFrame(); frame.MarginTop = new Unit(6, UnitType.Centimeter); frame.Width = new Unit(16, UnitType.Centimeter); //TABLE LABEL Paragraph p = frame.AddParagraph(); p.AddFormattedText("Purchased goods:", TextFormat.Bold); frame.AddParagraph("").AddLineBreak(); //TABLE Table table = frame.AddTable(); table.Style = "Table"; table.Borders.Color = tableColor; table.Borders.Width = 0.25; table.Borders.Left.Width = 0.5; table.Borders.Right.Width = 0.5; table.Rows.LeftIndent = 0; return(table); }
/// <summary> /// Appends a new <see cref="MigraDoc.DocumentObjectModel.Tables.Row"/> to <paramref name="table"/>. /// The new row will contain a single cell spanning the entire table. /// </summary> /// <param name="table">The <see cref="MigraDoc.DocumentObjectModel.Tables.Table"/> to append to.</param> /// <param name="height">The cell height.</param> /// <param name="header">The cell value.</param> /// <param name="style">The cell style name.</param> /// <param name="borderColor">The border colour.</param> /// <returns>The appended row.</returns> internal static Row AppendHeaderRow(this Table table, Unit height, string header, string style, Color borderColor) { Row row = table.AppendRow(height, header); row.Style = style; row.Shading = new Shading { Color = borderColor }; row.Cells[0].MergeRight = table.Columns.Count - 1; return row; }
public void addResultToPDF(Section section, MigraDoc.DocumentObjectModel.Color color, string txtResult) { // as each result is displayed in the listbox, also put the result into the PDF document too Paragraph paragraphTestResult = section.AddParagraph(); paragraphTestResult.Format.Font.Color = color; paragraphTestResult.AddFormattedText(txtResult, TextFormat.Bold); }
public static void Main(string[] args) { Document document = new Document(); Section section = document.AddSection(); section.PageSetup.PageFormat = PageFormat.A4; // формат листа а4 section.PageSetup.Orientation = Orientation.Portrait; section.PageSetup.BottomMargin = 10; //нижний отступ section.PageSetup.TopMargin = 10; //верхний отступ Paragraph paragraph = new Paragraph(); section.Add(paragraph); paragraph.Format.Font.Name = "Arial"; paragraph.Format.Font.Size = "22"; paragraph.Format.Alignment = ParagraphAlignment.Center; paragraph.Format.Font.Color = Color.FromRgb(100, 100, 100); paragraph.Format.SpaceAfter = "3cm"; paragraph.Format.SpaceBefore = "3cm"; Text text = new Text(""); paragraph.AddText("Мои мечты стремятся вдаль,"); paragraph.AddFormattedText(TextFormat.Bold); paragraph.AddLineBreak(); paragraph.AddText("Где слышны вопли и рыданья,"); paragraph.AddFormattedText(TextFormat.Italic);// форматированный текст paragraph.AddLineBreak(); paragraph.AddText("Чужую разделить печаль"); paragraph.AddLineBreak(); paragraph.AddText("И муки тяжкого страданья"); paragraph.AddLineBreak(); paragraph.AddLineBreak(); paragraph.AddText("Я там могу найти себе"); paragraph.AddLineBreak(); paragraph.AddText("Отраду в жизни, упоенье,"); paragraph.AddLineBreak(); paragraph.AddText("И там, наперекор судьбе,"); paragraph.AddLineBreak(); paragraph.AddText("Искать я буду вдохновенья."); Section imageSection = document.AddSection(); Image image = new Image("C:/Users/Валерия/Downloads/Есенин.jpg"); image.Width = 100; image.Height = 100; section.Add(image); var pdfRenderer = new PdfDocumentRenderer(true); pdfRenderer.Document = document; pdfRenderer.RenderDocument(); var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "123.pdf"); pdfRenderer.PdfDocument.Save(filePath);// сохраняем }
public static MigraDoc.DocumentObjectModel.Font CreateFont(float fsize, bool isBold, System.Drawing.Color color) { MigraDoc.DocumentObjectModel.Color colorx = ConvertColor(color); MigraDoc.DocumentObjectModel.Font font = new MigraDoc.DocumentObjectModel.Font(); //if(fontFamily.==FontFamily.GenericSansSerif) { font.Name = "Arial"; //} //if(fontFamily==FontFamily.GenericSerif) { // font.Name="Times"; //} font.Size = Unit.FromPoint(fsize); font.Bold = isBold; font.Color = colorx; return(font); }
/// <summary> /// Appends a <see cref="MigraDoc.DocumentObjectModel.Tables.Table"/> to <paramref name="document"/> /// with a specified number of columns. /// </summary> /// <param name="document">The <see cref="MigraDoc.DocumentObjectModel.Document"/> to append to.</param> /// <param name="columns">The number of columns to add.</param> /// <param name="borderColor">The table border colour.</param> /// <returns>The added <see cref="MigraDoc.DocumentObjectModel.Tables.Table"/>.</returns> internal static Table AppendTable(this Document document, int columns, Color borderColor) { Table table = document.LastSection.AddTable(); table.Style = PdfResources.StyleNameTable; Unit colWidth = (document.DefaultPageSetup.PageWidth - document.DefaultPageSetup.LeftMargin - document.DefaultPageSetup.RightMargin) / columns; for (var i = columns; i > 0; i--) { Column labelCol = table.AddColumn(colWidth); labelCol.Borders.Color = borderColor; labelCol.Borders.Visible = true; labelCol.Borders.Width = 0.25; } return table; }
public void VisitRunProperties(RunProperties element) { if (_pdfParagraph == null) { return; } _pdfText.Font.Bold = element.Bold != null; _pdfText.Font.Italic = element.Italic != null; _pdfText.Font.Underline = element.Underline != null ? Underline.Single : Underline.None; if (element.FontSize != null) { _pdfText.Font.Size = Convert.ToSingle(element.FontSize.Val) / 2; _pdfText.Font.Name = element.RunFonts.Ascii.Value; _pdfText.Font.Color = element.Color != null?PdfColor.Parse("#" + element.Color.Val) : PdfColor.Empty; } }
private void SetParagraphByRunStyles(StyleRunProperties element) { if (element == null) { return; } _pdfParagraph.Format.Font.Bold = element.Bold != null; _pdfParagraph.Format.Font.Italic = element.Italic != null; _pdfParagraph.Format.Font.Underline = element.Underline != null ? Underline.Single : Underline.None; if (element.FontSize != null) { _pdfParagraph.Format.Font.Size = Convert.ToSingle(element.FontSize.Val) / 2; _pdfParagraph.Format.Font.Name = element.RunFonts.Ascii?.Value ?? string.Empty; _pdfParagraph.Format.Font.Color = element.Color != null?PdfColor.Parse("#" + element.Color.Val) : PdfColor.Empty; } }
/// <summary> /// This method creates the document layout for this pdf file /// </summary> /// <param name="document">A species information document object holding all the information that should be included in the pdf file</param> /// <returns>A formated pdf document</returns> private Document createPdfDocument(Data.ArtDatabankenService.SpeciesInformationDocument document) { Document pdfFile = new Document(); pdfFile.DefaultPageSetup.TopMargin = Unit.FromCentimeter(1.2); pdfFile.DefaultPageSetup.RightMargin = Unit.FromCentimeter(1.2); pdfFile.DefaultPageSetup.BottomMargin = Unit.FromCentimeter(2.6); pdfFile.DefaultPageSetup.FooterDistance = Unit.FromCentimeter(0.8); pdfFile.DefaultPageSetup.LeftMargin = Unit.FromCentimeter(1.2); pdfFile.DefaultPageSetup.DifferentFirstPageHeaderFooter = true; pdfFile.DefaultPageSetup.OddAndEvenPagesHeaderFooter = true; pdfFile.Info.Title = "Arfaktablad om " + document.Taxon.Label + " [DyntaxaTaxonId: " + document.Taxon.Id.ToString() + "]"; pdfFile.Info.Subject = "Arfaktablad med beskrivning, utbredningsuppgifter, ekologi, hot och naturvårdsåtgärder, litteratur"; pdfFile.Info.Author = document.AuthorAndYear; // Get the predefined style Normal. Style style = pdfFile.Styles["Normal"]; // Because all styles are derived from Normal, the next line changes the // font of the whole document. Or, more exactly, it changes the font of // all styles and paragraphs that do not redefine the font. style.Font.Name = "Arial"; style.Font.Size = Unit.FromPoint(10); //Colors MigraDoc.DocumentObjectModel.Color colorOfRedlistPeriodBox = Color.FromCmyk(35, 25, 25, 5); // light grey MigraDoc.DocumentObjectModel.Color colorOfRedlistCategoryBox = Color.FromCmyk(5, 95, 100, 0); // Red MigraDoc.DocumentObjectModel.Color colorOfFrameLine = Color.FromCmyk(50, 10, 75, 0); // light green MigraDoc.DocumentObjectModel.Color colorOfHeaderTitleText = Color.FromCmyk(80, 60, 70, 80); // dark grey MigraDoc.DocumentObjectModel.Color colorOfHeaderText = Color.FromCmyk(35, 25, 25, 5); // light grey Section section = pdfFile.AddSection(); // Create main frame first TextFrame firstMainFrame = section.Headers.FirstPage.AddTextFrame(); firstMainFrame.RelativeHorizontal = RelativeHorizontal.Margin; firstMainFrame.RelativeVertical = RelativeVertical.Margin; firstMainFrame.Height = "26cm"; firstMainFrame.Width = "18,6cm"; firstMainFrame.LineFormat.Width = 1.5; firstMainFrame.LineFormat.Color = colorOfFrameLine; // End header first // Create header odd TextFrame mainFrame = section.Headers.Primary.AddTextFrame(); mainFrame.RelativeHorizontal = RelativeHorizontal.Margin; mainFrame.RelativeVertical = RelativeVertical.Margin; mainFrame.Height = "26cm"; mainFrame.Width = "18,6cm"; mainFrame.LineFormat.Width = 1.5; mainFrame.LineFormat.Color = colorOfFrameLine; // End header odd // Create header even TextFrame evenMainFrame = section.Headers.EvenPage.AddTextFrame(); evenMainFrame.RelativeHorizontal = RelativeHorizontal.Margin; evenMainFrame.RelativeVertical = RelativeVertical.Margin; evenMainFrame.Height = "26cm"; evenMainFrame.Width = "18,6cm"; evenMainFrame.LineFormat.Width = 1.5; evenMainFrame.LineFormat.Color = colorOfFrameLine; // End header even //Create Line text on first page Paragraph borderLineTextFrameFirstPageText; TextFrame borderLineTextFrameFirstPage; borderLineTextFrameFirstPage = section.AddTextFrame(); borderLineTextFrameFirstPage.MarginTop = -7; borderLineTextFrameFirstPage.MarginLeft = "133mm"; borderLineTextFrameFirstPageText = borderLineTextFrameFirstPage.AddParagraph("ARTFAKTABLAD"); borderLineTextFrameFirstPageText.Format.Font.Color = colorOfFrameLine; borderLineTextFrameFirstPageText.Format.Font.Size = Unit.FromPoint(12); borderLineTextFrameFirstPage.Height = 0; // Header 1 Paragraph commonName; if (document.CommonName.IsNotEmpty()) { //Swedish name with first letter upper case in title commonName = section.AddParagraph(char.ToUpper(document.CommonName[0]) + document.CommonName.Substring(1)); commonName.Format.Font.Color = colorOfHeaderTitleText; } else { commonName = section.AddParagraph("(Svenskt namn saknas)"); commonName.Format.Font.Color = colorOfHeaderText; } commonName.Format.Font.Size = Unit.FromPoint(20); commonName.Format.Alignment = ParagraphAlignment.Left; commonName.Format.LeftIndent = "0.5cm"; commonName.Format.RightIndent = "0.5cm"; commonName.Format.SpaceAfter = "0.2cm"; commonName.Format.SpaceBefore = "1cm"; //Header2 Paragraph scientificName = section.AddParagraph(document.ScientificName); scientificName.Format.Font.Size = Unit.FromPoint(12); scientificName.Format.Font.Color = colorOfHeaderTitleText; scientificName.Format.Font.Italic = true; scientificName.Format.Alignment = ParagraphAlignment.Left; scientificName.Format.LeftIndent = "0.5cm"; scientificName.Format.RightIndent = "0.5cm"; scientificName.Format.SpaceAfter = "1.2cm"; scientificName.Format.SpaceBefore = "0.2cm"; if (document.Taxon.Author.IsNotEmpty()) { scientificName.AddFormattedText(" " + document.Taxon.Author, TextFormat.NotItalic); } //Redlist information Table redlistSpeciesInformation = section.AddTable(); //redlistSpeciesInformation.Format.LeftIndent = "0.5cm"; //redlistSpeciesInformation.Borders.Width = 1.5; //redlistSpeciesInformation.Borders.Color = colorOfFrameLine; Column columnLeft = redlistSpeciesInformation.AddColumn(Unit.FromCentimeter(9.2)); columnLeft.Format.Alignment = ParagraphAlignment.Left; Column columnRight = redlistSpeciesInformation.AddColumn(Unit.FromCentimeter(9.2)); columnRight.Format.Alignment = ParagraphAlignment.Left; Row row = redlistSpeciesInformation.AddRow(); Cell cellTaxonInfo = row.Cells[0]; Cell cellCriteria = row.Cells[1]; List <ParagraphItem> paragraphItems = null; //Taxonomic information cellTaxonInfo = row.Cells[0]; Paragraph taxonomicInformation = cellTaxonInfo.AddParagraph(); taxonomicInformation.Format.Font.Color = colorOfHeaderText; taxonomicInformation.Format.Font.Size = Unit.FromPoint(8); taxonomicInformation.Format.LeftIndent = "0.5cm"; taxonomicInformation.Format.RightIndent = "0.5cm"; taxonomicInformation.Format.SpaceAfter = "0.2cm"; cellTaxonInfo.VerticalAlignment = VerticalAlignment.Bottom; paragraphItems = GetParagraphItems(document.AutomaticTaxonomicParagraph, document.ItalicStringsInText); foreach (ParagraphItem item in paragraphItems) { if (item.Italic) { taxonomicInformation.AddFormattedText(item.Text, TextFormat.Italic); taxonomicInformation.AddText(" "); } else { taxonomicInformation.AddText(item.Text); taxonomicInformation.AddText(" "); } } //Redlist Criteria if (document.RedlistCriteria.IsNotEmpty()) { Paragraph redlistCriteriaText = cellCriteria.AddParagraph(document.RedlistCriteria); redlistCriteriaText.Format.Font.Color = colorOfHeaderText; redlistCriteriaText.Format.Font.Size = Unit.FromPoint(8); redlistCriteriaText.Format.LeftIndent = "4cm"; redlistCriteriaText.Format.SpaceAfter = "0.2cm"; redlistCriteriaText.Format.Alignment = ParagraphAlignment.Left; cellCriteria.VerticalAlignment = VerticalAlignment.Bottom; } // Create Image if (_taxonImage.IsNotNull()) { Paragraph taxonParagraph = section.AddParagraph(); taxonParagraph.Format.Alignment = ParagraphAlignment.Left; taxonParagraph.Format.LeftIndent = "0.5cm"; taxonParagraph.Format.RightIndent = "0.5cm"; taxonParagraph.Format.SpaceAfter = "0.2cm"; _taxonImage.Save("taxon.png"); Image taxonImage = taxonParagraph.AddImage("taxon.png"); taxonImage.Height = Unit.FromCentimeter(9); taxonImage.LockAspectRatio = true; taxonImage.WrapFormat.Style = WrapStyle.TopBottom; } // Create Map if (_mapImage.IsNotNull()) { Paragraph mapParagraph = section.AddParagraph(); mapParagraph.Format.Alignment = ParagraphAlignment.Right; mapParagraph.Format.LeftIndent = "0.5cm"; mapParagraph.Format.RightIndent = "0.5cm"; mapParagraph.Format.SpaceAfter = "0.2cm"; _mapImage.Save("map.png"); Image mapImage = mapParagraph.AddImage("map.png"); mapImage.Height = Unit.FromCentimeter(9); mapImage.LockAspectRatio = true; mapImage.WrapFormat.Style = WrapStyle.TopBottom; } //Main document paragraphs Paragraph paragraph; if (_document.DescriptionParagraph.IsNotEmpty()) { paragraph = section.AddParagraph(); paragraph.Format.SpaceBefore = "0.5cm"; paragraph.Format.LeftIndent = "0.5cm"; paragraph.Format.RightIndent = "0.5cm"; paragraph.Format.Alignment = ParagraphAlignment.Left; paragraph.AddFormattedText("Beskrivning".ToUpper(), TextFormat.Bold); paragraph.AddLineBreak(); paragraph.Format.Alignment = ParagraphAlignment.Justify; paragraphItems = GetParagraphItems(document.DescriptionParagraph, document.ItalicStringsInText); foreach (ParagraphItem item in paragraphItems) { if (item.Italic) { paragraph.AddFormattedText(item.Text, TextFormat.Italic); } else { paragraph.AddText(item.Text); } } } if (_document.DistributionParagraph.IsNotEmpty()) { paragraph = section.AddParagraph(); paragraph.Format.SpaceBefore = "0.5cm"; paragraph.Format.LeftIndent = "0.5cm"; paragraph.Format.RightIndent = "0.5cm"; paragraph.Format.Alignment = ParagraphAlignment.Left; paragraph.AddFormattedText("Utbredning och status".ToUpper(), TextFormat.Bold); paragraph.AddLineBreak(); paragraphItems = GetParagraphItems(document.DistributionParagraph, document.ItalicStringsInText); foreach (ParagraphItem item in paragraphItems) { if (item.Italic) { paragraph.AddFormattedText(item.Text, TextFormat.Italic); } else { paragraph.AddText(item.Text); } } } if (_document.EcologyParagraph.IsNotEmpty()) { paragraph = section.AddParagraph(); paragraph.Format.SpaceBefore = "0.5cm"; paragraph.Format.LeftIndent = "0.5cm"; paragraph.Format.RightIndent = "0.5cm"; paragraph.Format.Alignment = ParagraphAlignment.Left; paragraph.AddFormattedText("Ekologi".ToUpper(), TextFormat.Bold); paragraph.AddLineBreak(); paragraph.Format.Alignment = ParagraphAlignment.Justify; paragraphItems = GetParagraphItems(document.EcologyParagraph, document.ItalicStringsInText); foreach (ParagraphItem item in paragraphItems) { if (item.Italic) { paragraph.AddFormattedText(item.Text, TextFormat.Italic); } else { paragraph.AddText(item.Text); } } } if (_document.ThreatsParagraph.IsNotEmpty()) { paragraph = section.AddParagraph(); paragraph.Format.SpaceBefore = "0.5cm"; paragraph.Format.LeftIndent = "0.5cm"; paragraph.Format.RightIndent = "0.5cm"; paragraph.Format.Alignment = ParagraphAlignment.Left; paragraph.AddFormattedText("Hot".ToUpper(), TextFormat.Bold); paragraph.AddLineBreak(); paragraph.Format.Alignment = ParagraphAlignment.Justify; paragraphItems = GetParagraphItems(document.ThreatsParagraph, document.ItalicStringsInText); foreach (ParagraphItem item in paragraphItems) { if (item.Italic) { paragraph.AddFormattedText(item.Text, TextFormat.Italic); } else { paragraph.AddText(item.Text); } } } if (_document.MeasuresParagraph.IsNotEmpty()) { paragraph = section.AddParagraph(); paragraph.Format.SpaceBefore = "0.5cm"; paragraph.Format.LeftIndent = "0.5cm"; paragraph.Format.RightIndent = "0.5cm"; paragraph.Format.Alignment = ParagraphAlignment.Left; paragraph.AddFormattedText("Åtgärder".ToUpper(), TextFormat.Bold); paragraph.AddLineBreak(); paragraph.Format.Alignment = ParagraphAlignment.Justify; paragraphItems = GetParagraphItems(document.MeasuresParagraph, document.ItalicStringsInText); foreach (ParagraphItem item in paragraphItems) { if (item.Italic) { paragraph.AddFormattedText(item.Text, TextFormat.Italic); } else { paragraph.AddText(item.Text); } } } if (_document.ExtraParagraph.IsNotEmpty()) { paragraph = section.AddParagraph(); paragraph.Format.SpaceBefore = "0.5cm"; paragraph.Format.LeftIndent = "0.5cm"; paragraph.Format.RightIndent = "0.5cm"; paragraph.Format.Alignment = ParagraphAlignment.Left; paragraph.AddFormattedText("Övrigt".ToUpper(), TextFormat.Bold); paragraph.AddLineBreak(); paragraph.Format.Alignment = ParagraphAlignment.Justify; paragraphItems = GetParagraphItems(document.ExtraParagraph, document.ItalicStringsInText); foreach (ParagraphItem item in paragraphItems) { if (item.Italic) { paragraph.AddFormattedText(item.Text, TextFormat.Italic); } else { paragraph.AddText(item.Text); } } } //Reference list if (_document.ReferenceParagraph.IsNotEmpty()) { paragraph = section.AddParagraph(); paragraph.Format.SpaceBefore = "0.5cm"; paragraph.Format.LeftIndent = "0.5cm"; paragraph.Format.RightIndent = "0.5cm"; paragraph.Format.Alignment = ParagraphAlignment.Left; paragraph.AddFormattedText("Litteratur".ToUpper(), TextFormat.Bold); paragraph.AddLineBreak(); string[] references = _document.ReferenceParagraph.Split('\n'); foreach (string reference in references) { Paragraph referenceParagraph = section.AddParagraph(); referenceParagraph.Format.LeftIndent = "1cm"; referenceParagraph.Format.FirstLineIndent = "-1cm"; referenceParagraph.Format.Alignment = ParagraphAlignment.Justify; referenceParagraph.Format.Font.Size = Unit.FromPoint(10); paragraphItems = GetParagraphItems(reference, document.ItalicStringsInReferences); foreach (ParagraphItem item in paragraphItems) { if (item.Italic) { referenceParagraph.AddFormattedText(item.Text, TextFormat.Italic); } else { referenceParagraph.AddText(item.Text); } } } } paragraph = section.AddParagraph(LINE); paragraph.Format.SpaceBefore = "4mm"; //Author information Table authorInformationBox = section.AddTable(); //authorInformationBox.Format.SpaceBefore = "5mm"; authorInformationBox.Borders.Width = 0; //authorInformationBox.Borders.Top.Width = Unit.FromPoint(0.25); Column columnAuthor = authorInformationBox.AddColumn(Unit.FromCentimeter(16)); columnAuthor.Format.Alignment = ParagraphAlignment.Justify; row = authorInformationBox.AddRow(); cellTaxonInfo = row.Cells[0]; Paragraph authorParagraph = cellTaxonInfo.AddParagraph(); authorParagraph.AddText(getUpdateInformation()); // Create footer first page IOResources.IOResource.slu_logotyp_web1.Save("slu.png"); Image firstFooterImage = section.Footers.FirstPage.AddImage("slu.png"); firstFooterImage.Height = Unit.FromCentimeter(1.9); firstFooterImage.LockAspectRatio = true; firstFooterImage.RelativeVertical = RelativeVertical.Line; firstFooterImage.RelativeHorizontal = RelativeHorizontal.Margin; firstFooterImage.WrapFormat.Style = WrapStyle.None; Paragraph firstFooterParagraph = section.Footers.FirstPage.AddParagraph(); firstFooterParagraph.AddLineBreak(); firstFooterParagraph.AddText("ArtDatabanken"); firstFooterParagraph.Format.Font.Size = 16; firstFooterParagraph.Format.Font.Bold = true; firstFooterParagraph.Format.Font.Name = "Arial"; firstFooterParagraph.Format.Alignment = ParagraphAlignment.Right; // end footer first page // Create footer Paragraph footerParagraph = section.Footers.Primary.AddParagraph(); footerParagraph.AddText("www.slu.se/artdatabanken"); footerParagraph.Format.Font.Size = 13; footerParagraph.Format.Font.Bold = true; footerParagraph.Format.Alignment = ParagraphAlignment.Center; Paragraph footerPageNo = section.Footers.Primary.AddParagraph(); footerPageNo.AddPageField(); footerPageNo.Format.Font.Color = colorOfFrameLine; footerPageNo.Format.Font.Size = 13; footerPageNo.Format.Alignment = ParagraphAlignment.Right; // end footer // Create even footer Paragraph EvenFooterParagraph = section.Footers.EvenPage.AddParagraph(); EvenFooterParagraph.AddText("www.slu.se/artdatabanken"); EvenFooterParagraph.Format.Font.Size = 13; EvenFooterParagraph.Format.Font.Bold = true; EvenFooterParagraph.Format.Alignment = ParagraphAlignment.Center; Paragraph evenfooterPageNo = section.Footers.EvenPage.AddParagraph(); evenfooterPageNo.AddPageField(); evenfooterPageNo.Format.Font.Color = colorOfFrameLine; evenfooterPageNo.Format.Font.Size = 13; evenfooterPageNo.Format.Alignment = ParagraphAlignment.Left; // end even footer return(pdfFile); }
private void SetTableHeader(ref Table table, Color tableColor) { var columns = new Dictionary<string, Dictionary<string, ParagraphAlignment>>(); columns.Add("#", new Dictionary<string, ParagraphAlignment> { {"1cm", ParagraphAlignment.Center} }); columns.Add("Title", new Dictionary<string, ParagraphAlignment> { {"6cm", ParagraphAlignment.Left} }); var taxesEnabled = _taxSettings.TaxesEnabled; var columnWidth = taxesEnabled ? "2cm" : "3cm"; columns.Add("Qty", new Dictionary<string, ParagraphAlignment> { {columnWidth, ParagraphAlignment.Center} }); columns.Add("Unit Price" + (taxesEnabled ? " (ex TAX)" : ""), new Dictionary<string, ParagraphAlignment> { {columnWidth, ParagraphAlignment.Right} }); if (taxesEnabled) { columns.Add("Tax Rate", new Dictionary<string, ParagraphAlignment> { {columnWidth, ParagraphAlignment.Right} }); } columns.Add("Net Sub Total", new Dictionary<string, ParagraphAlignment> { {"3cm", ParagraphAlignment.Right} }); foreach (var item in columns) { Column column = table.AddColumn(item.Value.First().Key); column.Format.Alignment = item.Value.First().Value; } Row row = table.AddRow(); row.HeadingFormat = true; row.Format.Alignment = ParagraphAlignment.Center; row.Format.Font.Bold = true; row.Shading.Color = tableColor; row.TopPadding = 2; row.BottomPadding = 2; int rowId = 0; foreach (var item in columns) { row.Cells[rowId].AddParagraph(item.Key); row.Cells[rowId].Format.Alignment = ParagraphAlignment.Center; rowId++; } table.SetEdge(0, 0, 5, 1, Edge.Box, BorderStyle.Single, 0.75, Color.Empty); }
///<summary>Only supports horizontal and vertical lines. Assumes single width, no dashes.</summary> public static void DrawLine(TextFrame frameContainer, System.Drawing.Color color, float x1, float y1, float x2, float y2) { MigraDoc.DocumentObjectModel.Color colorx = ConvertColor(color); TextFrame frameRect = new TextFrame(); TextFrame frame = new TextFrame(); //nearly as big as frameContainer. Its margins will position the line. frameRect.LineFormat.Color = colorx; if (x1 == x2) //vertical { frameRect.Width = Unit.FromPoint(.01); frame.MarginLeft = Unit.FromInch(x1 / 100); if (y2 > y1) //draw down { frameRect.Height = Unit.FromInch((y2 - y1) / 100); frame.MarginTop = Unit.FromInch(y1 / 100); } else //draw up { frameRect.Height = Unit.FromInch((y1 - y2) / 100); frame.MarginTop = Unit.FromInch(y2 / 100); } } else if (y1 == y2) //horizontal { frameRect.Height = Unit.FromPoint(.01); frame.MarginTop = Unit.FromInch(y1 / 100); if (x2 > x1) //right { frameRect.Width = Unit.FromInch((x2 - x1) / 100); frame.MarginLeft = Unit.FromInch(x1 / 100); } else //draw left { frameRect.Width = Unit.FromInch((x1 - x2) / 100); frame.MarginLeft = Unit.FromInch(x2 / 100); } } else { return; //diagonal lines not supported. } frame.Elements.Add(frameRect); frame.RelativeVertical = RelativeVertical.Page; frame.RelativeHorizontal = RelativeHorizontal.Page; frame.Top = TopPosition.Parse("0 in"); frame.Left = LeftPosition.Parse("0 in"); frame.Width = frameContainer.Width; Unit bottom = Unit.Zero; if (y1 > y2) { bottom = Unit.FromInch(y1 / 100); } else { bottom = Unit.FromInch(y2 / 100); } if (frameContainer.Height < bottom) { frameContainer.Height = bottom; } frame.Height = frameContainer.Height; frameContainer.Elements.Add(frame); }
private void btnStartTest_Click(object sender, EventArgs e) { // start creating the PDF file Document docPDFResults = new Document(); Section sectionMain = docPDFResults.AddSection(); // instantiate data class for dummy data ComponentChecklist componentChecklist = new ComponentChecklist(); try { int count = 0; Boolean isTestContinuing = true; Form2 form = new Form2(); // load values into the form txtbxMachine.Text = componentChecklist.Machine; txtbxIDNumber.Text = componentChecklist.IDNum; // start creating the PDF document Paragraph paragraphTest = sectionMain.AddParagraph(); paragraphTest.AddText("Test Results: " + txtbxMachine.Text + ", ID Number: " + txtbxIDNumber.Text + "\n" + System.DateTime.Now.ToString() + "\n\n"); while ((count < componentChecklist.TestsAndDirections.Length / 2) && (isTestContinuing == true)) { // get test txtbxTest.Text = componentChecklist.TestsAndDirections[count, 0]; form.testValue = componentChecklist.TestsAndDirections[count, 0]; // get directions txtbxDirection.Text = componentChecklist.TestsAndDirections[count, 1]; DialogResult result = form.ShowDialog(); if (result == DialogResult.Yes) { // the test result passed... string strPassResult = "PASSED: " + txtbxTest.Text; // result is in the listbox... displayResult(strPassResult); // and in the PDF document MigraDoc.DocumentObjectModel.Color colorGreen = MigraDoc.DocumentObjectModel.Color.FromCmyk(100, 0, 100, 50); addResultToPDF(sectionMain, colorGreen, strPassResult); count++; } else if (result == DialogResult.No) { // the test result failed... string strFailResult = "FAILED: " + txtbxTest.Text; // listbox result displayResult(strFailResult); // PDF document result MigraDoc.DocumentObjectModel.Color colorRed = MigraDoc.DocumentObjectModel.Color.FromCmyk(0, 100, 100, 0); addResultToPDF(sectionMain, colorRed, strFailResult); count++; } else if (result == DialogResult.Ignore) { // this test was skipped... string strSkipResult = "SKIPPED: " + txtbxTest.Text; // listbox result displayResult(strSkipResult); // PDF document result MigraDoc.DocumentObjectModel.Color colorBlack = MigraDoc.DocumentObjectModel.Color.FromCmyk(0, 0, 0, 100); addResultToPDF(sectionMain, colorBlack, strSkipResult); count++; } else if (result == DialogResult.Cancel) { // the entire test suite was canceled string strCancelResult = "CANCELED: " + txtbxTest.Text; // listbox result displayResult(strCancelResult); // PDF document result MigraDoc.DocumentObjectModel.Color colorBlack = MigraDoc.DocumentObjectModel.Color.FromCmyk(0, 0, 0, 100); addResultToPDF(sectionMain, colorBlack, strCancelResult); isTestContinuing = false; } } disableButtons(); // finish PDF document PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(); pdfRenderer.Document = docPDFResults; pdfRenderer.RenderDocument(); string filename = "TestResults.pdf"; pdfRenderer.PdfDocument.Save(filename); Process.Start(filename); } catch (Exception exception) { MessageBox.Show("Error: " + exception.ToString()); } }
public IActionResult OnGet(string pdf) { //get the session first! UserName = HttpContext.Session.GetString(SessionKeyName1); FirstName = HttpContext.Session.GetString(SessionKeyName2); SessionID = HttpContext.Session.GetString(SessionKeyName3); DatabaseConnect dbstring = new DatabaseConnect(); //creating an object from the class string DbConnection = dbstring.DatabaseString(); //calling the method from the class Console.WriteLine(DbConnection); SqlConnection conn = new SqlConnection(DbConnection); conn.Open(); using (SqlCommand command = new SqlCommand()) { command.Connection = conn; command.CommandText = @"SELECT EmployeeID, EmpFName + ' ' + EmpLName AS EmployeeName, EmpDoB AS EmployeeBornOn, EmpHireDate AS EmployedOn FROM EmployeesTable;"; var reader = command.ExecuteReader(); Employee = new List <Employee>(); while (reader.Read()) { Employee Row = new Employee(); //each record found from the table Row.EmployeeId = reader.GetInt32(0); Row.EmployeeFirstName = reader.GetString(1); Row.EmployeeDoB = reader.GetDateTime(2); Row.EmployeeDateHired = reader.GetDateTime(3); Employee.Add(Row); } } //PDF code here! if (pdf == "1") { //Create an object for pdf document Document doc = new Document(); Section sec = doc.AddSection(); Paragraph para = sec.AddParagraph(); para.Format.Font.Name = "Arial"; para.Format.Font.Size = 14; para.Format.Font.Color = Color.FromCmyk(0, 0, 0, 100); //black colour para.AddFormattedText("List of Employees", TextFormat.Bold); para.Format.SpaceAfter = "1.0cm"; para.AddFormattedText(); //Table Table tab = new Table(); tab.Borders.Width = 0.75; tab.TopPadding = 5; tab.BottomPadding = 5; //Column Column col = tab.AddColumn(Unit.FromCentimeter(3)); col.Format.Alignment = ParagraphAlignment.Center; tab.AddColumn(Unit.FromCentimeter(3)); tab.AddColumn(Unit.FromCentimeter(3)); tab.AddColumn(Unit.FromCentimeter(3)); //tab.AddColumn(Unit.FromCentimeter(3)); //Row Row row = tab.AddRow(); row.Shading.Color = Colors.Coral;//select your preference colour! //Cell for header Cell cell = new Cell(); cell = row.Cells[0]; cell.AddParagraph("EmployeeID"); cell = row.Cells[1]; cell.AddParagraph("Employee First Name"); cell = row.Cells[2]; cell.AddParagraph("Employee Date of Birth"); cell = row.Cells[3]; cell.AddParagraph("Employee Hire Date"); //Add data to table for (int i = 0; i < Employee.Count; i++) { row = tab.AddRow(); cell = row.Cells[0]; cell.AddParagraph(Convert.ToString(i + 1)); cell = row.Cells[1]; cell.AddParagraph(Employee[i].EmployeeFirstName); cell = row.Cells[2]; cell.AddParagraph(Convert.ToString(Employee[i].EmployeeDateHired)); cell = row.Cells[3]; cell.AddParagraph(Convert.ToString(Employee[i].EmployeeDoB)); } tab.SetEdge(0, 0, 4, (Employee.Count + 1), Edge.Box, BorderStyle.Single, 1.5, Colors.Black); sec.Add(tab); //Rendering PdfDocumentRenderer pdfRen = new PdfDocumentRenderer(); pdfRen.Document = doc; pdfRen.RenderDocument(); //Create a memory stream MemoryStream stream = new MemoryStream(); pdfRen.PdfDocument.Save(stream); //saving the file into the stream Response.Headers.Add("content-disposition", new[] { "inline; filename = ListofEmployees.pdf" }); return(File(stream, "application/pdf")); } return(Page()); }
/// <summary> /// Writes the value of <paramref name="repeater"/> to the <paramref name="table"/>. /// </summary> /// <param name="table">The <see cref="MigraDoc.DocumentObjectModel.Tables.Table"/> to write to.</param> /// <param name="repeater">The control.</param> /// <param name="container">The application data container.</param> /// <param name="level">The depth down the control tree being rendered (affects indenting).</param> private void WriteValueRepeater(Table table, RepeaterControl repeater, Dictionary<string, object> container, int level) { Color backgroundColour = new Color(100 - (level * PdfConstants.AlphaMultiplier * 100), this.BorderColour.C, this.BorderColour.M, this.BorderColour.Y, this.BorderColour.K); table.AppendHeaderRow(this.DefaultStyle.Font.Size, repeater.Label, PdfResources.StyleNameHeaderRow, backgroundColour) .Format.LeftIndent = new Unit(PdfConstants.IndentMultiplier * level, UnitType.Millimeter); if (((object[])container[repeater.Name]).Length == 0) { return; } Dictionary<string, object>[] items = (Dictionary<string, object>[])container[repeater.Name]; level = level < 5 ? level + 1 : level; foreach (Dictionary<string, object> repeaterItem in items) { this.WriteValue(table, repeater.Controls, repeaterItem, level); } }
/// <summary> /// Writes the value of <paramref name="group"/> to the <paramref name="table"/>. /// </summary> /// <param name="table">The <see cref="MigraDoc.DocumentObjectModel.Tables.Table"/> to write to.</param> /// <param name="group">The control.</param> /// <param name="container">The application data container.</param> /// <param name="level">The depth down the control tree being rendered (affects indenting).</param> private void WriteValueGroup(Table table, GroupControl group, Dictionary<string, object> container, int level) { Color backgroundColour = new Color(100 - (level * PdfConstants.AlphaMultiplier * 100), this.BorderColour.C, this.BorderColour.M, this.BorderColour.Y, this.BorderColour.K); table.AppendHeaderRow(this.DefaultStyle.Font.Size, group.Label, PdfResources.StyleNameHeaderRow, backgroundColour) .Format.LeftIndent = new Unit(PdfConstants.IndentMultiplier * level, UnitType.Millimeter); level = level < 5 ? level + 1 : level; this.WriteValue(table, group.Controls, container, level); }
private void StyleDoc(Document doc) { Color green = new Color(108, 179, 63), brown = new Color(88, 71, 76), lightbrown = new Color(150, 132, 126); var body = doc.Styles["Normal"]; body.Font.Size = Unit.FromInch(0.14); body.Font.Color = new Color(51, 51, 51); body.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple; body.ParagraphFormat.LineSpacing = 1.25; body.ParagraphFormat.SpaceAfter = 10; var footer = doc.Styles["Footer"]; footer.Font.Size = Unit.FromInch(0.125); footer.Font.Color = green; var h1 = doc.Styles["Heading1"]; h1.Font.Color = green; h1.Font.Bold = true; h1.Font.Size = Unit.FromPoint(15); var h2 = doc.Styles["Heading2"]; h2.Font.Color = green; h2.Font.Bold = true; h2.Font.Size = Unit.FromPoint(13); var h3 = doc.Styles["Heading3"]; h3.Font.Bold = true; h3.Font.Color = Colors.Black; h3.Font.Size = Unit.FromPoint(11); var links = doc.Styles["Hyperlink"]; links.Font.Color = green; var unorderedlist = doc.AddStyle("UnorderedList", "Normal"); var listInfo = new ListInfo(); listInfo.ListType = ListType.BulletList1; unorderedlist.ParagraphFormat.ListInfo = listInfo; unorderedlist.ParagraphFormat.LeftIndent = "1cm"; unorderedlist.ParagraphFormat.FirstLineIndent = "-0.5cm"; unorderedlist.ParagraphFormat.SpaceAfter = 0; var orderedlist = doc.AddStyle("OrderedList", "UnorderedList"); orderedlist.ParagraphFormat.ListInfo.ListType = ListType.NumberList1; // for list spacing (since MigraDoc doesn't provide a list object that we can target) var listStart = doc.AddStyle("ListStart", "Normal"); listStart.ParagraphFormat.SpaceAfter = 0; listStart.ParagraphFormat.LineSpacing = 0.5; var listEnd = doc.AddStyle("ListEnd", "ListStart"); listEnd.ParagraphFormat.LineSpacing = 1; var hr = doc.AddStyle("HorizontalRule", "Normal"); var hrBorder = new Border(); hrBorder.Width = "1pt"; hrBorder.Color = Colors.DarkGray; hr.ParagraphFormat.Borders.Bottom = hrBorder; hr.ParagraphFormat.LineSpacing = 0; hr.ParagraphFormat.SpaceBefore = 15; }
private Table SetTableStyle(ref Section section, Color tableColor) { TextFrame frame = section.AddTextFrame(); frame.MarginTop = new Unit(6, UnitType.Centimeter); frame.Width = new Unit(16, UnitType.Centimeter); //TABLE LABEL Paragraph p = frame.AddParagraph(); p.AddFormattedText("Purchased goods:", TextFormat.Bold); frame.AddParagraph("").AddLineBreak(); //TABLE Table table = frame.AddTable(); table.Style = "Table"; table.Borders.Color = tableColor; table.Borders.Width = 0.25; table.Borders.Left.Width = 0.5; table.Borders.Right.Width = 0.5; table.Rows.LeftIndent = 0; return table; }