如何从Windows Phone 生成PDF文档
我需要从我的Windows Phone应用程序生成PDF。
遗憾的是没有标准的免费的PDF生成库在Windows Phone上运行。 我不得不自己生成PDF,通过直接写入到文件格式。 这竟然是真的很容易! 源代码是在这个文档的底部,并在此链接:
- 下载源代码code.zip [60K,需要VS2012,包括控制台和WP8的应用程序]


为什么要在移动电话上生成PDF
举个例子,我想生成一个PDF,将其上传到用户的SkyDrive的帐户,让用户发送一封电子邮件,一个“分享”链接。 这就是当你点击“分享”按钮,微软自己的PDF阅读器应用程序的工作原理。
其他人认为,一个电话没有业务生成PDF文件,因为它是这样一个代价高昂的操作,你应该不是您的文档上传到您的PDF生成的Web服务器。 这是错误的。 PDF生成,至少对于种报告我产生(两页的非流动文本),是一种非常廉价的手术。 我敢打赌,它的成本更多的电池上传+下载,而不是生成PDF文档。
iTextSharp
http://itextpdf.com/ (AGPL许可证,或者您也可以购买商业许可,可在的NuGet)
iTextSharp的不能在Windows Phone的工作 ,
PDFSharp
http://www.pdfsharp.net/ (MIT许可证,可在的NuGet)
PDFSharp不能在Windows Phone上的工作
PDFJet
http://www.pdfjet.com/os/edition.html (BSD许可证;对不能提供的NuGet)
PDFJet不能在Windows Phone上的工作
PDFClown
http://pdfclown.wordpress.com/ (LGPL许可证;对不能提供的NuGet)
PDFClown不能在Windows Phone上的工作
自己写的PDF文件格式
http://acroeng.adobe.com/wp/?page_id=321 (PDF的文件格式的文档,我用的是1.2版本的PDF规范的)
下面的代码在Windows Phone上的运行得很好,PDF文件格式非常简单,如果你想要做的就是创建简单的报告-用文字,图表&C。 我们需要的是为展示如何创建PDF文件。 因此,这里是我的示例代码! 请注意,我使用一个额外功能“ENC”,这意味着非ASCII从Unicode(在.NET中使用)到WinAnsiEncoding(我选择要使用的PDF编码) - 完整的源代码是从顶部的链接下载这篇文章的。
Private Async Sub Button1_Click(sender As Object , e As RoutedEventArgs ) HandlesButton1.Click
Dim file = Await ApplicationData .Current.LocalFolder.CreateFileAsync( "a.pdf" , Windows.Storage. CreationCollisionOption .ReplaceExisting)
Using stream = Await System.IO. WindowsRuntimeStorageExtensions.OpenStreamForWriteAsync(file),
writer As New IO. StreamWriter (stream, Text. Encoding .UTF8)
Dim xrefs As New List ( Of 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()
stream.Write({&HC7, &HEC, &H8F, &HA2}, 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)
Dim sb As New Text. 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( "(ol" & enc( "é" ) & ") '" )
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() : Dim xref_pos = stream.Position
writer.WriteLine( "xref" )
writer.WriteLine( "1 " & xrefs.Count)
For Each xref In xrefs
writer.WriteLine( "{0:0000000000} {1:00000} n" , xref, 0)
Next
' 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" )
End Using
Await Windows.System. Launcher .LaunchFileAsync(file)
End Sub
如何从Windows Phone 生成PDF文档的更多相关文章
- 利用Java动态生成 PDF 文档
利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...
- DocFX生成PDF文档
使用DocFX生成PDF文档,将在线文档转换为PDF离线文档. 关于DocFX的简单介绍使用DocFX生成文档 使用docfx 命令 1.下载 https://github.com/dotnet/do ...
- ireport图形化界面生成pdf文档
一.ireport软件安装 1.下载软件的官网 https://community.jaspersoft.com/project/ireport-designer/releases 2.安装软件 ...
- Aspose.Words操作word生成PDF文档
Aspose.Words操作word生成PDF文档 using Aspose.Words; using System; using System.Collections.Generic; using ...
- 使用PHP生成PDF文档
原文:使用PHP生成PDF文档 实际工作中,我们要使用PHP动态的创建PDF文档,目前有许多开源的PHP创建PDF的类库,今天我给大家来介绍一款优秀的PDF库,它就是TCPDF,TCPDF是一个用于快 ...
- qt 利用 HTML 生成PDF文档,不能显示jpg图片
利用 QPrinter 和html 生成 pdf文档 其中用html语句有显示图片的语句 但只能显示png格式的图片,不能显示jpg格式图片. 经过排查:语法,文件路径等都正确,最终在stack ov ...
- 自动把动态的jsp页面(或静态html)生成PDF文档,并且上传至服务器
置顶2017年11月06日 14:41:04 阅读数:2311 这几天,任务中有一个难点是把一个打印页面自动给生成PDF文档,并且上传至服务器,然而公司框架只有手动上传文档,打印时可以保存为PDF在本 ...
- Spring Boot集成JasperReports生成PDF文档
由于工作需要,要实现后端根据模板动态填充数据生成PDF文档,通过技术选型,使用Ireport5.6来设计模板,结合JasperReports5.6工具库来调用渲染生成PDF文档.本人文采欠缺,写作能力 ...
- 使用PHP类TCPDF生成PDF文档
转自:http://www.blhere.com/1180.html 这两天遇到一个项目中,需要php自动处理生成pdf文档.在网上找了好几个类,最后决定使用TCPDF,使用的时候真是发现这个类真是强 ...
随机推荐
- 将ecshop中的session机制重写,从DB移植到Memcache中去
<?php if (!defined('IN_ECS')) { die('Hacking attempt'); } /*------------------------------------- ...
- (转载)SQL中导入图片
SQL中导入图片 分类: 论坛精贴 2006-05-10 12:07 398人阅读 评论(0) 收藏 举报 sqlimage服务器insertlogingo 1.建立过程CREATE PROCEDUR ...
- java JDBC操作MySQL数据库
一,首先在MYSQL建立一个数据库,例如Geek99DB: create database Geek99DB; use Geek99DB; 然后建立一个表CustomerTab: create tab ...
- 2014年度辛星css教程夏季版第六节
这一节我们就要讲到布局了,其实布局本身特别简单,但是要合理的布好局就不那么简单了,就像我们写文章一样,写一篇文章非常简单,但是要写一篇名著就很难了,这需要我们扎实的功底和对文学的理解,但是,千里之行, ...
- 【spring配置】 一组配置文件引出的问题
applicationContext.xml: <?xml version="1.0" encoding="UTF-8"?> <beans x ...
- JVM里面hashtable和hashmap实现原理
JVM里面hashtable和hashmap实现原理 文章分类:Java编程 转载 在hashtable和hashmap是java里面常见的容器类, 是Java.uitl包下面的类, 那么Hash ...
- JQ+AJAX实现多级联动
利用JQ与AJAX实现三级联动实现的效果: 当前两级改变时,后边一级或两级都会改变: 使用的数据库: html代码: <!doctype html> <html lang=" ...
- Quartz Scheduler 开发指南(1)
Quartz Scheduler 开发指南(1) 原文地址:http://www.quartz-scheduler.org/generated/2.2.2/html/qtz-all/ 实例化调度程序( ...
- float([x]): 将一个字符串或数转换为浮点数。如果无参数将返回0.0
float([x]): 将一个字符串或数转换为浮点数.如果无参数将返回0.0 >>> float(12) 12.0 >>> float(-122) -122.0 & ...
- 从某一日期开始过day天的日期
一个SX问我的,我就写了写......从2010.1.1开始,给了一组测试数据3的话输出2010.1.4星期1,所以说2010.1.1是星期五,总星期就是 (day+5)%7==0?7:(day+5) ...