Frequently Asked Question
Add text to document (Windows)
Last Updated 11 months ago
This article shows how to add text to a pdf.
To use these methods you need a Premium license
StorageFolder storageFolder = ApplicationData.Current.LocalFolder; StorageFile newFile = await storageFolder.CreateFileAsync("new.pdf", CreationCollisionOption.ReplaceExisting); IRandomAccessStream stream = await newFile.OpenAsync(FileAccessMode.ReadWrite); PDFDoc newDoc = new PDFDoc(); if (newDoc.Create(stream) == PDF_ERROR.err_ok) { float width = 297f, height = 210f; PDFPage page = newDoc.NewPage(0, width, height); PDFDocFont dfont = newDoc.NewFontCID("Arimo", 1 | 8);//bold and embed in horizontal writing if (dfont != null) { PDFPageFont pageFont = page.AddResFont(dfont); if (pageFont != null) { PDFPageContent pageContent = new PDFPageContent(); int x = 100, y = 100; float fontSize = 20, charSpace = 0, wordSpace = 0.3f; String text = "RadaeePDF\rTest!"; //add text highlight pageContent.GSSave(); PDFDocGState dgs = newDoc.NewGState(); dgs.SetFillAlpha(100); //alpha dgs.SetBlendMode(2); //Multipy PDFPageGState rgs = page.AddResGState(dgs); pageContent.GSSet(rgs); PDFPoint textSize = pageContent.GetTextSize(text, pageFont, fontSize, fontSize, charSpace, wordSpace); float textHalfHeight = textSize.y / 2; PDFPath path = new PDFPath(); path.MoveTo(x, y - textHalfHeight); path.LineTo(x + textSize.x, y - textHalfHeight); path.LineTo(x + textSize.x, y + textHalfHeight); path.LineTo(x, y + textHalfHeight); path.Close(); pageContent.SetFillColor(0x0000FF); pageContent.FillPath(path, true); pageContent.GSRestore(); //add text pageContent.GSSave(); PDFMatrix matrix = new PDFMatrix(1, 1, x, y); pageContent.GSSetMatrix(matrix); pageContent.TextBegin(); pageContent.TextSetFont(pageFont, fontSize); //set font and size pageContent.SetFillColor(0xff0000); pageContent.SetStrokeColor(0x10); pageContent.TextSetCharSpace(charSpace); pageContent.TextSetWordSpace(wordSpace); pageContent.TextSetLeading(20); pageContent.TextSetRenderMode(2);//fill and stroke pageContent.TextSetHScale(100);//set horizontal scale pageContent.DrawText(text); pageContent.TextEnd(); pageContent.GSRestore(); page.AddContent(pageContent, true); page.Close(); } } newDoc.Save(); newDoc.Close(); }