Reduce size of pdf in c# - c#

Is it possible to reduce size of a PDF?
I have couple of things saved in DB as PDF, image, docs, etc. saved as base64, but mostly PDFs and images.
I am able to reduce/scale down images, but would it be possible for PDFs too?
This is for a mobile application. I can compromise on quality (which I am doing with images).
Edit
Can't say what's in the pdf, depends on the client, I guess there is no easy/direct way to reduce the size of pdf.
What's your thoughts on compressing the PDF and decompressing at mobile side? Will it have any effect on reducing the size?

It mainly depends on your PDF:
You can remove embedded fonts
You can reduce images quality
You can optimize resources if duplicated (if source is iText it isn't a remote option)

Related

AWS S3 Files how to properly store images and image compressing

I am building a mobile app similar to Instagram in terms of working with Images. I am using amazon s3 to store the images and Mysql for the file path. I'm trying to ensure users upload great quality pictures but also ensure the file size is reasonable, Should I compress the images? Does anyone have an idea and what is the acceptable size for an image?
The definition of "acceptable" is totally up to you!
You should certainly store a high-quality image (probably at the original resolution), but you would also want to have smaller images for thumbnails and web/app viewing. This will make it faster to serve images and will reduce bandwidth costs.
A common technique is to have Amazon S3 trigger an AWS Lambda function when a new image is uploaded. The Lambda function can then resize the image into multiple sizes. Later, when your app wishes to retrieve an image, it can point to a resized image rather than the original.
When resizing images, you can also consider image quality. This allows JPG files to reduce in size without needing to reduce in resolution.

iTextSharp, Why when creating a PDF file size is 2 times larger than the original folder with images?

I need the finished PDF file to be 30% smaller than the original image folder.
There is a folder with images in tiff format. Then I add one image per page (Doc = new document (), etc.), the resulting document size is equal to the size of the image folder. But after passing the doc.Close() PDF file size increased two times (so I use compression PDF later and then the file is approximately equal to the folder), but I need the finished PDF file to be 30% smaller than the original image folder.
Most image formats are already compressed, so they won't compress any more. PDFs usually compress because they're mostly text, but one that is mostly images won't.
Also, compression routines usually assume that the data is suitable for compression. If you give them pre-compressed data, the result can be a larger file. It's hard to tell exactly what happened without seeing your files, but I guess that's the reason.
If you want a smaller file, you'll have to reduce the amount of information in your images. Crop them, reduce the colour depth, increase the compression or reduce the number of images.

is there a way to compress a paged .tiff file using C#?

At the end of my process, I need to upload several paged .tiff file images to a website. The files need to be very small, 500kb or less when i upload them.
The problem is, even with me resizing them a lot but at the same time being able to read a few lines of text that are in some of them, they are around 1mb each or so.
I first resize all images going into the tiff files but it's not enough. I need a way to change the quality of them to decrease their size as well.
Can C# do this or would I need a third party software to do it?
The files being uploaded MUST be .tiff.
You don't provide much detail about your data, so can only make some guesses as to what you might need to look at.
First, can you loose some resolution? Can you make the images smaller?
Second, can you loose some color depth? Are you saving the files in a color format when bilevel or greyscale images would suffice?
Third, how clean are these images? Are they photos, scanned documents, what? If they are scanned documents of text or drawings, then some pre-processing to remove noise can make a significant difference in size.
Lastly, what compression method are you saving the file with? Only a lossy format is going to give you the highest degree of compression is most circumstances.
Based on your follow-up:
1) If you can make smaller, this of course saves significant storage space. Determine what is the minimum acceptable resolution that they need to be and standardize on that.
2) If you need to persist color, then this step might not be as effective, since you would have to algorithmically decrease the dynamic range of colors used in the image to an acceptable level before compressing. If you are not sure what this means, then you would probably best skip considering this completely unless you can spend time learning more about image processing and/or using a image processing library that will simplify this for you.
3) I don't think you addressed this in your comments. If you want more precise help, you should update your original question and add much more detail about what you are trying to accomplish. Provide some explanations of what/why you need to do in order to help determine what tradeoffs make sense.
4) Yes, JPG is a lossy format, but I think you may be confusing a few different things (or I may not be understanding your intent from your description). If you are first resizing your original images down into a new JPG file (an intermediate image file), then you are building a TIFF file and inserting the resized JPG as a source image into a multi-page TIFF and saving that, then you need to realize that the process of how the files are compressed in the intermediate files do not necessarily have any correlation with the compression format used in the TIFF file. Depending on what you are using to build and create the TIFF file, the compression format used in the TIFF is done separately and you probably need to specify those parameters when you save that file. If this is what you are doing, then the intermediary process of saving the JPG files may be increasing the size a bit.

Image Compression in C#.Net

I am working on creating an Image Manipulation library using C# with MVC3. There is an option where users would be uploading multiple files. These files need to be compressed during transfer to server in order to cut down the upload time and bandwidth usage. We don't want to compromise with the quality and size of the images but do want to compress the images. Can somebody provide links to the articles which can be helpful to compress the images but maintaining their sizes and quality.
Look into an existing uploader with compression. Here's a few
silverlightfileupld (Silverlight)
flexupload (Flash)

How to compress image?

I have problem with image compression. I need to compres a lot of files (700-900kb) to files 70-80kb without
loss of quality. (or small loss ) I found menu item "Save for Web & Devices ..." in Photoshop. It works great.
But I don't want to use photoshop programmatically. May be someone knows how to solve this problem with
other third party components or frameworks?
Thanks for any ideas!
.NET has a number of image decoding/encoding libraries, often tied to a particular GUI framework (e.g. in Windows Forms you have System.Drawing.Image and for WPF, see the Imaging Overview chapter on msdn).
There are also third party libraries specialized in image conversion/compression that you can find online (both free and non free)
Generally though, the amount of saving you get from compressing an image highly depends on the original format. If you already have JPEG photos with normal compression (quality of 85%) then there is not much you can do in terms of making them smaller except resizing them. If you have raw bitmaps (e.g. BMP, uncompressed/low compression TIFF etc.) then you can expect quite large savings with most compressing formats
When choosing image format, consider this:
Photos and similar: JPEG will often do fine. Good savings with reasonable quality loss
Screenshots and similar: PNG will generally give best results (PNG is lossless). JPEG will often create highly visible artifacts on screenshots
Compressing an already compressed image (i.e. PNG, JPEG etc.) with a general purpose compression algorithm like ZIP or RAR will in practice not give you any savings. You may actually end up with a bigger file.
You can have a look at the FreeImage project. It has a C# wrapper that you can use.
Imagemagick allows you to batch-processing on files and offers a everything you could possible ask for when it comes to handling of images
E.g. to resize every image in folder (destroy originals) to QVGA do
mogrify -resize 320x240 *.jpg
To preserve aspect ratio do
mogrify -resize 320x240! *.jpg
If you need to traverse a directory structure, this is how you can do it in *nix based systems (also destroying originals)
find . -type f -name *.jpg -exec convert -resize 800x800 {} \;
There is also an quality switch available, see here

Categories

Resources