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
Related
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!
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.
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.
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.
I'm trying to replace a section of a PDF with different text. From research on all major PDF libraries for .NET, it seems this is complicated and not a trivial task. I think it may be easier to convert the PDF to an image, replace the text (always in the same place), then convert it back to a PDF (or leave it as an image if converting back isn't possible). Is it possible to extract an image from a PDF page with .NET?
If your text is in a known location, you can simply cover it with a rectangle filled with the background color, and then draw your text over top.
Note that the text will still be there, it simply won't be visible. Someone selecting text will still pick up the old stuff. If that's acceptable, it's quite trivial.
If the PDF was created from image, you can import it into Photoshop to edit it as an graphic. Or you can use screenshot program like "Snagit" to capture pdf page as image and use snagit's editor to erase old text and replace new one.
But this method may bring you problem is that the new added text may not the same font as text around it. Personally, I use pdf editor to replace text in pdf since the added text will be automatically fit with the original font and size.