Need Help with mapping word rectangle coordinates to generate pdf text widget using XRect

Hi,
Here is our problem scenario:
We have a word document that has a rectangular picture object.
We use Aspose.Words for .NET to read this shape from the word document , get its coordinates, save the coordinates for later and then delete the shape from the word document.
We convert this word document(without the rectangle objects) to ABCpdf document.
We then have to re-create text widgets in the pdf for the rectangle object in the exact position using XRect.class.
The text widgets when created are not placed in the correct coordinates in the pdf document. This is our problem.

Looks like the shape coordinates read using Aspose.Words for .NET is not mapping correctly to the XRect coordinates, hence the problem.

Research Details:
XRect coordinates are (left bottom right top) The origin of this space is at the bottom left of the document. Distances are measured up and right in points.

Sample code:
This is how we are capturing the word shape coordinates using Aspose.Words for .NET
info.Left = (float) shape.Left;
info.Bottom = (float)shape.Bottom;
info.Top = (float)shape.Top;
info.Right = (float)shape.Right;

             XRect(info.Left + " " + info.Bottom+ " " + info.Right + " " + info.Top);

That is not working.

Please let us know how we should be capturing the coordinates of the shape using Aspose.Words for .NET from the word document to place the text widget (rectangle) in the exact location in pdf document using xRect.

Looks like we may have to compute the coordinates manually using the page height, relative horizontal, vertical positions, width and height of the shape. Is that the only option we have? Please advise.

Will appreciate your immediate help with this issue. Thank you in advance
Best Regards,
-BP

@bprabu,

Thanks for your inquiry. The Aspose.Words.Layout namespace provides classes that allow to access information such as on what page and where on a page particular document elements are positioned, when the document is formatted into pages.

You can get the position of image using layout API. Please use LayoutCollector.GetEntity method to get an opaque position of the LayoutEnumerator which corresponds to the specified node. Please check the following code example. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");

LayoutCollector layoutCollector = new LayoutCollector(doc);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);

foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    var renderObject = layoutCollector.GetEntity(shape);
    layoutEnumerator.Current = renderObject;
    RectangleF location = layoutEnumerator.Rectangle;

    Console.WriteLine("Top : " + location.Y);
    Console.WriteLine("Left : " + location.X);

    Console.WriteLine("Right : " + location.X + shape.Width);
    Console.WriteLine("Bottom : " + location.Y + shape.Height);
}

Best Regards,
Tahir Manzoor

Hi Tahir,
Appreciate your prompt response, as always. That helps with computing the co-ordinates,including the page number. However, once I included the LayoutEnumerator, the shape is not getting removed from the word doc, after computing the shape co-ordinates. Without the enumerator, the shape is getting removed with Shape.Remove command. Researching that problem now. Let me know, if you have any ideas on why the shape will not remove from the word doc, with LayoutEnumerator. Here’s the method.

private SignaturePointInfo[] GatherSignatureTags(Aspose.Words.Document doc)
{
ArrayList list = new ArrayList();

        // Create and attach collector to the document before page layout is built.
        LayoutCollector layoutCollector = new LayoutCollector(doc);
       

        // This will build layout model and collect necessary information.
        doc.UpdatePageLayout();

        // Split nodes in the document into separate pages.
        DocumentPageSplitter splitter = new DocumentPageSplitter(layoutCollector);
        for (int page = 1; page <= doc.PageCount; page++)
        {
            Document pageDoc = new Document();
            pageDoc = splitter.GetDocumentOfPage(page);

         
            LayoutEnumerator layoutEnumerator = new LayoutEnumerator(pageDoc);

            // Build the FormField data of the signature tags in the document.
            NodeCollection shapes = pageDoc.GetChildNodes(NodeType.Shape, true);
            foreach (Shape shape in shapes)
            {
                // Only process eSigning shapes
                if (shape.AlternativeText == "BorrowerSignature" || shape.AlternativeText == "BorrowerInitials" ||
                    shape.AlternativeText == "CoborrowerSignature" || shape.AlternativeText == "CoborrowerInitials" ||
                    shape.AlternativeText == "OriginatorSignature" || shape.AlternativeText == "OriginatorInitials")
                {
                    // Collect the details
                    SignaturePointInfo info = new SignaturePointInfo();

                   
                    var renderObject = layoutCollector.GetEntity(shape);
                    layoutEnumerator.Current = renderObject;
                    RectangleF location = layoutEnumerator.Rectangle;
                    //Research RectangleF Bounds
                    info.ShapeID = shape.AlternativeText;
                    info.Page = page; 
                    info.PageHeight = (float)pageDoc.FirstSection.PageSetup.PageHeight;
                    info.Height = (float)shape.Height;
                    info.Width = (float)shape.Width;                                        
                    info.HorzPos= (float) location.X;
                    info.VertPos = (float)location.Y;
                                                             
                    // Add the shape info to our temp list
                    list.Add(info);
                    //Delete this shape so user doesn't see it when printing occurs
                    shape.Remove();

                }
            }
            if (page == 1)
            {
                finalDoc = pageDoc;

            }
            else
            {
                finalDoc.AppendDocument(pageDoc, ImportFormatMode.KeepSourceFormatting);
            }
            
        }

@bprabu,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

Best Regards,
Tahir Manzoor

Hi Tahir, Please find attached Solution DocToPdf. It loads a word document SigTest.doc, which has a signature object. Goal is to read the coordinates of the shape from the word document, remove the shape from the word document and save it as pdf. After removing the shape, only if I save the final document as doc(SigOutput.doc), I don’t see the shape, which is fine. However, if I save the finaldoc as pdf(SigOutput.pdf), I still see the shape in it, which is the problem. I don’t want to see the shape in the pdf document. How to solve this? Please help. Thank you. The enclosed zip file also has the SigTest.doc document and also the final documents SigTestOutput.doc and SigTestOutput.pdf
DocToPdf.zip (514.9 KB)

@bprabu,

Thanks for sharing the detail. Please call Document.UpdatePageLayout method before saving the document to PDF. This method is automatically invoked when you first convert a document to PDF, XPS, image or print it. However, if you modify the document after rendering and then attempt to render it again - Aspose.Words will not update the page layout automatically. In this case you should call UpdatePageLayout before rendering again.

Please check the following modified code.

public void Signature()
{
    Document doc = new Document(dataDir + "SigTest.doc");

    // Gather signature tag information and delete the tags
    this.SignaturePointList = this.GatherSignatureTags(doc);

    finalDoc.Save(dataDir + "\\SigTestOuput.doc");//This document does not have the shape, which is correct
    finalDoc.UpdatePageLayout();
    finalDoc.Save(dataDir + "\\17.6.pdf");//This document has the shape, which is incorrect. How to save it in pdf without the shape?

}

Best Regards,
Tahir Manzoor

That worked Tahir!!! It also fixed the other problem, I had, where the finaldoc was saving only the first page as pdf. Thanks much for all your help with this issue. Appreciate it!

Best Regards,
-BP