In my software I make 2 PDF files from 1 input file using iTextSharp. I'd like to convert these files into 2 different PNG images using GS, but something strange happens. I use this code for the conversion:
GhostscriptRasterizer rasterizer = new GhostscriptRasterizer();
rasterizer.Open(newFilePath1, gsInfo, false);
Image image = rasterizer.GetPage(300, 300, 1);
image.Save(subDirPath + serCod + "_S1.png");
rasterizer.Close();
rasterizer.Open(newFilePath2, gsInfo, false);
image = rasterizer.GetPage(300, 300, 1);
image.Save(subDirPath + serCod + "_S2.png");
rasterizer.Close();
When I save the first image it shows like a blank page and the file name is the same of newFilePath1 without .pdf but with .png.
When I look at the second image with the same file name as newFilePath2 with .png instead of .pdf, it has the image of the newFilePath1 pdf file.
How can I solve this problem?
I'd suggest you try the same operation using Ghostscript from the command line (instead of through Ghostscript.NET). If you get the same result then you can open a bug report at bugs.ghostscript.com and someone can look at it (remember to include the PDF file(s) and command lines).
Otherwise you'll have to contact jhabjan (the author of Ghostscript.NET) and have him investigate it.
Related
In my code to create a PDF an Image is being put on the PDF page as logo. But after Setup this image path is obviously not working. Looking for a way around this problem. Below is the current code.
System.Drawing.Image pimage = System.Drawing.Image.FromFile("C:\\Users\\Lenovo\\Desktop\\dev\\C# .net\\Aadi Paw Plethysmometer\\Aadi Paw Plethysmometer\\bin\\Debug\\spacecraft.jpg");
iTextSharp.text.Image ItextImage = iTextSharp.text.Image.GetInstance(pimage, System.Drawing.Imaging.ImageFormat.Jpeg);
ItextImage.ScalePercent(10f);
//ItextImage.SetAbsolutePosition(doc.Right, doc.Top );
ItextImage.Alignment = Element.ALIGN_RIGHT;
doc.Add(ItextImage);
A file path is not needed if the image is kept in debug. Just the name of the file followed by .filetype is sufficient. Hence it works even after SetUp file is created.
I need to insert an enhanced meta file image (.emf) to my worksheet. The image has to scale without losing resolution that's why I'm using emf format.
I've tried inserting the image from a memory stream and reading from a file, but somewhere during the process the emf is converted to jpg and no longer scales as the spreadhseet is viewed/printed in different sizes.
Note, when I use Interop.Excel the image inserts as an emf file and scales appropriately. It's when I use EPPlus that I have the problem. I really want to get away from Interop.Excel thought, that's why I'm using EPPlus.
// This works as expected, but I don't want to use the interop lib.
using Excel = Microsoft.Office.Interop.Excel;
myXlWorkBook.ActiveSheet.shapes.AddPicture(emfImageFileName, ....);
// This does not. The picture appears in the worksheet but does not scale.
using OfficeOpenXml;
using OfficeOpenXml.Drawing;
ExcelPicture pic = myXlWorkSheet.Drawings.AddPicture("Picture Name", new FileInfo(emfImageFileName));
// This doesn't work either.
Image img = Image.FromStream(myEmfImageStream);
ExcelPicture pic = myXlWorkSheet.Drawings.AddPicture("Picture Name", img);
My expected result should be an Excel worksheet with my image in it that scales properly when viewed/printed in various sizes.
Make sure you give it dimensions otherwise everything will be zero:
pic.From.Row = 0;
pic.From.Column = 0;
pic.To.Row = 30;
pic.To.Column = 23;
As for storing as a JPG, that sounds very strange, it should not randomly convert. You can confirm if it is or not by opening the XLSX file in 7-zip or rename to .ZIP and open. You should see the file stored in the media folder.
Also, watch for spaces in the emf file name as I have seen that trip up EPPlus.
UPDATE
For anyone who might come across this, it is a bug with Epplus. I have submitted a PR"
https://github.com/JanKallman/EPPlus/issues/496
In my App i generate an PDF-File with PDFSharp.Xamarin which I got from this site:
https://github.com/roceh/PdfSharp.Xamarin
Everything is working fine.
In my PDF-Document I have many Images, which are compressed.
But the file size of my PDF-Document is too large.
Is there a possibility to compress my PDF-Document before saving it?
How can I work with the PdfSharp.SharpZipLib.Zip Namespace to deflate the file size?
UPDATE:
Here is my Code:
document = new PdfDocument();
document.Info.Title = nameDok.Replace(" ", "");
document.Info.Author = "---";
document.Info.CreationDate = DateTime.Now;
document.Info.Subject = nameDok.Replace(" ", "");
//That is how i add Images:
XImage image = XImage.FromStream(lstr);
gfx.DrawImage(image, 465, YPrev - 2, newimagewf, newimagehf);
document.CustomValues.CompressionMode = PdfCustomValueCompressionMode.Compressed;
document.Options.FlateEncodeMode = PdfFlateEncodeMode.BestCompression;
document.Save(speicherPfad);
Thanks for everyone.
I only know the original PDFsharp, not the Xamarin port: images are deflated automatically using SharpZipLib.
Make sure to use appropriate source images (e.g. JPEG or PNG, depending on the image).
On the project start page they write:
"Currently all images created via XGraphics are converted to jpegs with 70% quality."
This could mean that images are re-compressed, maybe leading to larger files than before.
Take one JPEG file, convert it to PDF, and check the size of the image (in bytes) in the PDF file.
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
I would like to display images in excel file which is generated out of my c# code. Currently I am generating the xls file using the following code:
StringBuilder sb = new StringBuilder();
string imageInitialPath = Request.UrlReferrer.ToString().Substring(0, Request.UrlReferrer.ToString().LastIndexOf('/'));
sb.Append("<table border=1><tr><td colspan=3 rowspan=5 style=text-align: left;><img src='" + imageInitialPath + "/images/abc.JPG'/></td></tr></table>");
Here I am giving the path of an image from some folder. The image is gone from excel when deleted from that folder. Is there a way to keep the image in excel without actually depending on source?
Thanks
The image in the resulting excel file may still be referenced via the link, since the source is html.
I would suggest creating an actual Excel file format using this free library: http://epplus.codeplex.com/