예제 #1
0
        /// <summary>
        /// Add Test Suite information to document
        /// </summary>
        /// <param name="myDocument">Document</param>
        /// <param name="Suite">Test Suite</param>
        /// <param name="aChapter">Current chapter</param>
        private void AddTestSuiteInfo(Document myDocument, ONVIF_TestCases.TestCases_Class.TestSuite_Type Suite, ref Chapter aChapter)
        {
            Section section;
            int     sectionNumber = 1;

            //myDocument.NewPage();
            // go throug the suite and display all the test info
            if (Suite.Description != null)
            {
                Paragraph p0 = new Paragraph(new Chunk(Suite.Description + "\n\n", FontFactory.GetFont(FontFactory.TIMES, 16)));
                section = aChapter.AddSection(p0);
            }
            else
            {
                Paragraph p0 = new Paragraph(new Chunk("Test Suite - " + SuiteCounter.ToString() + "\n\n", FontFactory.GetFont(FontFactory.TIMES, 16)));
                section = aChapter.AddSection(p0);
            }

            SuiteCounter++;
            section.NumberDepth = 0;
            myDocument.Add(section);
            //myDocument.Add(new iTextSharp.text.Paragraph(Suite.Description + Environment.NewLine));

            if (Suite.Tests != null)
            {
                foreach (ONVIF_TestCases.TestCases_Class.Test_Type Test in Suite.Tests)
                {
                    AddTestTestInfo(myDocument, Test, ref section, ++sectionNumber);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Generates the document.
        /// </summary>
        public void GenerateDocument()
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Test.pdf";

            float    margin = Utilities.MillimetersToPoints(Convert.ToSingle(10));
            Document doc    = new Document(iTextSharp.text.PageSize.A4, margin, margin, margin, margin);

            doc.SetMargins(20, 20, 100, 0);
            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create));

            writer.SetFullCompression();
            writer.CloseStream = true;

            doc.Open();
            doc.NewPage();

            Chapter chapter1 = new Chapter(new Paragraph(string.Empty), 0);

            chapter1.NumberDepth = 0;
            Section   section1 = chapter1.AddSection(0f, "Basic Informations", 1);
            PdfPTable table1   = this.CreateBasicInfoTable();

            table1.SpacingAfter  = 20.0f;
            table1.SpacingBefore = 15.0f;
            section1.Add(table1);

            Section   section2 = chapter1.AddSection(0f, "Rally Length", 1);
            PdfPTable table2   = this.CreateRallyLengthTable();

            table2.SpacingAfter  = 20.0f;
            table2.SpacingBefore = 15.0f;
            section2.Add(table2);

            Chapter chapter2 = new Chapter(new Paragraph(string.Empty), 0);

            chapter2.NumberDepth = 0;
            Section          section3 = chapter2.AddSection(0f, "Transition Matrix", 1);
            List <PdfPTable> tables   = this.CreateTransitionMatrixTables();

            foreach (PdfPTable table in tables)
            {
                table.SpacingAfter  = 20.0f;
                table.SpacingBefore = 15.0f;
                section3.Add(table);
            }

            doc.Add(chapter1);
            doc.Add(chapter2);
            if (doc != null)
            {
                doc.Close();
            }

            doc = null;
            Process.Start(path);
        }
        private void AddSectionCommandTask()
        {
            Console.Write("請輸入新章節ID(輸入cancel取消): ");
            int    sectionID   = 0;
            string inputString = Console.ReadLine();

            if (inputString != "cancel")
            {
                while (inputString != "cancel" && (!int.TryParse(inputString, out sectionID) || editingChapter.ContainsSection(sectionID)))
                {
                    if (editingChapter.ContainsSection(sectionID))
                    {
                        Console.Write("ID已存在 請輸入新章節ID(整數)(輸入cancel取消): ");
                    }
                    else
                    {
                        Console.Write("不合法的輸入 請輸入新章節ID(整數)(輸入cancel取消): ");
                    }
                    inputString = Console.ReadLine();
                }
                if (inputString != "cancel")
                {
                    Console.Write("請輸入章節名稱: ");
                    string sectionName = Console.ReadLine();
                    editingChapter.AddSection(new Section(sectionID, sectionName));
                    ViewCommandTask();
                }
            }
        }
예제 #4
0
 public CreateSection(Chapter chapter, Paragraph title, int numberDepth)
 {
     title.Font.Size = 13;
     title.SetLeading(2.0f, 2.0f);
     section = chapter.AddSection(20f, title, numberDepth);
     section.IndentationLeft = 20f;
 }
예제 #5
0
        /// <summary>
        /// Adds test group info.
        /// </summary>
        private void AddTestGroupInfo()
        {
            Document.NewPage();

            Paragraph title   = new Paragraph("ONVIF TEST", FontFactory.GetFont(FontFactory.TIMES, 18));
            Chapter   chapter = new Chapter(title, 2);

            chapter.NumberDepth = 0;
            Paragraph empty = new Paragraph("\n\n");

            chapter.Add(empty);

            string  lastGroup = string.Empty;
            Section section   = null;

            foreach (TestInfo info in Log.TestResults.Keys.OrderBy(T => T.Category).ThenBy(TI => TI.Order))
            {
                string group = info.Group.Split('\\')[0];
                if (lastGroup != group)
                {
                    Paragraph groupTitle = new Paragraph(string.Format("\n\n{0}", group), FontFactory.GetFont(FontFactory.TIMES, 18));
                    section             = chapter.AddSection(groupTitle);
                    section.NumberDepth = 0;
                    Paragraph someSectionText = new Paragraph("\n");
                    section.Add(someSectionText);
                    lastGroup = group;
                }

                Paragraph testTitle = new Paragraph(info.Name);
                Anchor    ancor     = new Anchor(".");
                ancor.Name = info.Name;
                testTitle.Add(ancor);
                Section testSection = section.AddSection(testTitle);
                testSection.NumberDepth = 0;

                if (info.RequirementLevel == RequirementLevel.Optional)
                {
                    Paragraph optional = new Paragraph("* Optional Test");
                    testSection.Add(optional);
                }

                string testDescription = string.Empty;

                if (Log.TestResults.ContainsKey(info))
                {
                    testDescription = string.Format("\nTestResult\n{0}\n", Log.TestResults[info].ShortTextLog);
                    testDescription = testDescription.Replace(string.Format("{0}\r\n", info.Name), "");
                }
                else
                {
                    testDescription = "Test not run\n\n";
                }

                Paragraph sectionText = new Paragraph(testDescription, FontFactory.GetFont(FontFactory.TIMES, 11));
                testSection.Add(sectionText);
            }

            Document.Add(chapter);
        }
예제 #6
0
        //简单使用
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Document  document  = new Document();
            PdfWriter pdfWriter = PdfWriter.GetInstance(document, new FileStream("简单使用.pdf",
                                                                                 FileMode.OpenOrCreate));

            document.Open();

            PdfContentByte cb = pdfWriter.DirectContent; //最上方

            //画线
            cb.SetLineWidth(2);
            cb.MoveTo(0, 0);  //以当前页左下角为原点
            cb.LineTo(200, 300);

            cb.Stroke();

            //在某个具体位置绘制文本
            BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252,
                                              BaseFont.NOT_EMBEDDED);

            cb.BeginText();
            cb.SetFontAndSize(bf, 12);
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "This text is centered",
                               0, 100, 0);
            cb.EndText();

            //为了测试,添加3章
            for (int i = 0; i < 10; i++)
            {
                //章节的标题
                Paragraph cTitle = new Paragraph($"This is chapter {i}", FontFactory.GetFont(
                                                     FontFactory.COURIER, 18));
                Chapter chapter = new Chapter(cTitle, i);

                //如果是偶数章节书签默认不打开
                if (i % 2 == 0)
                {
                    chapter.BookmarkOpen = false;
                }

                //每章添加2个区域
                for (int j = 0; j < 60; j++)
                {
                    //子区域
                    Paragraph sTitle = new Paragraph($"This is section {j} in chapter {i}",
                                                     FontFactory.GetFont(FontFactory.COURIER, 12, BaseColor.BLUE));

                    Section section = chapter.AddSection(sTitle, 2);  //第二个参数表示树的深度
                }

                document.Add(chapter);
            }

            document.Close();
            Title = "简单使用";
        }
예제 #7
0
        public static void ExportTestSettings(Chapter chapter, Font font, int flowCount, long recordCount, float randomness)
        {
            Section settings = chapter.AddSection(new Paragraph("Test settings.", font));

            string version = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion;

            settings.Add(new Paragraph(String.Format("\t \t Benchmark - {0}", version)));
            settings.Add(new Paragraph(String.Format("\t \t Export date - {0}", DateTime.Now)));
            settings.Add(new Chunk("\n"));

            Section testSettings = chapter.AddSection(new Paragraph("Settings.", font));

            testSettings.Add(new Paragraph(String.Format("\t \t Flow count - {0}", flowCount)));
            testSettings.Add(new Paragraph(String.Format("\t \t Record count - {0}", recordCount)));
            testSettings.Add(new Paragraph(String.Format("\t \t Randomness - {0}%", randomness * 100)));
            testSettings.Add(new Paragraph(String.Format("\t \t Key type - {0}", randomness == 0f ? KeysType.Sequential : KeysType.Random)));
            testSettings.Add(new Chunk("\n"));
        }
예제 #8
0
    static int AddSection(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 2);
        Chapter obj  = (Chapter)LuaScriptMgr.GetNetObjectSelf(L, 1, "Chapter");
        uint    arg0 = (uint)LuaScriptMgr.GetNumber(L, 2);
        bool    o    = obj.AddSection(arg0);

        LuaScriptMgr.Push(L, o);
        return(1);
    }
