C# Microsoft.Office.Interop.Word calculate text height - c#

I’m writing an application that outputs a word file containing text an images. The blocks of text are of variable length. In some cases I want to know how much vertical space a block of text wil use in the document, given a certain width, so I can insert a page break to make it look nicer.
I’m using Microsoft.Office.Interop.Word
So the question is: Is it possible to calculate the resulting height given a certain width and a certain text style.
Cheers!

Related

Creating an image in C# then laying out on page

I need to take a spreadsheet and convert it into a price tag. I've done that part but I'm not to sure how to go about making an image that contains both the price an item name (This is all stored in a list.) Then lay it out on a 8 1/2 x 11 piece of paper.
I read this question here but its using the size of the text, which may vary depending on the name of the item. The TextBox (Or whatever is holding the text) needs to be the same size but have the text scale based on its size.
Take a look at these docs, in particular their example pd_PrintPage function. This takes a PrintPageEventArgs which contains a Graphics object that you can use to actually render your tag.
In particular, to leverage your linked question, there is a DrawImage(Image, Int32, Int32) method that renders the given image at a co-ordinate.
To handle scaling your text, you just need to compare how big your text would be with one font to how big you want it - work out the ratio of width/height, then scale the font you use to render so that it uses the smallest of those ratios. There's a good answer here which shows how to do that.
So:
Handle a print event
Find the right font size
Create your image
Print with your graphics object
I would do a mockup of the resulting code, but I don't have access to a C# IDE at the moment.

PDF doesn't wrap text lines automatically & respect line position

I'm trying to generate a PDF via code because not all actual PDF .NET libraries support the new Windows Runtime for Windows/Windows Phone 8.1.
The PDF is saved correctly, with only a bug of stream position count that I can fix easily, but, as you can see, the text doesn't wrap if line is too long.
I tried with PDF NewLine char (\n), but C# automatically convert it in the input string
Also, I can't understand the position of lines or objects to put into the document, because I follow this guide online that talk about a reversing axis disposition (x for height and y for width), but seems I didn't catch the right methodology (I put in my code a constant left position, at 40, and a variable top descreasing value (from 600, I'm not manage now the multipage if the value is less than 0).
This is the code of PDF generated:
http://pastebin.com/ZkZmbJdM
(Sorry if I use Pastebin, but using this editor Code function the code seems to be unformatted for special characters used for it)
Where am I doing wrong?
PDF is a graphical format trying to make you think it's a document format. But nope, it's just like drawing with GDI+ for instance. This is the reason why it can achieve the same rendered output across many platforms/programs/etc as opposed to text flow formats like doc/docx. And also, this is why it can really render anything.
So, as opposed to document formats, it is the responsibility of the program that generates the PDF to get the layout right. Think of it just as if you'd draw with GDI+.
In documents like docx or html, it's the rendering program that has to do the layout work. With such document, you just write text and the viewer will take care of laying it out.
Your PDF library certainly has the necessary code to measure the text length. Maybe even it has some code to provide some layout capabilities. You'll have to use these functions to do the layout.

Getting the number of lines spanned by wrapped text

I would like to count the number of lines a particular string will occupy when wrapped in a container with a fixed width, and using a given font...
My original problem was getting the height of a row in excel with wrapped text. I've given up on a straightforward solution so I would like to use an algorithm for determining the number of lines occupied (and hence the height of the row).
I realize I can develop my own algorithm making use of Graphics.MeasureString, but I was wondering if there is an already developed clean solution that won't make me go through this mess.

Measure the height of DrawHTMLText before drawing on document with Debenu Quick PDF Lib

I am using latest version of Debenu Quick PDF Library.
Is it possible to calculate the height of DrawHTMLText before drawing it on document?
I need it because, I want my application to decide where (x,y coordinates) to draw DrawHTMLText according to its dimensions.
For example if it exceeds document border from the bottom side I want it to pull it up to make it fit.
Thank you.
user3253797,
You can use the GetHTMLTextHeight function to determine the height before calling DrawHTMLText.
http://www.debenu.com/docs/pdf_library_reference/GetHTMLTextHeight.php
Note : DrawHTMLText will return any overflow text as a string that will not fit into the specified area. GetHTMLTextHeight should in theory return the text height for the text that can fit inside the box. If the text is too long to fit inside the one box then it sounds like you will need to modify the x,y positions and possibly the HTML text itself to make it all fit on one page.
Good luck.
Andrew.

SVG: How to create self-adjusting container for text

This is actually really two related questions. I would like to create an image of a page from a notepad with some variable text on it in SVG.
Does SVG provide any native text-wrapping capability or do I somehow need to calculate horizontal width of words and break the lines of text myself? How might I do this?
Does SVG provide any native way to wrap a container around text automatically? Or do I need to calculate the vertical height of a block of text and set the height of the container myself? How might I do this?
Edit: if it's relevant, I will be constructing the SVG string in C#.
There's no text wrapping capability in SVG 1.1 although it's being looked at for the upcoming SVG 2.0 specification. The SVG DOM allows you to get the width of text via getComputedTextLength
You can calculate the bounding box of a block of text by calling getBBox

Categories