Get image of annotation selection

PDF-XChange Editor SDK for Developers

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

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
lidds
User
Posts: 510
Joined: Sat May 16, 2009 1:55 pm

Get image of annotation selection

Post by lidds »

I was hoping you could help me with getting an image of the current annotation selection, basically the same image that you get when creating a new stamp from selection (please see attached image)

Thanks

Simon
Attachments
annotSelectionImg.png
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Get image of annotation selection

Post by Sasha - Tracker Dev Team »

Hello Simon,

Well you can draw the needed area onto the IIXC_Page and then save it as an Image, if that's what you need.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
lidds
User
Posts: 510
Joined: Sat May 16, 2009 1:55 pm

Re: Get image of annotation selection

Post by lidds »

OK, is it possible for you to guide me on what commands I would use to do this and then I can have a go. Tried the snapshot command, but this lost the current selection of the annotation items?

Thanks

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

Re: Get image of annotation selection

Post by Sasha - Tracker Dev Team »

Hello Simon,

Well then I will need more details on what do you mean by getting an Image.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
lidds
User
Posts: 510
Joined: Sat May 16, 2009 1:55 pm

Re: Get image of annotation selection

Post by lidds »

Hi Alex,

OK, so what I want to be able to do is the following:

1. User draws some annotation on a blank PDF document.
2. Click a button on my form and it produces an image, but using the boundary of just the annotation elements, NOT the PDF document page size.

Basically what you are doing when creating a stamp from selection, you are creating an image using the selection region RECT. That is what I want to do, but obviously I would need to automatically select all the annotation elements, obviously unless you already have a command(s) that can achieve this?

Thanks

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

Re: Get image of annotation selection

Post by Sasha - Tracker Dev Team »

Hello Simon,

Well, the easiest way is to loop through all of the annotations on page, and add them to selection, then call the createStampFromSelection command.

Code: Select all

int nSelID = pdfCtl.Inst.Str2ID("selection.annots", false);
PDFXEdit.IPXV_AnnotSelection itSel = (PDFXEdit.IPXV_AnnotSelection)pdfCtl.Doc.CreateStdSel((uint)nSelID);
itSel.Items.Insert(annot);
pdfCtl.Doc.ActiveSel = itSel;
itSel.Show(true);
Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
lidds
User
Posts: 510
Joined: Sat May 16, 2009 1:55 pm

Re: Get image of annotation selection

Post by lidds »

Alex,

Thank you for the code this is great. However I still need to generate an image of the selection so I can save this to my database so the users can easily see what the stamp looks like without using the stamp palette. It's a bit more than that, but that's the idea.

Thanks

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

Re: Get image of annotation selection

Post by Sasha - Tracker Dev Team »

Hello Simon,

Check out this code sample:

Code: Select all

