iTextSharp 打印PDF文件例子

本想找创建PDF的例子,结果找到了个iTextSharp 打印PDF文件例子

 

 

I am using iTextSharp in C# to write an application which can print pdf
documents on a network printer while programmatically configuring the
printer.

I am using the PrintDocument class in the System.Drawing.Printing namespace
along with PrinterSettings and PageSettings classes
in order to configure the Printer before printing. But since I am using
custom printing this will require
me to specify how to print the document in the PrintPage event of the
PrintDocument class. Now this PrintPage
event handler takes an instance of PrintPageEventArgs as an arguement, using
which I can get a handle on the
Printer Graphics Device context and use it to draw whatever I need to print.
So essentially I need a way to read
a pdf document page by page and draw each page content on the
PrintPageEventArgs.Graphics context.

This is a part of the code that I have written using the TallComponents
Rasterizer.NET component which works like a dream.
I need to have the same or similar functionality by using
iTextSharp(obviously coz its free). Can anyone give me any ideas or some
sample code to do this.

Thanks,

Soumya

private Document document;
private IEnumerator DocPages;
private ArrayList DocPagesList;

private const string PrinterName = @"//sutlej/SamsungM";
private const string DocLocation = @"D:/PB-OMSWeb/POC/FileStore/pcu.pdf";

private void cmdPrint_Click(object sender, System.EventArgs e)
   {
     FileStream file = null;
     DocPagesList = new ArrayList();
     try
     {
       file = new FileStream( DocLocation, FileMode.Open, FileAccess.Read );
       byte[] buffer = new byte[ file.Length ];
       file.Read( buffer, 0, buffer.Length );
       document = new Document( new BinaryReader( new MemoryStream(
buffer ) ) );

       for( int i = 0; i < document.Pages.Count; i++ )
       {
         DocPagesList.Add( i.ToString() );
       }

       DocPages = DocPagesList.GetEnumerator();
       DocPages.Reset();

       if( DocPages.MoveNext() )
       {
         PrintDocument oPrintDocument = new PrintDocument();
         PrinterSettings oPrinterSettings = new PrinterSettings();
         PageSettings oPageSettings = new PageSettings();

         oPrinterSettings.PrinterName = PrinterName;
         oPageSettings.Landscape = true;

         oPrintDocument.DocumentName = document.Title;
         oPrintDocument.PrinterSettings = oPrinterSettings;
         oPrintDocument.DefaultPageSettings = oPageSettings;

         oPrintDocument.PrintPage += new
PrintPageEventHandler(oPrintDocument_PrintPage);
         oPrintDocument.Print();
       }
     }
     catch( Exception exc )
     {
       lblResult.Text = exc.Message;
     }
     finally
     {
       file.Close();
     }
   }

private void oPrintDocument_PrintPage(object sender, PrintPageEventArgs e)
   {
     e.Graphics.PageUnit = GraphicsUnit.Point;

     if ( null != DocPages.Current )
     {
       int pageIndex = Convert.ToInt32( DocPages.Current );
       Page page = document.Pages[ pageIndex++ ];

       page.Draw( e.Graphics );
     }

     e.HasMorePages = DocPages.MoveNext();
   }

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值