原创|使用教程|编辑:李显亮|2020-11-25 10:43:00.417|阅读 363 次
概述:Spire系列文档处理API是国产开发工具中功能可媲美Aspose的强大控件,帮助开发者轻松将文档功能集成到应用程序中。临近年终,小编为您倾情献上Spire.PDF在.NET中格式转换的示例教程,记得收藏哦!
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
将文档从一种格式转换为另一种格式是Spire.PDF的主要功能之一。这种转换只不过是加载和保存操作的组合。因此,使用Spire.PDF可以将文档从任何受支持的加载格式转换为任何受支持的保存格式。
本文整理了包括在C#中以下文件格式的转换指南,希望对您有所帮助:
>>你可以点击这里下载Spire.PDF for .NET测试体验。
(篇幅较长,建议收藏阅读)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Spire.Pdf;
using System.Drawing;
using Spire.Pdf.Widget;
using Spire.Pdf.Fields;
using System.Threading;
using Spire.Pdf.HtmlConverter;
namespace PDF
{
class Program
{
static void Main(string[] args)
{
//Create a pdf document.
PdfDocument doc = new PdfDocument();
PdfPageSettings setting = new PdfPageSettings();
setting.Size = new SizeF(1000,1000);
setting.Margins = new Spire.Pdf.Graphics.PdfMargins(20);
PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
htmlLayoutFormat.IsWaiting = true;
String url = "//www.wikipedia.org/";
Thread thread = new Thread(() =>
{ doc.LoadFromHTML(url, false, false, false, setting,htmlLayoutFormat); });
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
//Save pdf file.
doc.SaveToFile("output-wiki.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("output-wiki.pdf");
}
}
}
using Spire.Pdf;
namespace ConvertPDFtoHtml
{
class Program
{
static void Main(string[] args)
{
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Test.pdf");
pdf.SaveToFile("Result.html", FileFormat.HTML);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Pdf;
using System.IO;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace TexttoPDF
{
class Program
{
static void Main(string[] args)
{
string text = File.ReadAllText("TestDocument.txt");
PdfDocument doc = new PdfDocument();
PdfSection section = doc.Sections.Add();
PdfPageBase page = section.Pages.Add();
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 11);
PdfStringFormat format = new PdfStringFormat();
format.LineSpacing = 20f;
PdfBrush brush = PdfBrushes.Black;
PdfTextWidget textWidget = new PdfTextWidget(text, font, brush);
float y = 0;
PdfTextLayout textLayout = new PdfTextLayout();
textLayout.Break = PdfLayoutBreakType.FitPage;
textLayout.Layout = PdfLayoutType.Paginate;
RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize);
textWidget.StringFormat = format;
textWidget.Draw(page, bounds, textLayout);
doc.SaveToFile("TxtToPDf.pdf", FileFormat.PDF);
}
}
//save and launch the file
doc.SaveToFile("image to pdf.pdf");
doc.Close();
System.Diagnostics.Process.Start("image to pdf.pdf");
using Spire.Pdf;
using System.Drawing;
using System.Drawing.Imaging;
namespace PDFtoImage
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("sample.pdf");
Image bmp = doc.SaveAsImage(0);
Image emf = doc.SaveAsImage(0, Spire.Pdf.Graphics.PdfImageType.Metafile);
Image zoomImg = new Bitmap((int)(emf.Size.Width * 2), (int)(emf.Size.Height * 2));
using (Graphics g = Graphics.FromImage(zoomImg))
{
g.ScaleTransform(2.0f, 2.0f);
g.DrawImage(emf, new Rectangle(new Point(0, 0), emf.Size), new Rectangle(new Point(0, 0), emf.Size), GraphicsUnit.Pixel);
}
bmp.Save("convertToBmp.bmp", ImageFormat.Bmp);
System.Diagnostics.Process.Start("convertToBmp.bmp");
emf.Save("convertToEmf.png", ImageFormat.Png);
System.Diagnostics.Process.Start("convertToEmf.png");
zoomImg.Save("convertToZoom.png", ImageFormat.Png);
System.Diagnostics.Process.Start("convertToZoom.png");
}
}
}
doc.SaveToFile(pdfFile, FileFormat.PDF);
using System;
using System.Collections.Generic;
using System.Text;
using Spire.Pdf;
namespace ConvertPdfToXps
{
class Program
{
static void Main(string[] args)
{
// Instatate an object of Spire.Pdf.PdfDocument
PdfDocument doc = new PdfDocument();
// Load PDF document
doc.LoadFromFile("sample.pdf");
// Save it to XPS format
doc.SaveToFile("sample.xps", FileFormat.XPS);
doc.Close();
System.Diagnostics.Process.Start("sample.xps");
}
}
}
using Spire.Pdf;
namespace ConvertPDFtoDoc
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("test.pdf");
doc.SaveToFile("PDFtoDoc.doc", FileFormat.DOC);
System.Diagnostics.Process.Start("PDFtoDoc.doc");
}
}
}
using Spire.Pdf;
namespace Convert_PDF_to_SVG
{
class Program
{
static void Main(string[] args)
{
PdfDocument document = new PdfDocument();
document.LoadFromFile("Test.pdf");
document.SaveToFile(@"E:\Program Files\Result.svg", FileFormat.SVG);
}
}
}
using System;
using System.Drawing;
using System.Drawing.Imaging;
using Spire.Pdf;
namespace SavePdfAsTiff
{
class Program
{
static void Main(string[] args)
{
PdfDocument document = new PdfDocument();
document.LoadFromFile(@"01.pdf");
JoinTiffImages(SaveAsImage(document),"result.tiff",EncoderValue.CompressionLZW);
System.Diagnostics.Process.Start("result.tiff");
}
private static Image[] SaveAsImage(PdfDocument document)
{
Image[] images = new Image[document.Pages.Count];
for (int i = 0; i < document.Pages.Count; i++)
{
images[i] = document.SaveAsImage(i);
}
return images;
}
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int j = 0; j < encoders.Length; j++)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
}
public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
{
//use the save encoder
Encoder enc = Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);
Image pages = images[0];
int frame = 0;
ImageCodecInfo info = GetEncoderInfo("image/tiff");
foreach (Image img in images)
{
if (frame == 0)
{
pages = img;
//save the first frame
pages.Save(outFile, info, ep);
}
else
{
//save the intermediate frames
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
pages.SaveAdd(img, ep);
}
if (frame == images.Length - 1)
{
//flush and close.
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}
}
}
}
如果您有任何疑问或需求,请随时,我们很高兴为您提供查询和咨询。 本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn