async Task CreateDocumentImages(C1PdfDocument pdf)
        {
            // calculate page rect (discounting margins)
            var rcPage = PdfUtils.PageRectangle(pdf);
            InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();

            // title
            Font font = new Font("Segoe UI Light", 16, PdfFontStyle.Italic);

            pdf.DrawString(Strings.ImagesDocumentTitle, font, Colors.Black, new Rect(72, 72, 400, 100));

            // load image into writeable bitmap
            WriteableBitmap wb   = new WriteableBitmap(880, 660);
            var             file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///PdfSamplesLib/Assets/pic.jpg"));

            wb.SetSource(await file.OpenReadAsync());

            // simple draw image
            var rcPic = new Rect(72, 100, wb.PixelWidth / 5.0, wb.PixelHeight / 5.0);

            pdf.DrawImage(wb, rcPic);

            // draw on page preserving aspect ratio
            var delta = 100.0;

            rcPic = new Rect(new Point(delta, delta), new Point(rcPage.Width - delta, rcPage.Height - delta));
            pdf.DrawImage(wb, rcPic, ContentAlignment.MiddleCenter, Stretch.Uniform);

            // translucent rectangle
            var clr = Color.FromArgb(50, 0, 255, 0);

            pdf.FillRectangle(clr, new Rect(200, 200, 300, 400));
        }
        static void CreateDocumentQuotes(C1PdfDocument pdf)
        {
            // calculate page rect (discounting margins)
            Rect rcPage = PdfUtils.PageRectangle(pdf);
            Rect rc     = rcPage;

            // initialize output parameters
            Font hdrFont   = new Font("Arial", 14, PdfFontStyle.Bold);
            Font titleFont = new Font("Arial", 24, PdfFontStyle.Bold);
            Font txtFont   = new Font("Times New Roman", 10, PdfFontStyle.Italic);

            // add title
            rc = PdfUtils.RenderParagraph(pdf, pdf.DocumentInfo.Title, titleFont, rcPage, rc);

            // build document
            foreach (string s in GetQuotes())
            {
                string[] authorQuote = s.Split('\t');

                // render header (author)
                var author = authorQuote[0];
                rc.Y += 20;
                rc    = PdfUtils.RenderParagraph(pdf, author, hdrFont, rcPage, rc, true);

                // render body text (quote)
                string text = authorQuote[1];
                rc.X     = rcPage.X + 36; // << indent body text by 1/2 inch
                rc.Width = rcPage.Width - 40;
                rc       = PdfUtils.RenderParagraph(pdf, text, txtFont, rcPage, rc);
                rc.X     = rcPage.X; // << restore indent
                rc.Width = rcPage.Width;
                rc.Y    += 12;       // << add 12pt spacing after each quote
            }
        }
        static void CreateDocumentPaperSizes(C1PdfDocument pdf)
        {
            // add title
            Font titleFont = new Font("Tahoma", 24, PdfFontStyle.Bold);
            Rect rc        = PdfUtils.PageRectangle(pdf);

            PdfUtils.RenderParagraph(pdf, pdf.DocumentInfo.Title, titleFont, rc, rc, false);

            // create constant font and StringFormat objects
            Font         font = new Font("Tahoma", 18);
            StringFormat sf   = new StringFormat();

            sf.Alignment     = HorizontalAlignment.Center;
            sf.LineAlignment = VerticalAlignment.Center;

            // create one page with each paper size
            bool firstPage = true;

            foreach (PaperKind pk in Enum.GetValues(typeof(PaperKind)))
            {
                // Silverlight doesn't have Enum.GetValues
                //PaperKind pk = fi;

                // skip custom size
                if (pk == PaperKind.Custom)
                {
                    continue;
                }

                // add new page for every page after the first one
                if (!firstPage)
                {
                    pdf.NewPage();
                }
                firstPage = false;

                // set paper kind and orientation
                pdf.PaperKind = pk;
                pdf.Landscape = !pdf.Landscape;

                // draw some content on the page
                rc = PdfUtils.PageRectangle(pdf);
                rc = PdfUtils.Inflate(rc, -6, -6);
                string text = string.Format(Strings.StringFormatTwoArg,
                                            pdf.PaperKind, pdf.Landscape);
                pdf.DrawString(text, font, Colors.Black, rc, sf);
                pdf.DrawRectangle(Colors.Black, rc);
            }
            //foreach (var fi in typeof(PaperKind).GetTypeInfo().GetFields(BindingFlags.Static | BindingFlags.Public))
            //{

            //}
        }
