我有一个图像,我想用c#写入itext Pdf文件。这是我的代码,用于从图表生成图像并写入itext pdf文件。
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
using (MemoryStream stream = new MemoryStream())
{
pdfDoc.Open();
pieChart.SaveImage(stream, ChartImageFormat.Png);
iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
chartImage.ScalePercent(75f);
pdfDoc.Add(chartImage);
}
pdfDoc.Close();
现在根据我的要求,我必须使用另存为对话框打开此pdf文件。这是我尝试打开此pdf文件的代码..
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 0;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
// Code to write the stream goes here.
PdfWriter.GetInstance(pdfDoc, myStream);
myStream.Close();
}
}
但我无法获取pdf文件.Dialogue Box即将推出,但我无法获取pdf文件。 请帮我解决这个问题。 提前谢谢。
答案 0 :(得分:0)
您需要将流字节数据保存到文件中......
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 0;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
// Code to write the stream goes here.
PdfWriter.GetInstance(pdfDoc, myStream);
myStream.Close();
using (FileStream file = new FileStream(saveFileDialog1.FileName, FileMode.Create, System.IO.FileAccess.Write,FileShare.ReadWrite)) {
byte[] bytes = new byte[myStream.Length];
ms.Read(bytes, 0, (int)myStream.Length);
file.Write(bytes, 0, bytes.Length);
myStream.Close();
}
}
}
您可以使用streamwriter编写流数据...