Calculate paragraph size in centimeters.. Is it possible? RRS feed

  • Question

  • Hi,
    Is there anyway on how can I calculate the paragraph size in centimeters?
    My idea is to build a document, by filling blank spaces left on a page that has a big paragraph.. If I knew the paragraph size, I could organize the document to save pages, joining small paragraphs on the same page.
    Nowadays I use the RTF with a RichTextBox, to do this.. But I want to migrate to the Open XML Format..

    Thanks a lot,

    Bruno Oliveira
    Thursday, February 18, 2010 12:31 PM

All replies

  • I dont know if the open XML sdk could do this, but if you were to use iText (pdf generation) there are calls you can make that will tell you the height of a group of characters in a given font and size. Then there is a property called 'leading' which is the space between two different lines (think line spacing in word), im not exactly sure where to find this in a word document, i might have to get back to you. But add the leading and the line height, iterate through each line and you should have the height of a paragraph.

    But it is definitely possible.
    Thursday, February 18, 2010 5:37 PM
  • Hi Adam,
    Thanks for your answer.. But in this particular case, the paragraphs are questions for exams.. And they may or may not have pictures within.. :-(
    So, if the paragraph has an image this approach would not work.. There is another way to find it, with the "Graphics.MeasureString" ( but is the same case.. is just for strings..

    thanks again,
    Thursday, February 18, 2010 5:44 PM
  • I think you would still be ok. since word stores each line separately, depending on the way you have the text wrap setup for the image you could just ignore the image and calculate the height of each line. So if you have text wrap on, you would just calculate the height of each line and take the max height from either the image or the number you calculated. If the image is inline, then for that particular line you would take the line height (calculated from the text height) or the image height, whichever is more.

    Im not sure if what im suggesting is the best way to accomplish what you are trying to do. But it is certainly possible, we do all of these calculations in the software we make, but we have to handle every type of object you can put in a word document, so our algorithm is extremely complex. whereas in your case it might be a little simpler. 

    Maybe someone else has a better way to solve this problem.

    Thursday, February 18, 2010 8:20 PM
  • Hi Adam..
    Thanks again for your answer..
    But I still not comfortable with this method.. It would not give me an accurate measure..  I really need to measure the paragraph just like MS Word prints it..
    With this method, I cannot measure tables for example.. :-(

    Friday, February 19, 2010 4:02 PM
  • Can you explain the problem you are trying to solve in more detail. There might be a way to solve it without going through all the pain of calculating the height for everything.
    Friday, February 19, 2010 7:29 PM
  • Hi Adam..

    Well, lets say that I have some questions for an exam. So I need to organize those questions in a way that it will print the exam with the fewest pages possible..
    For example, I have 4 questions (2 small and 2 big with images and tables) and if I knew their size in centimeters, I could organize then to occupies just 2 pages instead of 4 pages..
    That´s the ideia..

    Nowadays, I'm already doing that using RTF and the rich text box, to get the caret position and converting the twips to centimeters..

    Now, I need to figure out, how can I do that with DOCX.. ;-)

    Thanks for your help,

    Monday, February 22, 2010 4:47 PM
  • Hi bhmo,

    Thanks for your question.

    The Open XML SDK is a collection of classes that let you create and manipulate Open XML documents. It is NOT a replacement for the Office Object Model. It does NOT provide application behaviors such as layout (ex. pagination of WordprocessingML documents), formula calculation (which is very common in Excel) or recalculation functionality.

    Your requirement of columns calculating the paragraph size is some kinds of layout application behavior, which may be affected by many factors such as font, size... and Open XML SDK doesn't support. You could try to find other technology to achieve this.



    Friday, February 26, 2010 8:39 AM
  • Hi Bruno,

    Did you find any suitable solution for your case ?

    We are searching for exactly the same issue.






    Thursday, December 1, 2011 2:36 PM