C#生成PDF总结

(一)C#生成PDF总结

(1)iTextSharp控件对iTextSharp研究还可以表格、文字、各种GDI对象,图片,水印,文字旋转
(2)aspose的控件
(3)PDF Library这个类库(只单纯是有文字的,表格和文字)http://www.codeproject.com/KB/dotnet/PdfLibrary.aspx
(4)直接用.net的RDLC report 就可以啦,to PDF效果很好,也可以对付用户有变数,可以to 其他格式.

(二)iTextSharp生成PDF示列

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace PdfDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 我得第一个Pdf程序
        /// </summary>
        private void CreatePdf()
        {
            string fileName = string.Empty;
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = "我的第一个PDF";
            dlg.DefaultExt = ".pdf";
            dlg.Filter = "Text documents (.pdf)|*.pdf";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                fileName = dlg.FileName;
                Document document = new Document();
                PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
                document.Open();
                iTextSharp.text.Paragraph paragraph = new iTextSharp.text.Paragraph("Hello World");
                document.Add(paragraph);
                document.Close();
            }//end if
        }
        /// <summary>
        /// 设置页面大小、作者、标题等相关信息设置
        /// </summary>
        private void CreatePdfSetInfo()
        {
            string fileName = string.Empty;
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = "我的第一个PDF";
            dlg.DefaultExt = ".pdf";
            dlg.Filter = "Text documents (.pdf)|*.pdf";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                fileName = dlg.FileName;
                //设置页面大小
                iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(216f, 716f);
                pageSize.BackgroundColor = new iTextSharp.text.BaseColor(0xFF, 0xFF, 0xDE);
                //设置边界
                Document document = new Document(pageSize, 36f, 72f, 108f, 180f);
                PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
                // 添加文档信息
                document.AddTitle("PDFInfo");
                document.AddSubject("Demo of PDFInfo");
                document.AddKeywords("Info, PDF, Demo");
                document.AddCreator("SetPdfInfoDemo");
                document.AddAuthor("焦涛");
                document.Open();
                // 添加文档内容
                for (int i = 0; i < 5; i++)
                {
                    document.Add(new iTextSharp.text.Paragraph("Hello World! Hello People! " +
            "Hello Sky! Hello Sun! Hello Moon! Hello Stars!"));
                }
                document.Close();
            }//end if
        }
        /// <summary>
        /// 创建多个Pdf新页
        /// </summary>
        private void CreateNewPdfPage()
        {
            string fileName = string.Empty;
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = "创建多个Pdf新页";
            dlg.DefaultExt = ".pdf";
            dlg.Filter = "Text documents (.pdf)|*.pdf";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                fileName = dlg.FileName;
                Document document = new Document(PageSize.NOTE);
                PdfWriter writer= PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
                document.Open();
                // 第一页
                document.Add(new  iTextSharp.text.Paragraph("PDF1, PDF1, PDF1, PDF1, PDF1"));
                document.Add(new iTextSharp.text.Paragraph("PDF1, PDF1, PDF1, PDF1, PDF1"));
                document.Add(new iTextSharp.text.Paragraph("PDF1, PDF1, PDF1, PDF1, PDF1"));
                document.Add(new iTextSharp.text.Paragraph("PDF1, PDF1, PDF1, PDF1, PDF1"));
                // 添加新页面
                document.NewPage();
                // 第二页
                // 添加第二页内容
                document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));
                document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));
                document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));
                document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));
                document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));
                document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));
                // 添加新页面
                document.NewPage();
                // 第三页
                // 添加新内容
                document.Add(new iTextSharp.text.Paragraph("PDF3, PDF3, PDF3, PDF3, PDF3"));
                document.Add(new iTextSharp.text.Paragraph("PDF3, PDF3, PDF3, PDF3, PDF3"));
                document.Add(new iTextSharp.text.Paragraph("PDF3, PDF3, PDF3, PDF3, PDF3"));
                document.Add(new iTextSharp.text.Paragraph("PDF3, PDF3, PDF3, PDF3, PDF3"));
                // 重新开始页面计数
                document.ResetPageCount();
                // 新建一页
                document.NewPage();
                // 第四页
                // 添加第四页内容
                document.Add(new iTextSharp.text.Paragraph("PDF4, PDF4, PDF4, PDF4, PDF4"));
                document.Add(new iTextSharp.text.Paragraph("PDF4, PDF4, PDF4, PDF4, PDF4"));
                document.Add(new iTextSharp.text.Paragraph("PDF4, PDF4, PDF4, PDF4, PDF4"));
                document.Add(new iTextSharp.text.Paragraph("PDF4, PDF4, PDF4, PDF4, PDF4"));
                document.Close();
            }//end if
        }
        /// <summary>
        /// 生成图片pdf页(pdf中插入图片)
        /// </summary>
        public void ImageDirect()
        {
            string imagePath = AppDomain.CurrentDomain.BaseDirectory + @"Image\1.jpg"; //临时文件路径
            string fileName = string.Empty;
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = "我的第一个PDF";
            dlg.DefaultExt = ".pdf";
            dlg.Filter = "Text documents (.pdf)|*.pdf";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                fileName = dlg.FileName;
                Document document = new Document();
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
                document.Open();
                iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imagePath);
                img.SetAbsolutePosition((PageSize.POSTCARD.Width - img.ScaledWidth) / 2, (PageSize.POSTCARD.Height - img.ScaledHeight) / 2);
                writer.DirectContent.AddImage(img);
                iTextSharp.text.Paragraph p = new iTextSharp.text.Paragraph("Foobar Film Festival", new iTextSharp.text.Font(Font.FontFamily.HELVETICA, 22f));
                p.Alignment = Element.ALIGN_CENTER;
                document.Add(p);
                document.Close();
            }//end if
        }
        private void ReadPdf()
        {
            Console.WriteLine("读取PDF文档");
            try
            {
                // 创建一个PdfReader对象
                PdfReader reader = new PdfReader(@"D:\技术文档\sj\C#线程参考手册.pdf");
                // 获得文档页数
                int n = reader.NumberOfPages;
                // 获得第一页的大小
                iTextSharp.text.Rectangle psize = reader.GetPageSize(1);
                float width = psize.Width;
                float height = psize.Height;
                // 创建一个文档变量
                Document document = new Document(psize, 50, 50, 50, 50);
                // 创建该文档
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"C:\Read.pdf", FileMode.Create));
                // 打开文档
                document.Open();
                // 添加内容
                PdfContentByte cb = writer.DirectContent;
                int i = 0;
                int p = 0;
                Console.WriteLine("一共有 " + n + " 页.");
                while (i < n)
                {
                    document.NewPage();
                    p++;
                    i++;
                    PdfImportedPage page1 = writer.GetImportedPage(reader, i);
                    cb.AddTemplate(page1, .5f, 0, 0, .5f, 0, height / 2);
                    Console.WriteLine("处理第 " + i + " 页");
                    if (i < n)
                    {
                        i++;
                        PdfImportedPage page2 = writer.GetImportedPage(reader, i);
                        cb.AddTemplate(page2, .5f, 0, 0, .5f, width / 2, height / 2);
                        Console.WriteLine("处理第 " + i + " 页");
                    }
                    if (i < n)
                    {
                        i++;
                        PdfImportedPage page3 = writer.GetImportedPage(reader, i);
                        cb.AddTemplate(page3, .5f, 0, 0, .5f, 0, 0);
                        Console.WriteLine("处理第 " + i + " 页");
                    }
                    if (i < n)
                    {
                        i++;
                        PdfImportedPage page4 = writer.GetImportedPage(reader, i);
                        cb.AddTemplate(page4, .5f, 0, 0, .5f, width / 2, 0);
                        Console.WriteLine("处理第 " + i + " 页");
                    }
                    cb.SetRGBColorStroke(255, 0, 0);
                    cb.MoveTo(0, height / 2);
                    cb.LineTo(width, height / 2);
                    cb.Stroke();
                    cb.MoveTo(width / 2, height);
                    cb.LineTo(width / 2, 0);
                    cb.Stroke();
                    BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                    cb.BeginText();
                    cb.SetFontAndSize(bf, 14);
                    cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + ((n / 4) + (n % 4 > 0 ? 1 : 0)), width / 2, 40, 0);
                    cb.EndText();
                }
                // 关闭文档
                document.Close();
            }
            catch (Exception de)
            {
                Console.Error.WriteLine(de.Message);
                Console.Error.WriteLine(de.StackTrace);
            }
        }

        /// <summary>
        /// 创建表格
        /// </summary>
        public void CreateFirstTable()
        {
            string imagePath = AppDomain.CurrentDomain.BaseDirectory + @"Image\1.pm"; //临时文件路径
            string fileName = string.Empty;
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = "我的第一个PDF";
            dlg.DefaultExt = ".pdf";
            dlg.Filter = "Text documents (.pdf)|*.pdf";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                fileName = dlg.FileName;
                Document document = new Document();
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
                document.Open();
                PdfPTable table = new PdfPTable(3);
                PdfPCell cell;
                cell=new PdfPCell(new Phrase("Cell with colspan 3"));
                cell.Colspan = 3;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
                cell.Rowspan = 2;
                table.AddCell(cell);
                table.AddCell("row 1; cell 1");
                table.AddCell("row 1; cell 2");
                table.AddCell("row 2; cell 1");
                table.AddCell("row 2; cell 2");
                document.Add(table);
                document.Close();
            }//end if
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //CreatePdf();
            //CreatePdfPageSize();
            CreateNewPdfPage();
        }
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            CreateFirstTable();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            ImageDirect();
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            ReadPdf();
        }
    }
}

