随笔 - 224  文章 - 41  trackbacks - 0
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

享受编程

常用链接

留言簿(11)

随笔分类(159)

随笔档案(224)

文章分类(2)

文章档案(4)

经典c++博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜

网上关于c#有很多关于使用itext转换txt为pdf的教程,最近在使用kindle看书发现txt的字体不够大,在网上找了好多软件要吗不好用要吗要收费,所以决定自己写一个。

其实,核心代码没几行:
exe下载

 

private void doTXTtoPDFWork()
        
{
            
try
            
{
                iTextSharp.text.Rectangle c6 
= new iTextSharp.text.Rectangle(480600);
                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 漂漂 阅读(1323) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理