Identify image field type in looping through form fields

PDF-XChange Editor SDK for Developers

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Sean - Tracker, Paul - Tracker Supp, Chris - Tracker Supp, Tracker Supp-Stefan, Ivan - Tracker Software

Forum rules
DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.

When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
Post Reply
coblejesse
User
Posts: 32
Joined: Thu May 16, 2019 7:44 pm

Identify image field type in looping through form fields

Post by coblejesse » Fri May 31, 2019 1:01 pm

Hi Tracker,

I am looping through the form fields on documents, and saving the form field name, and value in our db. I would like to save the image in our db, from the image fields, but can't figure out how to identify an image form field, since it shows as a "FFT_PushButton". I've looked at other forum posts and do not see how I can accomplish this. Also, how would I pull the image data out of the form field, so i can convert it to a byte array to save to the db.

Code: Select all

PDFXEdit.IPXC_AcroForm fields = ucPdfViewer1.PDFXEditor.Doc.CoreDoc.AcroForm;
                for (uint i = 0; i < fields.FieldsCount; i++)
                {
                    PDFXEdit.IPXC_FormField formField = fields.Field[i];
                    if (formField.Type == PDFXEdit.PXC_FormFieldType.FFT_CheckBox || formField.Type == PDFXEdit.PXC_FormFieldType.FFT_Text ||
                        formField.Type == PDFXEdit.PXC_FormFieldType.FFT_RadioButton || formField.Type == PDFXEdit.PXC_FormFieldType.FFT_ComboBox ||
                        formField.Type == PDFXEdit.PXC_FormFieldType.FFT_ListBox)
                    {
                        string value = fields.Field[i].GetValueText();
                        //save text value to db
                    }

                    PDFXEdit.IPXC_Annotation annot = formField.Widget[0];
                    PDFXEdit.IPXC_AnnotData_Widget WData = (PDFXEdit.IPXC_AnnotData_Widget)annot.Data;
                    string s = WData.GetType().ToString();
                    if (s == "not showing image type")
                    {
                        //save image data to db here
                    }
                    string annotType = ucPdfViewer1.pxsInst.AtomToStr(annot.Type);
                    if (annotType == "not showing image type here as well")
                    {
                        //save image data to db here
                    }
                }

User avatar
Sasha - Tracker Dev Team
User
Posts: 4442
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Identify image field type in looping through form fields

Post by Sasha - Tracker Dev Team » Fri May 31, 2019 2:00 pm

Hello Jesse,

From what I know, these are the same field types and the UI just offers you different style presets for these.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

coblejesse
User
Posts: 32
Joined: Thu May 16, 2019 7:44 pm

Re: Identify image field type in looping through form fields

Post by coblejesse » Fri May 31, 2019 3:46 pm

I guess I'm confused now and that really didn't answer my question or maybe I just don't understand the answer at all. The below code was just an attempt at figuring out how to identify an image field from other field types. Maybe I'm going about it completely wrong and just ignore what I have wrote. Can you please point me in the right direction on how I might go about this inside the loop I had provided? Or just something to go off of to identify the image fields, and then grab the data to convert to byte array?

Code: Select all

PDFXEdit.IPXC_Annotation annot = formField.Widget[0];
                    PDFXEdit.IPXC_AnnotData_Widget WData = (PDFXEdit.IPXC_AnnotData_Widget)annot.Data;
                    string s = WData.GetType().ToString();
                    if (s == "not showing image type")
                    {
                        //save image data to db here
                    }
                    string annotType = ucPdfViewer1.pxsInst.AtomToStr(annot.Type);
                    if (annotType == "not showing image type here as well")
                    {
                        //save image data to db here
                    }

coblejesse
User
Posts: 32
Joined: Thu May 16, 2019 7:44 pm

Re: Identify image field type in looping through form fields

Post by coblejesse » Fri May 31, 2019 5:19 pm

Hi Alex,

Ok, I think I understand that the image field is just a push button and it just uses the icon of the push button as the image. My issue is now just getting the icon from the push button field.

Code: Select all

PDFXEdit.IPXC_Annotation annot = formField.Widget[0];
                    PDFXEdit.IPXC_AnnotData_Widget WData = (PDFXEdit.IPXC_AnnotData_Widget)annot.Data;
                    
                    if (WData.HasIcon(PDFXEdit.PXC_AnnotAppType.AAT_Normal) && formField.Type == PDFXEdit.PXC_FormFieldType.FFT_PushButton)
                    {
                        PDFXEdit.IPXC_XForm form = WData.GetIcon(PDFXEdit.PXC_AnnotAppType.AAT_Normal);
                        PDFXEdit.IPXC_Content content =  form.GetContent();
                    }

User avatar
Sasha - Tracker Dev Team
User
Posts: 4442
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Identify image field type in looping through form fields

Post by Sasha - Tracker Dev Team » Sat Jun 01, 2019 10:58 am

Hello Jesse,

Once you got the XForm's content, you can get the image itself from it. Here's the sample code that you can use to your cause:

Code: Select all

var imageList = new List<Image>();
IPXC_Content content = pdfCtl.Doc.CoreDoc.Pages[0].GetContent(PXC_ContentAccessMode.CAccessMode_Readonly);
for (uint i = 0; i < content.Items.Count; i++)
{
	IPXC_ContentItem item = content.Items[i];
	if ((item.Type != PXC_CIType.CIT_Image) && (item.Type != PXC_CIType.CIT_InlineImage))
		continue;
	PDFXEdit.IIXC_Page iPage = item.Image_CreateIXCPage();
	Bitmap b = new Bitmap((int)iPage.Width, (int)iPage.Height);
	Graphics g = Graphics.FromImage(b);
	g.Clear(Color.White);
	IntPtr hdc = g.GetHdc();
	iPage.DrawToDC((uint)hdc, 0, 0, iPage.Rect.right - iPage.Rect.left, iPage.Rect.bottom - iPage.Rect.top, 0, 0);
	g.ReleaseHdc(hdc);
	imageList.Add(b);
}
Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

