Add image to pdf

Discussions about PDF management.
Post Reply
nschafer
Posts: 28
Joined: Tue Oct 30, 2007 10:05 pm
Location: Florida, USA

Add image to pdf

Post by nschafer » Wed Aug 26, 2009 3:53 pm

Hello Loic,

I'm trying what should be a fairly simple task. To create a pdf with a series of images in it from my database. I retrieve the images without any trouble, and can display them in a GdViewer control with no problem. SaveToPDF also works fine. But creating a PDF and adding the image, so that I can create a multi-page PDF this way does not seem to work.

Here's my code, it's basic functionality is to loop through the rows in a datagridview, get a documentID from the first cell in the datagridview, open the images with that documentID (may be multiple pages). Loop through the images with that documentID adding each one to a PDF file. The looping seems to work fine, I get the correct number of pages, I just don't get any images on the pages.

Thanks,
Neal.

--- Edit 08/26/09 10:10 AM (UTC - 4) ---
I added a line of code to display the return value of PdfAddImageFromGdPictureImage(pdfid, imageid). It returns 0, indicating an error, but what could that error be?
---- End edit ---

Code: Select all

        private void btnMakePDF_Click(object sender, EventArgs e)
        {
            GdPictureImaging img = new GdPictureImaging();
            int pdfid;
            pdfid = img.PdfNewPdf("C:\\test.pdf");
            img.PdfSetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitInch);
            for (int z = 0; z < this.dataGridView1.Rows.Count; z++)
            {
                string DocID = this.dataGridView1.Rows[z].Cells[0].Value.ToString();
                this.getimages(DocID);
                for (int i = 1; i <= this.alPictures.Count; i++)
                {
                    int imageid;
                    float pgheight = (float)11;
                    float pgwidth = (float)8.5;
                    byte[] imgArray = (byte[])this.alPictures[i-1];
                    imageid = img.CreateGdPictureImageFromByteArray(ref imgArray);
                    GdViewer1.DisplayFromGdPictureImage(imageid);                        // Display Image - Works Fine
                    img.PdfNewPage(pdfid,pgwidth,pgheight);
                    int pdfImageID = img.PdfAddImageFromGdPictureImage(pdfid, imageid);
                    img.PdfDrawImage(pdfid, pdfImageID, 0, 0, pgwidth, pgheight);        // Image Does not show in PDF
                    img.SaveAsPDF(imageid, "C:\\test1.pdf", false, "", "", "", "", "");  // Save to PDF works Fine
                    //img.PdfDrawLine(pdfid, 1, 1, 5, 5, 1, Color.Red);                  // Drawing Line works fine
                }
            }
            img.PdfEndPdf(pdfid);
        }

nschafer
Posts: 28
Joined: Tue Oct 30, 2007 10:05 pm
Location: Florida, USA

Re: Add image to pdf

Post by nschafer » Wed Aug 26, 2009 4:14 pm

Working now. Just goes to show you that RTFM (Read the fine manual) is always the best first step. I had missed the following line in the documentation:
All the used images must be added into the pdf before using the PdfNewPage() function, otherwise this function will return 0. !
Moving the PdfAddImageFromGdPictureImage line before the PdfNewPage line solved the problem.

Neal.

nschafer
Posts: 28
Joined: Tue Oct 30, 2007 10:05 pm
Location: Florida, USA

Re: Add image to pdf

Post by nschafer » Wed Aug 26, 2009 5:05 pm

I decided to keep adding my corrections to this post so that if anyone else ran into similar issues this would be here to help. My previous post worked great for a single image, but for multiple images, only the first was included properly. It turns out I was still having the same trouble as noted above. All of the images must be added before any PdfNewPage calls are made, not just the ones to go in that page. so here's working code that puts all of the pages of all of the documents into a single PDF.

Hope this helps someone,
Neal.

Code: Select all

private void btnMakePDF_Click(object sender, EventArgs e)
{
    GdPictureImaging img = new GdPictureImaging();
    int pdfid;
    pdfid = img.PdfNewPdf("C:\\test.pdf");
    img.PdfSetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitInch);
    int imagecount = 0;
    float pgheight = (float)11;
    float pgwidth = (float)8.5;

    for (int z = 0; z < this.dataGridView1.Rows.Count; z++)
    {
        string DocID = this.dataGridView1.Rows[z].Cells[0].Value.ToString();
        this.getimages(DocID);  //Loads all images for the specified document into an arraylist as byte arrays
        int pdfImageID = 0;
        for (int i = 1; i <= this.alPictures.Count; i++)
        {
            int imageid;
            byte[] imgArray = (byte[])this.alPictures[i-1];
            imageid = img.CreateGdPictureImageFromByteArray(ref imgArray);
            GdViewer1.DisplayFromGdPictureImage(imageid);
            pdfImageID = img.PdfAddImageFromGdPictureImage(pdfid, imageid);
            if (pdfImageID > 0) imagecount++;
            Console.WriteLine(pdfImageID);
        }
    }
    for (int i = 1; i <= imagecount; i++)
    {
        img.PdfNewPage(pdfid, pgwidth, pgheight);
        img.PdfDrawImage(pdfid, i, 0, 0, pgwidth, pgheight);
    }
    img.PdfEndPdf(pdfid);
}


Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests