我正在从 silverlight 网格创建图像。转换为 png 并使用 iTextsharp 将其插入到 pdf 中。最终结果有一个非常模糊/模糊的pdf。如果我保存与 png 相同的图像,那就很清楚了。如何使用具有更清晰图像的 pdf 来改进结果?
以下是代码片段(在 Silverlight 端):
私人无效生成PDF(){
SaveFileDialog fsd = new SaveFileDialog();
fsd.DefaultFileName = "Report.pdf";
fsd.Filter = "PDF (*.pdf)|*.pdf|*PNG (*.png)|*.png";
bool? dialogResult = fsd.ShowDialog(); //fsd.ShowDialog();
if (dialogResult == true)
{
BusyWindow.IsBusy = true;
WriteableBitmap wBitmap = new WriteableBitmap(PreviewWindow.LayoutRoot, null);
int hgt = wBitmap.PixelHeight;
int wdth = wBitmap.PixelWidth;
EditableImage ei = new EditableImage(wdth, hgt);
for (int y = 0; y < hgt; y++)
{
for (int x = 0; x < wdth; x++)
{
int pixel = wBitmap.Pixels[((y * wdth) + x)];
ei.SetPixel(x, y, (byte)((pixel >> 16) & 0xff), (byte)((pixel >> 8) & 0xff), (byte)(pixel & 0xff), (byte)((pixel >> 24) & 0xff));
}
}
System.IO.Stream pngStream = ei.GetStream();
printBuffer = new Byte[pngStream.Length];
pngStream.Read(printBuffer, 0, printBuffer.Length);
switch (fsd.FilterIndex)
{
case 1:
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(new Uri(Application.Current.Host.Source.ToString().Substring(0, Application.Current.Host.Source.ToString().LastIndexOf("/")) + "/Png2Pdf.ashx"));
hwr.Method = "POST";
printStream = fsd.OpenFile();
printSyncContext = System.Threading.SynchronizationContext.Current;
hwr.BeginGetRequestStream(new AsyncCallback(PrintStart), hwr);
break;
case 2:
//Save the PNG to local disk
lc.Show();
System.IO.Stream fs = fsd.OpenFile();
fs.Write(printBuffer, 0, printBuffer.Length);
fs.Close();
BusyWindow.IsBusy = false;
lc.Close();
break;
}
}
}
在处理程序:
public void ProcessRequest(HttpContext context)
{
int PDFPageHeight = 1056;
int PDFPageWidth = 814;
if (context.Request.ContentLength > 0)
{
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.Document pdfDoc = new iTextSharp.text.Document(new iTextSharp.text.Rectangle(PDFPageWidth, PDFPageHeight), 0, 0, 0, 0);
iTextSharp.text.pdf.PdfWriter pdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, pdfStream);
pdfWriter.CloseStream = false;
pdfDoc.Open();
Bitmap bmp = new Bitmap(incomingStream);
int pages = (int)Math.Floor((decimal)(bmp.Height / PDFPageHeight));
for (int i = 0; i < pages; i++)
{
pdfDoc.NewPage();
Rectangle PageRect = new Rectangle(0, (PDFPageHeight * i), img.Width, PDFPageHeight);
Bitmap pageBMP = bmp.Clone(PageRect, System.Drawing.Imaging.PixelFormat.DontCare);
Image pageImage = (Image)pageBMP;
pdfDoc.Add(iTextSharp.text.Image.GetInstance(pageImage, System.Drawing.Imaging.ImageFormat.Png));
}
pdfDoc.Close();
context.Response.Clear();
context.Response.ContentType = "application/pdf";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=Export.pdf");
context.Response.AppendHeader("Content-Length", pdfStream.Length.ToString());
context.Response.Buffer = false;
if (pdfStream.Position > 0) { pdfStream.Position = 0; };
Byte[] bytesRead = new Byte[_BufferSize];
long toRead = pdfStream.Length;
while (toRead > 0 && context.Response.IsClientConnected)
{
if (_BufferSize > toRead)
{
_BufferSize = (int)toRead;
bytesRead = new Byte[_BufferSize];
};
pdfStream.Read(bytesRead, 0, _BufferSize);
context.Response.OutputStream.Write(bytesRead, 0, _BufferSize);
context.Response.Flush();
bytesRead = new Byte[_BufferSize];
toRead -= _BufferSize;
}
context.Response.OutputStream.Flush();
context.Response.End();
if (pdfStream != null)
{
pdfStream.Close();
pdfStream.Dispose();
}
}
}