Spire.Pdf的各种操作总结
Spire.Pdf 的各种操作总结
简介
试验新产品总是给我带来许多挑战,当然这也是⼀个引进创新技术的好⽅法。在这⾥我要跟⼤家分享的是使⽤Spire.Pdf的过程,它是来⾃E-iceblue公司的轻便PDF程序库。
设计情节
我以前经常没事就搞搞PDF。Spire.Pdf是⽤C# 写的PDF组件。他们⾃⼰声称:
pasting Spire.PDF for .NET is a professional PDF component applied to creating, writing, editing, handling and reading PDF files without any external dependencies within .NET application. Using this .NET PDF library, you can implement rich capabilities to  from scratch or process existing PDF documents entirely through C#/VB.NET without installing Adobe Acrobat.
先决条件
⾸先,从这下载它的免费版:
安装下载的软件,结果你会得到两个dll⽂件(Spire.License.dll和 Spire.Pdf.dll),如下图所⽰在你的程序中添加他们的引⽤:
创建PDF并设置它的格式
创建PDF和使⽤Spire.Pdf⼀样简单。这就意味着,仅仅⽤Spire.Pdf写⼏⾏代码就可以实现了。参照以下代码:
//Create a pdf document.
PdfDocument doc = new PdfDocument();
// Create one page
PdfPageBase page = doc.Pages.Add();
//Draw the text
page.Canvas.DrawString("Hello, I'm Created By SPIRE.PDF!",
new PdfFont(PdfFontFamily.TimesRoman, 30f),
pdf转htmlnew PdfSolidBrush(Color.Black), 10, 10);
//Save pdf file.
doc.SaveToFile("MyFirstPDF.pdf");
doc.Close();
以上代码创建了⼀个单页的PDF⽂件
现在我们在同⼀个PDF⽂件中再加点料,像边框啊,⽔印啦,再加⼀些有格式设置的图⽚。我为了让我这篇教程看上去⽐较简洁,就把所有代码附在了⽰例中。它是⼀个包含了所有代码的⼩型windows窗⼝程序,你可以从。
转换成其它格式并设置相应的格式:
基本上每个程序员都被这类的需求困扰过。从⼀种格式转换成另⼀种格式在开发模式中是⼀件痛苦的事情。举例来说,现在有很多程序员会问如何把HTML页⾯转换
为PDF?Spire.Pdf对这个问题给出了简单的解决⽅法。不仅仅如此,SPIRE.PDF还提供了如下的转换:
HTML To PDF
XPS to PDF
PDF to XPS
PDF to Image
HTML到PDF的转换:
我上⾯说的,这是⼏乎每个开发⼈员都需要的功能。⽤ Spire.Pdf的话,转换简直是飞⼀般的感觉。Spire.Pdf包含创建⽅法“LoadFromHTML”,它可以⽤URL做为参数⽽返回⼀个PDF⽂件。
//create PdfDocument instance
PdfDocument doc = new PdfDocument();
//load html from URL
string url = "le";
var thread = new Thread(() =>
{
doc.LoadFromHTML(url, false, true, true);
});
//set to single thread
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
//save to PDF document
doc.SaveToFile("FromHTML.pdf", FileFormat.PDF);
doc.Close();
System.Diagnostics.Process.Start("FromHTML.pdf");
从上⾯的⼏⾏代码中,你可以观察到我尝试从URL中创建⼀个PDF⽂件,只是⼀⾏叫做LoadFromHTML的⽅法就可以实现神奇的转换,下⾯⼏⾏为保存⽂件。下⾯在转换中有趣的事情就是SPIRE.PDF将链接地址的页⾯⾃动转换为PDF链接。
PDF到XPS的转换:
XPS,是另外⼀个Microsoft在2006年开发的流⾏⽂件格式。 Spire.Pdf也提供了将⽬标⽂件保存为XPS格式的选择。看下⾯代码, Spire.Pdf仅仅是加载了PDF⽂件并使⽤SaveToFile⽅法将⽂件保存为需要的格式。
//Input  pdf file
String file = @"G:\FromHTML.pdf";
//open pdf document
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(file);
//convert to xps file.
doc.SaveToFile(@"G:\PDFToXPS.xps", FileFormat.XPS);
doc.Close();
XPS到PDF的转换:
Spire.Pdf也提供了上述⽅法的逆向转换,那就是从XPS到PDF的转换,有所不同的是加载⽂件⽅法的不同。这⾥我们使⽤⽅法LoadFromXPS就可以了。
//open xps document
PdfDocument doc = new PdfDocument();
doc.LoadFromXPS(file);
//convert to pdf file.
doc.SaveToFile("XPSToPDF.pdf");
在Spire.Pdf使⽤枚举FileFormat,我们可以有4种格式的输出⽂件:DOC, HTML, PDF 和XPS。
撷取PDF –从PDF中撷取⽂本/图⽚
在平常⼯作中,我遇到过需要单独从PDF⽂件中撷取图⽚或⽂本的问题。
Spire.Pdf也给出了简洁的解决⽅案。它做的很好的地⽅是,提供⼀个单⾏的代码就是调⽤“that”就搞定了。//Create a pdf document.
PdfDocument doc = new PdfDocument();
// Load the PDF Document
doc.LoadFromFile(@"G:\sample.pdf");
// Image collection to hold
IList<Image> images = new List<Image>();
// Loop thru each pages
foreach (PdfPageBase page in doc.Pages)
{
// Check that page contains any images
if (page.ExtractImages() != null)
{
foreach (Image image in page.ExtractImages())
{
images.Add(image);
}
}
}
//close the document
doc.Close();
//save image
int index = 0;
foreach (Image image in images)
{
String imageFileName = String.Format("Image-{0}.png", index++);
image.Save(imageFileName, ImageFormat.Png);
}
在以上代码中图⽚被保存为png格式,输出界⾯如下图:
从PDF中撷取⽂本也是很简单。每个编程⼈员的职业⽣涯都会⾯对的撷取。Spire.Pdf可以⽤以下代码来解决:
//Create a pdf document.
PdfDocument doc = new PdfDocument();
// Load the PDF Document
doc.LoadFromFile(@"G:\sample.pdf");
// String for hold the extracted text
StringBuilder buffer = new StringBuilder();
foreach (PdfPageBase page in doc.Pages)
{
buffer.Append(page.ExtractText());
}
doc.Close();
//save text
String fileName = "";
File.WriteAllText(fileName, buffer.ToString());
buffer = null;
上⾯的程序⽚段就将⽂本提取出来并⽣成名为
结论
综上所述,换句话说,它⽤简单有效的⽅法处理PDF⽂件。SPIRE.PDF是⼀个很好的解决PDF 的API