Insert tiff at specific page in pdf and retrieve it again C#

Example requests & Code samples for GdPicture Toolkits.
Post Reply
bdekker
Posts: 1
Joined: Sat Mar 21, 2015 12:31 pm

Insert tiff at specific page in pdf and retrieve it again C#

Post by bdekker » Wed Apr 01, 2015 2:01 pm

Hi,

I'm using version 10 and trying to insert a tiff image in a pdf after a certain page. In another process I want to extract all the images from that pdf.

To insert the tiff image I use code below:

Code: Select all

                            string image_res_name = gdPicturePDF.AddImageFromGdPictureImage(imageId, false, false);

                            var pageHeight = gdPictureImaging.GetHeight(imageId);
                            var pageWidth = gdPictureImaging.GetWidth(imageId);

                            gdPictureImaging.ReleaseGdPictureImage(imageId);

                            gdPicturePDF.SetOrigin(PdfOrigin.PdfOriginBottomLeft);
                            gdPicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitPoint);
                            gdPicturePDF.InsertPage(pageWidth, pageHeight, pageIndex + 1);
                            gdPicturePDF.SelectPage(pageIndex + 1);

                            //Draw the image on the PDF page
                            gdPicturePDF.DrawImage(image_res_name, 0, 0, pageHeight, pageWidth);
                            //gdPicturePDF.AddImageFromGdPictureImage(imageId, false, true);

                            gdPicturePDF.SaveToFileInc(pdfFileOutputPath);
                            gdPicturePDF.CloseDocument();
This inserts a single tiff (or any single image) after a given pageindex and works fine.

In an other process I use code below to get all images from a pdf:

Code: Select all

                        gdPicturePDF.SelectPage(pageIndex + 1);
                        //Rendering the selected page to an image
                        int imageId = gdPicturePDF.RenderPageToGdPictureImageEx(200, false);
                        //checking if the image was rendered successfuly
                        if (imageId != 0)
                        {
                            //saving the image back to outputstream
                            gdPictureImaging.SaveAsStream(imageId, pngOutputStream, DocumentFormat.DocumentFormatPNG, 9);
                            gdPictureImaging.ReleaseGdPictureImage(imageId);

                            pngOutputStream.Position = 0;

                            result = true;
                        }
The inserted tiff gives back imageId = 0 here while the rest of the pdf is going just fine. So somehow gdPicture can't resolve the tiff as image anymore.
I have tried to rewrite code but then I'm facing another problem. I can use the code below to insert the tiff at the and, which i don't want but fro testing it was ok:

Code: Select all

                            gdPicturePDF.InsertPage(PdfPageSizes.PdfPageSizeA4, pageIndex + 1);
                            gdPicturePDF.SelectPage(pageIndex + 1);

                            //adding image as a resource and drawing it in a new page
                            gdPicturePDF.AddImageFromGdPictureImage(imageId, false, true);
                            if (gdPicturePDF.GetStat() == GdPictureStatus.OK)
                            {
                                //Save the new PDF to file
                                gdPicturePDF.SaveToFileInc(pdfFileOutputPath);
                                result = true;
                            }

                            //Clear resources
                            gdPicturePDF.CloseDocument();
                            gdPictureImaging.ReleaseGdPictureImage(imageId);
The tiff is now recognized when I load the pdf again and get all images but the inserted tiff is at the and of the pdf.

Due to the fact that I use a stream as input for reading the pdf which I get from my application, I've written al the gdPicture logic in a different project which will become a Library, rewriting code an rebuilding pdf files with remembering indexes, pages old en new pdf file is not realy an option.

I think I missed something, but can't figure out what.

Kind regards,

Bas

Cedric
Posts: 269
Joined: Sun Sep 02, 2012 7:30 pm

Re: Insert tiff at specific page in pdf and retrieve it agai

Post by Cedric » Mon Jun 08, 2015 10:42 am

Hello,

First thing to do is trying to understand why you get an imageId = 0 after the RenderPageToGdPictureImageEx call, without this information it is impossible to tell what is wrong.
If adding your image at the end of the PDF documents works properly, maybe a simple workaround would be to move the added page to the desired index using the MovePage method.

Regards,
Cedric

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests