Skip to main content Skip to footer

How to Draw Text from a Text File into a Generated PDF

Using ReadAllText, we can draw text from a text file into a generated PDF.

We can utilize the "Hello World" demo template found here to start. You can replace the "CreatePDF" method with the following:

        public void CreatePDF(Stream stream)
        {
            // Create a new PDF document:
            GcPdfDocument doc = new GcPdfDocument();
            // Add a page, get its graphics:
            GcPdfGraphics g = doc.NewPage().Graphics;
            //Read text from text file
            string text = File.ReadAllText(@"your_text_file.txt");
            //PDF text formatting 
            g.DrawString(text,
                new TextFormat() { Font = StandardFonts.Times, FontSize = 12 },
                new PointF(72, 72));
            // Save the PDF:
            doc.Save(stream);
        }

Of course, you can either include the text file in your solution, or use the file's location from elsewhere. You can then run the project and generate a PDF based on the read text.

 

Tye Glenz