예제 #9
0
        private void AddTestGroupInfo()
        {
            m_Document.NewPage();

            //Добавление основного заголовка ONVIF TEST
            Paragraph title    = new Paragraph("ONVIF TEST", FontFactory.GetFont(FontFactory.TIMES, 18));
            Chapter   chapter2 = new Chapter(title, 2);

            //Для отсутсвия нумерации
            chapter2.NumberDepth = 0;
            Paragraph someText = new Paragraph("\n\n");

            chapter2.Add(someText);

            //Добавление группы тестов
            Paragraph title1   = new Paragraph("Device Discovery Test Cases", FontFactory.GetFont(FontFactory.TIMES, 18));
            Section   section1 = chapter2.AddSection(title1);

            section1.NumberDepth = 0;
            Paragraph someSectionText = new Paragraph("\n\n");

            section1.Add(someSectionText);

            //Добавление теста
            Paragraph title11 = new Paragraph("8.1.1 - MULTICAST NVT HELLO MESSAGE");
            //Добавление конечной точки для ссылки из содержания
            Anchor anc11 = new Anchor(".");

            anc11.Name = "MULTICAST NVT HELLO MESSAGE";
            title11.Add(anc11);
            Section section11 = section1.AddSection(title11);

            section11.NumberDepth = 0;
            Paragraph someSectionText11 = new Paragraph("Test Results\nTest Marked as Skipped\nSkipping all test steps\nTest complete\nTest SKIPPED\n\n");

            section11.Add(someSectionText11);

            //Добавление теста
            Paragraph title12 = new Paragraph("8.1.2 - MULTICAST NVT HELLO MESSAGE VALIDATION");
            Anchor    anc12   = new Anchor(".");

            anc12.Name = "MULTICAST NVT HELLO MESSAGE VALIDATION";
            title12.Add(anc12);
            Section section12 = section1.AddSection(title12);

            section12.NumberDepth = 0;
            Paragraph someSectionText12 = new Paragraph("Test Results\nTest Marked as Skipped\nSkipping all test steps\nTest complete\nTest SKIPPED\n\n");

            section12.Add(someSectionText12);


            m_Document.Add(chapter2);
        }
예제 #10
0
        /// <summary>
        /// Create chapter content from html
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public Chapter CreateChapterContent(string html)
        {
            // Declare a font to used for the bookmarks
            iTextSharp.text.Font bookmarkFont = FontFactory.GetFont(FontFactory.HELVETICA, 16, iTextSharp.text.Font.NORMAL);

            Chapter chapter = new Chapter(new Paragraph(""), 0);

            chapter.NumberDepth = 0;

            // Create css for some tag
            StyleSheet styles = new StyleSheet();

            styles.LoadTagStyle("h2", HtmlTags.ALIGN_MIDDLE, "center");
            styles.LoadTagStyle("h2", HtmlTags.COLOR, "#F90");
            styles.LoadTagStyle("pre", "size", "10pt");

            // Split H2 Html Tag
            string pattern = @"<\s*h2[^>]*>(.*?)<\s*/h2\s*>";

            string[] result = Regex.Split(html, pattern);

            // Create section title & content
            int sectionIndex = 0;

            foreach (var item in result)
            {
                if (string.IsNullOrEmpty(item))
                {
                    continue;
                }

                if (sectionIndex % 2 == 0)
                {
                    chapter.AddSection(20f, new Paragraph(item, bookmarkFont), 0);
                }
                else
                {
                    foreach (IElement element in HTMLWorker.ParseToList(new StringReader(item), styles))
                    {
                        chapter.Add(element);
                    }
                }

                sectionIndex++;
            }

            chapter.BookmarkTitle = "Demo for Load More Button in Kendo UI Grid";
            return(chapter);
        }
예제 #11
0
        /// <summary>
        /// Create chapter content from html
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public Chapter CreateChapterContent(string html)
        {
            // Declare a font to used for the bookmarks
            var bookmarkFont = FontFactory.GetFont(FontFactory.HELVETICA, 16, Font.NORMAL, new Color(255, 153, 0));

            var chapter = new Chapter(new Paragraph(""), 0)
            {
                NumberDepth = 0
            };

            // Create css for some tag
            var styles = new StyleSheet();

            styles.LoadTagStyle("h2", HtmlTags.HORIZONTALALIGN, "center");
            styles.LoadTagStyle("h2", HtmlTags.COLOR, "#F90");
            styles.LoadTagStyle("pre", "size", "10pt");

            // Split H2 Html Tag
            var pattern = @"<\s*h2[^>]*>(.*?)<\s*/h2\s*>";
            var result  = Regex.Split(html, pattern);

            // Create section title & content
            var sectionIndex = 0;

            foreach (var item in result)
            {
                if (string.IsNullOrEmpty(item))
                {
                    continue;
                }

                if (sectionIndex % 2 == 0)
                {
                    chapter.AddSection(20f, new Paragraph(item, bookmarkFont), 0);
                }
                else
                {
                    foreach (IElement element in HTMLWorker.ParseToList(new StringReader(item), styles))
                    {
                        chapter.Add(element);
                    }
                }

                sectionIndex++;
            }

            chapter.BookmarkTitle = Constants.UnaAbbr + " " + Constants.CisAbbr;
            return(chapter);
        }
예제 #12
0
        /// <summary>
        /// Build Chapter
        /// </summary>
        private void BuildChapter(ReportInfo report, Document document, PdfWriter writer, PdfContentByte cb, Font font, Chapter chapter, int chapterNum)
        {
            var sectionNum = chapterNum + 1;

            foreach (ReportFileInfo info in report.PeportFiles)
            {
                var section = chapter.AddSection(20f, new Paragraph(info.Name, GetFont()), sectionNum);
                BuildReportFile(document, writer, cb, info, section);

                if (!(info.SubFiles == null || info.SubFiles.Count == 0))
                {
                    BuildSection(document, writer, cb, font, info.SubFiles, section, sectionNum);
                }
            }
        }
예제 #13
0
        public Chap0403()
        {
            Console.WriteLine("Chapter 4 example 3: Chapters and Sections");

            // step 1: creation of a document-object
            Document document = new Document(PageSize.A4, 50, 50, 50, 50);

            try
            {
                // step 2: we create a writer that listens to the document
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap0403.pdf", FileMode.Create));
                // step 3: we open the document
                document.Open();
                // step 4: we Add content to the document
                Paragraph title1   = new Paragraph("This is Chapter 1", FontFactory.GetFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));
                Chapter   chapter1 = new Chapter(title1, 2);
                chapter1.NumberDepth = 0;
                Paragraph someText = new Paragraph("This is some text");
                chapter1.Add(someText);
                Paragraph title11         = new Paragraph("This is Section 1 in Chapter 1", FontFactory.GetFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0)));
                Section   section1        = chapter1.AddSection(title11);
                Paragraph someSectionText = new Paragraph("This is some silly paragraph in a chapter and/or section. It contains some text to test the functionality of Chapters and Section.");
                section1.Add(someSectionText);
                document.Add(chapter1);

                Paragraph title2   = new Paragraph("This is Chapter 2", FontFactory.GetFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));
                Chapter   chapter2 = new Chapter(title2, 2);
                chapter2.NumberDepth = 0;
                chapter2.Add(someText);
                Paragraph title21  = new Paragraph("This is Section 1 in Chapter 2", FontFactory.GetFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0)));
                Section   section2 = chapter2.AddSection(title21);
                section2.Add(someSectionText);
                chapter2.BookmarkOpen = false;
                document.Add(chapter2);
            }
            catch (Exception de)
            {
                Console.Error.WriteLine(de.StackTrace);
            }
            // step 5: we close the document
            document.Close();
        }
예제 #14
0
        //1. 章节和区域(解决书签功能)
        //2 .章节之间默认会换页,就算前一章只占一行,当前章另起一页
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Document document = new Document();

            PdfWriter.GetInstance(document, new FileStream("章节和区域.pdf", FileMode.OpenOrCreate));
            document.Open();

            //添加10章
            for (int i = 0; i < 10; i++)
            {
                //章节的标题
                Paragraph cTitle = new Paragraph($"This is chapter {i}", FontFactory.GetFont(
                                                     FontFactory.COURIER, 18));
                Chapter chapter = new Chapter(cTitle, i);

                //如果是偶数章节书签默认不打开
                if (i % 2 == 0)
                {
                    chapter.BookmarkOpen = false;
                }

                //每章添加20个区域
                for (int j = 0; j < 60; j++)
                {
                    //子区域
                    Paragraph sTitle = new Paragraph($"This is section {j} in chapter {i}",
                                                     FontFactory.GetFont(FontFactory.COURIER, 12, BaseColor.BLUE));

                    Section section = chapter.AddSection(sTitle, 2);  //第二个参数表示树的深度
                }

                document.Add(chapter);
            }

            document.Close();
            Title = "章节和区域";
        }