coblejesse
User
Posts: 32
Joined: Thu May 16, 2019 7:44 pm

Re: Identify image field type in looping through form fields

Post by coblejesse » Sat Jun 01, 2019 2:20 pm

Thank you very much Alex! But for some reason I am having such a difficult time with this. I guess I don't understand fundamentally what this code is doing, and I can't find much info on it other than the interfaces properties/methods. Am I missing more documentation somewhere? I'd be happy to read it. From my code below, it is never finding a PXC_CIType.CIT_Image or PXC_CIType.CIT_InlineImage even though I know there is one, since the HasIcon of the form field is true. Am I doing this correctly in the fact that I'm getting the XForm from the AnnotData_Widget by GetIcon, and then getting the content from that XForm? Why would the GetIcon method bring back a whole XForm, and not just the icon? The content items count is around 200, which does not make any sense, as there are only 3 fields on the form I am trying to save? I'm sorry I'm difficult, I just don't know where else to turn for answers. I've read every forum post I could, and tried to make my way through the documentation, but it isn't very helpful.

Code: Select all

IPXC_Annotation annot = formField.Widget[0];
                    IPXC_AnnotData_Widget WData = (PDFXEdit.IPXC_AnnotData_Widget)annot.Data;

                    if (WData.HasIcon(PDFXEdit.PXC_AnnotAppType.AAT_Normal) && formField.Type == PDFXEdit.PXC_FormFieldType.FFT_PushButton)
                    {
                        IPXC_XForm form = WData.GetIcon(PXC_AnnotAppType.AAT_Normal);
                        IPXC_Content content = form.GetContent(PXC_ContentAccessMode.CAccessMode_Readonly);
                        //IPXC_Content content = ucPdfViewer1.PDFXEditor.Doc.CoreDoc.Pages[0].GetContent(PXC_ContentAccessMode.CAccessMode_Readonly);
                        IPXC_ContentItems items = content.Items;
                        for (UInt32 a = 0; a < items.Count; a++)
                        {
                            IPXC_ContentItem item = content.Items[a];

                            PXC_CIType itemType = item.Type;
                            if ((item.Type != PXC_CIType.CIT_Image) && (item.Type != PXC_CIType.CIT_InlineImage))
                                continue;
                            //It's never reaching here?
                            IIXC_Page iPage = item.Image_CreateIXCPage();
                            Bitmap b = new Bitmap((int)iPage.Width, (int)iPage.Height);
                            Graphics g = Graphics.FromImage(b);
                            g.Clear(System.Drawing.Color.White);
                            IntPtr hdc = g.GetHdc();
                            iPage.DrawToDC((uint)hdc, 0, 0, iPage.Rect.right - iPage.Rect.left, iPage.Rect.bottom - iPage.Rect.top, 0, 0);
                            g.ReleaseHdc(hdc);
                            //add image to datatable 
                        }
                    }

User avatar
Sasha - Tracker Dev Team
User
Posts: 4442
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Identify image field type in looping through form fields

Post by Sasha - Tracker Dev Team » Mon Jun 03, 2019 8:16 am

Hello Jesse,

I've been thinking about this one and probably I have a better solution for you.
Let's look through your scenario again. Your users add annotations in Word and then you are generating a PDF file. Then you can add some form fields to the document and happily store that document into the database. Then your user decides to add some more annotations and this is where the trouble begins.
There are two possible solutions for this one, depending on the situation.
1) Your users change only the annotations that are converted to the PDF annotations by 100% (meaning no content is being changed whatsoever).
If such a case is correct then what you can do is convert this document to the IPXC_Document, do the ExportComments operation for it into FDF (that's important) and close the newly generated IPXC_Document. Then remove all of the annotations that are not widgets from your old document with Form Fields. After all of that you do the ImportComments operation and thus you have successfully updated the document with new annotations. Then again this will work if only the annotations are being updated.
2) If the document's content is also being modified (for example, your users used some annotations that will be converted to the content as these are not the PDF annotations).
In this case you will have to insert pages from the newly generated IPXC_Document into the current document and then Move the Widget annotations from the old pages into the new positions. After this, remove the original pages and you will have the desired result.

All of this should be done this way, because once you have added the form fields the AcroForm is being updated with all of the needed data and then you will only sustain a stable document by using one of these methods. The good part of this is that you won't be needing to store some other data then a document in the database.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

coblejesse
User
Posts: 32
Joined: Thu May 16, 2019 7:44 pm

Re: Identify image field type in looping through form fields

Post by coblejesse » Mon Jun 03, 2019 3:53 pm

Thanks Alex for the reply. So in case 1, after I have exported the comments to FDF, how would i go about getting rid of the annotations that were created in word? I see how I can do the rest of it, but this one step is where I'm hung up.

User avatar
Sasha - Tracker Dev Team
User
Posts: 4442
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Identify image field type in looping through form fields

Post by Sasha - Tracker Dev Team » Tue Jun 04, 2019 7:22 am

Hello Jesse,

You mean there can already be some annotations in the original file when you are adding them from Word?
I though your file was a content only file along some annotations that you are converting from Word. If that's the case, you should delete all of the annotations in the original file (except Widgets) and replace them with the new annotations from the Word.

Cheers,
Alex
Join us at Google+:
https://plus.google.com/+PDFXChangeEditorTS
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ

Post Reply