如何将数组中的 2 个或多个字节项合并为一个字节元素
首先我想说,我是 C# 初学者,所以要有耐心:)
我有字节数组。一项代表 pdf 文件的一页。我现在需要将这些数组合并为一个。我希望这个新数组将显示两页,一层一层地显示。在这种情况下:
第 1 页(来自项目 [0])
Page2(来自 Items[1.])
这是好主意吗?我需要这个来将 pdf 作为图像附加到报告服务而不使用数据库。
编辑:
这是我的代码:
System.Collections.ArrayList items = new System.Collections.ArrayList();
System.IO.FileStream fs = new System.IO.FileStream("C://1.pdf", System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] pdf = new byte[fs.Length];
fs.Read(pdf, 0, (int)fs.Length);
PDFParser.Parse parser = new PDFParser.Parse();
System.Collections.Generic.List<System.Drawing.Image> images = parser.Split(pdf);
object dataByte = null;
for (int i = 0; i < images.Count; i++)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
images[i].Save(ms, System.Drawing.Imaging.ImageFormat.Png);
items.Add(ms.ToArray());
}
以及 PDFParser.dll 中的方法:
public class Parse
{
public List<Image> Split(byte[] document)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(document);
Document pdfDoc = new Document(new BinaryReader(ms));
Page page = null;
List<Image> returnVal = new List<Image>();
float resolution = 100;
float scale = resolution / 72f;
int bmpW = (int) (scale * pdfDoc.Pages[0].Width);
int bmpH = (int) (scale * pdfDoc.Pages[0].Height);
for (int i = 0; i < pdfDoc.Pages.Count; i++)
{
page = pdfDoc.Pages[i];
using (Bitmap bitmap = new Bitmap(bmpW, bmpH))
{
Graphics graphics = Graphics.FromImage(bitmap);
graphics.ScaleTransform(scale, scale);
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
page.Draw(graphics);
returnVal.Add((Image)bitmap.Clone());
}
}
return returnVal;
}
}
In start I want say, I am beginner in C# so be patient :)
I have array of bytes. One item represent page of pdf file. I need now merge this arrays to one. I hope this new array will show two pages one above other. In this case:
Page1 (from Items[0])
Page2 (from Items[1.])
Its good idea? I need this to attach pdf as images to reporting services wihtout using Database.
EDIT:
Here is my code:
System.Collections.ArrayList items = new System.Collections.ArrayList();
System.IO.FileStream fs = new System.IO.FileStream("C://1.pdf", System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] pdf = new byte[fs.Length];
fs.Read(pdf, 0, (int)fs.Length);
PDFParser.Parse parser = new PDFParser.Parse();
System.Collections.Generic.List<System.Drawing.Image> images = parser.Split(pdf);
object dataByte = null;
for (int i = 0; i < images.Count; i++)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
images[i].Save(ms, System.Drawing.Imaging.ImageFormat.Png);
items.Add(ms.ToArray());
}
and method from PDFParser.dll:
public class Parse
{
public List<Image> Split(byte[] document)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(document);
Document pdfDoc = new Document(new BinaryReader(ms));
Page page = null;
List<Image> returnVal = new List<Image>();
float resolution = 100;
float scale = resolution / 72f;
int bmpW = (int) (scale * pdfDoc.Pages[0].Width);
int bmpH = (int) (scale * pdfDoc.Pages[0].Height);
for (int i = 0; i < pdfDoc.Pages.Count; i++)
{
page = pdfDoc.Pages[i];
using (Bitmap bitmap = new Bitmap(bmpW, bmpH))
{
Graphics graphics = Graphics.FromImage(bitmap);
graphics.ScaleTransform(scale, scale);
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
page.Draw(graphics);
returnVal.Add((Image)bitmap.Clone());
}
}
return returnVal;
}
}
3'rd party program what I used:
http://www.tallcomponents.com/pdfrasterizer3.aspx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
恐怕这行不通!您看到的字节数组是代表每个 PDF 页面的 PDF 对象的序列化形式。将这两者合并在一起将产生一个字节数组,该数组无法反序列化为有效的 PDF 文档对象。
为了实现您的需求,请将每个字节数组反序列化为合适的 PDF 表示形式,然后使用合适的 API 将文档合并在一起。
I'm afraid this just isn't going to work! The array of bytes that you see is a serialized form of the PDF object that represents each PDF page. Merging these two together will result in an array of bytes which cannot be de-serialized into a valid PDF document object.
To achieve what you require, deserialize each byte array into a suitable PDF representation, than use a suitable API to merge the documents together.
对 ColinE 的回答+1。要实现您想要实现的效果,您需要使用一些第三方 C# PDF 库。您可以在这个问题的答案中找到一些开源 PDF 库的链接< /a> 或那里。
更新
至于您的代码 - 合并两个图像,以便一个图像紧接着另一个图像出现,您可以使用
Graphics
类,如下所示:替换
为
+1 to ColinE's answer. To implement the effect you want to achieve you'll need to use some 3rd party C# PDF library. You can find some links to open-source PDF libraries in answers to this SO question or there.
UPDATE
As for your code - to merge two images so one appears right after another you can use
Graphics
class like this:Replace
with
如果其他人正在寻找类似的东西,您只想合并两个数组而不关心维度和内容:
In case someone else is looking for something similar where you just want to merge the two arrays and not care about dimensions and stuff: