使用iTextSharp实现PDF文件套打功能

当前项目有个需求是给多个单位提供PDF文件下载

而这些文件都是一批文档,只是数据和单位不一致而已

于是想用iTextSharp往空模板上增加单位数据等信息。

网上资料也不少,这里大致整理下

1. 开源C# PDF操作类库搜集

http://csharp-source.net/open-source/pdf-libraries

2.RubyPdf关于中文iTextSharp的说明

http://www.cnblogs.com/hardrock/archive/2006/09/23/512605.html

3.iText的官网,java的,代码里大致改改就能用,多数是大小写和 方法 属性 封装不同

http://itextdocs.lowagie.com/tutorial/

4.关于往PDF上添加文本

参考了 PDF上添加水印的文章

改成了添加文本:

         struct  PDFTextInfo
        {
            
public   string  sText;
            
public   int  iPageNo;
            
public   int  iPosX;
            
public   int  iPosY;

            
public   override   string  ToString()
            {
                
return   string .Format( " Page:{0},x:{1},y:{2},text:{3} " new   object [] { iPageNo,iPosX,iPosY,sText});
            }
        }

        
private   void  AddTextToPDF( string  fromPDFFile,  string  toPDFFile, PDFTextInfo[] texts)
        {
            
// 打开原文件
            PdfReader reader  =   new  PdfReader(fromPDFFile);
            
int  n  =  reader.NumberOfPages;

            
// 新建文件句柄
            PdfStamper stamp  =   new  PdfStamper(reader,  new  FileStream(toPDFFile, FileMode.Create));
            PdfContentByte waterMark;

            
// 加载宋体字体
            BaseFont bfComic  =  BaseFont.CreateFont( " SURSONG.TTF " , BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font font 
=   new  iTextSharp.text.Font(bfComic,  10 );

            
// process
             for  ( int  i  =   0 ; i  <  texts.Length; i ++ )
            {
                waterMark 
=  stamp.GetOverContent(texts[i].iPageNo);

                iTextSharp.text.Phrase ph 
=   new  Phrase(texts[i].sText, font);

                ColumnText.ShowTextAligned(waterMark, Element.ALIGN_LEFT, ph, texts[i].iPosX, texts[i].iPosY, 
0 );
            }
          
            stamp.Close();
            reader.Close();
        }

其中,加载宋体字体那段,由于我机器上宋体是SIMSUN.TTC 加载出错,只好随便搞了个方正的字体了。。囧



转载于:https://www.cnblogs.com/calmzeal/archive/2008/09/18/1293409.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值