我正在使用PDFSharp,并且我正在从位图图像创建pdf。我可以保存和查看图像作为PDF没有问题。但是现在我要做的是在将pdf保存为实际文件之前,我想将其转换为byte []。在保存之后,我可以将其保存为byte []。我将有两种不同的方法,一种将pdf保存到用户可以打开它的文件,另一种将pdf保存到将发送到数据库的字节,但我可能不会同时使用这两种方法。 。我可能需要保存文件一天,另一个保存为一个字节而不需要将其保存为文件。我有什么:
public void DrawImage(Bitmap thumbnail)//thumbnail is created with another method
{
PdfDocument document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XImage image = XImage.FromGdiPlusImage(thumbnail);
gfx.DrawImage(image, 0, 0, 612, 792);
string filename = string.Format(@"{0}.pdf", Guid.NewGuid());
//This is Save(string path), there is also a Save(memoryStream stream)
document.Save(filename);
//so before I save it as a *.PDF I would like to save it as byte[] that can be sent to a Database, and eventually read from and convert the byte to a viewable *.pdf
//This saves a Bitmap as a pdf successfully
}
我希望我有意义,如果需要,我可以进一步解释
答案 0 :(得分:6)
您可以将MemoryStream
用于此目的:
byte[] pdfData;
using (var ms = new MemoryStream()) {
document.Save(ms);
pdfData = ms.ToArray();
}
答案 1 :(得分:1)
尝试使用MemoryStream:
using(var ms = new MemoryStream()){
document.Save(ms);
byte[] bytes = ms.ToArray();
}