예제 #15
0
        //public void PDModel2Html(PDModel m)
        //{
        //    Export(m, ExportTyep.HTML);
        //}

        private void Export(IList <PDTable> tableList, string title, ExportTyep exportType)
        {
            Document  doc = new Document(PageSize.A4.Rotate(), 20, 20, 20, 20);
            DocWriter w;

            switch (exportType)
            {
            case ExportTyep.PDF:
                w = PdfWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create, FileAccess.Write));
                break;

            case ExportTyep.RTF:
                w = RtfWriter2.GetInstance(doc, new FileStream(fileName, FileMode.Create, FileAccess.Write));
                break;

            case ExportTyep.HTML:
                w = HtmlWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create, FileAccess.Write));
                break;

            default:
                break;
            }

            doc.Open();
            doc.NewPage();

            //IList<PDTable> tableList = m.AllTableList;

            //Chapter cpt = new Chapter(m.Name, 1);
            Chapter cpt = new Chapter(title, 1);
            Section sec;

            //doc.AddTitle(m.Name);
            doc.AddTitle(title);
            doc.AddAuthor("Kalman");
            doc.AddCreationDate();
            doc.AddCreator("Kalman");
            doc.AddSubject("PDM数据库文档");

            foreach (PDTable table in tableList)
            {
                sec = cpt.AddSection(new Paragraph(string.Format("{0}[{1}]", table.Name, table.Code), font));

                if (string.IsNullOrEmpty(table.Comment) == false)
                {
                    Chunk chunk = new Chunk(table.Comment, font);
                    sec.Add(chunk);
                }

                t = new Table(9, table.ColumnList.Count);

                //t.Border = 15;
                //t.BorderColor = Color.BLACK;
                //t.BorderWidth = 1.0f;
                t.AutoFillEmptyCells = true;
                t.CellsFitPage       = true;
                t.TableFitsPage      = true;
                t.Cellpadding        = 3;
                //if (exportType == ExportTyep.PDF) t.Cellspacing = 2;
                t.DefaultVerticalAlignment = Element.ALIGN_MIDDLE;

                t.SetWidths(new int[] { 200, 200, 150, 50, 50, 50, 50, 50, 300 });

                t.AddCell(BuildHeaderCell("名称"));
                t.AddCell(BuildHeaderCell("代码"));
                t.AddCell(BuildHeaderCell("数据类型"));
                t.AddCell(BuildHeaderCell("长度"));
                t.AddCell(BuildHeaderCell("精度"));
                t.AddCell(BuildHeaderCell("主键"));
                t.AddCell(BuildHeaderCell("外键"));
                t.AddCell(BuildHeaderCell("可空"));
                t.AddCell(BuildHeaderCell("注释"));

                foreach (PDColumn column in table.ColumnList)
                {
                    t.AddCell(BuildCell(column.Name));
                    t.AddCell(BuildCell(column.Code));
                    t.AddCell(BuildCell(column.DataType));
                    t.AddCell(BuildCell(column.Length == 0 ? "" : column.Length.ToString()));
                    t.AddCell(BuildCell(column.Precision == 0 ? "" : column.Precision.ToString()));
                    t.AddCell(BuildCell(column.IsPK ? " √" : ""));
                    t.AddCell(BuildCell(column.IsFK ? " √" : ""));
                    t.AddCell(BuildCell(column.Mandatory ? "" : " √"));
                    t.AddCell(BuildCell(column.Comment));
                }

                sec.Add(t);
            }

            doc.Add(cpt);
            doc.Close();
        }
예제 #16
0
        private List <SOTable> Export(DbSchema schema, SODatabase db, List <SOTable> tableList, ExportTyep exportType)
        {
            if (schema == null)
            {
                throw new ArgumentException("参数schema不能为空", "schema");
            }
            if (db == null)
            {
                throw new ArgumentException("参数dbName不能为空", "dbName");
            }

            Document  doc = new Document(PageSize.A4.Rotate(), 20, 20, 20, 20);
            DocWriter w;

            switch (exportType)
            {
            case ExportTyep.PDF:
                w = PdfWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create, FileAccess.Write));
                break;

            case ExportTyep.RTF:
                w = RtfWriter2.GetInstance(doc, new FileStream(fileName, FileMode.Create, FileAccess.Write));
                break;

            case ExportTyep.HTML:
                w = HtmlWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create, FileAccess.Write));
                break;

            default:
                break;
            }

            doc.Open();
            doc.NewPage();

            if (tableList == null)
            {
                tableList = schema.GetTableList(db);
            }

            Chapter cpt = new Chapter(db.Name, 1);
            Section sec;

            #region
            if (exportType != ExportTyep.HTML)
            {
                doc.AddTitle(db.Name);
                doc.AddAuthor("Kalman");
                doc.AddCreationDate();
                doc.AddCreator("Kalman");
                doc.AddSubject("数据库文档");
            }

            foreach (SOTable table in tableList)
            {
                sec = cpt.AddSection(new Paragraph(table.Name, font));

                if (string.IsNullOrEmpty(table.Comment) == false)
                {
                    Chunk chunk = new Chunk(table.Comment, font);
                    sec.Add(chunk);
                }

                List <SOColumn> columnList = schema.GetTableColumnList(table);

                t = new Table(7, columnList.Count);

                t.AutoFillEmptyCells = true;
                t.CellsFitPage       = true;
                t.TableFitsPage      = true;
                t.Cellpadding        = 3;
                //if (exportType == ExportTyep.PDF) t.Cellspacing = 2;
                t.DefaultVerticalAlignment = Element.ALIGN_MIDDLE;

                t.SetWidths(new int[] { 200, 150, 50, 50, 50, 100, 300 });

                t.AddCell(BuildHeaderCell("名称"));
                t.AddCell(BuildHeaderCell("数据类型"));
                t.AddCell(BuildHeaderCell("主键"));
                t.AddCell(BuildHeaderCell("标志"));
                t.AddCell(BuildHeaderCell("可空"));
                t.AddCell(BuildHeaderCell("默认值"));
                t.AddCell(BuildHeaderCell("注释"));

                foreach (SOColumn column in columnList)
                {
                    t.AddCell(BuildCell(column.Name));
                    t.AddCell(BuildCell(GetDbColumnType(column)));
                    t.AddCell(BuildCell(column.PrimaryKey ? " √" : ""));
                    t.AddCell(BuildCell(column.Identify ? " √" : ""));
                    t.AddCell(BuildCell(column.Nullable ? " √" : ""));
                    t.AddCell(BuildCell(column.DefaultValue == null ? "" : column.DefaultValue.ToString()));
                    t.AddCell(BuildCell(column.Comment));
                }

                sec.Add(t);
            }

            doc.Add(cpt);
            doc.Close();
            #endregion
            return(tableList);
        }
예제 #17
0
파일: PDFCreator.cs 프로젝트: xmatakt/DP
 /// <summary>
 /// Adds section into provided chapter
 /// </summary>
 /// <param name="chapter">Chapter in which new section will be included</param>
 /// <param name="indentation">Indentation from the left margin</param>
 /// <param name="title">Section title</param>
 /// <param name="numberDepth">Number depth of the section (if 0 => section number will not be visible)</param>
 /// <returns>PDF section</returns>
 internal Section AddSection(Chapter chapter, float indentation, string title, int numberDepth)
 {
     return(chapter.AddSection(indentation, title, numberDepth));
 }
예제 #18
0
        public static void ExportComputerSpecification(Chapter chapter, Font font, ComputerConfiguration computerInfo)
        {
            Section sectionPC = chapter.AddSection(new Paragraph("Computer specification.", font));

            sectionPC.Add(new Chunk("\n"));

            Section osSection = sectionPC.AddSection(new Paragraph("Operating System.", font));
            string  bits      = computerInfo.OperatingSystem.Is64bit ? " 64bit" : "32bit";

            osSection.Add(new Paragraph(String.Format("\t \t {0} {1}", computerInfo.OperatingSystem.Name, bits)));

            sectionPC.Add(new Chunk("\n"));

            Section processor = sectionPC.AddSection(new Paragraph("Processors.", font));

            foreach (var pr in computerInfo.Processors)
            {
                processor.Add(new Paragraph(String.Format("\t \t Name: {0}", pr.Name)));
                processor.Add(new Paragraph(String.Format("\t \t Threads: {0}", pr.Threads)));
                processor.Add(new Paragraph(String.Format("\t \t Max clock speed: {0} MHz", pr.MaxClockSpeed)));
            }

            sectionPC.Add(new Chunk("\n"));

            Section   memory = sectionPC.AddSection(new Paragraph("Memory modules.", font));
            PdfPTable table  = new PdfPTable(3);

            table.AddCell(CreateHeaderPdfPCell("Type"));
            table.AddCell(CreateHeaderPdfPCell("Capacity (GB)"));
            table.AddCell(CreateHeaderPdfPCell("Speed (MHz)"));

            foreach (var mem in computerInfo.MemoryModules)
            {
                table.AddCell(new PdfPCell(new Phrase(mem.MemoryType.ToString())));
                table.AddCell(new PdfPCell(new Phrase(mem.Capacity.ToString()))
                {
                    HorizontalAlignment = Element.ALIGN_RIGHT
                });
                table.AddCell(new PdfPCell(new Phrase(mem.Speed.ToString()))
                {
                    HorizontalAlignment = Element.ALIGN_RIGHT
                });
            }

            memory.Add(new Chunk("\n"));
            memory.Add(table);

            sectionPC.Add(new Chunk("\n"));

            Section storage = sectionPC.AddSection(new Paragraph("Storages.", font));

            table = new PdfPTable(3);

            table.AddCell(CreateHeaderPdfPCell("Model"));
            table.AddCell(CreateHeaderPdfPCell("Size (GB)"));
            table.AddCell(CreateHeaderPdfPCell("Partitions"));

            foreach (var stor in computerInfo.StorageDevices)
            {
                table.AddCell(new PdfPCell(new Phrase(stor.Model)));
                table.AddCell(new PdfPCell(new Phrase(stor.Size.ToString()))
                {
                    HorizontalAlignment = Element.ALIGN_RIGHT
                });
                table.AddCell(new PdfPCell(new Phrase(string.Join(",", stor.DriveLetters.Select(x => x.Replace(":", ""))))));
            }

            storage.Add(new Chunk("\n"));
            storage.Add(table);
        }
예제 #19
0
파일: PDFCreator.cs 프로젝트: xmatakt/DP
 /// <summary>
 /// Adds section into provided chapter
 /// </summary>
 /// <param name="chapter">Chapter in which new section will be included</param>
 /// <param name="indentation">Indentation from the left margin</param>
 /// <param name="paragraph">Paragraph for section title</param>
 /// <param name="numberDepth">Number depth of the section (if 0 => section number will not be visible)</param>
 /// <returns>PDF section</returns>
 internal Section AddSection(Chapter chapter, float indentation, Paragraph paragraph, int numberDepth)
 {
     return(chapter.AddSection(indentation, paragraph, numberDepth));
 }
