Upload
Maximum file size: 1 MB. Files accepted: pdf.
Click here to browse files.
fileerrors
Convert to
Source file:
filename
Target file type:
- Demo
- Java
- C# source
This demo shows you how to convert a PDF document to Docx, XPS, HTML, Image, etc.
import com.spire.pdf.FileFormat; import com.spire.pdf.PdfDocument; import com.spire.pdf.conversion.PdfStandardsConverter; import com.spire.pdf.graphics.PdfImageType; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class PdfConvertDemo { public void pdfConversion(String pdfFile,String convertType) throws IOException { String outputFileName = "output"; PdfDocument pdf=new PdfDocument(); pdf.loadFromFile(pdfFile); PdfStandardsConverter standardsConverter = new PdfStandardsConverter(pdfFile); switch (convertType) { case "DOCX": outputFileName += ".docx"; pdf.saveToFile(outputFileName, FileFormat.DOCX); break; case "DOC": outputFileName += ".doc"; pdf.saveToFile(outputFileName, FileFormat.DOC); break; case "XPS": outputFileName += ".xps"; pdf.saveToFile(outputFileName, FileFormat.XPS); break; case "XLSX": outputFileName += ".xlsx"; pdf.saveToFile(outputFileName, FileFormat.XLSX); break; case "POSTSCRIPT": outputFileName += ".ps"; pdf.saveToFile(outputFileName, FileFormat.POSTSCRIPT); break; case "HTML": outputFileName += ".html"; pdf.saveToFile(outputFileName, FileFormat.HTML); break; case "IMAGE": BufferedImage image=null; for (int i = 0; i < pdf.getPages().getCount(); i++) { image= pdf.saveAsImage(i, PdfImageType.Bitmap); ImageIO.write(image,"PNG",new File(outputFileName+i+".png")); } break; case "SVG": outputFileName+=".svg"; pdf.saveToFile(outputFileName, FileFormat.SVG); break; case "TIFF": outputFileName += ".tiff"; pdf.saveToTiff(outputFileName); break; case "PDFA1A": outputFileName += ".pdf"; standardsConverter.toPdfA1A(outputFileName); break; case "PDFA1B": outputFileName += ".pdf"; standardsConverter.toPdfA1B(outputFileName); break; case "PDFA2A": outputFileName += ".pdf"; standardsConverter.toPdfA2A(outputFileName); break; case "PDFA2B": outputFileName += ".pdf"; standardsConverter.toPdfA2B(outputFileName); break; case "PDFA3A": outputFileName += ".pdf"; standardsConverter.toPdfA3A(outputFileName); break; case "PDFA3B": outputFileName += ".pdf"; standardsConverter.toPdfA3B(outputFileName); break; } pdf.close(); } }
using Spire.Pdf; using Spire.Pdf.Conversion; using Spire.Pdf.Graphics; using System; using System.Drawing; using System.Drawing.Imaging; namespace DemoOnlineCode { class PdfConvertDemo { public void PdfConversions(string pdfFile, string convertType) { String outputFileName = "output"; PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile(pdfFile); PdfStandardsConverter standardsConverter = new PdfStandardsConverter(pdfFile); switch (convertType) { case "DOCX": outputFileName += ".docx"; pdf.SaveToFile(outputFileName, FileFormat.DOCX); break; case "DOC": outputFileName += ".doc"; pdf.SaveToFile(outputFileName, FileFormat.DOC); break; case "XPS": outputFileName += ".xps"; pdf.SaveToFile(outputFileName, FileFormat.XPS); break; case "XLSX": outputFileName += ".xlsx"; pdf.SaveToFile(outputFileName, FileFormat.XLSX); break; case "POSTSCRIPT": outputFileName += ".ps"; pdf.SaveToFile(outputFileName, FileFormat.POSTSCRIPT); break; case "HTML": outputFileName += ".html"; pdf.ConvertOptions.SetPdfToHtmlOptions(true, false, 1, false); pdf.SaveToFile(outputFileName, FileFormat.HTML); break; case "IMAGE": Image image = null; for (int i = 0; i < pdf.Pages.Count; i++) { image = pdf.SaveAsImage(i, PdfImageType.Bitmap); image.Save(outputFileName + i + ".png",ImageFormat.Png); } break; case "SVG": outputFileName += ".svg"; pdf.SaveToFile(outputFileName, FileFormat.SVG); break; case "TIFF": outputFileName += ".tiff"; JoinTiffImages(SaveAsImage(pdf), outputFileName, EncoderValue.CompressionLZW); break; case "PDFA1A": outputFileName += ".pdf"; standardsConverter.ToPdfA1A(outputFileName); break; case "PDFA1B": outputFileName += ".pdf"; standardsConverter.ToPdfA1B(outputFileName); break; case "PDFA2A": outputFileName += ".pdf"; standardsConverter.ToPdfA2A(outputFileName); break; case "PDFA2B": outputFileName += ".pdf"; standardsConverter.ToPdfA2B(outputFileName); break; case "PDFA3A": outputFileName += ".pdf"; standardsConverter.ToPdfA3A(outputFileName); break; case "PDFA3B": outputFileName += ".pdf"; standardsConverter.ToPdfA3B(outputFileName); break; } pdf.Close(); } private static Image[] SaveAsImage(PdfDocument document) { Image[] images = new Image[document.Pages.Count]; for (int i = 0; i < document.Pages.Count; i++) { //Use the document.SaveAsImage() method save the pdf as image 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 System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag; EncoderParameters ep = new EncoderParameters(2); ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame); ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.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++; } } } }
No Matter How Big or Small Your Project is,
Any technical question related to our product, contact us at support@e-iceblue.com.
Any question related to the purchase of product, contact us at sales@e-iceblue.com.
If you don't find the function you want, please request a free demo from us.