我想用iText Sharp將文本文件轉換爲PDF。 但是,轉換本身並不是問題,我無法創建PDF文件,使其看起來與文本文件中文本的對齊方式完全一致。試圖將純文本文件轉換爲iTextSharp而不會丟失格式
這是代碼我使用:
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格式,而不會丟失文本文件的格式? 任何幫助表示讚賞。
我對使用MemoryStream的'ms'有點困惑。 (或者'data'是剛剛添加新段落時錯誤命名的東西?) – JYelton
另請參見[在C#中將txt轉換爲pdf](http://stackoverflow.com/q/4906455/161052)(已關閉)或[ItextSharp適合文本到頁面](http://stackoverflow.com/q/4805767/161052)。 – JYelton