Issue Converting Bytes to an Image from a PDF file - arrays

I am trying to convert a pdf file to a Bitmap by first getting the Bytes of the PDF file and then converting it into a MemoryStream to Be converted into the Bitmap.
This worked successfully when converting images but not working with a pdf.
Dim bytes As Byte() = System.IO.File.ReadAllBytes("C:\Users\s.ferry\Downloads\test2.pdf")
Dim myimage As Image
Dim msPdf As System.IO.MemoryStream = New System.IO.MemoryStream(bytes)
myimage = System.Drawing.Image.FromStream(msPdf)
upBmp = myimage
Above is the code snippet I am using to try and accomplish this. I am getting an error on the last line to say the parameter msPdf is not validSee Here
I was hoping to accomplish this without having to introduce a 3rd party source but don't think I will have a choice.
Any help is appreciated

I Don't think this is possible without a third party Library.
A useful and free tool for this is Spire.PDF
More Info Here

It's understandable that you might think you could create an instance of the System.Drawing.Image class from a PDF directly. In many cases a PDF document consists of a single-page that is no more than a scanned image so some users have the perspective that it's nothing more than an image format.
But most PDFs are much more complicated than this. Online you will find a plethora of PDF software, however Rasterization of a PDF page to an image is a very complicated task that many vendors don't always do correctly.

Related

How to single out an object in a bytearray obtained from a pdf?

Let's say I have a pdf with 10 balloon callouts. Now I have converted that pdf into a bytearray using the following code.
file = open('cc.txt','rb')
for line in open("123.pdf", 'rb').readlines():
file.write(line)
file.close()
Now, how can I identify the bytes that denote the balloon callouts?
I tried googling, but wasn't able to find any answers. Help me out guys.
You can't because this is not how PDF files are structured internally (they are not line based). You need a PDF parsing library to get the objects corresponding to the balloon call-outs.

is base-64 just for jpeg or gif? how about a tiff? how can I convert string (which is part of XML and belong to a tiff file) to byte[]?

