C# 从文本文件向PDF文件添加文本

C# 从文本文件向PDF文件添加文本,c#,.net,pdf,itext,C#,.net,Pdf,Itext,这是我的代码: using (FileStream msReport = new FileStream(pdfPath, FileMode.Create)) { //step 1 using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 140f, 10f)) { try

这是我的代码:

        using (FileStream msReport = new FileStream(pdfPath, FileMode.Create))
        {
            //step 1
            using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 140f, 10f))
            {
                try
                {
                    // step 2.
                    StreamReader sr = new StreamReader("TextFile.txt");
                    string line;
                    PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, msReport);
                    pdfWriter.PageEvent = new ITextEvents();

                    //open the stream 
                    pdfDoc.Open();

                    for (int i = 0; i < 100; i++)
                    {
                        if ((line = sr.ReadLine()) != null)
                        {
                            Paragraph para = new Paragraph(line, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 6));

                            para.Alignment = Element.ALIGN_LEFT;

                            pdfDoc.Add(para);

                            //pdfDoc.NewPage();
                        }
                    }
                    sr.Close();
                    pdfDoc.Close();
使用(FileStream msReport=newfilestream(pdfPath,FileMode.Create))
{
//第一步
使用(文档pdfDoc=新文档(PageSize.A4、10f、10f、140f、10f))
{
尝试
{
//第二步。
StreamReader sr=新的StreamReader(“TextFile.txt”);
弦线;
PdfWriter PdfWriter=PdfWriter.GetInstance(pdfDoc,msReport);
pdfWriter.PageEvent=new ITextEvents();
//开河
pdfDoc.Open();
对于(int i=0;i<100;i++)
{
如果((line=sr.ReadLine())!=null)
{
段落段落=新段落(行,新的iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA,6));
para.Alignment=Element.ALIGN_LEFT;
pdfDoc.增补(第6段);
//pdfDoc.NewPage();
}
}
高级关闭();
pdfDoc.Close();

它正在工作…它读取我计算机中的文本文件并将其添加到PDF中…问题是-它没有保留文本文件的格式。它只是添加文本。我需要保持它的原样。有什么方法可以保留文本的格式吗?我能以某种方式将其添加到表格并格式化表格,或者类似的东西吗?

您也可以通过读取转义序列来完成此操作,就像当行更改时,您应该将其读取为\n,然后将其与文本一起写入PDF。

尝试此操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf; 

namespace TxtToPdf
{
    class Program
    {
        static void Main(string[] args)
        {
            //Read the Data from Input File

            StreamReader rdr = new      StreamReader("Path/Test.txt");

           //Create a New instance on Document Class

            Document doc = new Document();

           //Create a New instance of PDFWriter Class for Output File

           PdfWriter.GetInstance(doc, new  FileStream("Path/Test.pdf", FileMode.Create));

           //Open the Document

            doc.Open();

           //Add the content of Text File to PDF File

           doc.Add(new Paragraph(rdr.ReadToEnd()));

           //Close the Document

           doc.Close();


           //Open the Converted PDF File

           System.Diagnostics.Process.Start("Path/Test.pdf");
        }
    }
}

定义文本的格式。所谓纯文本的格式通常是人为的。你说的是制表符吗?然后你必须用空格替换它们(因为制表符字符在PDF中没有任何意义)。你说的是空格吗?然后你需要使用单空格字体。你说的是标记吗(与创建堆栈溢出问题类似)?然后需要首先解析该格式语法(使用其他工具)。我认为这个问题是重复的,而实际答案是我对这个问题的回答,我应该更具体一些,对不起。文本的格式像一个表格,在…之间有不同数量的空格,它是用C#…写的。好的,在这种情况下,我会将问题标记为重复。你应该使用
iTextSharp.text.Font。FontFamily.COURIER
而不是
iTextSharp.text.Font.FontFamily.HELVETICA
。将尝试它,不确定这是否会解决它…如果这是答案,我会感到惊讶。我认为这个问题是重复的,实际答案是我给出的关于你提供的链接也使用escape squence:/like I的问题的答案说。呃……你说的是换行符。这不是问题。如果你将包含换行符的
字符串
包装在
段落
中,你会在生成的PDF中看到换行符。人们通常遇到的主要问题是,他们使用的是比例字体(在OP中,是Helvetica)而不是单间距字体(如Courier)。你的答案不会以任何方式反映这一点。