예제 #20
0
        public static void Genera(string sourcePath, string content)
        {
            sourcePath = HttpContext.Current.Server.MapPath(sourcePath);
            //定义一个Document,并设置页面大小为A4,竖向
            iTextSharp.text.Document doc = new Document(PageSize.A4);
            try
            {
                //写实例
                PdfWriter.GetInstance(doc, new FileStream(sourcePath, FileMode.Create));
                //打开document
                doc.Open();


                //载入字体
                BaseFont baseFont = BaseFont.CreateFont(
                    "C:\\WINDOWS\\FONTS\\SIMHEI.TTF", //黑体
                    BaseFont.IDENTITY_H,              //横向字体
                    BaseFont.NOT_EMBEDDED);
                iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, 9);


                //写入一个段落, Paragraph
                doc.Add(new Paragraph("第一段:" + content, font));
                doc.Add(new Paragraph("这是第二段 !", font));


                #region 图片
                //以下代码用来添加图片,此时图片是做为背景加入到pdf文件当中的:

                Stream inputImageStream     = new FileStream(HttpContext.Current.Server.MapPath("~/images/logo.jpg"), FileMode.Open, FileAccess.Read, FileShare.Read);
                iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream);
                image.SetAbsolutePosition(0, 0);
                image.Alignment = iTextSharp.text.Image.UNDERLYING;    //这里可以设定图片是做为背景还是做为元素添加到文件中
                doc.Add(image);

                #endregion
                #region 其他元素
                doc.Add(new Paragraph("Hello World"));
                //另起一行。有几种办法建立一个段落,如:
                Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph.\n", FontFactory.GetFont(FontFactory.HELVETICA, 12)));
                Paragraph p2 = new Paragraph(new Phrase("This is my second paragraph.", FontFactory.GetFont(FontFactory.HELVETICA, 12)));
                Paragraph p3 = new Paragraph("This is my third paragraph.", FontFactory.GetFont(FontFactory.HELVETICA, 12));
                //所有有些对象将被添加到段落中:
                p1.Add("you can add string here\n\t");
                p1.Add(new Chunk("you can add chunks \n")); p1.Add(new Phrase("or you can add phrases.\n"));
                doc.Add(p1); doc.Add(p2); doc.Add(p3);

                //创建了一个内容为“hello World”、红色、斜体、COURIER字体、尺寸20的一个块:
                Chunk chunk = new Chunk("创建了一个内容为“hello World”、红色、斜体、COURIER字体、尺寸20的一个块", FontFactory.GetFont(FontFactory.COURIER, 20, iTextSharp.text.Font.COURIER, new iTextSharp.text.Color(255, 0, 0)));
                doc.Add(chunk);
                //如果你希望一些块有下划线或删除线,你可以通过改变字体风格简单做到:
                Chunk chunk1 = new Chunk("This text is underlined", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.UNDEFINED));
                Chunk chunk2 = new Chunk("This font is of type ITALIC | STRIKETHRU", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.ITALIC | iTextSharp.text.Font.STRIKETHRU));
                //改变块的背景
                chunk2.SetBackground(new iTextSharp.text.Color(0xFF, 0xFF, 0x00));
                //上标/下标
                chunk1.SetTextRise(5);
                doc.Add(chunk1);
                doc.Add(chunk2);

                //外部链接示例:
                Anchor anchor = new Anchor("website", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.UNDEFINED, new iTextSharp.text.Color(0, 0, 255)));
                anchor.Reference = "http://itextsharp.sourceforge.net";
                anchor.Name      = "website";
                //内部链接示例:
                Anchor anchor1 = new Anchor("This is an internal link\n\n");
                anchor1.Name = "link1";
                Anchor anchor2 = new Anchor("Click here to jump to the internal link\n\f");
                anchor2.Reference = "#link1";
                doc.Add(anchor); doc.Add(anchor1); doc.Add(anchor2);

                //排序列表示例:
                List list = new List(true, 20);
                list.Add(new ListItem("First line"));
                list.Add(new ListItem("The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?"));
                list.Add(new ListItem("Third line"));
                doc.Add(list);

                //文本注释:
                Annotation a = new Annotation("authors", "Maybe its because I wanted to be an author myself that I wrote iText.");
                doc.Add(a);

                //包含页码没有任何边框的页脚。
                iTextSharp.text.HeaderFooter footer = new iTextSharp.text.HeaderFooter(new Phrase("This is page: "), true);
                footer.Border = iTextSharp.text.Rectangle.NO_BORDER;
                doc.Footer    = footer;


                //Chapter对象和Section对象自动构建一个树:
                iTextSharp.text.Font f1 = new iTextSharp.text.Font();
                f1.SetStyle(iTextSharp.text.Font.BOLD);
                Paragraph cTitle  = new Paragraph("This is chapter 1", f1);
                Chapter   chapter = new Chapter(cTitle, 1);
                Paragraph sTitle  = new Paragraph("This is section 1 in chapter 1", f1);
                Section   section = chapter.AddSection(sTitle, 1);
                doc.Add(chapter);

                //构建了一个简单的表:
                Table aTable = new Table(4, 4);
                aTable.AutoFillEmptyCells = true;
                aTable.AddCell("2.2", new System.Drawing.Point(2, 2));
                aTable.AddCell("3.3", new System.Drawing.Point(3, 3));
                aTable.AddCell("2.1", new System.Drawing.Point(2, 1));
                aTable.AddCell("1.3", new System.Drawing.Point(1, 3));
                doc.Add(aTable);
                //构建了一个不简单的表:
                Table table = new Table(3);
                table.BorderWidth = 1;
                table.BorderColor = new iTextSharp.text.Color(0, 0, 255);
                table.Cellpadding = 5;
                table.Cellspacing = 5;
                Cell cell = new Cell("header");
                cell.Header  = true;
                cell.Colspan = 3;
                table.AddCell(cell);
                cell             = new Cell("example cell with colspan 1 and rowspan 2");
                cell.Rowspan     = 2;
                cell.BorderColor = new iTextSharp.text.Color(255, 0, 0);
                table.AddCell(cell);
                table.AddCell("1.1");
                table.AddCell("2.1");
                table.AddCell("1.2");
                table.AddCell("2.2");
                table.AddCell("cell test1");
                cell                 = new Cell("big cell");
                cell.Rowspan         = 2;
                cell.Colspan         = 2;
                cell.BackgroundColor = new iTextSharp.text.Color(0xC0, 0xC0, 0xC0);
                table.AddCell(cell);
                table.AddCell("cell test2");
                // 改变了单元格“big cell”的对齐方式:
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment   = Element.ALIGN_MIDDLE;
                doc.Add(table);

                #endregion
                //关闭document
                doc.Close();
                //打开PDF,看效果
                //Process.Start(sourcePath);
            }
            catch (DocumentException de)
            {
                Console.WriteLine(de.Message);
                Console.ReadKey();
            }
            catch (IOException io)
            {
                Console.WriteLine(io.Message);
                Console.ReadKey();
            }
        }
예제 #21
0
        public Chap0402()
        {
            Console.WriteLine("Chapter 4 example 2: Chapters and Sections");

            // step 1: creation of a document-object
            Document document = new Document(PageSize.A4, 50, 50, 50, 50);

            try
            {
                // step 2: we create a writer that listens to the document
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap0402.pdf", FileMode.Create));
                // step 3: we open the document
                document.Open();
                // step 4: we Add content to the document
                // we define some fonts
                Font chapterFont    = FontFactory.GetFont(FontFactory.HELVETICA, 24, Font.NORMAL, new Color(255, 0, 0));
                Font sectionFont    = FontFactory.GetFont(FontFactory.HELVETICA, 20, Font.NORMAL, new Color(0, 0, 255));
                Font subsectionFont = FontFactory.GetFont(FontFactory.HELVETICA, 18, Font.BOLD, new Color(0, 64, 64));
                // we create some paragraphs
                Paragraph blahblah     = new Paragraph("blah blah blah blah blah blah blaah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah");
                Paragraph blahblahblah = new Paragraph("blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blaah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah");
                // this loop will create 7 chapters
                for (int i = 1; i < 8; i++)
                {
                    Paragraph cTitle  = new Paragraph("This is chapter " + i, chapterFont);
                    Chapter   chapter = new Chapter(cTitle, i);

                    if (i == 4)
                    {
                        blahblahblah.Alignment = Element.ALIGN_JUSTIFIED;
                        blahblah.Alignment     = Element.ALIGN_JUSTIFIED;
                        chapter.Add(blahblah);
                    }
                    if (i == 5)
                    {
                        blahblahblah.Alignment = Element.ALIGN_CENTER;
                        blahblah.Alignment     = Element.ALIGN_RIGHT;
                        chapter.Add(blahblah);
                    }
                    // Add a table in the 6th chapter
                    if (i == 6)
                    {
                        blahblah.Alignment = Element.ALIGN_JUSTIFIED;
                    }
                    // in every chapter 3 sections will be Added
                    for (int j = 1; j < 4; j++)
                    {
                        Paragraph sTitle  = new Paragraph("This is section " + j + " in chapter " + i, sectionFont);
                        Section   section = chapter.AddSection(sTitle, 1);
                        // in all chapters except the 1st one, some extra text is Added to section 3
                        if (j == 3 && i > 1)
                        {
                            section.Add(blahblah);
                        }
                        // in every section 3 subsections are Added
                        for (int k = 1; k < 4; k++)
                        {
                            Paragraph subTitle   = new Paragraph("This is subsection " + k + " of section " + j, subsectionFont);
                            Section   subsection = section.AddSection(subTitle, 3);
                            if (k == 1 && j == 3)
                            {
                                subsection.Add(blahblahblah);
                            }
                            subsection.Add(blahblah);
                        }
                        if (j == 2 && i > 2)
                        {
                            section.Add(blahblahblah);
                        }
                    }
                    document.Add(chapter);
                }
            }
            catch (Exception de)
            {
                Console.Error.WriteLine(de.StackTrace);
            }
            // step 5: we close the document
            document.Close();
        }
