原創(chuàng)|使用教程|編輯:李顯亮|2020-11-25 10:43:00.417|閱讀 363 次
概述:Spire系列文檔處理API是國產(chǎn)開發(fā)工具中功能可媲美Aspose的強大控件,幫助開發(fā)者輕松將文檔功能集成到應用程序中。臨近年終,小編為您傾情獻上Spire.PDF在.NET中格式轉(zhuǎn)換的示例教程,記得收藏哦!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
將文檔從一種格式轉(zhuǎn)換為另一種格式是Spire.PDF的主要功能之一。這種轉(zhuǎn)換只不過是加載和保存操作的組合。因此,使用Spire.PDF可以將文檔從任何受支持的加載格式轉(zhuǎn)換為任何受支持的保存格式。
本文整理了包括在C#中以下文件格式的轉(zhuǎn)換指南,希望對您有所幫助:
>>你可以點擊這里下載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 = "http://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++;
}
}
}
}
如果您有任何疑問或需求,請隨時,我們很高興為您提供查詢和咨詢。 本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn