예제 #1
0
        ///<summary></summary>
        public static void DrawString(TextFrame frameContainer, string text, MigraDoc.DocumentObjectModel.Font font, RectangleF rectF,
                                      ParagraphAlignment alignment)
        {
            TextFrame frame = new TextFrame();
            Paragraph par   = frame.AddParagraph();

            par.Format.Font      = font.Clone();
            par.Format.Alignment = alignment;
            par.AddText(text);
            frame.RelativeVertical   = RelativeVertical.Page;
            frame.RelativeHorizontal = RelativeHorizontal.Page;
            frame.MarginLeft         = Unit.FromInch(rectF.Left / 100);
            frame.MarginTop          = Unit.FromInch(rectF.Top / 100);
            frame.Top   = TopPosition.Parse("0 in");
            frame.Left  = LeftPosition.Parse("0 in");
            frame.Width = Unit.FromInch(rectF.Right / 100f);        //    frameContainer.Width;
            Unit bottom = Unit.FromInch(rectF.Bottom / 100f);

            if (frameContainer.Height < bottom)
            {
                frameContainer.Height = bottom;
            }
            frame.Height = frameContainer.Height;
            //LineFormat lineformat=new LineFormat();
            //lineformat.Width=1;
            //frame.LineFormat=lineformat;
            frameContainer.Elements.Add(frame);
        }
예제 #2
0
        ///<summary></summary>
        public static void DrawString(TextFrame frameContainer, string text, MigraDoc.DocumentObjectModel.Font font, float xPos, float yPos)
        {
            TextFrame frame = new TextFrame();
            Paragraph par   = frame.AddParagraph();

            par.Format.Font = font.Clone();
            par.AddText(text);
            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;
            FontStyle fontstyle = FontStyle.Regular;

            if (font.Bold)
            {
                fontstyle = FontStyle.Bold;
            }
            System.Drawing.Font fontSystem = new System.Drawing.Font(font.Name, (float)font.Size.Point, fontstyle);
            float fontH  = fontSystem.Height;
            Unit  bottom = Unit.FromInch((yPos + fontH) / 100);

            if (frameContainer.Height < bottom)
            {
                frameContainer.Height = bottom;
            }
            frame.Height = frameContainer.Height;
            //LineFormat lineformat=new LineFormat();
            //lineformat.Width=1;
            //frame.LineFormat=lineformat;
            frameContainer.Elements.Add(frame);
        }
예제 #3
0
파일: PDFHelper.cs 프로젝트: rjakech/SCMS
        public static Section AddHeading(this Section section, string heading, Action <TextFrame, Paragraph> callback = null)
        {
            var headingTextFrame = section.AddTextFrame();
            var tmpHeadParagraph = headingTextFrame.AddParagraph(heading);

            headingTextFrame.Top              = ShapePosition.Top;
            tmpHeadParagraph.Format.Font      = CallibriFont11.Clone();
            tmpHeadParagraph.Format.Font.Size = 15.PixelsToUnitPoints();

            if (callback != null)
            {
                callback(headingTextFrame, tmpHeadParagraph);
            }

            return(section);
        }