Exemple #4
0
        static void CreateDocumentTOC(C1PdfDocument pdf)
        {
            // create pdf document
            pdf.DocumentInfo.Title = Strings.TableOfContentsDocumentTitle;

            // add title
            Font titleFont = new Font("Tahoma", 24, PdfFontStyle.Bold);
            Rect rcPage    = PdfUtils.PageRectangle(pdf);
            Rect rc        = PdfUtils.RenderParagraph(pdf, pdf.DocumentInfo.Title, titleFont, rcPage, rcPage, false);

            rc.Y += 12;

            // create nonsense document
            var  bkmk       = new List <string[]>();
            Font headerFont = new Font("Arial", 14, PdfFontStyle.Bold);
            Font bodyFont   = new Font("Times New Roman", 11);

            for (int i = 0; i < 30; i++)
            {
                // create ith header (as a link target and outline entry)
                string header = string.Format("{0}. {1}", i + 1, BuildRandomTitle());
                rc = PdfUtils.RenderParagraph(pdf, header, headerFont, rcPage, rc, true, true);

                // save bookmark to build TOC later
                int pageNumber = pdf.CurrentPage + 1;
                bkmk.Add(new string[] { pageNumber.ToString(), header });

                // create some text
                rc.X     += 36;
                rc.Width -= 36;
                for (int j = 0; j < 3 + _rnd.Next(20); j++)
                {
                    string text = BuildRandomParagraph();
                    rc    = PdfUtils.RenderParagraph(pdf, text, bodyFont, rcPage, rc);
                    rc.Y += 6;
                }
                rc.X     -= 36;
                rc.Width += 36;
                rc.Y     += 20;
            }

            // start Table of Contents
            pdf.NewPage();                      // start TOC on a new page
            int tocPage = pdf.CurrentPage;      // save page index (to move TOC later)

            rc        = PdfUtils.RenderParagraph(pdf, Strings.TableOfContentsDocumentTitle, titleFont, rcPage, rcPage, true);
            rc.Y     += 12;
            rc.X     += 30;
            rc.Width -= 40;

            // render Table of Contents
            Pen dottedPen = new Pen(Colors.Gray, 1.5f);

            dottedPen.DashStyle = C1.Xaml.Pdf.DashStyle.Dot;
            StringFormat sfRight = new StringFormat();

            sfRight.Alignment = HorizontalAlignment.Right;
            rc.Height         = bodyFont.Size * 1.2;
            foreach (string[] entry in bkmk)
            {
                // get bookmark info
                string page   = entry[0];
                string header = entry[1];

                // render header name and page number
                pdf.DrawString(header, bodyFont, Colors.Black, rc);
                pdf.DrawString(page, bodyFont, Colors.Black, rc, sfRight);

#if true
                // connect the two with some dots (looks better than a dotted line)
                string dots = ". ";
                var    wid  = pdf.MeasureString(dots, bodyFont).Width;
                var    x1   = rc.X + pdf.MeasureString(header, bodyFont).Width + 8;
                var    x2   = rc.Right - pdf.MeasureString(page, bodyFont).Width - 8;
                var    x    = rc.X;
                for (rc.X = x1; rc.X < x2; rc.X += wid)
                {
                    pdf.DrawString(dots, bodyFont, Colors.Gray, rc);
                }
                rc.X = x;
#else
                // connect with a dotted line (another option)
                var x1 = rc.X + pdf.MeasureString(header, bodyFont).Width + 5;
                var x2 = rc.Right - pdf.MeasureString(page, bodyFont).Width - 5;
                var y  = rc.Top + bodyFont.Size;
                pdf.DrawLine(dottedPen, x1, y, x2, y);
#endif
                // add local hyperlink to entry
                pdf.AddLink(Strings.PoundSign + header, rc);

                // move on to next entry
                rc = PdfUtils.Offset(rc, 0, rc.Height);
                if (rc.Bottom > rcPage.Bottom)
                {
                    pdf.NewPage();
                    rc.Y = rcPage.Y;
                }
            }

            // move table of contents to start of document
            PdfPage[] arr = new PdfPage[pdf.Pages.Count - tocPage];
            pdf.Pages.CopyTo(tocPage, arr, 0, arr.Length);
            pdf.Pages.RemoveRange(tocPage, arr.Length);
            pdf.Pages.InsertRange(0, arr);
        }
Exemple #5
0
        static void CreateDocumentTextFlow(C1PdfDocument pdf)
        {
            // load long string from resource file
            string text = Strings.ResourceNotFound;

            using (var sr = new StreamReader(typeof(BasicTextPage).GetTypeInfo().Assembly.GetManifestResourceStream("PdfSamples.Resources.flow.txt")))
            {
                text = sr.ReadToEnd();
            }
            text = text.Replace("\t", "   ");
            text = string.Format("{0}\r\n\r\n---oOoOoOo---\r\n\r\n{0}", text);

            // create pdf document
            pdf.DocumentInfo.Title = Strings.TextFlowDocumentTitle;

            // add title
            Font titleFont = new Font("Tahoma", 24, PdfFontStyle.Bold);
            Font bodyFont  = new Font("Tahoma", 9);
            Rect rcPage    = PdfUtils.PageRectangle(pdf);
            Rect rc        = PdfUtils.RenderParagraph(pdf, pdf.DocumentInfo.Title, titleFont, rcPage, rcPage, false);

            rc.Y     += titleFont.Size + 6;
            rc.Height = rcPage.Height - rc.Y;

            // create two columns for the text
            Rect rcLeft = rc;

            rcLeft.Width  = rcPage.Width / 2 - 12;
            rcLeft.Height = 300;
            rcLeft.Y      = (rcPage.Y + rcPage.Height - rcLeft.Height) / 2;
            Rect rcRight = rcLeft;

            rcRight.X = rcPage.Right - rcRight.Width;

            // start with left column
            rc = rcLeft;

            // render string spanning columns and pages
            for (; ;)
            {
                // render as much as will fit into the rectangle
                rc = PdfUtils.Inflate(rc, -3, -3);
                int nextChar = pdf.DrawString(text, bodyFont, Colors.Black, rc);
                rc = PdfUtils.Inflate(rc, +3, +3);
                pdf.DrawRectangle(Colors.LightGray, rc);

                // break when done
                if (nextChar >= text.Length)
                {
                    break;
                }

                // get rid of the part that was rendered
                text = text.Substring(nextChar);

                // switch to right-side rectangle
                if (rc.Left == rcLeft.Left)
                {
                    rc = rcRight;
                }
                else // switch to left-side rectangle on the next page
                {
                    pdf.NewPage();
                    rc = rcLeft;
                }
            }
        }