【发布时间】:2017-06-27 07:04:50
【问题描述】:
就我而言,我正在研究将 PDF 转换为 BMP,然后将 BMP 转换为 PDF。
我确实包含输入和输出的文件。 顺便说一句,我想制作内存流而不是文件,但我想保留“a.pdf”和“a_bmp.pdf”。 我标记了(#)。我想在这部分(#)中将文件转换为内存流。这是我的代码。 您能告诉我如何将文件转换为内存流吗?
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");
【问题讨论】:
-
你已经在你的代码中做到了!?
-
为什么要将所有内容都保存在内存中? :))
-
因为速度。我想将文件(扩展名:仅 ".bmp" )转换为内存。