(三)代码下载

代码下载

(三)参考链接

http://www.cnbeta.com/articles/60484.htm 在线导出PDF的好去处
http://bbs.csdn.net/topics/310095053 PDF导出的讨论
http://www.cnblogs.com/EKPK/archive/2009/06/04/1495867.html 用C#制作PDF文件全攻略
http://blog.csdn.net/aasswwe/article/details/7639768
http://blog.sina.com.cn/s/blog_82662ce70100t0s6.html Pdf常见用法
http://www.tuicool.com/articles/nuyAFz HTML生成PDF(c#)
http://stackoverflow.com/questions/tagged/itextsharp itextsharp相关问题
http://www.itextpdf.com/book/examples.php 官方文档,虽然是Java版本的但类库略有不同,在java中一些getFunction和setFunction在C#转为属性,可以作为参考文档。

时间: 2024-08-11 20:00:21

C#生成PDF总结的相关文章

thinkphp整合系列之tcpdf类生成pdf文件

php生成pdf文件的需求是不怎么常见的:当然也是有的: 既然已经整合使用了:那就写篇博客来讲解下吧: 示例项目:http://git.oschina.net/shuaibai123/thinkphp-bjyadmin 一:引入tcpdf /ThinkPHP/Library/Vendor/Tcpdf 把tcpdf整个目录拷到自己的项目中: 二:函数 /Application/Common/Common/function.php /** * 生成pdf * @param  string $html

