PDFsharp & MigraDoc Foundation http://forum.pdfsharp.com/ |
|
Sample code: Create PDF from TextStream http://forum.pdfsharp.com/viewtopic.php?f=8&t=3072 |
Page 1 of 1 |
Author: | cirwin01 [ Thu Mar 12, 2015 2:20 pm ] |
Post subject: | Sample code: Create PDF from TextStream |
Forum Moderation: original subject was "Here's some sample code", original location was "Feature Request". I couldn't post it elsewhere, but thought this might be useful to others. You might want to include a modified version in your samples. Code: /*
The CreatePDFFromTextStream is an example of using the PDFSharp libraries to render text files to PDF. This particular example assumes text file output from a mainframe system where the report width is 132 columns. In this example, the maximum number of lines before a Form Feed character is 106, so this code forces a new page after 108 lines. I hope this saves you some time, Chris Irwin, 3/12/2016, chris.irwin@cincyis.com */ using System; using PdfSharp.Pdf; using PdfSharp.Drawing; using System.IO; namespace cincyis.com { class PDFProcessing { public static void CreatePDFFromTextFile(string InputFile, string OutputFile) { const double LINE_SPACING = 6.8; const double FONT_SIZE = 7.5; const int MAX_LINES = 112; StreamReader sr = new StreamReader(InputFile); PdfDocument document = new PdfDocument(); XFont font = new XFont(System.Drawing.FontFamily.GenericMonospace, FONT_SIZE, XFontStyle.Regular, null); string textAfterFormFeed = ""; while (sr.Peek() >= 0) { PdfPage page = document.AddPage(); XGraphics gfx = XGraphics.FromPdfPage(page); bool formFeed = false; string buffer = ""; double pos = 0; int lines = 0; if (textAfterFormFeed != "") { gfx.DrawString(textAfterFormFeed, font, XBrushes.Black, new XRect(0, pos, page.Width, page.Height), XStringFormats.TopLeft); pos = pos + LINE_SPACING; lines++; textAfterFormFeed = ""; } while (sr.Peek() >= 0 && !formFeed) { buffer = sr.ReadLine(); if (buffer.IndexOf("\x0c") >= 0) { formFeed = true; if (buffer.IndexOf("\x0c") + 1 != buffer.Length) textAfterFormFeed = buffer.Substring(buffer.IndexOf("\x0c") + 1, buffer.Length - buffer.IndexOf("\x0c") - 1); if (buffer.IndexOf("\x0c") > 0) gfx.DrawString(buffer.Substring(0, buffer.IndexOf("\x0c")), font, XBrushes.Black, new XRect(0, pos, page.Width, page.Height), XStringFormats.TopLeft); } else gfx.DrawString(buffer, font, XBrushes.Black, new XRect(0, pos, page.Width, page.Height), XStringFormats.TopLeft); pos = pos + LINE_SPACING; lines++; if (lines > MAX_LINES) formFeed = true; } } sr.Close(); document.Save(OutputFile); document.Close(); } } } static void Main(string[] args) { PDFProcessing.CreatePDFFromTextFile(@"C:\Temp\Test.txt", @"C:\Temp\Test.pdf"); } } |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |