Prevent PDFsharp from saving an image file? - c#

I have prevented my program from saving images downloaded as files. (They are saved in Image variables instead). However, my application is slowed down significantly because the PDFsharp libraries save the image files anyway before drawing them to the PDF document. This is done deep within a hierarchy of calls by its functions.
Is there a simple fix to get around this?

JPEG images are simply copied into the PDF file, all other image formats have to be converted to the PDF format. AFAIK PDFsharp does not save the images to the local file system, however they are saved into a memory stream during the conversion.
There is a simple fix to get around this: write a better conversion and submit it to the PDFsharp team.

I think PDFSharp always need to save first. It cannot handle byte image that will used to import an image. I recently encountered it when I also coded and used pdfsharp. My process is to save an image then import to code in pdfsharp. I have a part that I need to delete all temporary images.

Related

IText7 - IText7 is Not Properly Drawing SVG Documents

Project Information : .NETCore v3.1 - IText7 v7.1.11
We have a PDF Document builder library created by IText7 nuget package, where we can dynamically building large PDF files for our customers.
Normally we are working with .jpg format but a new feature is added to our online side which is we have started to work with also interactive SVG files with hotspots integrated to our UI.
So, when our application builds a PDF Document it also needs to import those SVG files, we can use it by:
var image = SvgConverter.ConvertToImage(
new FileStream(imagePath, FileMode.Open, FileAccess.Read), pdfDocument);
The Original File (I can't upload a SVG so I am uploading as jpg but this is not important because I just want to show the line thickness):
Output is shown below, as you can see on the left side the lines become very thick and parts are not shown as expected.
Steps:
SvgConverter.DrawOnCanvas(svgStream, pdfCanvas);
SvgConverter.DrawOnDocument(svgStream, pdfDocument);
SvgConverter.DrawOnPage(svgStream, pdfPage);
SvgConverter.ConvertToXObject(svgStream, pdfDocument);
SvgConverter.ConvertToImage(svgStream, pdfDocument);
I have tried them all but results are same for all.
Questions:
PDF and SVG files are vectors, so can't we use them as integrated through IText, why should we need to convert it to a Raster? Why do we need a converter?
Is there a way to decrease thickness or a way to not to lose image quality?
Thank you for your time!
When you invoke SvgConverter.ConvertToImage or SvgConverter.ConvertToXObject, your SVG is not converted into a raster image - it still remains a vector image. So you can use the integrated SVG converter workflow and you are in fact using it with the SvgConverter. The converter is needed to process SVG file format into more PDF-specific structures, so it performs some conversion because PDF does not support SVG directly. This is not vector -> raster conversion though.
Regarding the problem with the line thickness, first think you should do is to try with the latest version - as far as I see you are trying with 7.1.11 while it's dated back to around a year and 7.1.15 is out already. If the problem persists then it's a bug in the SVG support in iText and you can try to minimize the SVG file to see if there is a workaround to achieve proper conversion until the bug is fixed for your case and/or report the problem to iText (StackOverflow is not the right place to report bugs).

save a file in memory instead of a physical path

I am trying to use Office Interop assembly to export a PowerPoint slide to an image. The SaveAs(), SaveCopyAs(), and Export() methods all accept a physical path as the output folder.
I want to put it in memory for further processing instead of creating a bunch of images on disks that I have to clear them later.
How can I export a slide and hold it in the memory temporarily instead of saving it in a physical folder?
Calling the Slide's .Copy method will put the slide on the clipboard in a number of formats, including PNG, GIF, JPG, DIB, BMP and metafile/enhanced metafile.
It should be possible to access the format you want from there.
There is no way of writing an Office Apps files straight to memory. You can look at Open XML SDK for Office, but i think, it will be easier if you just save the image to a file and load it through memorystream.

Creating print previews of documents

I need to create a software which will create print previews of the documents of following formats: the MS Office documents (.doc(x), .ppt(x), .xls(x)), images, .txt files and PDF files. I have made a working prototype using XPS files. So basically I do the following: I convert the office files to .xps using Office Automation and then I render the .xps documents to images. I simply create XPS files from images and .txt by adding text or an image to FlowDocument, then rendering it. But I have found out that there is no way to convert PDF to XPS fast (A document which has 600 pages takes more than 2 minutes to convert and this is totally not suitable). So I am stuck at this point. It seems that I should start over again, using the different file format. Should I rewrite my program using PDF, for example, or is there any other way to accomplish my task? And if I should use PDF, could you, please, suggest me a good PDF C# library to render previews of pages as fast as possible? I tried using Websupergoo's ABCPdf, but it is too slow, because it does not allow me to render the previews to System.Windows.Media.Imaging.BitmapSource, only System.Drawing.Bitmap, so I have to convert Bitmap to BitmapSource and it takes up a lot of time.
Thanks in advance.
Use Ghostscipt to convert PDF to images. Though, I don't know why you wouldn't just use the PDF. I have used GhostScript for a number of PDF/Image manipulation tasks.
http://www.wibit.net/blog/integrating_ghostscript_c
Ghostscript will output any PDF to images to the settings you specify. I think you can use it as a DLL or as a commandline process.

Consolidate the same image used multiple times in a PDF

I am generating PDF documents using DevExpress XtraReports.
I am using the same image over and over (in rows of status lights).
The PDF generated seems to duplicate the image definition for each image included. I would prefer if it included the image once and referenced it wherever it needed another copy - this would drastically reduce the size of my PDF docs.
Is there any way to achieve this using DevExpress or even post processed via a third party application. Any help is appreciated.
Two options:
OPT1: I suppose your image is a background or a company logo and the image is the same on all the pages of the pdf. If yes, then create the pdf without the image. Post-process the pdf and add the image on all the pages (you can do that using itext/itextsharp or pdflib).
OPT2: take your actual pdf and convert it using Ghoscript. Using Ghsoscript you can do a "pdf to pdf" conversion. During the conversion Ghostscript try to identify repeated images and removes them. The resulting file is smaller. (Ghostscript is not always able to do that... try with you pdf file).
It is possible to re-use the same image content in multiple locations throughout your document. But it's a fair bit easier to do this while adding the image(s) to the PDF.
I'm not sure if DevExpress supports this.

Modifying JPEG headers in C#

I'm trying to open a file to bytes, convert it to a string, modify some data (think Steganography) and convert the file back to bytes and save it as a jpeg. So far, everything I've tried has corrupted the file in converting it to a string. I've tried converting it to a 64-bit string, but of course that's a bit hard to modify the data in :P
Any suggestions on how I can do this properly, without corrupting my file?
I don't have this in C# but in PHP, but you can take a look and adaptate to C#.
http://www.havenard.110mb.com/fotomagica/
This is my site where there's a tool to modify the EXIF data of a JPEG and build "magic pictures" which display something in the thumbnail which is not the real picture.
It opens the JPEG, slice its sectors, and build it back ignoring irrelevant sectors and placing my custom made EXIF header.
And this is the source of the PHP classes:
http://www.havenard.110mb.com/fotomagica/class.JpegMapper.php.txt (ExifMapper is incomplete)
http://www.havenard.110mb.com/fotomagica/class.DataMapper.php.txt
You can study it and rebuild in C#, it is really simple to slice a JPEG as you will see.
Usage of this PHP class (only JpegMapper):
$jpg = new JpegMapper('picture.jpg');
$jpg->save_filtered('filtere picture.jpg'); // save removing irrelevant sectors
It is great to get any JPEG even smaller (sometimes a lot smaller).

Categories

Resources