2014-05-15 57 views
1

如何在winRT应用程序中生成pdf?我使用iTextSharp在Windows应用商店生成pdf,但winRT没有文件流,文件模式或文件夹。帮助C#/ XAML winRT应用程序导出PDF

这里是我的代码:

iTextSharp.text.pdf.PdfWriter writer = 
    iTextSharp.text.pdf.PdfWriter.GetInstance(
     doc, new System.IO.FileStream(System.IO.Directory.GetCurrentDirectory() + 
             "\\ScienceReport.pdf", System.IO.FileMode.Create 
     ) 
    ); 
+0

您是否获得运行时错误PDF文件?我不认为WinRT和Windows 8 Pro的应用程序API不同。 – BradleyDotNET

+0

这里是我的代码: 'iTextSharp.text.pdf.PdfWriter作家= iTextSharp.text.pdf.PdfWriter.GetInstance(DOC, 新System.IO.FileStream(System.IO.Directory.GetCurrentDirectory()+“\\ ScienceReport.pdf“, System.IO.FileMode.Create));' 但在文件流,FileMOde和目录中有一条红线,表示命名空间System.IO中不存在这些命名空间。在此先感谢 – Paul

+0

啊,你正试图使用​​标准的.NET在Windows应用商店中做到这一点。如果我理解正确,那更有意义。为了将来的参考,请使用邮件下方的“编辑”按钮在问题中包含代码,因为它更易于阅读。 – BradleyDotNET

回答

7

我可以生成在WinRT中

 string path = @"ExportPDF.pdf"; 
     var storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder; 

     // Create empty PDF file. 
     var file = await storageFolder.CreateFileAsync(path, CreationCollisionOption.ReplaceExisting); 
     if (file != null) 
     { 
      await FileIO.WriteTextAsync(file, string.Empty); 
     } 

     // Open to PDF file for read/write. 
     StorageFile sampleFile = await storageFolder.GetFileAsync(path); 
     var stream = await sampleFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite); 

     // Create an instance of the document class which represents the PDF document itself. 
     Document document = new Document(PageSize.A4, 25, 25, 30, 30); 

     // Create an instance to the PDF file by creating an instance of the PDF 
     // Writer class using the document and the filestrem in the constructor. 
     PdfWriter writer = PdfWriter.GetInstance(document, stream.AsStream()); 

     // Add meta information to the document 
     document.AddAuthor("Jigs"); 
     document.AddCreator("Sample application"); 
     document.AddKeywords("PDF App"); 
     document.AddSubject("Document subject - Describing the steps creating a PDF document"); 
     document.AddTitle("The document title - PDF creation"); 

     // Open the document to enable you to write to the document 
     document.Open(); 
     // Add a simple and wellknown phrase to the document in a flow layout manner 
     document.Add(new Paragraph("Hello!")); 

     // Close the document 
     document.Close(); 
     // Close the writer instance 
     writer.Close(); 
+0

它也适用于uwp应用程序吗? – batmaci

相关问题