65.9K
CodeProject 正在变化。 阅读更多。
Home

将文本文件转换为 PDF 文件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.57/5 (20投票s)

2005年12月14日

CPOL

1分钟阅读

viewsIcon

256899

downloadIcon

8166

本文档展示了如何将文本文件转换为 PDF 文件。

Sample Image

引言

本文介绍了一个基本的文本到 PDF 库。使用此库,您可以将纯文本文件转换为 PDF 格式(PDF 是 Portable Document Format 的缩写)。您可以将文本文件在任何位置分成 PDF 页面(只要在第 1 列设置分页符“1”即可)。本文还提供了一个示例应用程序,演示如何使用此 DLL。

背景

我开始编写这个库是因为我实在找不到一个好的 C# 库来导出我的报告。当然,您可以创建自己的库以满足自己的需求。

库的实现

为了实现分页功能,我创建了 StartPageEndPageStartObjectStorePage 函数。如果流读取器读取输入文本文件中的分页符“1”,我就能够结束当前页面并开始新页面。这就是整个思路。

Dotext 函数如下

private void DoText(StreamReader sr)
{
    string strLine = string.Empty;

    //Start Page
    StartPage();

    try
    {
        while (sr.Peek() >= 0)
        {
            //Get one string at a time from the input text file
            strLine = sr.ReadLine()+"\r\n";

            //If yPos <= this.margin?
            if(yPos <= this.margin) 
            {
                //Invoke EndPage and StartPage functions
                EndPage();
                StartPage();
            }

            if(strLine == "" || strLine == null)
            {
                FileStreamWrite(outFileStream,@"T*\r\n");
            }
            else
            {
                //Is there a page break "1"?
                int cmpPageVal = String.Compare(strLine.Substring(0,1),"1");
                
                //Is there a Formfeed?
                int cmpfVal    = String.Compare(strLine.Substring(0,1),"\f");
            
                bool bl = false;

                //Formfeed
                if(cmpfVal == 0)
                {
                    //Invoke EndPage and StartPage functions
                    EndPage();
                    StartPage();
                }
                else
                {
                    //If there is a page break "1"
                    if (cmpPageVal == 0)
                    {
                        //Invoke EndPage and StartPage functions
                        EndPage();
                        StartPage();

                        //Remove the page break "1"
                        strLine = strLine.Remove(0,1);
                    }

                    FileStreamWrite(outFileStream,@"(");

                    //Convert "strLine" to a char array
                    char[] textchars=strLine.ToCharArray();

                    for (int index=0;index<textchars.Length;index++)
                    {
                        char c=textchars[index];

                        //If there is page break
                        if(c=='1' && strLine.Length == 2)
                        {
                            EndPage();
                            StartPage();
                        }
                            //new line
                        else if(c=='\n')
                        {
                            if (!bl)
                                FileStreamWrite(outFileStream,@")'");
                            else
                                FileStreamWrite(outFileStream,@"T*\n");
                        
                            bl = true;
                        }
                        else
                        {
                            FileStreamWrite(outFileStream,c.ToString());
                            bl=false;
                        }
                    }

                    if (!bl)
                        FileStreamWrite(outFileStream,@")\r\n");
                }

            }

            //Set yPos
            yPos -= leadSize;
        }//for loop

        //Close file
        sr.Close();
        sr = null;

        //End page
        EndPage();

    }
    catch( Exception ex ) 
    {
        string error = ("The process failed: " + ex.Message);
    }
}

使用此库

在示例应用程序中,我在一个 .aspx 页面中放置了一个 Button

这是按钮 Click 函数背后的代码

private void Button1_Click(object sender, System.EventArgs e)
{
    //The input text file "TextFile.txt"
    string fileName = @"TextFile.txt";
    string filePath = Server.MapPath("temp/" + fileName);
    
    // Create a new PdfWriter
    TextPDF.PdfWriter pdfWriter = 
       new TextPDF.PdfWriter(842.0f, 1190.0f, 10.0f, 10.0f);

    if(filePath.Length > 0)
    {
        //Write to a PDF file
        pdfWriter.Write(filePath);
    }
}

结论

通过使用此库,您可以轻松地将纯文本文件转换为 PDF 格式的文件。您还可以在文本文件的任何位置创建分页。

注意:我将 PDF 输出文件放在“c:\temp\txtPdf.pdf”目录下;输入文本文件放在“TextPdfSample\temp\TextFile.txt”目录下。

历史

  • 2005/10/12 - 发布了这篇文章。
© . All rights reserved.