How (if possible to do so) can I create an SVG image, using C#? I wish to take one SVG, overlay another SVG on it, and save it as a third SVG image.
In my specific case, I allow a module of my software to provide an icon for a folder. I want to overlay a warning or error icon on top of the folder icon when there is an error in the contained data.
I recommend using SVG Rendering Engine to do this. Create a third document, and put the two given svgs one after the other into it. (Nesting svg documents in each other is completely valid):
var icon = SvgDocument.Open(...);
var overlayIcon = SvgDocument.Open(...);
var overlayed = new SvgDocument();
overlayed.Children.Add(icon);
overlayed.Children.Add(overlayIcon);
overlayed.Write(...); // saving
Note: check the overlaying icon's size, viewbox, etc., you may have to change these through SvgDocument.Width, SvgDocument.Height, SvgDocument.ViewBox to get proper result.
SVG is a very simple text format, even simpler than HTML. Overlaying SVGs is a work for XML library. You can just open it and append every node from one file to another file.
Just try taking these two text files:
http://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg
http://upload.wikimedia.org/wikipedia/commons/6/6b/Bitmap_VS_SVG.svg
Open the first one, copy contents of the the <svg>...</svg>, paste at the end of the other file (just before the ending </svg> tag). You can check in some editor or validator that the result is just a perfectly fine SVG file.
Use System.Xml or System.Xml.Linq for handling it from your C# code. Additionally you can add new nodes, edit attributes like width, height, color, etc... if you want:
<svg width="637" height="637">
<path style="fill:white" d="... "/>
...
</svg>
Have fun.
Related
I'm looking for a way to get the result of an overlay between two pdf documents.
We have a document with a single page and only a header and another document with multiple pages and full content (header and body). We're looking for way to generate an overlay pdf between those documents, so that the resulting document with the content gets its header overwritten in each page with the single page document header. Basically like this:
Is there a opensource c# library, which can handle this and not convert the text to a picture.
I looked at PdfSharp and docnet, but couldn't figure it out with either of them.
So far we are using pdfbox, but we'd like to get rid of the java dependency.
Simple solution with PDFsharp: draw a white rectangle that hides the original header, then draw the new header on top of the rectangle.
Drawback: The old header is still contained in the document.
I'm trying to split and SVG into many different SVG files each one containing one element original file.
My main problem is not the slipt in itself since for each element I have to split, I get the svg element, remove everything else from the original svg, add back the element into the main layer and then save the file with the name I want. This works fine.
The real problem is that each file I create has the view centered and dimensioned as in the original svg file. So usually it's misplaced and wide (since in the original file it would contain all elements that now are split into different files).
So I need to resize the canvas to the element that remains in the file.
This very function is done by inkscape with the command
inkscape --verb=FitCanvasToDrawing --verb=FileSave --verb=FileClose
But unfortunally this verb doesn't work in --without-gui mode, so if I call it in the code I see thousands of inkscape instances opening, fitting the canvas and then save and close the windows. It works but it's not good for a batch application (they have this "bug" since quite some years).
So I resorted to use SVG engine (https://github.com/vvvv/SVG) but it has a bug that prevents the right calculation of the element bounds (https://github.com/vvvv/SVG/issues/331), so I cannot change the viewbox or the svg element to the right values.
Any suggestion on how to calculate the right position and size? Or any other library (any language that can run in a batch) that works for that?
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'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.
Given a PDF file with color and black & white pages, is there any way with C# to find out among the given pages which are color and which are black & white?
My recommendation is to render each page to an image and then check each pixel for RGB values not equal to each other. If R=G=B for each pixel then it's a grayscale image.
You could then perform actions (such as extracting a page to another document or printing the page) on the pages based on whether they are color pages or black and white pages, etc.
This can be achieved by using my companies PDF developer library, Quick PDF, or potentially by one of the open source PDF libraries that Kenneth suggested.
Short of parsing all the postscript content, probably not. There's no flag on a PDF page that says it is or is not b&w or color. So you'd have to check the color of every element placed on the page to figure out if it was color or not. I'm not sure what libraries exist for reading PDFs on C# but you would need one that will read all the elements.
Similarly, any images you have on the page would need to be checked for color and that is not simple. Color image formats can hold b&w images.
Check out:
PDF-Analyser
I use his tools for text extraction and pdf analysis. Very inexpensive, royalty free, and work well. I think GetPDFColourStyle as part of the PDFLayoutPlus library should do the trick.
Convert each page into bitmap image and then look through each pixel in the image you would be able to catch colours and then differentiate color pages.
refer this Post for more details.
Note: If your are detecting this colors for printing sake, then you have to detect CMYK colors not RGB, CMYK is the printer standard color mode, and RGB is a display color mode.
There is a solution.
You can parse each page content bytes and look for color operators like 'rg, RG, k, K, sc, SC, scn, SCN' and read out all the color values and color spaces defined in each page.
Take a look at this example: http://habjan.blogspot.com/2013/09/proof-of-concept-converting-pdf-files.html
It implements / parses all color operators and I think it will be a good start point and reference to help you code what you need.