我想在Windows應用程序8.1(不Windows電話)C#生成PDF文件,我已經嘗試使用後記,因爲我沒有找到任何免費的軟件包來生成PDF。如果有請讓我知道。PDF生成在Windows應用程序8.1
這是我所使用PostScript嘗試示例代碼,
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("MyFirstPdf.pdf", Windows.Storage.CreationCollisionOption.ReplaceExisting);
using (var stream = await System.IO.WindowsRuntimeStorageExtensions.OpenStreamForWriteAsync(file))
{
using (var writer = new System.IO.StreamWriter(stream, System.Text.Encoding.UTF8))
{
List<long> xrefs = new List<long>();
writer.WriteLine("%PDF-1.7");
writer.Write("%");
writer.Flush();
byte[] bytes = { 0, 0, 0, 0 };
stream.Write(bytes, 0, 4);
stream.Flush();
writer.WriteLine("");
writer.Flush();
stream.Flush();
xrefs.Add(stream.Position);
writer.WriteLine("1 0 obj");
writer.WriteLine("<<");
writer.WriteLine(" /Type /Catalog");
writer.WriteLine(" /Pages 2 0 R");
writer.WriteLine(">>");
writer.WriteLine("endobj");
writer.Flush();
stream.Flush();
xrefs.Add(stream.Position);
writer.WriteLine("2 0 obj");
writer.WriteLine("<<");
writer.WriteLine(" /Type /Pages");
writer.WriteLine(" /Kids [3 0 R]");
writer.WriteLine(" /Count 1");
writer.WriteLine(">>");
writer.WriteLine("endobj");
writer.Flush();
stream.Flush();
xrefs.Add(stream.Position);
writer.WriteLine("3 0 obj");
writer.WriteLine("<<");
writer.WriteLine(" /Type /Page");
writer.WriteLine(" /Parent 2 0 R");
writer.WriteLine(" /MediaBox [0 0 612 792]");
// Default userspace units: 72/inch, origin at bottom left
writer.WriteLine(" /Resources");
writer.WriteLine(" <<");
writer.WriteLine(" /ProcSet [/PDF/Text]");
// This PDF uses only the Text ability
writer.WriteLine(" /Font");
writer.WriteLine(" <<");
writer.WriteLine(" /F0 4 0 R");
// I will define three fonts, #4, #5 and #6
writer.WriteLine(" /F1 5 0 R");
writer.WriteLine(" /F2 6 0 R");
writer.WriteLine(" >>");
writer.WriteLine(" >>");
writer.WriteLine(" /Contents 7 0 R");
writer.WriteLine(">>");
writer.WriteLine("endobj");
writer.Flush();
stream.Flush();
xrefs.Add(stream.Position);
writer.WriteLine("4 0 obj");
writer.WriteLine("<<");
writer.WriteLine(" /Type /Font");
writer.WriteLine(" /Subtype /Type1");
writer.WriteLine(" /Encoding /WinAnsiEncoding");
writer.WriteLine(" /BaseFont /Times-Roman");
writer.WriteLine(">>");
writer.Flush();
stream.Flush();
xrefs.Add(stream.Position);
writer.WriteLine("5 0 obj");
writer.WriteLine("<<");
writer.WriteLine(" /Type /Font");
writer.WriteLine(" /Subtype /Type1");
writer.WriteLine(" /Encoding /WinAnsiEncoding");
writer.WriteLine(" /BaseFont /Times-Bold");
writer.WriteLine(">>");
writer.Flush();
stream.Flush();
xrefs.Add(stream.Position);
writer.WriteLine("6 0 obj");
writer.WriteLine("<<");
writer.WriteLine(" /Type /Font");
writer.WriteLine(" /Subtype /Type1");
writer.WriteLine(" /Encoding /WinAnsiEncoding");
writer.WriteLine(" /BaseFont /Times-Italic");
writer.WriteLine(">>");
writer.Flush();
stream.Flush();
xrefs.Add(stream.Position);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
//Lables
sb.AppendLine("BT");
sb.AppendLine("/F0 15 Tf");
sb.AppendLine("20 TL");
sb.AppendLine("30.0 760.0 Td");
sb.AppendLine("(testlist demo1)' Tj");
sb.AppendLine("ET");
writer.WriteLine("7 0 obj");
writer.WriteLine("<<");
writer.WriteLine(" /Length " + sb.Length);
writer.WriteLine(">>");
writer.WriteLine("stream");
writer.Write(sb.ToString());
writer.WriteLine(" q"); //added
writer.WriteLine(" 156 0 0 272 100 200 cm");
writer.WriteLine(" /Img1 Do");
writer.WriteLine(" Q");
writer.WriteLine("endstream");
writer.WriteLine("endobj");
writer.Flush();
stream.Flush();
dynamic xref_pos = stream.Position;
writer.WriteLine("xref");
writer.WriteLine("1 " + xrefs.Count);
long xref = 0;
foreach (long xref_loopVariable in xrefs)
{
xref = xref_loopVariable;
writer.WriteLine("{0:0000000000} {1:00000} n", xref, 0);
}
// PDF-TRAILER. Every PDF ends with this trailer.
writer.WriteLine("trailer");
writer.WriteLine("<<");
writer.WriteLine(" /Size " + xrefs.Count);
writer.WriteLine(" /Root 1 0 R");
writer.WriteLine(">>");
writer.WriteLine("startxref");
writer.WriteLine(xref_pos);
writer.WriteLine("%%EOF");
}
是的,它工作正常,併產生正常工作在微軟邊緣的PDF,但同時打開相同的文件使用Adobe Reader它給空白輸出..
另一個解決方案我已經嘗試使用PDFDocument,但在那也是我越來越同樣的問題。
請幫幫我。等待迴應。
提前致謝
事情都看在[Rotativa(https://github.com/webgio/Rotativa),它可能會幫助! –
Itextsharp是一個很好的PDF操作庫 – lyz
你的代碼不會生成PostScript,它的PDF有很大的不同!如果您可以生成PostScirpt(Windows本身可以從打印系統執行),那麼Ghostscript可以從中創建一個PDF文件。我無法真正告訴你你製作的PDF文件有什麼問題,因爲你沒有把它放在任何地方。將它張貼在公開的地方,可能我可以告訴你它有什麼問題。 – KenS