private void SavePageRectAsAnImage(IPXV_Inst Inst, IPXC_Page page, PXC_Rect rcRectOnPage)
{
	IUIX_Inst uiInst = (IUIX_Inst)Inst.GetExtension("UIX");
	IAUX_Inst auxInst = (IAUX_Inst)Inst.GetExtension("AUX");
	IIXC_Inst ixcInst = (IIXC_Inst)Inst.GetExtension("IXC");
	PXC_Matrix pageMatrix = page.GetMatrix(PXC_BoxType.PBox_ViewBox);// includes Page.Rotation
	PXC_Rect rcPage = page.get_Box(PXC_BoxType.PBox_ViewBox); // Page.Rotation not included
	PXC_Rect rcPageToCopy = rcRectOnPage;
	rcPageToCopy = auxInst.MathHelper.Rect_Intersect(ref rcPageToCopy, ref rcPage);

	PXC_Rect rcTmp = rcPage;
	PXC_Matrix pageToDeviceMatrix;

	double pw = rcTmp.right - rcTmp.left;
	double ph = Math.Abs(rcTmp.top - rcTmp.bottom);

	// calc valid scale coeffs
	double sx;
	double sy;
	double zoomLevel = 100;
	sx = sy = (uiInst.DPI * zoomLevel) / 7200.0;
	pageToDeviceMatrix = pageMatrix;
	auxInst.MathHelper.Matrix_Scale(ref pageToDeviceMatrix, sx, sy);
	rcTmp = rcPageToCopy;
	auxInst.MathHelper.Rect_Transform(ref pageToDeviceMatrix, ref rcTmp);
			
	double w = rcTmp.right - rcTmp.left; // in pixels, but without advanced-rotation
	double h = Math.Abs(rcTmp.top - rcTmp.bottom); // in pixels, but without advanced-rotation

	long iw = (long)(w + 0.5);
	long ih = (long)(h + 0.5);

	sx *= w / (rcTmp.right - rcTmp.left);
	sy *= h / Math.Abs(rcTmp.top - rcTmp.bottom);
	// calc scale matrix
	PXC_Matrix sm;
	sm.a = sx;
	sm.b = 0;
	sm.c = 0;
	sm.d = -sy;
	sm.e = 0;
	sm.f = 0;
	// build final matrix
	pageToDeviceMatrix = pageMatrix;
	pageToDeviceMatrix = auxInst.MathHelper.Matrix_Multiply(ref pageToDeviceMatrix, ref sm);
	rcTmp = rcPageToCopy;
	auxInst.MathHelper.Rect_Transform(ref pageToDeviceMatrix, ref rcTmp);

	tagRECT rc;
	rc.left = (int)Math.Round(rcTmp.left);
	rc.right = rc.left + (int)Math.Round(rcTmp.right - rcTmp.left);
	rc.top = (int)Math.Round(rcTmp.top);
	rc.bottom = rc.top + (int)Math.Round(Math.Abs(rcTmp.top - rcTmp.bottom));

	pageToDeviceMatrix = auxInst.MathHelper.Matrix_Translate(ref pageToDeviceMatrix, -rc.left, -rc.top + Math.Abs(rc.top - rc.bottom));

	iw = rc.right - rc.left;
	ih = Math.Abs(rc.top - rc.bottom);
	if (iw == 0 || ih == 0)
		return;

	IIXC_Page pxcPage = ixcInst.Page_CreateEmpty((uint)iw, (uint)ih, PDFXEdit.IXC_PageFormat.PageFormat_8RGB, 0x0000FF);
			
	PXC_Matrix mF = pageToDeviceMatrix;
	PDFXEdit.tagRECT destRect;
	destRect.bottom = (int)ih;
	destRect.right = (int)iw;// Set DPI for copy
	destRect.top = 0;
	destRect.left = 0;
	page.DrawToIXCPage(pxcPage, ref destRect, ref mF);
	pxcPage.set_FmtInt((uint)PDFXEdit.IXC_FormatParametersIDS.FP_ID_FORMAT, (uint)PDFXEdit.IXC_ImageFileFormatIDs.FMT_PNG_ID);
	pxcPage.set_FmtInt((uint)PDFXEdit.IXC_FormatParametersIDS.FP_ID_ITYPE, 21);
	pxcPage.set_FmtInt((uint)PDFXEdit.IXC_FormatParametersIDS.FP_ID_XDPI, (uint)uiInst.DPI);
	pxcPage.set_FmtInt((uint)PDFXEdit.IXC_FormatParametersIDS.FP_ID_YDPI, (uint)uiInst.DPI);

	PDFXEdit.IIXC_Image iImage = ixcInst.CreateEmptyImage();
	iImage.InsertPage(pxcPage);

	iImage.Save("D:\\PageRect.png", IXC_CreationDisposition.CreationDisposition_Overwrite);

}
Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
lidds
User
Posts: 510
Joined: Sat May 16, 2009 1:55 pm

Re: Get image of annotation selection

Post by lidds »

Alex,

That is some code, thanks.

Just have one question, the code is working and saving the image, however it is producing an image of the whole page whereas I want it just to produce and image of the annotation bound box. I guess I am doing something wrong and passing the wrong box to the procedure, please see my code below:

Code: Select all

    private void btnGetImage_Click(object sender, EventArgs e)
    {
      uint cp = 0;
      PDFXEdit.IPXV_Document doc = pdfCtl.Doc;
      PDFXEdit.IPXV_PagesLayoutManager pl = doc.ActiveView.PagesView.Layout;
      cp = pl.CurrentPage;

      PDFXEdit.IPXC_Page srcPage = doc.CoreDoc.Pages[cp];

      PDFXEdit.PXC_Rect srcRect = srcPage.get_Box(PDFXEdit.PXC_BoxType.PBox_ViewBox);

      SavePageRectAsAnImage(pdfCtl.Inst, doc.CoreDoc.Pages[cp], srcRect);
    }
Items selected:
selection.png
Image Produced:
PageRect.png
Thanks

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

Re: Get image of annotation selection

Post by Sasha - Tracker Dev Team »

Hello Simon,

Well of course - you will have to specify the needed rectangle for yourself - that's the easy part - the code sample that I have written for you was a big deal.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
lidds
User
Posts: 510
Joined: Sat May 16, 2009 1:55 pm

