public static void SetDocumentInfo(this C1PdfDocument pdf, string title) { // set document info var di = pdf.DocumentInfo; di.Author = Strings.DocumentAuthor; di.Subject = Strings.DocumentSubject; di.Title = title; // render footers // this reopens each page and adds content to them (now we know the page count). var font = new Font("Arial", 8, PdfFontStyle.Bold); var fmt = new StringFormat(); fmt.Alignment = HorizontalAlignment.Right; fmt.LineAlignment = VerticalAlignment.Bottom; for (int page = 0; page < pdf.Pages.Count; page++) { pdf.CurrentPage = page; var text = string.Format(Strings.Documentfooter, di.Title, page + 1, pdf.Pages.Count); pdf.DrawString( text, font, Colors.DarkGray, PdfUtils.Inflate(pdf.PageRectangle, -72, -36), fmt); } }
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)) //{ //} }
/// <summary> /// Shows how to position and align text /// </summary> static void CreateDocumentText(C1PdfDocument pdf) { // use landscape for more impact pdf.Landscape = true; // measure and show some text var text = Strings.DocumentBasicText; var font = new Font("Segoe UI Light", 12, PdfFontStyle.Italic); // create StringFormat used to set text alignment and line spacing var fmt = new StringFormat(); fmt.LineSpacing = -1.5; // 1.5 char height fmt.Alignment = HorizontalAlignment.Center; // measure it var sz = pdf.MeasureString(text, font, 72 * 3, fmt); var rc = new Rect(pdf.PageRectangle.Width / 2, 72, sz.Width, sz.Height); rc = PdfUtils.Offset(rc, 110, 0); // draw a rounded frame rc = PdfUtils.Inflate(rc, 0, 0); pdf.FillRectangle(Colors.Teal, rc, new Size(0, 0)); //pdf.DrawRectangle(new Pen(Colors.DarkGray, 5), rc, new Size(0, 0)); rc = PdfUtils.Inflate(rc, -10, -10); // draw the text //pdf.RotateAngle = 90; pdf.DrawString(text, font, Colors.White, rc, fmt); //pdf.RotateAngle = 0; // point in center for rotate the text rc = pdf.PageRectangle; var pt = new Point(rc.X + rc.Width / 2, rc.Y + rc.Height / 2); // rotate the string in small increments var step = 6; text = Strings.DocumentBasicText2; for (int i = 0; i <= 360; i += step) { pdf.RotateAngle = i; var s = string.Format(text, i); font = new Font("Courier New", 8 + i / 30.0, PdfFontStyle.Bold); byte b = (byte)(255 * (1 - i / 360.0)); pdf.DrawString(s, font, Color.FromArgb(0xff, b, b, b), pt); } }
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; } } }