.NET生成PDF文件

C#未借助第三方组件,自己封装通用类,生成PDF文件. 调用方式: //路径 string path = @"C:\yuannwu22.pdf"; //内容 string strContent = "ddd3232342434d"; new PDFGenerator.PDFGenerator(path, strContent).Create(); 下载地址: http://pan.baidu.com/s/1kTIchev?

【原创】岁月如歌 一款网易歌单生成pdf的软件

介绍 这是一款可以将网易云音乐的歌单中所有歌词输出为pdf的软件. 项目持续维护地址 http://brightguo.com/song-list-to-pdf/ 目前没有搜到相关网易歌单导出为pdf的软件,因此我特地将此软件开发出来免费给大家使用,不清楚会有多少人有这个需求,能帮一个是一个吧~ 本站下载链接(速度较慢)      百度云下载链接 支持操作系统 >= Win7 如有问题欢迎写信给我 [email protected] 使用 软件只有一个界面,将歌单地址复制粘贴进去,点击生成后即可

使用TCPDF插件生成pdf以及pdf的中文处理

目录(?)[+] 多种多样的pdf开发库 WKHTMLTOPDF 2FPDF 3TCPDF 中文问题 做了这么多年项目,以前只是在别人的项目中了解过PHP生成pdf文件,知道并不难,但是涉及到了pdf开发库,首先介绍pdf库. 多种多样的pdf开发库 1.WKHTMLTOPDF wkhtmltopdf是一个很好的解决方案,基本上可以原样输出html页面中的内容,包括:图片/代码高亮部分css/页头/页尾等.有php和命令行方式,大概思路如下: 1) 先获取所有的远程html,然后生成wkhtml

ThinkPHP3.2.3扩展之生成PDF文件(MPDF)

目前是PHP生成PDF文件最好的插件了,今天介绍下在ThinkPHP3.2.3里如何使用. 先安照路径放好如图. 下面是使用方法 public function pdf(){ //引入类库 Vendor('mpdf.mpdf'); //设置中文编码 $mpdf=new \mPDF('zh-cn','A4', 0, '宋体', 0, 0); //html内容 $html='<h1><a name="top"></a>一个PDF文件</h1>

利用ItextPdf、core-renderer-R8 来生成PDF

近期因为工作上的须要,须要做一个简历产品的下载功能,而下载的形式要去为PDF,内容要求为整个简历的内容,并且格式上要求和简历的格式排版时一致的!前期调研.开发,最后測试上线.差点儿相同花了7天的时间.当然,期间主要完毕了主体功能.如今的话,该功能已经相当完好. 以下,我主要是总结下我在这个开发的过程中遇到的问题和总结的心得.希望能帮组有这方面须要的人. 原创文章,转载请注明出处:http://blog.csdn.net/jessonlv 前期调研 前期调研的时候,在网上看了非常多关于转pdf的相

MFC使用Haru free pdf lib生成pdf文件

MFC使用Haru free pdf lib生成pdf文件 我们先创建一个项目MFCPDFDemo,在上面添加一个编辑框和一个按钮: haru free pdf类库下载地址 这里直接下载dll,如果想以静态库的方式使用,得下载源码自己编译.然后把这个dll添加到项目中,怎么添加就不说了吧,还是说一下吧,万一有新手看不懂呢.解压后我们要用到三个东西:libhpdf.dlllibhpdf.libinclude整个文件夹解压后复制libhpdf.dll到项目的release和debug目录下:libh

怎么用PHP在HTML中生成PDF文件

原文:Generate PDF from html using PHP 译文:使用PHP在html中生成PDF 译者:dwqs 利用PHP编码生成PDF文件是一个非常耗时的工作.在早期,开发者使用PHP并借助FPDF来生成PDF文件.但是如今,已经有很多函数库可以使用了,并且能够从你提供的HTML文件生成PDF文档.这让原先耗时的工作变得非常简单了. FPDF是很早就被使用的,其特点如下: FPDF FPDF是一个允许使用纯PHP生成PDF文档的PHP类,换句话说,没有使用PDFlib 函数库.

ITextSharp用来生成 PDF 的一个组件

iTextSharp 是用来生成  PDF 的一个组件,在 1998 年夏天的时候,Bruno Lowagie ,iText 的创作者,参与了学校的一个项目,当时使用 HTML 来生成报告,但是,使用 HTML 打印的效果很不理想.最后,他发现,使用 PDF 可以完美解决打印问题,为了能够在各个系统中使用,iText 组件库诞生了. 网页上面浏览pdf,目前一般是先转成swf格式,再查看. http://sourceforge.net/projects/itextsharp/files/

java文本、表格word转换生成PDF加密文件代码下载

原文:java文本.表格word转换生成PDF加密文件代码下载 代码下载地址:http://www.zuidaima.com/share/1550463239146496.htm 这个实现了PDF加密功能,和一些基本的问题. java文本.表格word转换生成PDF加密文件代码下载,布布扣,bubuko.com