Re: Get image of annotation selection

Post by lidds »

Thanks Alex,

And really do appreciate the code and the work to write it. Last question I promise. I have converted the code from C# to VB.Net and there are a couple of items that are showing as an error:

Code: Select all

Dim rcPage As PXC_Rect = page.get_Box(PXC_BoxType.PBox_ViewBox)
page.get_Box = page.Box

Code: Select all

pxcPage.set_FmtInt(CType(PDFXEdit.IXC_FormatParametersIDS.FP_ID_FORMAT, UInteger), CType(PDFXEdit.IXC_ImageFileFormatIDs.FMT_PNG_ID, UInteger))
pxcPage.set_FmtInt

I have tried

Code: Select all

pxcPage.FmtInt(CType(PDFXEdit.IXC_FormatParametersIDS.FP_ID_FORMAT))
But this gives me the error

Property access must assign to the property or use its value.

Sorry to be a pain.

Thanks

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

Re: Get image of annotation selection

Post by Sasha - Tracker Dev Team »

Hello Simon,

I do not write at VB at all, but this was not so hard to figure out from the error itself:

Code: Select all

Dim rcPage As PDFXEdit.PXC_Rect = page.Box(PDFXEdit.PXC_BoxType.PBox_ViewBox)
page.FmtInt(CType(PDFXEdit.IXC_FormatParametersIDS.FP_ID_FORMAT, UInteger)) = CType(PDFXEdit.IXC_ImageFileFormatIDs.FMT_PNG_ID, UInteger)
Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
lidds
User
Posts: 510
Joined: Sat May 16, 2009 1:55 pm

Re: Get image of annotation selection

Post by lidds »

Alex,

I must be having one of those days as I can't figure out how to get the page RECT of the selected annotations. I am doing the following but it is throwing an error on the below line:

Code: Select all

Dim prs As IPXV_PageRectSelection = CType(docSel, IPXV_AnnotSelection)

Code: Select all

        Dim nSelID As Integer = Me.docStampPreview.Inst.Str2ID("selection.annots", False)
        Dim page As PDFXEdit.IPXC_Page = Me.docStampPreview.Doc.CoreDoc.Pages(0)
        Dim nAnnotCount As UInteger = page.GetAnnotsCount()
        Dim itSel As PDFXEdit.IPXV_AnnotSelection = CType(Me.docStampPreview.Doc.CreateStdSel(CType(nSelID, UInteger)), PDFXEdit.IPXV_AnnotSelection)

        If nAnnotCount <> 0 Then
            For i As UInteger = 0 To nAnnotCount - 1
                Dim annot As PDFXEdit.IPXC_Annotation = page.GetAnnot(i)

                If annot.IsMarkup Then
                    itSel.Items.Insert(annot, i)
                    Me.docStampPreview.Doc.ActiveSel = itSel
                End If
            Next
        End If

        itSel.Show(True)

        Dim cp As UInteger = 0
        Dim doc As PDFXEdit.IPXV_Document = Me.docStampPreview.Doc
        Dim pl As PDFXEdit.IPXV_PagesLayoutManager = doc.ActiveView.PagesView.Layout
        cp = pl.CurrentPage
        Dim srcPage As PDFXEdit.IPXC_Page = doc.CoreDoc.Pages(cp)

        Dim docSel As PDFXEdit.IPXV_DocSelection = Me.docStampPreview.Doc.GetSel(itSel.ID)
        Dim prs As IPXV_PageRectSelection = CType(docSel, IPXV_AnnotSelection)
        Dim selectionRect As PXC_Rect = prs.PageRect

        SavePageRectAsAnImage(Me.docStampPreview.Inst, srcPage, selectionRect)
Really sorry to bother you with something so simple.

Thanks

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

Re: Get image of annotation selection

Post by Sasha - Tracker Dev Team »

Hello Simon,

I don't know if you need to add the annotations to the selection or you just want to calculate the bounding rectangle of all of the annotations. I have shown the sample on how to create and add the annotations to the selection for the createStampFromSel feature. If you already have the selection (for example the user selects the annotations) you should just obtain it from the ActiveSel.
Then, when you have the IPXV_AnnotSelection, you should get each annotation from it (or just get the needed annots from the page if you don't need the selection at all), and then get each annotation's bounding rectangle https://sdkhelp.pdf-xchange.com/vi ... ation_Rect. Then by having all of the rectangles, you can manually create the bounding rectangle for all of the annotations.
To summarize: there is no ready method for this and the overall bounding rectangle should be created from the bounding rectangles of each separate annotation.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Post Reply