服务端用iTextSharp产生图片内容PDF分页问题
nices 2010-10-12 05:03:53 服务端代码如下,其中 context.Request.InputStream是从Silverlight传过来的,从一个StackPanel转换为EditableImage的Stream。现在产生的PDF没有分页,所有的内容都在同一页。
System.IO.Stream incomingStream = context.Request.InputStream; //Get the incoming stream
System.Drawing.Image img = System.Drawing.Image.FromStream(incomingStream); //Create and image from the stream
int pageHeight = img.Height + (_Margin * 2);
int pageWidth = img.Width + (_Margin * 2);
iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(img, System.Drawing.Imaging.ImageFormat.Png);
System.IO.MemoryStream pdfStream = new System.IO.MemoryStream();
//iTextSharp.text.Font pdfFont = iTextSharp.text.Font.
iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(new iTextSharp.text.Rectangle(pageWidth, pageHeight));
//iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4);
pdfDoc.NewPage();
iTextSharp.text.pdf.PdfWriter pdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, pdfStream);
//Insure this is set otherwise when the close method is called on the iTextSharp.text.Document the memory stream
//we are writting to will be closed
pdfWriter.CloseStream = false;
//Open PDF and add image to it
pdfDoc.Open();
pdfDoc.Add(pdfImage);
//Close the PDF document to commit the changes
pdfDoc.Close();
//Insure the stream is at the beginning
if (pdfStream.Position > 0) { pdfStream.Position = 0; };
int contentLength = (int)pdfStream.Length;
//Stream the exported pdf to the client this is needed since the file size could be large in size
Byte[] bytesRead = new Byte[contentLength];
pdfStream.Read(bytesRead, 0, contentLength);