我在vb.Net中找到了一个生成PDF文本的代码,我尝试在C#中翻译它并且它有效 How to generate PDF on Windows Phone in VB or C# 但是文本的问题很长,就像一个段落,所以它不会进入下一行,它会显示一行,而pdf上只显示6或7个单词 这就是它的表现方式
代码是:
private async void EditButton_Click(object sender, RoutedEventArgs e)
{
listitem = (e.OriginalSource as MenuFlyoutItem).DataContext as WritePadFileContent;
//MessageDialog messageDialog = new MessageDialog(listitem.Name.ToString());
//await messageDialog.ShowAsync();
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(listitem.Name.ToString()+".pdf", CreationCollisionOption.GenerateUniqueName);
using (Stream stream = await WindowsRuntimeStorageExtensions.OpenStreamForWriteAsync(file))
{
var writer = new StreamWriter(stream, Encoding.UTF8);
List<long> xrefs = new List<long>();
// PDF-HEADER
writer.WriteLine("%PDF-1.2");
// PDF-BODY. Convention is to start with a 4-byte binary comment
// so everyone recognizes the pdf as binary. Then the file has
// a load of numbered objects, #1..#7 in this case
writer.Write("%"); writer.Flush();
byte[] vbbyte = new byte[4] { 0, 0, 0, 0 };
stream.Write(vbbyte, 0, 4);
stream.Flush();
writer.WriteLine("");
// #1; catalog - the overall container of the entire PDF
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");
// #2; page-list - we have only one child page
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");
// #3; page - this is our page. We specify size, font resources, and the contents
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");
// #4, #5, #6; three font resources, all using fonts that are built into all PDF-viewers
// We//re going to use WinAnsi character encoding, defined below.
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(">>");
// #7; contents of page. This is written in postscript, fully described in
// chapter 8 of the PDF 1.2 reference manual.
writer.Flush(); stream.Flush(); xrefs.Add(stream.Position);
var sb = new StringBuilder();
sb.AppendLine("BT"); // BT = begin text object, with text-units the same as userspace-units
sb.AppendLine("/F0 40 Tf"); // Tf = start using the named font "F0" with size "40"
sb.AppendLine("40 TL"); // TL = set line height to "40"
sb.AppendLine("230.0 400.0 Td");// Td = position text point at coordinates "230.0", "400.0"
sb.AppendLine("(Hello all)"); // Apostrophe = print the text, and advance to the next line
sb.AppendLine("/F2 20 Tf");//
sb.AppendLine("20 TL");//
sb.AppendLine("0.0 0.2 1.0 rg");// rg = set fill color to RGB("0.0", "0.2", "1.0")
//sb.AppendLine("(olaaaa" + "é" + ") '");
//sb.AppendLine("(olaaaa" + "é" + ") '");
sb.AppendLine("(" + listitem.Description.ToString() + ") '");
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("endstream");
writer.WriteLine("endobj"); ;
// PDF-XREFS. This part of the PDF is an index table into every object #1..#7
// that we defined.
writer.Flush(); stream.Flush(); var xref_pos = stream.Position;
writer.WriteLine("xref");
writer.WriteLine("1 " + xrefs.Count);
foreach (object xref in xrefs)
{
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");
writer.Flush(); stream.Flush();
}
await Launcher.LaunchFileAsync(file);
}
因此,如果有其他代码可以帮助我使用Windows Phone 8.1 C#或其他示例。 此代码添加pdf文件的名称
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(listitem.Name.ToString()+".pdf", CreationCollisionOption.GenerateUniqueName);
此代码允许我将描述添加到pdf
中sb.AppendLine("(" + listitem.Description.ToString() + ") '");
答案 0 :(得分:0)
有了这个最后的代码,我被困住了,因为它是用postscript编写的,所以无法使用它,我无法找到与第8章相关的PDF 1.2参考手册。
但我尝试使用另一个库,它可以与WP8.1 C#一起使用 Xfinium是一个简单的库,因此您最常下载库Xfinium 这是代码的屏幕截图(第二个按钮)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Xfinium.Pdf;
using Xfinium.Pdf.Actions;
using Xfinium.Pdf.Core;
using Xfinium.Pdf.Graphics;
using Xfinium.Pdf.Graphics.FormattedContent;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641
namespace App40
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// TODO: Prepare page for display here.
// TODO: If your application contains multiple pages, ensure that you are
// handling the hardware Back button by registering for the
// Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
// If you are using the NavigationHelper provided by some templates,
// this event is handled for you.
}
private void Button_Click(object sender, RoutedEventArgs e)
{
}
//this is a tutrila can help you with simple text
private async void Button_Click1(object sender, RoutedEventArgs e)
{
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("a.pdf", CreationCollisionOption.ReplaceExisting);
using (Stream stream = await WindowsRuntimeStorageExtensions.OpenStreamForWriteAsync(file))
{
PdfFixedDocument document = new PdfFixedDocument();
PdfPage page = document.Pages.Add();
// Create a standard font with Helvetica face and 24 point size
PdfStandardFont helvetica = new PdfStandardFont(PdfStandardFontFace.Helvetica, 24);
// Create a solid RGB red brush.
PdfBrush brush = new PdfBrush(PdfRgbColor.Red);
// Draw the text on the page.
string cha = "title";
page.Graphics.DrawString(cha, helvetica, brush, 250, 50);
// Draw the text on the page. max tableau 50
string ch = "azj aeiajiahfioe foiehfioehfiehfie feifjaepfjaepofjaepo fpaejfpeafjaefaefhevpzevje vjepzvihzev zep";
page.Graphics.DrawString(ch, helvetica, brush, 10, 100);
document.Save(stream);
}
await Launcher.LaunchFileAsync(file);
//FileStream input = File.OpenRead("optionalcontent-src.pdf");
//PdfFile file = new PdfFile(input);
}
//this is a tutrila can help you with paragraph block
private async void Button_Click2(object sender, RoutedEventArgs e)
{
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("a.pdf", CreationCollisionOption.ReplaceExisting);
using (Stream stream = await WindowsRuntimeStorageExtensions.OpenStreamForWriteAsync(file))
{
string paragraph2Block1Text = "The development style is based on fixed document model, where each page is created as needed " +
"and content is placed at fixed position using a grid based layout.\r\n" +
"This gives you access to all PDF features, whether you want to create a simple file " +
"or you want to create a transparency knockout group at COS level, and lets you build more complex models on top of the library.";
PdfStandardFont textFont = new PdfStandardFont(PdfStandardFontFace.TimesRoman, 12);
PdfFormattedParagraph paragraph2 = new PdfFormattedParagraph();
paragraph2.SpacingAfter = 3;
paragraph2.FirstLineIndent = 10;
paragraph2.HorizontalAlign = PdfStringHorizontalAlign.Justified;
paragraph2.LineSpacingMode = PdfFormattedParagraphLineSpacing.Multiple;
paragraph2.LineSpacing = 1.2;
paragraph2.Blocks.Add(new PdfFormattedTextBlock(paragraph2Block1Text, textFont));
PdfFormattedContent formattedContent = new PdfFormattedContent();
formattedContent.Paragraphs.Add(paragraph2);
PdfFixedDocument document = new PdfFixedDocument();
PdfPage page = document.Pages.Add();
page.Graphics.DrawFormattedContent(formattedContent, 50, 50, 500, 700);
page.Graphics.CompressAndClose();
document.Save(stream);
}
await Launcher.LaunchFileAsync(file);
}
}
}