Why MigraDoc gives an "image not found" with this fileless image? - c#

I have the "image.png" fileless image included in my WPF C# project as "embedded resourse". The full name of such image is "myapplication.image.png".
I am using such image in a document generated via MigraDoc. However, the document generated contains all the contenent that I planified, but a gray square within wrote "image not found" instead of image "image.png".
In order to use "image.png" in my document via MigraDoc, I added the file "image.png" as embedded resource to my project. Therefore, I followed this sample to include this image in the document.
My resulting code looks like the following:
byte[] imageStream = LoadImage("myapplication.image.png");
string imageFilename = MigraDocFilenameFromByteArray(imageStream);
Image image = para.AddImage(imageFilename);
Where "LoadImage" and "MigraDocFilenameFromByteArray" methods are coded as in the sample.
What am I missing?
Would someone provide a pointer, please?

If using NuGet, please note that you have to check 'Include prerelease' in order for MigraDoc v1.50.x to show up in the list of packages. Note that this is the 'Version', not the 'Runtime Version' number (right-click your MigraDoc reference and check properties). The most recent stable release is only v1.32.x.

As suggested by #User241.007, the issue was using 1.32 and nor 1.50 or later. Hence, everything is working now that I removed 1.32 and installed 1.50 via package manager.

Related

How to create a new PDF as new without changing the metadata?

