尝试将纯文本文件转换为iTextSharp而不会丢失格式 [英] Trying to convert plain text file to iTextSharp without losing format

查看:116
本文介绍了尝试将纯文本文件转换为iTextSharp而不会丢失格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用iText Sharp将文本文件转换为PDF。
转换本身不是问题,但我无法创建PDF文件,使其看起来与文本文件中文本的对齐方式完全相同。

I am trying to convert text files to PDF with iText Sharp. The conversion itself is not a problem however, I am unable to create the PDF file such that it looks exactly the way the text in the text file is aligned.

这是我正在使用的代码:

This is the code I am using:

public void GetPDF(string data)
    {

        using (MemoryStream ms = new MemoryStream())
        {
            Rectangle docSize = new Rectangle(612, 798);
            Document myDocument = new Document(PageSize.A4.Rotate());

            PdfWriter.GetInstance(myDocument, new FileStream
                                         ("MyNewPDF.pdf", FileMode.Create));

            // step 3:  Open the document now using
            myDocument.Open();

            // step 4: Now add some contents to the document
            myDocument.Add(new Paragraph(data));
            myDocument.Close();
        }
    }

是否有人建议如何做只需将文本文件转换为PDF而不会丢失文本文件的格式?
任何帮助都表示赞赏。

Does anybody have a suggestion as to what to do to simply convert a text file into PDF without losing the format of the text file? Any help is appreciated.

推荐答案

纯文本文件不包含格式。当您参考文本的对齐方式时,我必须假设您正在查看具有固定宽度或等宽字体的文本文件。因此,当您将文本文件转换为PDF时,应指定使用固定宽度/等宽字体(例如Lucida Console,Courier New,Consolas等)

A plain text file contains no formatting. When you refer to how the text is aligned, I have to presume you are viewing the text file with a fixed-width or monospace font. Therefore when you convert the text file to PDF, you should specify that a fixed-width/monospace font is used (such as Lucida Console, Courier New, Consolas, etc.)

接下来,通常不在纯文本文件中指定自动换行。文本的长段落将以回车/换行符结束,就是这样。 PDF转换可能会在页面边界处引入一些自动换行。要控制它,你需要选择一个包装你想要的字体大小,或者手动插入一些CR / LF。

Next, word wrap is not generally specified in a plain text file. Long paragraphs of text will end with a carriage return/newline character and that's it. The PDF conversion is likely to introduce some word-wrapping at the page boundaries. To control this you will need to select a font size that wraps where you want it, or insert some CR/LF's manually.

这篇关于尝试将纯文本文件转换为iTextSharp而不会丢失格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