Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my case, I am studying to convert PDF to BMP and then convert BMP to PDF.

I did consist of files which input and output. By the way, I would like to make memory streams instead of files but I would like to keep "a.pdf"(input file) and "a_bmp.pdf"(output file). I marked (#). I would like to convert files to memory streams in this part(#).
Here is my code. Could you please advise me how to convert files to memory streams?

What I have tried:

Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document("a.pdf");

 for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
 {
    string f = String.Format("{0:D2}", pageCount);
  using (FileStream imageStream = new FileStream(#"a_" + f + "_out" + ".bmp", FileMode.Create))
                {                     
                    Resolution resolution = new Resolution(100);                                         
                    BmpDevice bmpDevice = new BmpDevice(resolution);                     
                    bmpDevice.Process(pdfDocument.Pages[pageCount], imageStream);                       
                    imageStream.Close();   
                }
  using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(#"a_" + f + "_out" + ".bmp"**))
                {
                    Aspose.Imaging.ImageOptions.BmpOptions Settings = new 
                    Aspose.Imaging.ImageOptions.BmpOptions();
                    Settings.BitsPerPixel = 24;

                    image.Save(#"a_" + f + "_out.bmp", Settings);
                }
            }

            Pdf pdf1 = new Pdf();

            foreach (string f in Directory.GetFiles(#"\\", "*_out.bmp"))
            {                   
                FileStream fs = new FileStream(f, FileMode.Open, FileAccess.Read);
                byte[] tmpBytes = new byte[fs.Length];
                fs.Read(tmpBytes, 0, Convert.ToInt32(fs.Length));

                MemoryStream mystream = new MemoryStream(tmpBytes);
                Bitmap b = new Bitmap(mystream);

                Aspose.Pdf.Generator.Section sec1 = new Aspose.Pdf.Generator.Section(pdf1);

                sec1.PageInfo.Margin.Top = 0;
                sec1.PageInfo.Margin.Bottom = 0;
                sec1.PageInfo.Margin.Left = 0;
                sec1.PageInfo.Margin.Right = 0;
                pdf1.Sections.Add(sec1);   

                Aspose.Pdf.Generator.Image image1 = new Aspose.Pdf.Generator.Image(sec1);              
                sec1.Paragraphs.Add(image1);
                image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Bmp;            
                image1.ImageInfo.ImageStream = mystream;
                image1.ImageScale = 1;
            }
            pdf1.Save("a_bmp.pdf");
Posted
Updated 27-Jun-17 1:23am
v3

1 solution

Something like this?
C#
IO.FileStream fs = new IO.FileStream(myFile, IO.FileMode.Open, IO.FileAccess.Read);
IO.MemoryStream ms = new IO.MemoryStream();
fs.CopyTo(ms);
 
Share this answer
 
Comments
Pete O'Hanlon 29-Jun-17 8:49am    
Before anyone asks - my update was approving this from the moderation queue :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900