I have the original PDF file created by Apache FOP 1.0 with basic metadata added by Apache (producer, date of creation). I was editing the file and additional metadata appeared that I don't want to have (modify date and other). Is it possible to create a new file based on a file edited in such a way that there are no editing remains?
<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.6-c016 91.163616, 2018/10/29-16:58:49 ">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about=""
xmlns:pdf="http://ns.adobe.com/pdf/1.3/"
xmlns:xmp="http://ns.adobe.com/xap/1.0/"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<pdf:PDFVersion>1.4</pdf:PDFVersion>
<pdf:Producer>Apache FOP Version 1.0</pdf:Producer>
<xmp:CreateDate>2019-08-20T11:09:15+02:00</xmp:CreateDate>
<xmp:CreatorTool>Apache FOP Version 1.0</xmp:CreatorTool>
<xmp:MetadataDate>2019-08-20T11:09:15+02:00</xmp:MetadataDate>
<dc:date>
<rdf:Seq>
<rdf:li>2019-08-20T11:09:15+02:00</rdf:li>
</rdf:Seq>
</dc:date>
</rdf:Description>
</rdf:RDF>
</x:xmpmeta>
<?xpacket end="w"?>
In addition to metadata, there is also the possibility of restoring the original version by removing the added lines in notepad ++ (which I do not want), i tried to replace the streams, but I fail (editing and new metadata are added to the file, they do not change the old text (incremental update?)). I would like the edited file to look like the original one made in Apache FOP 1.0. I tried to create XSL-FO with Word2FO.xsl, but I could not create a file with the same appearance as the original.
I can use a window application (Windows), console, website or something for C #, everything I've found so far either works badly or adds souvenirs after myself (watermark, change of producer, additional incremental update etc.).
Our DynamicPDF Merger product allows you to merge the PDF without changing its contents. Also there are options to suppress the XMP metadata or set the producer as needed.
Below is the C# code to merge a PDF, remove the XMP metadata (using the MergeOptions class) and setting the producer.
MergeOptions options = new MergeOptions();
options.XmpMetadata = false;
MergeDocument document = new MergeDocument(#"Source PDF file path", options);
document.Producer = "My producer";
document.Draw(#"path to save the output PDF");
You can refer to the documentation on the MergeOptions class here: http://docs.dynamicpdf.com/NET_Help_Library_19_08/DynamicPDF~ceTe.DynamicPDF.Merger.MergeOptions_members.html
A fully functional evaluation edition of the DynamicPDF Merger product is available on Nuget (Package ID: ceTe.DynamicPDF.CoreSuite.NET):
https://www.nuget.org/packages/ceTe.DynamicPDF.CoreSuite.NET/
More information and the option to download it from our site can be found here:
https://www.dynamicpdf.com/Merge-PDF-.NET.aspx

C# MVC generate PDF from View with NReco.PdfGenerator

I'm currently working on a project where I need to create a "dashboard" which can be exported as pdf. I wanted to use Rotativa but as our application uses .NET framework 4.0 it's not possible. So I found the NReco PdfGenerator.
Now that's the code how I create the PDF result:
var ViewAsString = RenderViewAsString("~/Views/QMetrics/StandardDashboard.cshtml", viewModel);
var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
htmlToPdf.PageWidth = 1600;
htmlToPdf.PageHeight = 900;
var pdfBytes = htmlToPdf.GeneratePdf(ViewAsString);
FileResult FileResult = new FileContentResult(pdfBytes, "application/pdf");
FileResult.FileDownloadName = "Dashboard-" + viewModel.ProjectName + "-" +
DateTime.Now.ToString() + "-.pdf";
return FileResult;
It successfully creates the PDF page with all the content that comes from the backend (Project information, and so on) but the page looks very ugly. On the original page I have 2 columns and on the PDF page it puts everything in one column. I tried a few different page sizes and I also changed the layout to be non-responsive but nothing has changed.
My first suggesstion was that the referenced CSS and JS files are not included when the PDF get's created, so I copied all the stuff that comes from external files (bootstrap, Chart.js) and pasted it directly in the .cshtml file. But nothing changed at all. My Chart is not rendering/loading and the missing CSS stuff is still not there.
On the NReco PDFGenerator website they say that it supports complex CSS code and also javascript code so I don't really understand why this is not working.
Has anyone here experiences with NReco or can someone recommend something else that works for .NET 4.0?
NReco PdfGenerator internally uses wkhtmltopdf tool, so you can check it and its options.
Regarding 2 columns: if you don't use flex/grid layout everything should work fine. Possibly you need to disable wkhtmltopdf smart shrinking logic (enabled by default) and define web page 'window' size explicitely (with "--viewport-size 1600" option).
Regarding CSS and charts: you need to check that CSS files could be accessed by wkhtmltopdf, simplest way to do that is running wkhtmltopdf.exe from the command line and check console log output (or, handle PdfGenerator's "LogReceived" event in C#). For Chart.js ensure that chart container div has explicit width (not in %), and that there are no js errors (you can get them in console by specifying "--debug-javascript" option). If your js code uses 'bind' method you have to include polyfill as WebKit engine version used in wkhtmltopdf doesn't support 'bind'.

convert a png file to a pcx file using c#

I'm trying to convert a .png file to a .pcx file. The scenario is the following:
I'm using a TSC TTP-343C label printer. On the labels I have to print images. TSC provides a library documentation for developers. Since I can only print images on those labels using pcx files I have to convert all the images to pcx images. Any other format or even incorrect pcx format (e.g. if the user just renamed the file ending) will not be printed on the label.
I've seen this post linking to the Magick library. In this post, the OP is trying to convert a bmp file to a pcx file which is not exactly what I try to achieve. I looked at the Magick documentation about converting images. I tried to convert the images like:
using (MagickImage img = new MagickImage(png)) // png is a string containing the path of the .png file
{
img.Format = MagickFormat.Pcx;
img.Write(pcx); // pcx is a string containing the path of the new .pcx file
}
Unfortunately this is not saving the image correctly. The label printer still cannot print the image on the label. I tried printing a correct pcx file and this worked fine. So I guess the only reason why it's still not working is that the converted file is not a real pcx file.
Is there a way to do such a conversion? If yes, how can I achieve that? My application is a windows forms application, written in C# using .NET framework 4.5.2.
EDIT:
Here you can see an example how to print a label with a pcx file:
TSC.openport(sPrinterName);
TSC.setup("100", "100", "4", "8", "1", "3.42", "0");
TSC.clearbuffer();
TSC.downloadpcx(#"\\PathToPcxFile\test.pcx", "test.pcx");
TSC.sendcommand("PUTPCX 35," + y + ",\"test.pcx\"");
TSC.printlabel("1", "1");
TSC.closeport();
This code works fine on real pcx files. The methods of the TSC library can you find here.
downloadpcx(a,b)
Description: Download mono PCX graphic files to the printer Parameter:
a: string; file name (including file retrieval
path)
b: string, names of files that are to be downloaded in the
printer memory (Please use capital letters)
Source: http://www.tscprinters.com/cms/upload/download_en/DLL_instruction.pdf
EDIT II:
A pcx file which is working (created using photoshop) looks like this (if it helps you):
PCX files are (at best) palette-based.
So to create a valid pcx output you need to add this one line:
using (MagickImage image = new MagickImage(sourcePng))
{
image.Format = MagickFormat.Pcx;
image.ColorType = ColorType.Palette; // <----
image.Write(targetPcx);
}
Your image as pcx file

How to get path of Properties.Resources.Image in .NET

I included an image as a resource following this post:
How to create and use resources in .NET
I am using PDFSharp library to create a PDF. The method to draw an image, requires the path of the image. How do I get the path of Properties.Resources.Image?
Or is there another way to do this?
The Properties.Resources.Image is in-memory resource.
You can save Image to temp file and the get the path.
var path = Path.GetTempPath();
Properties.Resources.logo.Save(path);
Above uses Bitmap.Save
You can actually create an image, without saving it, using XImage.FromGdiPlusImage():
var image = XImage.FromGdiPlusImage(Properties.Resources.logo);
As of PDFsharp/MigraDoc 1.50 beta 2 and newer you no longer need a path when using MigraDoc. It was already mentioned that PDFsharp does not need a filename, as images can be read from e.g. streams.
MigraDoc still requires a string. You encode the image data as a string (BASE64 format) and pass that string as a filename.
See also:
http://pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx

Source attribute of Silverlight MultiScaleImage

I learned we should assgin a .bin file to the Source attribute of Siverlight MultiScaleImage. For example, I learned from here (refers to code sample in section Anonymous Methods for Events),
http://www.soulsolutions.com.au/Blog/tabid/73/EntryId/410/Silverlight-Deep-Zoom-Sample-Code-Part-2.aspx
But I have tried using export function of Deep Zoom Composor will never generate .bin file. Here are my screen snapshots. Any ideas what is wrong?
(I am using VSTS 2008 + .Net 3.5 + C#.)
The MultiScaleImage control's Source property can be set in a couple different ways. If you're setting it in XAML, you can point to the dzc_output.xml file (located in the GeneratedImages folder) directly like so:
<MultiScaleImage x:Name="Foo" Source="/GeneratedImages/dzc_output.xml" />
You can also set it in code, you you need to take the extra step of assigning it as a DeepZoomImageTileSource:
Foo.Source = new DeepZoomImageTileSource(
new Uri("/GeneratedImages/dzc_output.xml", UriKind.Relative));
Hope that helps!
Inside the GeneratedImages folder is an xml file called dzc_output.xml that should be the new end-point for a MultiScaleImage.
With the new versions of DeepZoomComposer, the bin file format has been replaced with this new xml file.

Categories

Resources