/// <summary> /// Add a text to PDF page and return the layout result. /// </summary> public PdfLayoutResult AddParagraph(PdfPage page, string text, RectangleF bounds, bool isTitle, bool mainTitle = false) { //Set string format, font size, style. PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Justify); PdfFontStyle style = PdfFontStyle.Regular; float size = 13; if (mainTitle) { format = new PdfStringFormat(PdfTextAlignment.Center); } if (isTitle) { size = 18; if (mainTitle) { size = 24; } } if (isTitle && !mainTitle) { style = PdfFontStyle.Bold; } //Create text element and draw it to PDF page. PdfTextElement element = new PdfTextElement(text, new PdfStandardFont(PdfFontFamily.Helvetica, size, style), PdfBrushes.Black); element.StringFormat = format; return(element.Draw(page, bounds)); }
/// <summary> /// Add content in table of content and return the layout result. /// </summary> PdfLayoutResult AddTableOfContents(PdfPage page, string text, RectangleF bounds, bool isTitle, int pageNo, float x, float y, PdfPage destPage) { //Set font style. PdfFontStyle style = isTitle ? PdfFontStyle.Bold : PdfFontStyle.Regular; //Create a font and draw the text to PDF page. PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 13, style); page.Graphics.DrawString(pageNo.ToString(), font, PdfBrushes.Black, new RectangleF(480, bounds.Top + 5, bounds.Width, bounds.Height)); //Create a pdf destination for document link annotation. PdfDestination pdfDestination = new PdfDestination(destPage, new PointF(x, y)); RectangleF annotationBounds = RectangleF.Empty; if (isTitle) { annotationBounds = new RectangleF(bounds.Left, bounds.Top - 45, bounds.Width, font.Height); } else { annotationBounds = new RectangleF(bounds.Left + 20, bounds.Top - 45, bounds.Width - 20, font.Height); } //Create document link annotation with bounds. PdfDocumentLinkAnnotation annotation = new PdfDocumentLinkAnnotation(annotationBounds, pdfDestination); annotation.Border.Width = 0; //Add annotation to PDF page. page.Annotations.Add(annotation); //Measure the text size to draw in PDF page. string str = text + ' '; float width = isTitle ? font.MeasureString(text).Width + 20 : font.MeasureString(text).Width + 40; for (float i = width; i < 470;) { str = str + '.'; i = i + 3.6140000000000003F; } //Create text element and draw it to PDF page. PdfTextElement element = new PdfTextElement(str, font, PdfBrushes.Black); if (isTitle) { bounds = new RectangleF(bounds.Left, bounds.Top + 5, bounds.Width, bounds.Height); } else { bounds = new RectangleF(bounds.Left + 20, bounds.Top + 5, bounds.Width, bounds.Height); } return(element.Draw(page, bounds)); }
private void LoadedDocument_SubstituteFont(object sender, PdfFontEventArgs args) { //Get the font name string fontName = args.FontName.Split(',')[0]; //Get the font style PdfFontStyle fontStyle = args.FontStyle; SKTypefaceStyle sKFontStyle = SKTypefaceStyle.Normal; if (fontStyle != PdfFontStyle.Regular) { if (fontStyle == PdfFontStyle.Bold) { sKFontStyle = SKTypefaceStyle.Bold; } else if (fontStyle == PdfFontStyle.Italic) { sKFontStyle = SKTypefaceStyle.Italic; } else if (fontStyle == (PdfFontStyle.Italic | PdfFontStyle.Bold)) { sKFontStyle = SKTypefaceStyle.BoldItalic; } } SKTypeface typeface = SKTypeface.FromFamilyName(fontName, sKFontStyle); SKStreamAsset typeFaceStream = typeface.OpenStream(); MemoryStream memoryStream = null; if (typeFaceStream != null && typeFaceStream.Length > 0) { //Create fontData from type face stream. byte[] fontData = new byte[typeFaceStream.Length - 1]; typeFaceStream.Read(fontData, typeFaceStream.Length); typeFaceStream.Dispose(); //Create the new memory stream from font data. memoryStream = new MemoryStream(fontData); } //set the font stream to the event args. args.FontStream = memoryStream; }
public void Setup(string firstPageName) { //Create a new PDF document. Document = new PdfDocument(); Document.Pages.PageAdded += Pages_PageAdded; //Add a page to the document. AddPage(firstPageName); AccentColor = new PdfColor(89, 106, 122); AccentBrush = new PdfSolidBrush(AccentColor); PdfGridStyle2Color = new PdfColor(237, 125, 49); PdfGridStyle2AltColor = new PdfColor(251, 228, 213); PdfGridStyle2Brush = new PdfSolidBrush(PdfGridStyle2Color); NormalFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 10); PdfFontStyle style = PdfFontStyle.Bold; NormalFontBold = new PdfStandardFont(PdfFontFamily.TimesRoman, 10, style); SubHeadingFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 18); }
private Stream GetFontStream(Font font) { String fontFileName = null; if (font.Name == "Arial") { int advancedStyle = (int)(PdfFontStyle.Strikeout | PdfFontStyle.Underline); advancedStyle = advancedStyle & (int)font.Style; int normalStyle = (int)(PdfFontStyle.Regular | PdfFontStyle.Bold | PdfFontStyle.Italic); normalStyle = normalStyle & (int)font.Style; PdfFontStyle style = (PdfFontStyle)normalStyle; if (style == PdfFontStyle.Regular) { fontFileName = "arial.ttf"; } else if (style == PdfFontStyle.Bold) { fontFileName = "arialbd.ttf"; } else if (style == PdfFontStyle.Italic) { fontFileName = "ariali.ttf"; } else if (style == (PdfFontStyle.Bold | PdfFontStyle.Italic)) { fontFileName = "arialbi.ttf"; } font.Style = (PdfFontStyle)advancedStyle; } if (fontFileName == null) { throw new ArgumentException("fontName"); } return(GetFontStream(fontFileName)); }