Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Jul 17, 2020 8:34 am

Hi, im trying to calculate distance from inserted image to top/bottom of image parrent page. Is it possible? Tryed pic.top property but its value is 0 (my pic is like in middle of the page).

Thanks!

giriss88
 
Posts: 4
Joined: Fri Jul 17, 2020 8:23 am

Fri Jul 17, 2020 9:02 am

Hello,

Thanks for your inquiry.
Please refer to the following code to calculate distance from image to top/bottom of page. Kindly note that the default unit of Spire.PDF is point. If there is any other question, please feel free to contact us.
Code: Select all
    PdfDocument pdf = new PdfDocument();
    pdf.LoadFromFile("test.pdf");

    foreach (PdfPageBase page in pdf.Pages)
    {
        foreach (PdfImageInfo pif in page.ImagesInfo)
        {
            RectangleF bounds = pif.Bounds;
            //float topDistance = bounds.Top;
            //float bottomDistance = page.Size.Height - bounds.Bottom;
            float topDistance = bounds.Y;
            float bottomDistance = page.Size.Height - bounds.Y - bounds.Height;

            ////Convert the unit
            //PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            //float pixelTop = unitCvtr.ConvertUnits(topDistance, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel);
            //float pixelBottom = unitCvtr.ConvertUnits(bottomDistance, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel);
        }
    }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Jul 17, 2020 10:09 am

Thank You for quick response but it seems like response for Spire.PDF, Im using Spire.Doc (Office Word document).

Thanks!

giriss88
 
Posts: 4
Joined: Fri Jul 17, 2020 8:23 am

Fri Jul 17, 2020 10:36 am

Hello,

Apologize for my mistake.
Regarding your requirement, you can first use our Spire.Doc to convert Word document to PDF, then use the code I provided above to calculate distance from image to top/bottom of page, as shown below. If there is any question, please provide your input file for further investigation.
Code: Select all
    Document doc = new Document();
    doc.LoadFromFile("test.docx");
    MemoryStream ms = new MemoryStream();
    doc.SaveToStream(ms,Spire.Doc.FileFormat.PDF);

    PdfDocument pdf = new PdfDocument();
    pdf.LoadFromStream(ms);

    foreach (PdfPageBase page in pdf.Pages)
    {
        foreach (PdfImageInfo pif in page.ImagesInfo)
        {
            RectangleF bounds = pif.Bounds;
            //float topDistance = bounds.Top;
            //float bottomDistance = page.Size.Height - bounds.Bottom;
            float topDistance = bounds.Y;
            float bottomDistance = page.Size.Height - bounds.Y - bounds.Height;

            ////Convert the unit
            //PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            //float pixelTop = unitCvtr.ConvertUnits(topDistance, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel);
            //float pixelBottom = unitCvtr.ConvertUnits(bottomDistance, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel);
        }
    }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Jul 17, 2020 11:08 am

Thank you, so there is no other option without converting to pdf first? I want to save final file to docx, so I dont need pdf. But if there is not other way, I will have to do it as you say.

Thank you :wink:

giriss88
 
Posts: 4
Joined: Fri Jul 17, 2020 8:23 am

Fri Jul 17, 2020 12:43 pm

Hello,

Thanks for your response.
Kindly note that Word document is flow layout. I directly inserted a picture to Word file in Microsoft Word and set its position to center, and then parsed and viewed its internal data, and found that the value of the top distance and bottom distance of the picture is 0, as shown below. Thus, for this kind of picture, converting the Word file to PDF first and then calculating the distance may be the best solution for you.
internal_data.png


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Jul 17, 2020 1:48 pm

Thans for information. So there is no way to calculate position of paragraps or any other item in document? In Word.Interop Im able to read Shape.Top, its a pitty that this cant be done with Spire.Doc.

But thanks anyway, I will give it try with pdf.

giriss88
 
Posts: 4
Joined: Fri Jul 17, 2020 8:23 am

Mon Jul 20, 2020 2:24 am

Hello,

Thanks for your response and sorry for the late reply as weekend.
Maybe my explanation is not detailed enough. For image and shape, if the position layout type is "Alignment", such as "set its position to center" I mentioned above, it is not possible to get the specific value of the image/shape position as you expect using our Spire.Doc. But if the position layout type is "Absolution position" or "Relative position" as shown in the attached picture, you can refer to the following code to calculate the distance.
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile("sample.docx");
            Section section = doc.Sections[0];
            foreach (Paragraph para in section.Paragraphs)
            {
                foreach (DocumentObject obj in para.ChildObjects)
                {
                    if (obj is ShapeObject)
                    {
                        ShapeObject shape = obj as ShapeObject;
                        double topDistance = shape.VerticalPosition;
                        double bottomDistance = section.PageSetup.PageSize.Height - topDistance - shape.Height;
                    }

                    if (obj is DocPicture)
                    {
                        DocPicture picture = obj as DocPicture;
                        double topDistance = picture.VerticalPosition;
                        double bottomDistance = section.PageSetup.PageSize.Height - topDistance - picture.Height;
                    }
                }
            }

Besides, since Word document is flow layout and does not contain the layout information of paragraphs, so its unable to calculate the position of paragraph. Hope you can understand.
If there is any question, please provide your test file so that we can investigate accordingly and make an accurate response.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc