文档转换攻略第五弹!国产工具Spire.PDF 2020格式转换全新攻略来啦!

Spire系列文档处理API是国产开发工具中功能可媲美Aspose的强大控件,帮助开发者轻松将文档功能集成到应用程序中。临近年终,小编为您倾情献上Spire.PDF在.NET中格式转换的示例教程,记得收藏哦!

将文档从一种格式转换为另一种格式是Spire.PDF的主要功能之一。这种转换只不过是加载和保存操作的组合。因此,使用Spire.PDF可以将文档从任何受支持的加载格式转换为任何受支持的保存格式。

  • HTML和PDF互转
  • 文本转PDF
  • 图像和PDF互转
  • XPS和PDF互转
  • PDF转DOC
  • PDF转SVG
  • PDF转TIFF

>>你可以点击这里下载Spire.PDF for .NET测试体验。

(篇幅较长,建议收藏阅读)

HTML转换为PDF

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 = "https://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");        }    }}

PDF转换为HTML

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);        }    }}

文本转换为PDF

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);        }    }

图像转换为PDF

//save and launch the filedoc.SaveToFile("image to pdf.pdf");doc.Close();System.Diagnostics.Process.Start("image to pdf.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");        }    }}

XPS转换为PDF

doc.SaveToFile(pdfFile, FileFormat.PDF);

PDF转换为XPS

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");        }    }}

PDF转换为DOC

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");        }    }}

PDF转换为SVG

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 FilesResult.svg", FileFormat.SVG);        }    }}

PDF转换为TIFF

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++;            }        }    }}

如果您有任何疑问或需求,请随时联系客服,我们很高兴为您提供查询和咨询
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年10月22日
下一篇 2020年10月22日

相关推荐

发表回复

登录后才能评论