Daily using/supporting

Get Firefox browser!
Get Thunderbird!
Get Opera browser!
Get The Gimp!
Get Inkscape!
Get LibreOffice!
Get Videolan!
Get Linux!
Get Mandriva!
Get Joomla!
Hacker Emblem

Archives

Which topics would you like us to cover more?

Latest comments

Latest tweets

about 1 day ago Using REDIPS.drag to add drag and drop to your .Net webapplication #li #dib0 http://t.co/n8zY3s7d
about 7 days ago http://t.co/cknQcDbo #Kindle
about 15 days ago Freedom isn't the ability to choose what to do or say, but the ability to choose what not to do or say #freedom
about 29 days ago http://t.co/61KTQknI #Kindle
12 Apr 2012 Force the use of a networking adapter using C# #li #dib0 http://t.co/ZTJOPzOz
9 Apr 2012 Mandriva 2010.2 and USB devices in Virtualbox http://t.co/fwq9gbHB
9 Apr 2012 Execute a http request to you own site with PHP http://t.co/DIvWPrpd
Home Architecture, security and coding Removing images using PdfSharp
Removing images using PdfSharp
Written by Division by Zero   
Wednesday, 02 February 2011 11:56

Last week someone asked me if it is possible to remove images from a Pdf to save storage. Of course it is! The next question is how.

It took me a while to figure it out. Using PdfSharp, an (not working! At least not on the Pdf I got) example on exporting images with PdfSharp and a few hour of trial and error I got a working version. I must admit it is the brute force method, since it doesn't only overwrite images, also other object like forms. Here's how it works:

// Open the PDF
PdfDocument doc = PdfReader.Open("inputfile.pdf", PdfDocumentOpenMode.Modify);

// Loop through every page
foreach (PdfPage page in doc.Pages)
{
	// Get all the resources for every page
	PdfDictionary resource = page.Elements.GetDictionary("/Resources");
	if (resource != null)
	{
	       // Get all the external objects
	       PdfDictionary objects = resource.Elements.GetDictionary("/XObject");
	       if (objects != null)
	       {
	               // Loop through every item in the external objects
	               ICollection
 items = objects.Elements.Values;
	               foreach (PdfItem item in items)
	               {
	                       // Get the reference
	                       PdfReference reference = item as PdfReference;
	                       if (reference != null)
	                       {
	                               // Get the underlying values as Pdf Object
	                               PdfDictionary xObject = reference.Value as PdfDictionary;
	                               // Overwrite the data of the object with 1 byte
	                               xObject.Stream.Value = new byte[0];
	                       }
	               }
	       }
	}
}

// Save the Pdf as another file
doc.Save("outputfile.pdf");
 

Add comment


Security code
Refresh

I love deadlines. I like the whooshing sound they make as they fly by. - Douglas Adams


© 2009 - 2012, Division by Zero

Template based on the empire template by joomlashack 

Valid XHTML 1.0 Strict  Valid CSS!  Creative Commons License
This work by Division by Zero is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Netherlands License.