예제 #22
0
        public void CreateTaggedPdf11()
        {
            InitializeDocument("11");

            Chapter c =
                new Chapter(
                    new Paragraph("First chapter", new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD, BaseColor.BLUE)),
                    1);

            c.TriggerNewPage = false;
            c.Indentation    = 40;
            Section s1 =
                c.AddSection(new Paragraph("First section of a first chapter",
                                           new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE)));

            s1.Indentation = 20;
            Section s2 =
                s1.AddSection(new Paragraph("First subsection of a first section of a first chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));

            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Second subsection of a first section of a first chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Third subsection of a first section of a first chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s1 =
                c.AddSection(new Paragraph("Second section of a first chapter",
                                           new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE)));
            s1.Indentation = 20;
            s2             =
                s1.AddSection(new Paragraph("First subsection of a second section of a first chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Second subsection of a second section of a first chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Third subsection of a second section of a first chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s1 =
                c.AddSection(new Paragraph("Third section of a first chapter",
                                           new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE)));
            s1.Indentation = 20;
            s2             =
                s1.AddSection(new Paragraph("First subsection of a third section of a first chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Second subsection of a third section of a first chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Third subsection of a third section of a first chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            document.Add(c);

            c =
                new Chapter(
                    new Paragraph("Second chapter", new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD, BaseColor.BLUE)),
                    2);
            c.TriggerNewPage = false;
            c.Indentation    = 40;
            s1 =
                c.AddSection(new Paragraph("First section of a second chapter",
                                           new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE)));
            s1.Indentation = 20;
            s2             =
                s1.AddSection(new Paragraph("First subsection of a first section of a second chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Second subsection of a first section of a second chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Third subsection of a first section of a second chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s1 =
                c.AddSection(new Paragraph("Second section of a second chapter",
                                           new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE)));
            s1.Indentation = 20;
            s2             =
                s1.AddSection(new Paragraph("First subsection of a second section of a second chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Second subsection of a second section of a second chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Third subsection of a second section of a second chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s1 =
                c.AddSection(new Paragraph("Third section of a second chapter",
                                           new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE)));
            s1.Indentation = 20;
            s2             =
                s1.AddSection(new Paragraph("First subsection of a third section of a second chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Second subsection of a third section of a second chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Third subsection of a third section of a second chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            document.Add(c);

            c =
                new Chapter(
                    new Paragraph("Third chapter", new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD, BaseColor.BLUE)),
                    3);
            c.TriggerNewPage = false;
            c.Indentation    = 40;
            s1 =
                c.AddSection(new Paragraph("First section of a third chapter",
                                           new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE)));
            s1.Indentation = 20;
            s2             =
                s1.AddSection(new Paragraph("First subsection of a first section of a third chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Second subsection of a first section of a third chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Third subsection of a first section of a third chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s1 =
                c.AddSection(new Paragraph("Second section of a third chapter",
                                           new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE)));
            s1.Indentation = 20;
            s2             =
                s1.AddSection(new Paragraph("First subsection of a second section of a third chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Second subsection of a second section of a third chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Third subsection of a second section of a third chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s1 =
                c.AddSection(new Paragraph("Third section of a third chapter",
                                           new Font(Font.FontFamily.HELVETICA, 13, Font.BOLD, BaseColor.BLUE)));
            s1.Indentation = 20;
            s2             =
                s1.AddSection(new Paragraph("First subsection of a third section of a third chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Second subsection of a third section of a third chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            s2 =
                s1.AddSection(new Paragraph("Third subsection of a third section of a third chapter",
                                            new Font(Font.FontFamily.HELVETICA, 10, Font.BOLD, BaseColor.BLUE)));
            s2.Indentation = 10;
            s2.Add(new Paragraph("Some text..."));
            document.Add(c);

            document.Close();

            int[] nums = new int[] { 114, 60 };
            CheckNums(nums);
            CompareResults("11");
        }
예제 #23
0
        private void btnTest_Click(object sender, EventArgs e)
        {
            // 来自:https://blog.csdn.net/jsjpanxiaoyu/article/details/52922119
            //Document:(文档)生成pdf必备的一个对象,生成一个Document示例
            Document document = new Document(PageSize.A4, 30, 30, 5, 5);
            //为该Document创建一个Writer实例:
            PdfWriter.GetInstance(document, new FileStream("e://" + "Chap0101.pdf", FileMode.Create));
            //打开当前Document
            document.Open();
            //为当前Document添加内容:
            document.Add(new Paragraph("Hello World"));
            //另起一行。有几种办法建立一个段落,如:
            Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph.\n", FontFactory.GetFont(FontFactory.HELVETICA, 12)));
            Paragraph p2 = new Paragraph(new Phrase("This is my second paragraph.", FontFactory.GetFont(FontFactory.HELVETICA, 12)));
            Paragraph p3 = new Paragraph("This is my third paragraph.", FontFactory.GetFont(FontFactory.HELVETICA, 12));
            //所有有些对象将被添加到段落中:
            p1.Add("you can add string here\n\t");
            p1.Add(new Chunk("you can add chunks \n")); p1.Add(new Phrase("or you can add phrases.\n"));
            document.Add(p1); document.Add(p2); document.Add(p3);

            //创建了一个内容为“hello World”、红色、斜体、COURIER字体、尺寸20的一个块:
            Chunk chunk = new Chunk("Hello world", FontFactory.GetFont(FontFactory.COURIER, 20, iTextSharp.text.Font.COURIER, new iTextSharp.text.Color(255, 0, 0)));
            document.Add(chunk);
            //如果你希望一些块有下划线或删除线,你可以通过改变字体风格简单做到:
            Chunk chunk1 = new Chunk("This text is underlined", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.UNDEFINED));
            Chunk chunk2 = new Chunk("This font is of type ITALIC | STRIKETHRU", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.ITALIC | iTextSharp.text.Font.STRIKETHRU));
            //改变块的背景
            chunk2.SetBackground(new iTextSharp.text.Color(0xFF, 0xFF, 0x00));
            //上标/下标
            chunk1.SetTextRise(5);
            document.Add(chunk1);
            document.Add(chunk2);

            //外部链接示例:
            Anchor anchor = new Anchor("website", FontFactory.GetFont(FontFactory.HELVETICA, 12, iTextSharp.text.Font.UNDEFINED, new iTextSharp.text.Color(0, 0, 255)));

            anchor.Reference = "http://itextsharp.sourceforge.net/";
            anchor.Name = "website";
            //内部链接示例:
            Anchor anchor1 = new Anchor("This is an internal link\n\n");
            anchor1.Name = "link1";
            Anchor anchor2 = new Anchor("Click here to jump to the internal link\n\f");
            anchor2.Reference = "#link1";
            document.Add(anchor); document.Add(anchor1); document.Add(anchor2);

            //排序列表示例:
            List list = new List(true, 20);
            list.Add(new iTextSharp.text.ListItem("First line"));
            list.Add(new iTextSharp.text.ListItem("The second line is longer to see what happens once the end of the line is reached. Will it start on a new line?"));
            list.Add(new iTextSharp.text.ListItem("Third line"));
            document.Add(list);

            //文本注释:
            Annotation a = new Annotation("authors", "Maybe its because I wanted to be an author myself that I wrote iText.");
            document.Add(a);

            //包含页码没有任何边框的页脚。
            HeaderFooter footer = new HeaderFooter(new Phrase("This is page: "), true);
            footer.Border = iTextSharp.text.Rectangle.NO_BORDER;
            document.Footer = footer;

            //Chapter对象和Section对象自动构建一个树:
            iTextSharp.text.Font f1 = new iTextSharp.text.Font();
            f1.SetStyle(iTextSharp.text.Font.BOLD);
            Paragraph cTitle = new Paragraph("This is chapter 1", f1);
            Chapter chapter = new Chapter(cTitle, 1);
            Paragraph sTitle = new Paragraph("This is section 1 in chapter 1", f1);
            Section section = chapter.AddSection(sTitle, 1);
            document.Add(chapter);

            ////构建了一个简单的表:

            //iTextSharp.text.Table aTable = new iTextSharp.text.Table(4, 4);

            //aTable.AutoFillEmptyCells = true;

            //aTable.AddCell("2.2", new Point(2, 2));

            //aTable.AddCell("3.3", new Point(3, 3));

            //aTable.AddCell("2.1", new Point(2, 1));

            //aTable.AddCell("1.3", new Point(1, 3));

            //document.Add(aTable);

            ////构建了一个不简单的表:

            //iTextSharp.text.Table table = new iTextSharp.text.Table(3);

            //table.BorderWidth = 1;

            //table.BorderColor = new iTextSharp.text.Color(0, 0, 255);

            //table.Cellpadding = 5;

            //table.Cellspacing = 5;

            //Cell cell = new Cell("header");

            //cell.Header = true;

            //cell.Colspan = 3;

            //table.AddCell(cell);

            //cell = new Cell("example cell with colspan 1 and rowspan 2");

            //cell.Rowspan = 2;

            //cell.BorderColor = new iTextSharp.text.Color(255, 0, 0);

            //table.AddCell(cell);

            //table.AddCell("1.1");

            //table.AddCell("2.1");

            //table.AddCell("1.2");

            //table.AddCell("2.2");

            //table.AddCell("cell test1");

            //cell = new Cell("big cell");

            //cell.Rowspan = 2;

            //cell.Colspan = 2;

            //cell.BackgroundColor = new iTextSharp.text.Color(0xC0, 0xC0, 0xC0);
            //table.AddCell(cell);
            //table.AddCell("cell test2");
            //// 改变了单元格“big cell”的对齐方式:
            //cell.HorizontalAlignment = Element.ALIGN_CENTER;
            //cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            //document.Add(table);

            //关闭Document

            document.Close();
        }
예제 #24
0
        private void button4_Click(object sender, EventArgs e)
        {
            //Control ctl = this.dataGridView1;

            Control ctl = Control.FromHandle(this.dataGridView1.Handle);

            {
                Bitmap bt = new Bitmap(ctl.Width, ctl.Height);
                ctl.DrawToBitmap(bt, new System.Drawing.Rectangle(0, 0, bt.Width, bt.Height));
                bt.Save("abc.gif",System.Drawing.Imaging.ImageFormat.Gif);

                Document document = new Document(PageSize.A4, 10,10, 10,10);
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Application.StartupPath + @"\abcd.pdf", FileMode.Create));
                writer.ViewerPreferences = (PdfWriter.CenterWindow | PdfWriter.FitWindow | PdfWriter.PageModeUseNone);
                document.Open();

                //使用宋体字体
                BaseFont baseFont = BaseFont.CreateFont("C:\\WINDOWS\\FONTS\\simsun.ttc,0",
                                                        BaseFont.IDENTITY_H,
                                                        BaseFont.NOT_EMBEDDED);

                PdfContentByte cb = writer.DirectContent;

                Chapter chapter1 = new Chapter(new Paragraph("This is Chapter 1"), 1);
                Section section1 = chapter1.AddSection(20f, "Section 1.1", 2);
                Section section2 = chapter1.AddSection(20f, "Section 1.2", 2);
                Section subsection1 = section2.AddSection(20f, "Subsection 1.2.1", 3);
                Section subsection2 = section2.AddSection(20f, "Subsection 1.2.2", 3);
                Section subsubsection = subsection2.AddSection(20f, "Sub Subsection 1.2.2.1", 4);
                Chapter chapter2 = new Chapter(new Paragraph("This is Chapter 2"), 1);
                Section section3 = chapter2.AddSection("Section 2.1", 2);
                Section subsection3 = section3.AddSection("Subsection 2.1.1", 3);
                Section section4 = chapter2.AddSection("Section 2.2", 2);
                chapter1.BookmarkTitle = "Changed Title";
                chapter1.BookmarkOpen = true;
                chapter2.BookmarkOpen = false;
                document.Add(chapter1);
                document.Add(chapter2);

                ZapfDingbatsList zlist = new ZapfDingbatsList(49, 15);
                zlist.Add("One");
                zlist.Add("Two");
                zlist.Add("Three");
                zlist.Add("Four");
                zlist.Add("Five");
                document.Add(zlist);

                RomanList romanlist = new RomanList(true, 20);
                romanlist.IndentationLeft = 30f;
                romanlist.Add("One");
                romanlist.Add("Two");
                romanlist.Add("Three");
                romanlist.Add("Four");
                romanlist.Add("Five");
                document.Add(romanlist);

                PdfPTable table1 = new PdfPTable(3);
                PdfPCell cell1 = new PdfPCell(new Phrase("Header spanning 3 columns"));
                cell1.Colspan = 3;
                cell1.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
                table1.AddCell(cell1);
                table1.AddCell(cell1);
                table1.AddCell("Col 1 Row 2");
                table1.AddCell("Col 2 Row 2");
                table1.AddCell("Col 3 Row 2");

                table1.SetWidths(new int[]{50,100,100});

                table1.WidthPercentage = 100f;
                document.Add(table1);

                //using it = iTextSharp.text;

                //PdfPTable table2 = new PdfPTable(3);
                //table2.AddCell("Cell 1");
                //PdfPCell cell2 = new PdfPCell(new it.Phrase("Cell 2", new Font(Font.HELVETICA, 8f, Font.NORMAL, Color.YELLOW)));
                //cell2.BackgroundColor = new Color(0, 150, 0);
                //cell2.BorderColor = new Color(255, 242, 0);
                //cell2.Border = Rectangle.BOTTOM_BORDER | Rectangle.TOP_BORDER;
                //cell2.BorderWidthBottom = 3f;
                //cell2.BorderWidthTop = 3f;
                //cell2.PaddingBottom = 10f;
                //cell2.PaddingLeft = 20f;
                //cell2.PaddingTop = 4f;
                //table2.AddCell(cell2);
                //table2.AddCell("Cell 3");
                //document.Add(table2);

                System.Drawing.Image img = bt;
                MemoryStream mem = new MemoryStream();
                img.Save(mem, System.Drawing.Imaging.ImageFormat.Gif);
                byte[] bytes = mem.ToArray();

                iTextSharp.text.Image img2 = iTextSharp.text.Image.GetInstance(bytes);
                iTextSharp.text.Image img3 = iTextSharp.text.Image.GetInstance(bytes);
                img2.ScalePercent(100f);
                img3.ScalePercent(100f);
                img2.SetAbsolutePosition(50f, 400f);
                img3.SetAbsolutePosition(50f, 400f - img2.Height);
                //cb.AddImage(img2);
                //cb.AddImage(img3);

                cb.BeginText();

                float Xleading = 27.5f;
                float Xdelta = 10f;
                float Yleading = 27.5f;
                float Ydelta = 20f;

                cb.SetLineWidth(4f);
                cb.MoveTo(Xleading, (842 - Yleading - Ydelta));
                cb.LineTo((595f - Xleading), (842f - Yleading - Ydelta));
                cb.Stroke();

                cb.EndText();

                document.NewPage();

                //绘制近下方细直线上的文字
                cb.BeginText();

                BaseFont fbaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
                iTextSharp.text.Font font = new iTextSharp.text.Font(fbaseFont);

                cb.SetFontAndSize(fbaseFont,15);
                cb.SetColorFill(BaseColor.LIGHT_GRAY);

                PdfPCell cell = new PdfPCell();
                PdfPTable table = new PdfPTable(5);

                cell.HorizontalAlignment = Element.ALIGN_LEFT;

                table.AddCell(cell);
                document.Add(table);

                document.AddAuthor("ms");
                document.AddCreationDate();
                document.AddTitle("TEST");
                //document.Add(new Paragraph("", font));
                //document.Add(new Paragraph("    你好, PDF !", font));
                //document.Add(new Paragraph("    你好, PDF !", font));
                //document.Add(new Paragraph("    你好, PDF !", font));

                cb.ShowTextAligned(Element.ALIGN_LEFT, "一二三   :", 50, 800f, 0);
                cb.ShowTextAligned(Element.ALIGN_LEFT, "一二三   :", 50, 770f, 0);

                cb.EndText();

                cb.AddImage(img2);

                document.Close();
            }

            Control.ControlCollection ctls = this.Controls;

            foreach (Control item in ctls)
            {
                item.Enabled = false;
                //item.Width += 100;
            }
        }
예제 #25
0
파일: page2.cs 프로젝트: tossnet/Blazor-PDF
        public static void PageBookmark(Document pdf)
        {
            float indentation = 20;
            Font  _fontStyle  = FontFactory.GetFont("Tahoma", 8f, Font.ITALIC);
            Font  _linkStyle  = FontFactory.GetFont("Tahoma", 8f, Font.UNDERLINE, BaseColor.Blue);

            Chapter chapter1 = new Chapter(new Paragraph("Bookmarks and Links"), 1)
            {
                BookmarkTitle = "Text & co",
                BookmarkOpen  = true
            };

            chapter1.AddSection(indentation, "Section 1.1", 2);

            pdf.Add(chapter1);


            // Add a link to anchor
            var click = new Anchor("Click to an anchor-target in this document", _linkStyle)
            {
                Reference = "#target"
            };
            var paragraph1 = new Paragraph();

            paragraph1.IndentationLeft = indentation;
            paragraph1.Add(click);

            pdf.Add(paragraph1);


            // Add Paragraph
            var paragraph = new Paragraph(_lopsem, _fontStyle)
            {
                SpacingBefore   = 10f,
                SpacingAfter    = 10f,
                IndentationLeft = indentation,
                Alignment       = Element.ALIGN_JUSTIFIED
            };

            pdf.Add(paragraph);


            // Add simple Link
            Anchor link = new Anchor("www.sodeasoft.com", _linkStyle)
            {
                Reference = "https://www.sodeasoft.com"
            };
            var paragraph3 = new Paragraph("Web link : ", _fontStyle)
            {
                IndentationLeft = indentation
            };

            paragraph3.Add(link);

            pdf.Add(paragraph3);


            // To add paragraph and add at the end the link:
            // paragraph.Add(link);

            Section section2 = chapter1.AddSection(indentation, "Section 1.2", 2);

            {
                section2.TriggerNewPage = false;

                Section subsection1 = section2.AddSection(indentation, "Subsection 1.2.1", 3);
                Section subsection2 = section2.AddSection(20f, "Subsection 1.2.2", 3);
                subsection2.AddSection(indentation, "Sub Subsection 1.2.2.1", 4);
            }

            pdf.Add(section2);


            Chapter chapter2 = new Chapter(new Paragraph("This is Chapter 2"), 2)
            {
                BookmarkOpen   = false,
                TriggerNewPage = true
            };

            Section section3 = chapter2.AddSection("Section 2.1", 3);

            section3.AddSection("Subsection 2.1.1", 4);
            chapter2.AddSection("Section 2.2", 3);


            pdf.Add(chapter2);

            // Add the target from the Anchor above
            Anchor target = new Anchor("This is the Target");

            target.Name = "target";
            Paragraph paragraph2 = new Paragraph
            {
                target
            };

            pdf.Add(paragraph2);
        }
예제 #26
0
        private void Form1_Load(object sender, EventArgs e)
        {
            iText.Rectangle pageSize = new iText.Rectangle(216f, 1000f);
            pageSize.BackgroundColor = BaseColor.BLUE;
            Document document = new Document();
            //			Document document = new Document(pageSize, 72, 72, 144, 144);
            //			Document doc = new Document(PageSize.LETTER);
            //			Document doc = new Document(PageSize.LETTER.Rotate());

            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"HelloWorld.pdf", FileMode.Create));

            writer.PageEvent = new FoxDogGeneric1();

            document.AddTitle("Hello world example");
            document.AddSubject("This example shows how to add metadata");
            document.AddKeywords("Metadata, iText, step 3, tutorial");
            document.AddCreator("My program using iText");
            document.AddAuthor("Bruno Lowagie");
            document.AddHeader("Expires", "0");
            document.Open();

            /* chapter04/FoxDogChunk1.java */
            document.Add(new Paragraph("This is an example of chunks"));
            iText.Font font = new iText.Font(iText.Font.FontFamily.COURIER, 10, iText.Font.BOLD);
            font.SetColor(0xFF, 0xFF, 0xFF);
            Chunk fox = new Chunk("quick brown fox", font);

            fox.SetBackground(new BaseColor(0xa5, 0x2a, 0x2a));
            Chunk jumps = new Chunk(" jumps over ", new iText.Font());
            Chunk dog   = new Chunk("the lazy dog", new iText.Font(iText.Font.FontFamily.TIMES_ROMAN, 14, iText.Font.ITALIC));

            document.Add(fox);
            document.Add(jumps);
            document.Add(dog);

            document.Add(new Paragraph("This is an example of a Phrase"));

            /* chapter04/FoxDogPhrase.java */
            Phrase phrase = new Phrase(30);
            Chunk  space  = new Chunk(' ');

            phrase.Add(fox);
            phrase.Add(jumps);
            phrase.Add(dog);
            phrase.Add(space);

            for (int i = 0; i < 10; i++)
            {
                document.Add(phrase);
            }

            document.Add(new Paragraph("This is an example of a Paragraph"));

            /* chapter04/FoxDogParagraph.java */
            String    text      = "Quick brown fox jumps over the lazy dog.";
            Phrase    phrase1   = new Phrase(text);
            Phrase    phrase2   = new Phrase(new Chunk(text, new iText.Font(iText.Font.FontFamily.TIMES_ROMAN)));
            Phrase    phrase3   = new Phrase(text, new iText.Font(iText.Font.FontFamily.COURIER));
            Paragraph paragraph = new Paragraph();

            paragraph.Add(phrase1);
            paragraph.Add(space);
            paragraph.Add(phrase2);
            paragraph.Add(space);
            paragraph.Add(phrase3);
            paragraph.Alignment       = Element.ALIGN_RIGHT;
            paragraph.IndentationLeft = 20;
            document.Add(paragraph);
            document.Add(paragraph);

            document.Add(new Paragraph("This is an example of a link. Click the text below."));

            /* chapter04/FoxDogAnchor1.java */
            Anchor anchor = new Anchor("Quick brown fox jumps over the lazy dog.");

            anchor.Reference = "http://en.wikipedia.org/wiki/The_quick_brown_fox_jumps_over_the_lazy_dog";
            document.Add(anchor);

            document.Add(new Paragraph("This is an example of some lists"));

            /* chapter04/FoxDogList1.java */
            List list1 = new List(List.ORDERED, 20);

            list1.Add(new ListItem("the lazy dog"));
            document.Add(list1);
            List list2 = new List(List.UNORDERED, 10);

            list2.Add("the lazy cat");
            document.Add(list2);
            List list3 = new List(List.ORDERED, List.ALPHABETICAL, 20);

            list3.Add(new ListItem("the fence"));
            document.Add(list3);
            List list4 = new List(List.UNORDERED, 30);

            list4.SetListSymbol("----->");
            list4.IndentationLeft = 10;
            list4.Add("the lazy dog");
            document.Add(list4);
            List list5 = new List(List.ORDERED, 20);

            list5.First = 11;
            list5.Add(new ListItem("the lazy cat"));
            document.Add(list5);
            List list = new List(List.UNORDERED, 10);

            list.SetListSymbol("*");
            list.Add(list1);
            list.Add(list3);
            list.Add(list5);
            document.Add(list);

            /* chapter04/FoxDogChapter1.java */
            Chapter chapter1 = new Chapter(new Paragraph("This is a sample sentence:", font), 1);

            chapter1.Add(new Paragraph(text));
            Section section1 = chapter1.AddSection("Quick", 0);

            section1.Add(new Paragraph(text));
            document.Add(chapter1);

            /* chapter04/FoxDogScale.java */
            Chunk     c = new Chunk("quick brown fox jumps over the lazy dog");
            float     w = c.GetWidthPoint();
            Paragraph p = new Paragraph("The width of the chunk: '");

            p.Add(c);
            p.Add("' is ");
            p.Add(w.ToString());
            p.Add(" points or ");
            p.Add((w / 72f).ToString());
            p.Add(" inches or ");
            p.Add((w / 72f * 2.54f).ToString());
            p.Add(" cm.");
            document.Add(p);

            document.Add(new Paragraph("SetGenericTag Example"));

            /* chapter04/FoxDogGeneric1.java */
            p   = new Paragraph();
            fox = new Chunk("Quick brown fox");
            fox.SetGenericTag("box");
            p.Add(fox);
            p.Add(" jumps over ");
            dog = new Chunk("the lazy dog.");
            dog.SetGenericTag("ellipse");
            p.Add(dog);
            document.Add(p);

            document.Close();

            string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

            webBrowser1.Navigate(string.Concat(appPath, "\\", @"HelloWorld.pdf"));
        }
예제 #27
0
        /// <summary>
        /// Generates the eval report.
        /// </summary>
        /// <returns>The eval report.</returns>
        /// <param name="model">Model.</param>
        /// <param name="path">Path.</param>
        public static string GenerateEvalReport(EvalReportModel model, string path)
        {
            Document content = new Document();

            try
            {
                //准备字体
                BaseFont bfsun     = BaseFont.CreateFont(@"c:\Windows\fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                BaseFont titleFont = BaseFont.CreateFont(AppDomain.CurrentDomain.BaseDirectory + "\\bin\\方正宋黑简体.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                var      font      = new Font(bfsun);

                // make content pdf (calculate page number)
                PdfWriter contentWriter = PdfWriter.GetInstance(content, new System.IO.FileStream(path, System.IO.FileMode.Create));
                //ContentEvent eventd = new ContentEvent();
                //contentWriter.PageEvent = eventd;
                content.Open();
                #region 页眉页脚
                //PdfContentByte cb = contentWriter.DirectContent;
                //PdfTemplate template = cb.CreateTemplate(500, 200);
                ////template.MoveTo(0,(content.PageSize.Width-200/2));
                //template.LineTo(500, 0);
                //template.Stroke();
                //template.BeginText();
                //template.SetFontAndSize(bfsun, 10);
                //template.SetTextMatrix(0, 0);
                //template.ShowText(model.CompanyName);
                //template.EndText();
                //cb.AddTemplate(template, 0, (content.PageSize.Width - 200) / 2);
                #endregion

                #region 生成首页
                //添加标题
                //var titleChapter = new Chapter(new Paragraph(new Chunk("封面", new Font(font) { Size = 24 })) { Alignment = Element.ALIGN_CENTER }, 1);
                //titleChapter.NumberDepth = -1;
                //content.Add(titleChapter);

                content.Add(new Paragraph(model.Title, new Font(font)
                {
                    Size = 24
                })
                {
                    Alignment = Element.ALIGN_CENTER
                });
                content.Add(new Paragraph(" "));
                //添加副标题
                content.Add(new Paragraph(model.SecTitle, new Font(font)
                {
                    Size = 16
                })
                {
                    Alignment = Element.ALIGN_CENTER
                });
                //titleChapter.NumberDepth = -1;


                content.Add(new Paragraph(" ")
                {
                    SpacingBefore = 40f
                });


                #region 用表格模拟“综合等级”的星
                var levelTable = new PdfPTable(new float[] { 90f, 470f });
                var levelCell  = new PdfPCell()
                {
                    Border              = 0,
                    VerticalAlignment   = Element.ALIGN_MIDDLE,
                    HorizontalAlignment = Element.ALIGN_CENTER
                };
                //levelCell.Border = 0;
                levelCell.AddElement(new Paragraph("综合等级:", font)
                {
                    Alignment = Element.ALIGN_CENTER
                });
                levelCell.VerticalAlignment = Element.ALIGN_MIDDLE;

                levelTable.AddCell(levelCell);
                //第二个单元格
                var starCell = new PdfPCell();
                var sttable  = new PdfPTable(15);
                for (int i = 1; i < 6; i++)
                {
                    string imgPath;
                    if (i <= model.Level)
                    {
                        imgPath = System.Web.HttpContext.Current.Server.MapPath("~/images/ui_star_red.png");
                    }
                    else
                    {
                        imgPath = System.Web.HttpContext.Current.Server.MapPath("~/images/ui_star_red_null.png");
                    }
                    sttable.AddCell(new PdfPCell(Image.GetInstance(imgPath))
                    {
                        Border              = 0,
                        VerticalAlignment   = Element.ALIGN_MIDDLE,
                        HorizontalAlignment = Element.ALIGN_CENTER
                    });
                }
                for (int i = 0; i < 10; i++)
                {
                    sttable.AddCell(new PdfPCell(new Phrase(""))
                    {
                        Border = 0
                    });
                }

                levelTable.AddCell(new PdfPCell(sttable)
                {
                    Border = 0
                });

                content.Add(levelTable);
                #endregion

                try
                {
                    var fimg = Image.GetInstance(model.FrontImage);
                    fimg.ScaleAbsolute(content.PageSize.Width, 200);

                    fimg.Alignment = Element.ALIGN_CENTER;
                    content.Add(fimg);
                }
                catch
                {
                }


                content.Add(new Paragraph(" ")
                {
                    SpacingBefore = 20f
                });
                PdfPTable homeTable = new PdfPTable(3);
                //homeTable.TotalWidth = 340;

                foreach (var item in model.FrontCoverDataItems)
                {
                    var spaceCell = new PdfPCell();
                    spaceCell.AddElement(new Paragraph("  "));
                    spaceCell.Border = 0;
                    var cell1 = new PdfPCell();
                    cell1.AddElement(new Paragraph(item.Key + ":", font));
                    cell1.Border = 0;
                    var cell2 = new PdfPCell();
                    cell2.AddElement(new Paragraph(item.Value, font));
                    cell2.Border = 0;
                    homeTable.Rows.Add(new PdfPRow(new[] { spaceCell, cell1, cell2 }));
                }
                //homeTable.HorizontalAlignment=
                content.Add(homeTable);

                #endregion

                //List<Chapter> chapterList = new List<Chapter>();

                #region 生成内容

                for (int i = 0; i < model.Items.Count; i++)
                {
                    var     item    = model.Items[i];
                    Chapter chapter = new Chapter(new Paragraph(item.Title, new Font(titleFont)
                    {
                        Size = 16
                    })
                    {
                        Alignment = Element.ALIGN_CENTER
                    }, i + 2);
                    chapter.NumberDepth = -1;
                    //chapter.NumberDepth = 1;

                    //chapterList.Add(chapter);
                    foreach (var c in item.Contents)
                    {
                        chapter.Add(new Paragraph(c, font));
                    }

                    for (int j = 0; j < item.Sections.Count; j++)
                    {
                        var section = item.Sections[j];
                        var sec     = chapter.AddSection(new Paragraph(section.SectionTitle, new Font(titleFont)
                        {
                            Size = 14
                        }), j + 1);
                        sec.NumberDepth = -1;
                        foreach (var c in section.Contents)
                        {
                            sec.Add(new Paragraph(c, font) /* FirstLineIndent = 26 */ }
                            {
                            );
                        }
예제 #28
0
        public Chap0701()
        {
            Console.WriteLine("Chapter 7 example 1: my first XML");

            // step 1: creation of a document-object
            Document document = new Document();

            try
            {
                // step 2:
                // we create a writer that listens to the document
                // and directs a XML-stream to a file
                XmlWriter.GetInstance(document, new FileStream("Chap0701.xml", FileMode.Create), "itext.dtd");

                // step 3: we open the document
                document.Open();

                // step 4: we add content to the document
                Paragraph paragraph = new Paragraph("Please visit my ");
                Anchor    anchor1   = new Anchor("website (external reference)", FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.UNDERLINE, new Color(0, 0, 255)));
                anchor1.Reference = "http://www.lowagie.com/iText/";
                anchor1.Name      = "top";
                paragraph.Add(anchor1);
                document.Add(paragraph);

                Paragraph entities = new Paragraph("These are some special characters: <, >, &, \" and '");
                document.Add(entities);

                document.Add(new Paragraph("some books I really like:"));
                List     list;
                ListItem listItem;
                list     = new List(true, 15);
                listItem = new ListItem("When Harlie was one", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12));
                listItem.Add(new Chunk(" by David Gerrold", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 11, Font.ITALIC)).SetTextRise(8.0f));
                list.Add(listItem);
                listItem = new ListItem("The World according to Garp", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12));
                listItem.Add(new Chunk(" by John Irving", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 11, Font.ITALIC)).SetTextRise(-8.0f));
                list.Add(listItem);
                listItem = new ListItem("Decamerone", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12));
                listItem.Add(new Chunk(" by Giovanni Boccaccio", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 11, Font.ITALIC)));
                list.Add(listItem);
                document.Add(list);

                paragraph = new Paragraph("some movies I really like:");
                list      = new List(false, 10);
                list.Add("Wild At Heart");
                list.Add("Casablanca");
                list.Add("When Harry met Sally");
                list.Add("True Romance");
                list.Add("Le mari de la coiffeuse");
                paragraph.Add(list);
                document.Add(paragraph);

                document.Add(new Paragraph("Some authors I really like:"));
                list            = new List(false, 20);
                list.ListSymbol = new Chunk("*", FontFactory.GetFont(FontFactory.HELVETICA, 20, Font.BOLD));
                listItem        = new ListItem("Isaac Asimov");
                list.Add(listItem);
                List sublist;
                sublist            = new List(true, 10);
                sublist.ListSymbol = new Chunk("", FontFactory.GetFont(FontFactory.HELVETICA, 8));
                sublist.Add("The Foundation Trilogy");
                sublist.Add("The Complete Robot");
                sublist.Add("Caves of Steel");
                sublist.Add("The Naked Sun");
                list.Add(sublist);
                listItem = new ListItem("John Irving");
                list.Add(listItem);
                sublist            = new List(true, 10);
                sublist.ListSymbol = new Chunk("", FontFactory.GetFont(FontFactory.HELVETICA, 8));
                sublist.Add("The World according to Garp");
                sublist.Add("Hotel New Hampshire");
                sublist.Add("A prayer for Owen Meany");
                sublist.Add("Widow for a year");
                list.Add(sublist);
                listItem = new ListItem("Kurt Vonnegut");
                list.Add(listItem);
                sublist            = new List(true, 10);
                sublist.ListSymbol = new Chunk("", FontFactory.GetFont(FontFactory.HELVETICA, 8));
                sublist.Add("Slaughterhouse 5");
                sublist.Add("Welcome to the Monkey House");
                sublist.Add("The great pianola");
                sublist.Add("Galapagos");
                list.Add(sublist);
                document.Add(list);

                paragraph = new Paragraph("\n\n");
                document.Add(paragraph);

                Table table = new Table(3);
                table.BorderWidth = 1;
                table.BorderColor = new Color(0, 0, 255);
                table.Padding     = 5;
                table.Spacing     = 5;
                Cell cell = new Cell("header");
                cell.Header  = true;
                cell.Colspan = 3;
                table.AddCell(cell);
                table.EndHeaders();
                cell             = new Cell("example cell with colspan 1 and rowspan 2");
                cell.Rowspan     = 2;
                cell.BorderColor = new Color(255, 0, 0);
                table.AddCell(cell);
                table.AddCell("1.1");
                table.AddCell("2.1");
                table.AddCell("1.2");
                table.AddCell("2.2");
                table.AddCell("cell test1");
                cell         = new Cell("big cell");
                cell.Rowspan = 2;
                cell.Colspan = 2;
                table.AddCell(cell);
                table.AddCell("cell test2");
                document.Add(table);

                Image jpeg = Image.GetInstance("myKids.jpg");
                document.Add(jpeg);
                Image png = Image.GetInstance(new Uri("http://www.lowagie.com/iText/examples/hitchcock.png"));
                document.Add(png);
                Anchor anchor2 = new Anchor("please jump to a local destination", FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 255)));
                anchor2.Reference = "#top";
                document.Add(anchor2);

                document.Add(paragraph);

                // we define some fonts
                Font chapterFont    = FontFactory.GetFont(FontFactory.HELVETICA, 24, Font.NORMAL, new Color(255, 0, 0));
                Font sectionFont    = FontFactory.GetFont(FontFactory.HELVETICA, 20, Font.NORMAL, new Color(0, 0, 255));
                Font subsectionFont = FontFactory.GetFont(FontFactory.HELVETICA, 18, Font.BOLD, new Color(0, 64, 64));
                // we create some paragraphs
                Paragraph blahblah     = new Paragraph("blah blah blah blah blah blah blaah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah");
                Paragraph blahblahblah = new Paragraph("blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blaah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah");

                // this loop will create 7 chapters
                for (int i = 1; i < 8; i++)
                {
                    Paragraph cTitle  = new Paragraph("This is chapter " + i, chapterFont);
                    Chapter   chapter = new Chapter(cTitle, i);

                    if (i == 4)
                    {
                        blahblahblah.Alignment = Element.ALIGN_JUSTIFIED;
                        blahblah.Alignment     = Element.ALIGN_JUSTIFIED;
                        chapter.Add(blahblah);
                    }
                    if (i == 5)
                    {
                        blahblahblah.Alignment = Element.ALIGN_CENTER;
                        blahblah.Alignment     = Element.ALIGN_RIGHT;
                        chapter.Add(blahblah);
                    }
                    // add a table in the 6th chapter
                    if (i == 6)
                    {
                        blahblah.Alignment = Element.ALIGN_JUSTIFIED;
                        chapter.Add(table);
                    }
                    // in every chapter 3 sections will be added
                    for (int j = 1; j < 4; j++)
                    {
                        Paragraph sTitle  = new Paragraph("This is section " + j + " in chapter " + i, sectionFont);
                        Section   section = chapter.AddSection(sTitle, 1);
                        // in all chapters except the 1st one, some extra text is added to section 3
                        if (j == 3 && i > 1)
                        {
                            section.Add(blahblah);
                        }
                        // in every section 3 subsections are added
                        for (int k = 1; k < 4; k++)
                        {
                            Paragraph subTitle   = new Paragraph("This is subsection " + k + " of section " + j, subsectionFont);
                            Section   subsection = section.AddSection(subTitle, 3);
                            if (k == 1 && j == 3)
                            {
                                subsection.Add(blahblahblah);
                                subsection.Add(table);
                            }
                            subsection.Add(blahblah);
                        }
                        if (j == 2 && i > 2)
                        {
                            section.Add(blahblahblah);
                            section.Add(table);
                        }
                    }
                    document.Add(chapter);
                }
            }
            catch (DocumentException de)
            {
                Console.Error.WriteLine(de.Message);
            }
            catch (IOException ioe)
            {
                Console.Error.WriteLine(ioe.Message);
            }

            // step 5: we close the document
            document.Close();
        }