使用iTextsharp创建的pdf模糊或模糊

时间:2013-04-22 16:12:41

标签: c# asp.net itextsharp

我正在使用silverlight网格创建一个图像。转换为png并使用iTextsharp将其插入到pdf中。最终结果是非常模糊/模糊的pdf。如果我保存与png相同的图像,则非常清楚。如果pdf具有更清晰的图像,如何改善结果?

以下是代码段(在Silverlight结束时):

private void GeneratePDF() {

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;
    }

}

}

At Handler:

 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();
            }
        }
    }

0 个答案:

没有答案