示例#1
0
        public static void AddText2()
        {
            //This example is using document level builder(flow-like), if the text inserted is out of one page,
            //the builder will insert the left text on a second page automatically
            PdfDocument document = new PdfDocument();

            //Create document level builder
            using (PdfDocumentBuilder builder = new PdfDocumentBuilder(document))
            {
                //Set page size and margins
                builder.SectionState.PageSize    = PaperTypeConverter.ToSize(PaperTypes.A4);
                builder.SectionState.PageMargins = new Padding(20);

                //Set text alignment
                builder.ParagraphState.HorizontalAlignment = Editing.Flow.HorizontalAlignment.Center;
                //Set font style
                builder.CharacterState.SetFont(new FontFamily("LegacySansEFOP-Book"));
                builder.CharacterState.FontSize = 40;
                builder.InsertParagraph();
                builder.InsertText("Document Title");
                builder.InsertLineBreak();

                //Add several paragraphs to page
                builder.ParagraphState.HorizontalAlignment = Editing.Flow.HorizontalAlignment.Left;
                builder.CharacterState.FontSize            = 20;
                for (int i = 0; i < 20; i++)
                {
                    builder.InsertParagraph();
                    string text = "";
                    for (int j = 1; j < 11; j++)
                    {
                        text += "This is sentence " + j.ToString() + ". ";
                    }
                    builder.InsertText(text);
                    builder.InsertLineBreak();
                }
            }

            using (FileStream fs = File.Create("InsertText2.pdf"))
            {
                PdfFile pdfFile = new PdfFile();
                pdfFile.Export(document, fs);
            }
        }