PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Apr 26, 2024 7:54 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Tue Mar 04, 2008 3:43 pm 
Offline

Joined: Tue Mar 04, 2008 3:31 pm
Posts: 8
I'm using ASP.NET 2.0, writing in C#. I have a virtual directory in IIS mapped to a share that has scanned images. The format on these is that a single entry in our database may contain multiple scanned images, with the filename like filename-1.jpg, filename-2.jpg, etc. Only "filename" is stored in our db.

I was hoping I could get PDFSharp to work so that rather than display any images as img links on my page, I could merge all appropriate images into a single PDF that is presented to the user. I thought I had it based on the sample code, but I keep running into problems, either with my code locking up the file access, or it just hangs on me, or gives a blank screen with nothing. I've tried debugging through it but don't seem to be getting anywhere. I'll post my code - hopefully I'm just doing something silly and someone can point out how I should be doing this? Thanks!

Code:
//// Create new PDF document
                    PdfDocument document = new PdfDocument();
                    document.Info.CreationDate = DateTime.Now;
                    document.Info.Title = "Letter";
                    document.Info.Author = "My Company Name";
                    document.Info.Subject = "Server time: " + DateTime.Now.ToShortTimeString();

                    for (int i = 1; ; i++)
                    {
                        FullFilename = ServerImagePath + "\\" + filename + "-" + i.ToString() + ".jpg";
                        if (File.Exists(FullFilename))
                        {
                            try
                            {
                                // Create new page
                                PdfPage page = document.AddPage();

                                FileStream fs = new FileStream(FullFilename, FileMode.Open, FileAccess.Read);

                                document.Save(fs, false);
                                Response.Clear();
                                Response.ContentType = "application/pdf";
                                Response.AddHeader("content-length", fs.Length.ToString());
                                //Response.BinaryWrite(fs.ToArray());
                                BinaryReader br = new BinaryReader(fs);
                                for (long l = 0; l < fs.Length; l++)
                                {
                                    Response.OutputStream.WriteByte(br.ReadByte());
                                }

                                fs.Close();
                               
                            }
                            catch (Exception ex)
                            {
                                Response.Flush();
                                Response.End();
                                break;
                            }
                        }
                        else
                            break;


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Mar 05, 2008 3:14 pm 
Offline

Joined: Tue Mar 04, 2008 3:31 pm
Posts: 8
An update, I'm at least getting a PDF now, but it's saying it's corrupted. My updated code from what I posted above:

Code:
try
                        {
                            document.Info.CreationDate = DateTime.Now;
                            document.Info.Title = "";
                            document.Info.Author = "";
                            document.Info.Subject = "Server time: " + DateTime.Now.ToShortTimeString();

                            FileStream fs = new FileStream(FullFilename, FileMode.Open);

                            // Create new page
                            PdfPage page = document.AddPage();

                            //Write PDF
                            document.Save(fs, false);
                            Response.Clear();
                            Response.ClearContent();
                            Response.ClearHeaders();
                            Response.ContentType = "application/pdf";
                            Response.AddHeader("content-length", fs.Length.ToString());
                           
                            BinaryReader br = new BinaryReader(fs);
                            Byte[] dataBytes = br.ReadBytes((int)(fs.Length - 1));
                            Response.BinaryWrite(dataBytes);
                           

                            fs.Close();
                            Response.Flush();
                        }
                        catch (Exception ex)
                        {
                            //Handle this better!
                            Response.Write(ex.Message.ToString());
                            Response.Flush();
                            Response.Close();
                            Response.End();
                            break;
                        }
                        Response.Flush();
                        Response.Close();
                        Response.End();


This is giving me a PDF, but it's saying "not a PDF or corrupted". What am I doing wrong? Thanks!


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Mar 05, 2008 8:38 pm 
Offline

Joined: Tue Mar 04, 2008 3:31 pm
Posts: 8
Arrrgh, I'm getting closer. This gets me my jpg image into the PDF, but it's showing up severely shrunk down. It also appears to be replicated three times - so the end result is I have my PDF, with the image in it, but it's very distorted. Here's the code I've got that creates this:

Code:
document.Info.CreationDate = DateTime.Now;
                    document.Info.Title = "";
                    document.Info.Author = "";
                    document.Info.Subject = "Server time: " + DateTime.Now.ToShortTimeString();

                    int i = 1;
                    FullFilename = ServerImagePath + "\\" + filename + "-" + i.ToString() + ".jpg";
MemoryStream ms = new MemoryStream();
                       
                        //// Create new PDF page
                        PdfPage page = document.AddPage();
                        XImage image = XImage.FromFile(FullFilename);
                       
                        page.Width = image.Width;
                        page.Height = image.Height;
                        XGraphics gfx = XGraphics.FromPdfPage(page);
                        gfx.DrawImage(image, 0, 0);
                        gfx.Dispose();
                        image.Dispose();
                        page.Close();
                        //Send PDF to browser
                        document.Save(ms, false);
                        Response.Clear();
                        Response.ContentType = "application/pdf";
                        Response.AddHeader("content-length", ms.Length.ToString());
                        Response.AppendHeader("content-disposition", string.Format("attachment;filename=output.pdf"));
                        Response.BinaryWrite(ms.ToArray());
                       
                        Response.Flush();
                        ms.Close();


I tried this code from another post:


Help!
Code:
//page.Width = XUnit.FromInch(image.Width / image.HorizontalResolution);
                        //page.Height = XUnit.FromInch(image.Height / image.VerticalResolution);


thinking that would help the proportion problem, but that just made the page the size of the shrunk image. And it still has the repeating problem.

The jpegs I'm working with are scanned documents in grayscale. Does that affect anything?


Top
 Profile  
Reply with quote  
 Post subject: I got it! Almost.....
PostPosted: Wed Mar 05, 2008 10:10 pm 
Offline

Joined: Tue Mar 04, 2008 3:31 pm
Posts: 8
Aha! I've got it now looping through the jpg images (after changing the PdfImage.cs file to fix the grayscale jpg problem), loading them to PDFs, and stretching the JPG to fill the letter-sized PDF:

Code:
document.Info.CreationDate = DateTime.Now;
                    document.Info.Title = "";
                    document.Info.Author = "";
                    document.Info.Subject = "Server time: " + DateTime.Now.ToShortTimeString();

                    int i = 1;
                    FullFilename = ServerImagePath + "\\" + filename + "-" + i.ToString() + ".jpg";
                   
                    //Create OUTBOUND stream for the PDF to write to
                    MemoryStream ms = new MemoryStream();

                    while (File.Exists(FullFilename))
                    {
                        try
                        {
                           
                            // Create new PDF page
                            PdfPage page = document.AddPage();

                            XImage image = XImage.FromFile(FullFilename);
                            //page.Width = XUnit.FromInch(image.Width / image.HorizontalResolution);
                            //page.Height = XUnit.FromInch(image.Height / image.VerticalResolution);
                            //page.Width = image.Width;
                            //page.Height = image.Height;

                            XGraphics gfx = XGraphics.FromPdfPage(page);
                            gfx.DrawImage(image, 0, 0, image.Width, image.Height);
                            gfx.Dispose();
                            image.Dispose();
                            page.Close();
                            //Send PDF to browser
                           
                           
                        }
                        catch (Exception ex)
                        {
                            //Handle this better!
                            Response.Write(ex.Message.ToString());
                            Response.Flush();
                            Response.Close();
                            Response.End();
                            break;
                        }
                        finally
                        {
                           
                        }
                        i++;
                        FullFilename = ServerImagePath + "\\" + filename + "-" + i.ToString() + ".jpg";
                    }

                    document.Save(ms, false);
                    Response.Clear();
                    Response.ContentType = "application/pdf";
                    Response.AddHeader("content-length", ms.Length.ToString());
                    Response.AppendHeader("content-disposition", string.Format("attachment;filename=output.pdf"));
                    Response.BinaryWrite(ms.ToArray());
                    Response.Flush();
                    Response.Close();
                    Response.End();
                    ms.Close();


Now I'm noticing that stretching the jpg to fill the PDF page loses a lot of quality. These are grayscale jpg scans, on letter sized paper. But their DPI is 200. Viewing the jpg images straight in the browser looks a lot better than the images written to pdf, but I believe that is the same result as if I had printed the jpg images to pdf or to a laser printer anyway.

So, is there any suggested ways to improve quality of images added to pdf documents? Just a higher resolution? Are there any tricks using the built in graphics libraries to improve quality?


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 407 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group