使用 Aspose.PDF 包进行 PDF 和图片间的相互转换,Aspose.PDF 是一个收费组件,但可以申请试用
安装 Aspose.PDF 包
1
| Install-Package Aspose.Pdf
|
申请试用
可以在 Aspose官方网站 申请试用,需要提供可以接收邮件的邮箱,申请通过之后会收到一封包含 Aspose.Pdf.lic 文件下载链接的邮件。
需要将下载的 Aspose.Pdf.lic 文件放到程序根目录。
PDF 转图片
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
public static void ToImage() { License license = new License(); license.SetLicense("Aspose.Pdf.lic");
string path = Directory.GetCurrentDirectory() + "/";
Document pdfDocument = new Document(path + "PDF_To_Image.pdf");
for (int pageCount = 1; pageCount <= 2; pageCount++) { FileStream imageStream = new FileStream(path + "PDF_To_Image/" + pageCount + ".png", FileMode.Create); PngDevice pngDevice = new PngDevice();
pngDevice.Process(pdfDocument.Pages[pageCount], imageStream);
imageStream.Close(); } }
|
图片转 PDF
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
public static void ToPDF() { License license = new License(); license.SetLicense("Aspose.Pdf.lic");
string path = Directory.GetCurrentDirectory() + "/"; Document doc = new Document();
MemoryStream mystream = null;
int count = 2; for (int pageCount = 1; pageCount <= count; pageCount++) { Page page = doc.Pages.Add();
FileStream fs = new FileStream(path + pageCount + ".png", FileMode.Open, FileAccess.Read); byte[] tmpBytes = new byte[fs.Length]; fs.Read(tmpBytes, 0, int.Parse(fs.Length.ToString()));
mystream = new MemoryStream(tmpBytes);
Bitmap b = new Bitmap(mystream);
page.PageInfo.Margin.Bottom = 0; page.PageInfo.Margin.Top = 0; page.PageInfo.Margin.Left = 0; page.PageInfo.Margin.Right = 0;
page.CropBox = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height);
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
page.Paragraphs.Add(image);
image.ImageStream = mystream; } path = path + "Image_To_PDF.pdf"; doc.Save(path);
mystream.Close(); }
|
Source code
PDF.Tools