Output image with build - c#

I am using MigraDoc, and would like to insert an image in a document. The simplest way to do this, is to provide a path for the image file (rather than having it in the resources). So what I would like to do is, to include an image as a file, alongside the other files in the build. Then MigraDoc can use the image based on relative path.
But I have no clue how to do this or if it is even possible. So can I make my project include an image file, outside the resources.
P.S. I know that there is a work around, where you include the image in the resources, save it in a temporary folder before MigraDoc has to use it. It just seems like a simpler design, if the image is always available.
P.P.S. There is a work around, to make MigraDoc use images from the project resources (see accepted answer).

As shown on the MigraDoc site you can use images you have as a byte[] to create PDF files. This can easily be used with images from resources (as shown on the site).
You just convert the image resource to a special string and pass that string where MigraDoc expects a filename.
static string MigraDocFilenameFromByteArray(byte[] image)
{
return "base64:" +
Convert.ToBase64String(image);
}
Link to official MigraDoc site:
http://www.pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx
And MigraDoc can also use images that are stored in the program folder with your assemblies. You can use the Assembly class to find out where your assembly is stored and search that folder for images that should be there. This has nothing to do with MigraDoc, just some basic .NET features.

Related

VSTO how add image from embedded resources

I try import image to Word file from embedded resources. But i don't know how do it.
For example:
Globals.ThisDocument.InlineShapes.AddPicture(Resources1.ImageLogo.ToString());
But it isn't work, becose need full path to file as string.
Globals.ThisDocument.InlineShapes.AddPicture(#"C:\tmp\ImageLogo.jpg");
How can i import (add) image from embedded resources to Word file?
Thanks a lot!
I tried insert as "stream file", but my knowledge is not enough, to solve this problem.
The InlineShapes.AddPicture method doesn't accept streams or any other .net types. You need to save the embedded file to the disk and then specify the file path to the method. The first parameter is the path and file name of the picture.

How can I check if a PDF file is using embedded fonts?

I have a folder where multiple clients upload multiple PDF files.
Some of them are using embedded fonts, some doesn't.
I've been working on a service that optimizes (in terms of file size) the PDF files in this folder.
Each user may be uploading around 400 files, weighing anywhere between 80K to 10M, and my task is to optimize all of them to the smallest possible file size with minimal quality lose.
the PDF Library is doing a great job with it. My only problem is that I can't remove all embedded fonts from all files, since some of the files might use these fonts and the result would be a file that I can't use.
So my questions are:
How can I detect what files use and what files doesn't use embedded fonts?
When optimizing the files that use embedded fonts, How can I remove only the unused fonts?
what I want to achieve is to remove all embedded fonts from most of the files, but keep the embedded fonts in the files where I actually need them. I understand that it depends on the fonts I have on my system (these files should stay on a single system so portability is not that important to me), so I try to find a way to identify, before optimizing, what files will look OK without embedded fonts, and what files I need to keep the embedded fonts.
APDFL has a PDFontIsEmbedded() call. The DotNet interface's Font class has an Embedded property. Saving with the GarbageCollect SaveFlag should remove any unreferenced indirect objects, including fonts.
Note that Resource Dictionaries could potentially be shared by multiple pages so that fonts not used by one page might be used by another page that uses the same resource dictionary.
The Adobe PDF Library version 15 and up have a service that will optimize PDF files for you.
The Optimizer has a function to subset all embedded fonts. What that will do is create a subset of each font limited to only the glyphs of that font actually used by the document. The API is below.
void Datalogics::PDFL::PDFOptimizer::SetOption (OptimizerOption option, bool value)
void Datalogics::PDFL::PDFOptimizer::Optimize (Document document, string newPath)
This is the option that you need
SubsetAllEmbeddedFonts 

MigraDoc add image to table from stream

I've been using MigraDoc to generate PDFs and I've got the layout of everything working great except for adding in images. for the part of the program we're generating these PDFs for we're using a program that only saves their information as XML and from there am able to convert the XML into System.Drawing.Image objects. The problem I'm running into is somehow getting the System.Drawing.Image objects now into my PDFs.
MigraDoc was designed to work with filenames. So one option is saving the stream to a temporary file and then use the filename.
Or modify the MigraDoc source code to work with image objects - such a patch can be found on the PDFsharp forum.
BTW: The downvote is not from me. Maybe someone thinks that very little effort was used to post the question here.
Update (January 16, 2016): With version 1.50 (currently available as beta version only, but it is pretty stable) you can pass images in the filename: the filename will contain the image bytes using BASE64 encoding.
It's a bit of a hack, but should work for all scenarios.
Sample code here:
http://pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx
So MigraDoc still uses filenames only, but it is no longer necessary to save images in temporary files.
I don't know where this solution started working, but I usually solve this problem with simple method to convert stream into virtual file path.
internal static string LoadImageFromBytes(byte[] cData)
{
return $"base64:{Convert.ToBase64String(cData)}";
}
And then use it in AddImage() method:
row[0].AddImage(LoadImageFromBytes(byteArray))

Can i retrieve images and shapes from word file and display them in PictureBox control of C#?

I want to extract images and shapes from word file and display them in picturebox control of C#.Is it possible? and if its then how ? i am searching it for many days but didn't find any solution. So plz help me
If you're working with MS Word documents in Open Office XML format (e.g. .docx files) you could try reading them out of the file directly. The .docx file is just a zip file and the images are contained in a media directory inside them. This page has more information on the file format.1
Alternatively I guess you could try automating Word using COM, to open the file and then retrieve the image that way. This approach has the disadvantage of spawning an instance of Word.
Either method will involve more detailed steps than I've described here, but should be do-able.
You can actually do it without going in to the trouble of using OpenXML or word interop libs.
This is what you need to do.
Rename your MyDoc.docx file with MyDoc.zip.
Then unzip the contents. You will get a very specific folder structure.
Ex: _rels, docProps,word, [Content_Types].xml
Go into word folder
Inside the word folder you will see a folder called media. That's where your images are.
Now you should be able to read and load these images in to picture boxes.

Embed word document into another WITHOUT icon

How to embed a word document into another word document via OpenXML SDK, but showing content, not an icon of word? Such, as we do it manually in word: Insert object from file -> WITHOUT checking "Dispaly as icon"?
I've found this article, but it uses an icon. I've also tried to use OpenXML SDK Productivity Tool, but shows only generated binary data.
EDITED:
I use the following code:
DrawAspect = OleDrawAspectValues.Content
and then i add image part:
var imagePart = mainDocumentPart.AddNewPart<ImagePart>("image/x-emf", imagePartId);
GenerateImagePart(imagePart);
But my image part - is just an array of bytes of word's icon.
So, in this case happens the following: when i open generated document, it shows embedded document as an icon, but when i double click this embedded document, edit it and save changes, the embedded document is shown as a content, so maybe it's possible in some way to show this content without editing embedded document? Should i use instead of array of bytes of word's icon an array of bytes of doc's screenshot?
Not sure i described it clear, so please ask
I'm afraid what you are asking for is almost impossible.
The only difference as far as the word file is concerned between the icon and the embedded file, is the image.
When you don't use a icon Word pretty much just take a screenshot of the document you are embedding and inserts that in place of the Icon graphic.
I've uploaded an example I grabbed from a Word file I made. Found this little gem in the /media folder inside the .docx file.
So basicly, your only choice in resolving this if you can't live with the Icon is to somehow grab a picture of the word-file you want to embed and insert that instead of the Icon image.
How you'd go about that can't be pretty. First of all the open xml sdk contains no such functionality. I tried playing a bit around with office interop as well, but no luck.
I only see two possible ways to achieve this.
First one is via Interop. You'll need to install a "pretend printer" like the ones that print to PDF instead of sending it to a printer. This one however needs to print to an image format. The format of the file in the Media folder was .emf but I'm not positive thats a requirement.
Anyways, should the above somehow be possible you could embed that picture, pretty much using the example you link from Microsoft, and just change this size of the "icon" which now would be an image of the document.
Second possibility would be to open the word document as a process, set the document size to 72% (or whatever makes the document be the only one on screen on your desktop) and the grab a print screen and cut it down to just the document and the use that as your image for the embedding.
For the record, I don't recommend you do any of the above, but thoose are the only options I see.
Should someone have a better solution to this I'm all ears.
Finally, should you decide that you want to push on with this, I'll be happy to code up an example of option number 2 if you reply and tell me you'd like that.
Kaspar
There is a nice wrapper API (Document Builder 2.2) around open xml specially designed to merge documents, with flexibility of choosing the paragraphs to merge etc. You can download it from here.
Using this tool you can embed a paragraph of another word document or entire word document as per your requirement.
The documentation and screen casts on how to use it are here.
Hope this helps.

Categories