예제 #4
0
        public virtual ActionResult RunReportToPdf(ReportParameters parms)
        {
            using (var db = NewDbContext())
            {
                IDbCommand cmd;
                double     fontSize = 9;

                try
                {
                    cmd = ReportCommand(parms);
                }
                catch (Exception ex)
                {
                    throw;
                }
                db.Database.Connection.Open();
                cmd.Connection = (IDbConnection)db.Database.Connection;

                PdfDocument pdfDocument = new PdfDocument();
                pdfDocument.Info.Title    = parms.ReportName;
                pdfDocument.Info.Author   = User.Identity.GetUserName();
                pdfDocument.Info.Subject  = parms.ReportDescription;
                pdfDocument.Info.Keywords = "";
                PdfPage page = pdfDocument.AddPage();
                page.Size = PdfSharp.PageSize.Letter;

                Document doc = new Document();
                Section  sec = doc.AddSection();
                sec.PageSetup.PageFormat     = PageFormat.Letter;
                sec.PageSetup.StartingNumber = 1;
                sec.PageSetup.RightMargin    = ".25in";
                sec.PageSetup.LeftMargin     = ".25in";
                sec.PageSetup.TopMargin      = ".75in";
                sec.PageSetup.BottomMargin   = ".25in";
                // 72 units per inch
                Style style = doc.Styles["Normal"];
                style.Font.Name           = "Verdana";
                page.Orientation          = (parms.PdfOrientation == "L") ? PdfSharp.PageOrientation.Landscape : PdfSharp.PageOrientation.Portrait;
                sec.PageSetup.Orientation = (parms.PdfOrientation == "L") ? Orientation.Landscape : Orientation.Portrait;

                double[] columnWidth = CalculateColumnWidths(parms, cmd, page, fontSize);
                double   availWidth  = page.Width.Point - sec.PageSetup.RightMargin.Point - sec.PageSetup.LeftMargin.Point - columnWidth.Length * 4;

                if (columnWidth.Sum() > availWidth)
                {
                    //page.Orientation = PdfSharp.PageOrientation.Landscape;
                    //sec.PageSetup.Orientation = Orientation.Landscape;
                    availWidth = page.Width.Point - sec.PageSetup.RightMargin.Point - sec.PageSetup.LeftMargin.Point - columnWidth.Length * 4;
                    while (columnWidth.Sum() > availWidth && fontSize > 5)
                    {
                        fontSize    = fontSize - 1;
                        columnWidth = CalculateColumnWidths(parms, cmd, page, fontSize);
                    }
                }
                var blankCol = Array.FindIndex(parms.ColumnNames, c => c.Equals("blank"));
                if (blankCol >= 0)
                {
                    double unused = availWidth - columnWidth.Sum();
                    columnWidth[blankCol] += unused;
                }

                var titleFont = new MigraDoc.DocumentObjectModel.Font("Verdana", 15);
                titleFont.Bold = true;
                var font = new MigraDoc.DocumentObjectModel.Font("Verdana", fontSize);

                var frame = sec.Headers.Primary.AddTextFrame();
                frame.Width            = page.Width.Point - sec.PageSetup.RightMargin.Point - sec.PageSetup.LeftMargin.Point;
                frame.Top              = ".25in";
                frame.Left             = 0;
                frame.RelativeVertical = MigraDoc.DocumentObjectModel.Shapes.RelativeVertical.Page;

                Paragraph paragraph = frame.AddParagraph();
                paragraph.Format.Font      = titleFont;
                paragraph.Format.Alignment = ParagraphAlignment.Center;
                paragraph.AddText(parms.ReportName);
                paragraph                  = frame.AddParagraph();
                paragraph.Format.Font      = font;
                paragraph.Format.Alignment = ParagraphAlignment.Center;
                paragraph.AddText(parms.ReportDescription);

                frame                      = sec.Headers.Primary.AddTextFrame();
                frame.Height               = ".2in";
                frame.Width                = "1.5in";
                frame.Top                  = ".30in";
                frame.Left                 = (page.Width.Point - sec.PageSetup.LeftMargin.Point - sec.PageSetup.RightMargin.Point - frame.Width.Point);
                frame.RelativeVertical     = MigraDoc.DocumentObjectModel.Shapes.RelativeVertical.Page;
                paragraph                  = frame.AddParagraph();
                paragraph.Format.Font      = font.Clone();
                paragraph.Format.Alignment = ParagraphAlignment.Right;
                paragraph.AddText("Page ");
                paragraph.AddPageField();
                paragraph.AddText(" of ");
                paragraph.AddNumPagesField();

                Table table = sec.AddTable();
                table.Style               = "Table";
                table.Borders.Color       = MigraDoc.DocumentObjectModel.Colors.DarkGray;
                table.Borders.Width       = 0.25;
                table.Borders.Left.Width  = 0.25;
                table.Borders.Right.Width = 0.25;
                table.Rows.LeftIndent     = 0;
                table.Rows.Alignment      = RowAlignment.Center;
                table.Format.Font         = new MigraDoc.DocumentObjectModel.Font("Verdana", fontSize);
                for (int x = 0; x < parms.ColumnNames.Length; x++)
                {
                    PdfTableColumnAdd(table, columnWidth[x], parms.ColumnNames[x]);
                }

                Row row = table.AddRow();
                row.HeadingFormat    = true;
                row.Format.Font.Bold = true;
                row.Shading.Color    = MigraDoc.DocumentObjectModel.Colors.LightGray;

                for (int x = 0; x < parms.ColumnNames.Length; x++)
                {
                    switch (parms.ColumnNames[x])
                    {
                    default:
                        row.Cells[x].AddParagraph(parms.ColumnHeadings[x]);
                        break;
                    }
                }

                using (var reader = cmd.ExecuteReader())
                {
                    int rowCnt = 0;
                    while (reader.Read())
                    {
                        try
                        {
                            rowCnt++;
                            row = table.AddRow();
                            for (int i = 0; i < parms.ColumnNames.Length; i++)
                            {
                                switch (reader[i].GetType().Name)
                                {
                                case "Boolean":
                                    row.Cells[i].AddParagraph(BoolToString((bool)reader[i]));
                                    break;

                                case "DateTime":
                                    row.Cells[i].AddParagraph(Zpm.FormatDate(reader[i]));
                                    break;

                                default:
                                    row.Cells[i].AddParagraph(reader[i].ToString());
                                    break;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            throw;
                        }
                    }
                }

                PdfDocumentRenderer renderer = new PdfDocumentRenderer(true);
                renderer.Document = doc;
                renderer.RenderDocument();

                string reportHandle = Guid.NewGuid().ToString();
                using (MemoryStream ms = new MemoryStream())
                {
                    renderer.PdfDocument.Save(ms);
                    TempData[reportHandle] = ms.ToArray();
                }
                return(Json(reportHandle));
            }
        }