PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Dec 13, 2017 8:34 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Thu Mar 12, 2015 2:20 pm 
Offline

Joined: Wed Mar 11, 2015 5:47 pm
Posts: 1
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");
        }
     
        }


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group