BitmapImage to PDF

时间:2018-04-26 13:22:46

标签: c# pdf bitmap uwp syncfusion

我有一些问题需要在里面创建带有图片的PDF。我知道如何创建PDF但不知道如何在里面显示图片。问题是,我发现的所有功能都不支持BitmapImage。我尝试过ItextSharp和Syncfusion。有谁知道我怎么能解决这个问题?我找到的几乎所有指南都不能用于UWP。

这里我尝试将BitmapImage转换为System.IO.Stream,但这不起作用。

StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(@"C:\Users\IT\source\repos\App3\App3");
try
{
    StorageFile file2 = await folder.GetFileAsync("test.pdf");
    await file2.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
catch
{

}
StorageFile file = await folder.CreateFileAsync("test.pdf");


using (Windows.Storage.Streams.IRandomAccessStream writeStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
    //Create a new PDF document.
    PdfDocument document = new PdfDocument();
    Stream s = writeStream.AsStream();
    //Add a page to the document.
    PdfPage page = document.Pages.Add();
    //Create PDF graphics for the page.
    PdfGraphics graphics = page.Graphics;
    foreach (var st in Waren)
    {
        StorageFolder folder2 = await StorageFolder.GetFolderFromPathAsync(@"C:\Users\IT\Pictures");
        StorageFile file2 = await folder2.GetFileAsync(st.Image + ".jpg");
        using (IRandomAccessStream stream = await file2.OpenAsync(FileAccessMode.Read))
        {

            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
            WriteableBitmap bmp = new WriteableBitmap((int)decoder.PixelWidth, (int)decoder.PixelHeight);
            bmp.SetSource(stream);

            // show the image in the UI if you want.
            byte[] buffer = null;

            using (MemoryStream ms = new MemoryStream())
            {
                Stream s1 = bmp.PixelBuffer.AsStream();
                s1.CopyTo(ms);

                buffer = ms.ToArray();
                Stream stream2 = new MemoryStream(buffer);
                PdfBitmap image = new PdfBitmap(stream2);
                //Draw the image
                graphics.DrawImage(image, 0, 0);
            }
        }
    }

    //Set the standard font.
    PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
    //Draw the text.
    graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
    //Save the document.
    document.Save(s);
    //Close the document.
    document.Close(true);

1 个答案:

答案 0 :(得分:1)

您正在项目中使用syncfusion UWP库,因此我帮助您在帖子中添加syncfusion标记。

回到你的问题。您想在pdf文件中绘制图像。然后,我引用syncfusion document来创建代码示例供您参考:

private async void Button_Click(object sender, RoutedEventArgs e)
{
        //Creates an empty PDF document instance
        PdfDocument document = new PdfDocument();

        //Adding new page to the PDF document
        PdfPage page = document.Pages.Add();

        //Creates new PDF font
        PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 12);

        //Drawing text to the PDF document
        page.Graphics.DrawString("Hello world", font, PdfBrushes.Black, 10, 10);

        StorageFile storageFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"panda.jpg");

        using (var filestream = await storageFile.OpenAsync(FileAccessMode.Read))
        {
            Stream st = filestream.AsStream();
            PdfBitmap pdfImage = new PdfBitmap(st);
            page.Graphics.DrawImage(pdfImage,0,20,500,500);
        }

        MemoryStream stream = new MemoryStream();

        //Saves the PDF document to stream
        await document.SaveAsync(stream);

        //Close the document

        document.Close(true);

        //Save the stream as PDF document file in local machine

        Save(stream, "Result.pdf");

}

async void Save(Stream stream, string filename)
{

    stream.Position = 0;
    StorageFile stFile;
    if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
    {
        FileSavePicker savePicker = new FileSavePicker();
        savePicker.DefaultFileExtension = ".pdf";
        savePicker.SuggestedFileName = "Sample";
        savePicker.FileTypeChoices.Add("Adobe PDF Document", new List<string>() { ".pdf" });
        stFile = await savePicker.PickSaveFileAsync();
    }
    else
    {
        StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
        stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
    }
    if (stFile != null)
    {
        Windows.Storage.Streams.IRandomAccessStream fileStream = await stFile.OpenAsync(FileAccessMode.ReadWrite);
        Stream st = fileStream.AsStreamForWrite();
        st.Write((stream as MemoryStream).ToArray(), 0, (int)stream.Length);
        st.Flush();
        st.Dispose();
        fileStream.Dispose();
    }
}

enter image description here

请注意我将图片放在项目的根目录中,如果我想从我的代码中获取它,我需要使用Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"panda.jpg")方法,并且InstalledLocation中的文件被读取-只要。你不能写它。