網上關于c#有很多關于使用itext轉換txt為pdf的教程,最近在使用kindle看書發現txt的字體不夠大,在網上找了好多軟件要嗎不好用要嗎要收費,所以決定自己寫一個。
其實,核心代碼沒幾行:
exe下載
private void doTXTtoPDFWork()

{
try

{
iTextSharp.text.Rectangle c6 = new iTextSharp.text.Rectangle(480, 600);
Document document = new Document(c6);
string pdfSavePath = txtPath.Substring(0, txtPath.Length - 3);
pdfSavePath += "pdf";
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfSavePath, FileMode.Create));//創建Writer實例

document.Open();

document.AddAuthor("androidxiamen");
// document.AddCreationDate();
// document.AddCreator("廈門同步網絡有限公司");

// document.AddKeywords("移動互聯網");
// document.AddProducer();
// document.AddSubject("kindle 6 寸");
// document.AddTitle("明朝那些事");



BaseFont bfHei = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(bfHei, 20);


float titleLineHeight = 45f;
float normalLineHeight = 20f;

Paragraph pBlank = new Paragraph(" ", font);
pBlank.Leading = normalLineHeight;

StreamReader sr = new StreamReader(txtPath, GetEncoding(txtPath));
String line = "";

while ((line = sr.ReadLine()) != null)

{
if (line == "")

{
document.Add(pBlank);//空行
}
Chunk chnk4 = new Chunk(line, font);


document.Add(GetPTxt(new Chunk[]
{ chnk4 }, normalLineHeight));

curLine++;
UpdateUIPro();
}
sr.Close();

document.Close();
UpdateUIFinish();
}
catch

{
MessageBox.Show("哦,出現了未知的情況,我已經快掛了!!");
}

}
源代碼下載
posted on 2012-05-02 11:47
漂漂 閱讀(1365)
評論(0) 編輯 收藏 引用