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 = @"http:////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();
   }
 
posted @ 2011-01-27 22:06  zhh  阅读(2007)  评论(1编辑  收藏  举报