我们在iOS上的C_xamarin表单应用程序上使用xfinium。我有一些测试代码,可以用几段文本创建一个pdf文件,它工作正常。但是,当我尝试包含图像时,生成的pdf不包含图像,acrobat reader会给出“此页上存在错误”。有问题的图像是我的应用程序中的一个资源(不是从url加载的)。
string path = Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);
string file = System.IO.Path.Combine(path, "report.pdf");
PdfFixedDocument document = new PdfFixedDocument();
PdfStandardFont helveticaBold = new PdfStandardFont(PdfStandardFontFace.HelveticaBold, 16);
PdfPage page = document.Pages.Add();
page.Width = 8 * 72 + 36;
page.Height = 11 * 72;
PdfBrush blackBrush = new PdfBrush(PdfRgbColor.Black);
page.Graphics.DrawString("Hello", helveticaBold, blackBrush, 20, 50);
string imageFileName = "UI_Assets_Image.png";
FileStream imageStream = System.IO.File.Open(imageFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
PdfPngImage image = new PdfPngImage(imageStream);
Debug.WriteLine(" image is " + image.Width + " x " + image.Height);
// Reports 1373 x 417 which is correct so I believe it is
// loading the image correctly
// Draw image in the page width
PdfSize size = page.Graphics.DrawImage(image, 36, 75, 540, 166);
Debug.WriteLine(" DrawImage() returned " + size.Width + " x " + size.Height);
// Reports 540 x 166 which seems reasonable
using (FileStream stream = System.IO.File.Create(file))
{
document.Save(stream, null);
}
如果我不包括图像,pdf就可以了。