I wrote this in my application for displaying a TIFF Image :
byte[] b = convert.frombase64("ADFsf/s1ugdGHREHR/+/235gjhjhfcg/+kdhjgvkhfv/gcngcxsfdzsdf......=")
but It doesn't work. I received this message on loading the tiff :
run-time error '31037'
system error &H800401C2 (-2147221054)
I don't know exactly why?
When I save this tiff Image in this way :
file.writeallbytes("z.tiff",b);
I can open it , It means, It saved correctly.
now my problem is, I can`t display it in my application and image loading has got some problems.
thanks
Base-64 is certainly not only for JPEG or GIF. It can be used to represent any string of binary data (including plain text.) The base-64-encoded data you gave is malformed (as can be seen by writing the bytestream to a .tiff file), though.
My issue was done!
byte[] b = convert.frombase64("ADFsf/s1ugdGHREHR/+/235gjhjhfcg/+kdhjgvkhfv/gcngcxsfdzsdf......=")
method: convert.frombase64 -> can be used for any string of binary data
My problem was about loading a tiff file.
Firstly I must identify the pages in tiff image.
I split my tiff image to single pages by frame dimension method.

iTextSharp to generate PDF from WPF FixedDocument

I have a simple WPF app that displays and prints some
reports with a FixedDocument.
How can generate PDF's from that, with a free and open solution,
such as iTextSharp?
A WPF FixedDocument, also known as an XPS document, is a definite improvement over PDF. It has many capabilities that PDF lacks. In most cases it is better to distribute your document as XPS rather than PDF, but sometimes it is necessary to convert from XPS to PDF, for example if you need to open the document on devices that have only PDF support. Unfortunately most free tools to convert from XPS to PDF, such as CutePDF and BullzipPDF, require installing a printer driver or are not open source.
A good open-source solution is to use the "gxps" tool that is part of GhostPDL. GhostPDL is part of the Ghostscript project and is open-source licensed under GPL2.
Download GhostPDL from http://ghostscript.com/releases/ghostpdl-8.71.tar.bz2 and compile it.
Copy the gxps.exe executable into your project as Content and call it from your code using Process.Start.
Your code might look like this:
string pdfPath = ... // Path to place PDF file
string xpsPath = Path.GetTempPath();
using(XpsDocument doc = new XpsDocument(xpsPath, FileAccess.Write))
XpsDocument.CreateXpsDocumentWriter(doc).Write(... content ...);
Process.Start("gxps.exe",
"-sDEVICE=pdfwrite -sOutputFile=" +
pdfPath +
"-dNOPAUSE " +
xpsPath).WaitForExit();
// Now the PDF file is found at pdfPath
A simple way, which is easy, but probably not the most efficient way is to render the Fixed document to an image and then embed the image in a PDF using iTextSharp.
I have done it this way before successfully. Initially I tried to convert the control primitives (shapes) to PDF equivalents, but this proved too hard.
If you can get it into an image from WPF then you can import it into iTextSharp like they do in this article. You can even avoid the filesystem all together if you write it to a MemoryStream and then use that instead of using a FileStream.
http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images
IF you want to do it programatically, your Best bet would be the following path XPS (Fixed Document) -> Print to PS -> Use Ghostscript to read the PS and convert to PDF.
If you dont care about reading the PDF back in the code, you can print to any one of the free PDF printers to which you can pass the destination path. This way your target PDF file will still be searchable if you have any test in your report.

Silverlight Image Loading

I'm a beginner just beginning to work with Silverlight with a very basic question. I want to display a .png image. I have already done it in the page.xaml file but I would like to do it in code (C#) so that I can add and remove images while my program is running. I have seen some code in which you add an image to the Children of a Canvas, but when I do this no images are ever displayed. Could someone provide some code and where to put it? Here is what I've been working with. There are no exceptions, but no image appears.
page.myCanvas.Children.Add(LoadImage("Image/MrBlue"));
public Image LoadImage(string resource)
{
Image img = new Image();
Uri uri = new Uri(resource, UriKind.Relative);
ImageSource imgSrc = new System.Windows.Media.Imaging.BitmapImage(uri);
img.SetValue(Image.SourceProperty, imgSrc);
return img;
}
The image is set to "Resource" and "Do not Copy."
Debugging Silverlight can be a pain, although it's quite possible to set up in VS2008 (which you might already have done. If you haven't feel free to ask...) and that can catch some of the 'simple' errors like having the wrong Uri for the image you want. Your code looks fine to me, although what I'm using is slightly different. If you want an example from a working app, the function I use for loading images is:
public void ShowPicture(Uri location)
{
Image pic = new Image();
pic.Source = new BitmapImage(location);
Grid.SetColumn(pic, 1);
Grid.SetRow(pic, 1);
LayoutRoot.Children.Add(pic);
}
Note that I have a using statement that includes System.Windows.Media.Imaging.
Even without full debugging, a utility like fiddler that shows the http requests might help track down bad Uris in code, which is all I can think of that might be wrong here. Hope it helps.
I tested your code and it works fine for me, so as Raumornie already hinted, it would seem to most likely be an issue with the path to the image. As per your code, is the image file located in a folder called Image in your Silverlight project and just named MrBlue? On a quick look, it seems to be missing the .png, or?
Good luck!

Getting a CGImage out of a PDF file

I have a PDF file where every page is a (LZW) TIFF file. I know this because I created it. I want to be able to load it and save it as a bunch of TIFF files.
I can open the PDF file with CGPDFDocumentCreateWithURL, and get a page. I can even draw the page onto the screen.
What I WANT to do is draw the page into a bitmapContext, so that I can use CGBitmapContextCreateImage to get the image into a CGImageRef. However, in order to create a bitmap context, I need to know the size and resolution of the image. I can't seem to find out how to get either a CGPDFDocument or a CGPDFPage to tell me the resolution of the image object on that page.
Is there an easier way to do this that I'm not realizing?
thanks.
Ghostscript will work for you here :
gs -sDEVICE=tiff32nc -sOutputFile=foo-Page%d.tif foo.pdf
For 2 page document foo.pdf you should get :
foo-Page1.tif
foo-Page2.tif
From memory I think the output resolution from GS is that of the containing Page, not necessarily the resolution of the embedded file (unless these are the same to begin with).
If this is the case and you want to recover the image as it was originally res-wise, you can use iText (java) or iTextSharp(.net) to get to the image content stream (ie. Bytes) and write them out to disk in the format of your choice, after converting the content stream into a PdfImage iirc.
Hope the ghostscript option is applicable to save writing yet another utility...

Resources