Adding external images to PDF using iText - image

I'm having trouble figuring out how to add an external image (referenced by a URL) to a PDF using iText. Is this kind of thing possible?
The PDF spec in 7.1.5 says you should be able to reference a PDF via a URL by using a URL specification. This is what I've got so far:
PdfFileSpecification pdfSpec =
PdfFileSpecification.url(writer, "http://www.someurl.com/test.jpg");
StringBufferInputStream sbis = new StringBufferInputStream("");
PdfStream dict = new PdfStream(sbis, writer);
dict.put(PdfName.FILTER, PdfName.DCTDECODE)
dict.put(PdfName.TYPE, PdfName.XOBJECT);
dict.put(PdfName.SUBTYPE, PdfName.IMAGE);
dict.put(PdfName.WIDTH, new PdfNumber(100));
dict.put(PdfName.HEIGHT, new PdfNumber(100));
dict.put(PdfName.BITSPERCOMPONENT, new PdfNumber(8));
dict.put(PdfName.LENGTH, new PdfNumber(0));
dict.put(PdfName.F, pdfSpec);
PdfIndirectObject img = writer.addToBody(dict);
I know I still need to make sure the color space is added and stuff, but my main concern right now is actually getting this image into the body of the document. I can't figure out how to do this... it seems I can't get a reference to a PdfPage or the resources dictionary or anything. Is this possible using iText?
As a side note, this exercise is useless if I'm going to be presented with a security warning when the view tries to go load the image. Does anyone know if that is the case?

External content is described in the PDF spec, but almost no PDF processor does actually support them. By now Acrobat 9 has support for it, but I would be very cautious with that feature: Your clients or users may not be able to see the referenced content.

Related

How to add auto image watermark in orchard CMS?

I use Orchard cms. I want when images uploaded, the images get watermark automatically. How can I do this?
To add watermark automatically, you have to add a OnPublished handler for ImagePart as following:
OnPublished<ImagePart>((context, part) => {
var mediaPart = part.As<MediaPart>();
// Here you can add watermark code
});
If you want to add the watermark on the original uploaded image, you have to call it from the handler directly, but if you want to use media processing module mechanism (Which will keep the original image as is, and create a new one with filters applied and save it in _Profiles folder), then you can add a new implementation for IImageFilterProviderto add a new filter for Orchard media processing pipeline.
Finally, I recommend you to use ImageResizer.Plugins.Watermark plugin to achieve this, because Orchard already use ImageResizer component as default image processing framework.
Update: Please refer to this link for complete implementation, or this repo for source code.
This should be possible by providing a custom implementation of IImageFilterProvider. Search for this interface in the solution, you will find a ResizeFilter and FormatFilter in the Orchard.MediaProcessing module as an example.
Also, there are some articles like http://www.davidhayden.me/blog/developing-custom-image-filters-in-orchard-cms.

Stop Magento/tinyMCE forcing object tag type

So iv been reading about peoples problems with tiny MCE "messing up" code. I understand it isn't, but I have a very minor problem that is become a major problem.
Im trying to add a simple object tag to one of my CMS pages. I need the object type to be "text/html" but whenever I save the page it converts this to "application/x-shockwave-flash"
I don't want to turn off the tinyMCE cleanup option as by the sounds of things this is a really bad thing to do.
So is there a way to stop it changing just the type attribute?
I have tried adding the code to a CMS widget and adding the widget to the page but this get the same altered result.
This thread (Object tag is not working) seems similar, but simply changing the type isn't working for me as it is automatically changing back to flash upon save.
The url i am trying to load looks like this...
(http://www.domain.com/animations/embed/one/o-t-t-d?player_width=100%&player_height=100%) I have been told that this will return flash if it is available or html if not.
Im assuming that because i'm saving the page on a PC with flash available its recognising and changing the type.
Any help appreciated.
You can edit the file js/mage/adminhtml/wysiwyg/tiny_mce/setup.js to make this happen.
Look for the settings array (around line 100). Add 'extended_valid_elements' there, like this;
var settings = {
...
extended_valid_elements: 'object[type]',
...
};
This way, TinyMCE will know the type attribute is valid and won't change it.

How do I detect preloaded images that haven't been attached to the page using HtmlUnit?

I'm loading some page content using HtmlUnit, and I would like to be able to capture the URLs of images created in the following fashion:
<script type="text/javascript">
var img = new Image();
img.src = 'slider.png';
</script>
The key issue here is that the image is never attached to the document, so I can't find it by iterating over 'img' tags. Furthermore, HtmlUnit does not actually generate a web request for the image (as it shouldn't), so I can't pick it up by overriding the WebConnection and intercepting the request.
In a normal browser, the image would be preloaded when the src attribute is set. If an image would normally be loaded, I want to be able to tell that that would happen.
My constraint is that I do not control the content, so I can't just load the image differently. The content comes from a 3rd party, and I am validating it and recording anomalies. I prefer not to gather this information via a different tool, since HtmlUnit is doing a lot of other tasks for me already.

Download pdf or image through ajax

I would like to send a lot of data through ajax request to my server which will generate pdf or jpg format according to that data.
Now i have done all that, my issue is to how output that generated pdf/jpg back to the user trough ajax? I guess i might be able to use json for that, but im not really sure how, and i think there would be a lot issues with pdf.
Also if some one gonna suggest using form with hidden inputs that will not work since i have really big multidimensional array with lot's of data and it would simply take to much effort to make it work.
By the way, i am using jquery, but anything else is acceptable as long as it does the job done without making me to rewrite half of my script.
To display a JPG
AJAX: You can return the data hex encoded (be sure to set the content type appropriately: header('Content-type: image/jpeg')). Then you just inject an <img/> element into the DOM and set it's src attribute to the returned Data URI.
HTML: Also, you could inject the <img/>'s with a normal src URL to some location on your server.
For PDF
It's a little more tricky. Some browsers display PDF's natively (Chrome/Firefox), others rely on optional third-party plugins. You can detect these plugins, but can't control whether the PDF is displayed in a window/frame or is downloaded.
If you choose to display, you can create a new window/tab to display it or display it in an iframe dynamically.

Preload the Image in the webbrowser of windows phone

I'm designing a news-reading app and using the NavigateToString() of a webbrowser to show some string. Now I want to implement the off-line reading function, the html string had already been downloaded, except the image.
Windows phone has implemented the image-cache function, once the Image is requested, it had been cached. But now problem is that, all the html strings are stored in a array, some of those html strings hadn't been shown throught the navigateToString(), namely the imgs in those string couldn't show up if the internet is disconnected.
So I'm wondering how to cache the imgs in the webbrowser?
thanks,
ellic
It ain't gonna be easy or pretty.
Here's one thing you can do...
Parse the HTML using something like the HtmlAgilityPack. Linq should be a good solution for finding all the images. If you want to use xpath, you can do that too.
Find all Images in the HTML and use a WebClient (or better yet, a WebRequest) to fetch them.
Save them as part of your database that you use to store your HTML strings (say, Base64 encoded if your medium needs to be strings, key'ed by the URI to the image);
When you want to display the offline HTML, again, parse the HTML and replace all references to images with the data protocol equivalent of your cached images.

Resources