Substitute Image

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF CoreSuite for .NET (v7)  /  Re: Substitute Image

DynamicPDF CoreSuite for .NET (v7) Forum

 Apr 29 2014 7:52 PM
I have a PDF document that was provided by a third party, and is intended to be a product brochure. The idea is we would dynamically customize the brochure for users of our software so that the logo, and potentially verbiage, would be injected when the file is requested.

Ideally I'd like to have a placeholder element on a page in advance (for position and size), and set the byte array for the image prior to returning the document to the user. But I'm having problem getting this to work.

I am having problems locating the placeholder element that I need to use for setting the image. I was hoping it would be something like document.Pages[0].Elements["elementId"].SetImageData(theNewImageData);
but I'm not having much luck so far.

I see methods to add a brand new element using the image data I provide, but no way to dynamically change the image data for an existing element, or by ElementId (I tried but it shows 0 total elements even though the pdf does have content in it). Alternatively, I could create a brand new element using the image data intended and just give it the same coordinates as the placeholder element, but then I need a way to remove the placeholder image, or to hide it, but I don't see methods for that either yet.

Any help you can provide would be appreciated

In summary, I have three things I'm struggling with:
How to retrieve an existing element by an identifier of some kind
How to set the ImageData of an existing element
How to remove an existing element
 Apr 30 2014 9:46 AM
Posted by a ceTe Software moderator
Hello,

You can add image data to the PDF using Image page element of our DynamicPDF Generator API. Please refer to the documentation on Image page element here. Also please note that you can add images to the desired positions and set the required dimensions using X, Y, Width and Height properties.

1.It is not possible to retrieve existing page elements details using our DynamicPDF API.
2.It is not possible to set the image data of an existing page element but you can add image data using image page element.
3.It is not possible to remove any existing data or element of an existing PDF.

Thanks,
ceTe Software Support Team.
 Apr 30 2014 12:10 PM
Posted by a ceTe Software moderator
Hello,

We looked into your requirements further. You can place a hidden form field (TextField) in the PDF and then dynamically access this using full name and can get its X, Y, width and height values using our DynamicPDF Merger API. You can refer to the documentation on acroform fields here. You can set these retrieved values of the hidden form field to the image page element and add it to PDF page. 

You will need to use the PdfDocument class members to get the positions and dimensions of the hidden form field in the existing PDF. Please refer to the documentation on PdfDocument class members here. Please note that it is not possible to replace the existing image with other using our DynamicPDF API. Below is the sample code to add hidden form field and save the PDF to disk then open it using Merger product API features to add the image in the position of hidden field.

Sample code to create PDF by adding hidden field.

            Document document = new Document();
            Page page = new Page();
            document.Pages.Add(page);
            ceTe.DynamicPDF.PageElements.Forms.TextField textFieldObj = new ceTe.DynamicPDF.PageElements.Forms.TextField("txt_name", 100, 100, 200, 200);
            textFieldObj.Visible = false;
            page.Elements.Add(textFieldObj);
            //Save the document.
            document.Draw(@"C:\Temp\MyDocument.pdf");

Sample code for opening the PDF to add image in the position of hidden field.

            PdfDocument pdf = new PdfDocument(@"C:\Temp\MyDocument.pdf");
            MergeDocument mergeDocument = new MergeDocument(pdf);
            //Get the positions and dimensions of the existing form fied by accessing it using full name.
            int pagenumber = pdf.Form.Fields["txt_name"].GetOriginalPageNumber();
            float x = pdf.Form.Fields["txt_name"].GetX(mergeDocument.Pages[pagenumber-1]);
            float y = pdf.Form.Fields["txt_name"].GetY(mergeDocument.Pages[pagenumber - 1]);
            float width = pdf.Form.Fields["txt_name"].Width;
            float height = pdf.Form.Fields["txt_name"].Height;
            //Get the image data
            ImageData imageData = ImageData.GetImage(@"Path for the image");
            //Create Image object using imageData and setting the positions and dimensions.
            Image image = new Image(imageData, x, y);
            image.Width = width;
            image.Height = height;
            mergeDocument.Pages[pagenumber - 1].Elements.Add(image);
            mergeDocument.Draw(@"C:\Temp\MyNewDocument.pdf");

Thanks,
ceTe Software Support Team.
 May 08 2014 1:55 PM
So I've got the code working exactly as you described using the hidden form element. But now the image I am adding to the PDF is distorted and doesn't show correct within the PDF, but it looks fine when not in the PDF. Is there something I have to take into account with regards to DPI to get the image to appear as intended within the file?

Also as soon as I add the image the PDF gets watermarked. Even though we are using Dynamic PDF Merger Professional which (from my understanding) should have a valid license to add images using the functionality of Dynamic PDF Generator?
 May 09 2014 11:56 AM
Posted by a ceTe Software moderator
Hello,

Adding new form fields to the PDF is an Enterprise edition feature and you have license to use only Professional edition features. This is reason for getting watermark on the output PDF. You can refer to the feature chart for the DynamicPDF Merger product here.

Regarding behavior of image getting distorted when added to PDF, please send over the following details to support@cete.com so that we can look into it further.

1.Image file.
2.Input PDF (to which you are adding hidden form field and image).
3.Sample code using which we can recreate the behavior.
4.Exact version and build number of the DynamicPDF DLL file. This information is available in the DLL reference properties (version and description fields) in Visual Studio.

Thanks,
ceTe Software Support Team.
 May 12 2014 1:06 PM
I'm not adding any new form controls, I'm just adding an image. Why would that require a higher license?

I'll send off the details...
 May 14 2014 4:22 PM
Posted by a ceTe Software moderator
Hello,

After looking at the details sent to us, the image distortion is caused because the image height and width are less than that of the placeholder’s, so when the placeholder dimensions are assigned to the image, it is getting stretched and distorted. Commenting out the below two lines of code should add the image with its original dimensions.

image.Height = placeholderElement.Height;
image.Width = placeholderElement.Width;

Regarding the watermark, the original template being used is tagged. This means that any new page elements (image, text etc) that are added to the PDF will get automatically tagged. Since tagging is an enterprise feature the PDF is being watermarked. Tagging can be turned off by setting the Tag property to null before calling the Draw method.

document.Tag = null;

Thanks,
ceTe Software Support Team.
 May 14 2014 4:30 PM
Hi, thanks for your response. Setting Tag to NULL fixed my watermark issue, thanks.

As for the other one, you're right but theres a second half that was causing me problems. The form control I was using to determine what size to scale the image to, and what size to create the new Image element to, is in Points, not Pixels. So while it's fine for the Image control, it was wrong for scaling the source image to match that of the image element.

I need to do some math on converting the Points to Pixels and I hope it should be fine after that.

All times are US Eastern Standard time. The time now is 10:50 PM.