网上关于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
漂漂 阅读(1358)
评论(0) 编辑 收藏 引用