Tab 1
此演示向您展示如何将 PDF 文档转换为 Docx、XPS、HTML、图像等。
Upload
Maximum file size: 1 MB. Files accepted: pdf.
Click here to browse files.
fileerrors
Convert to
Source file:
filename
Target file type:
如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo。
如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。。
Tab 2
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();
}
}
Tab 3
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++;
}
}
}
}