当前位置:网站首页>【C#/VB.NET】 将PDF转为SVG/Image, SVG/Image转PDF
【C#/VB.NET】 将PDF转为SVG/Image, SVG/Image转PDF
2022-07-03 06:04:00 【xuhss_com】
优质资源分享
学习路线指引(点击解锁) | 知识定位 | 人群定位 |
---|---|---|
🧡 Python实战微信订餐小程序 🧡 | 进阶级 | 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。 |
Python量化交易实战 | 入门级 | 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统 |
SVG是一种图形文件格式,它的英文全称为Scalable Vector Graphics,意思为可缩放的矢量图形。它在放大或者改变尺寸的情况下其图形质量不会有所损失,且与 JPG 和 GIF 图像比起来,尺寸更小,且可压缩性更强。本文将介绍如何实现将PDF文档与SVG/Image相互转换的方法。经过综合对比之后,最后筛选出这一方法。此方法操作起来比较方便且代码量较少。下面是我整理的详细步骤及C#/VB.NET代码供大家参考。
**类库引入及代码思路:**本次功能测试中,使用到的是 Free Spire.PDF for .NET。Spire.PDF.dll文件的引入方法如下:方法1:将 Free Spire.PDF for .NET 下载到本地,解压,安装。安装完成后,找到安装路径下BIN文件夹中的Spire.PDF.dll。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序。方法2:通过 NuGet 安装。可通过以下2种方法安装:(1)可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“Free Spire.PDF”,点击“安装”。等待程序安装完成。(2)将以下内容复制到PM控制台安装。Install-Package FreeSpire.PDF -Version 8.2.0
将PDF转为SVG格式
具体步骤:
- 创建 PdfDocument 类的对象。
- 调用 PdfDocument.LoadFromFile() 方法加载 PDF 文档。
- 通过 PdfDocument.SaveToFile() 方法保存为 SVG 格式。
完整代码:
【C#】
using System;
using Spire.Pdf;
namespace PdfToSVG
{
class Program
{
static void Main(string[] args)
{
//新建并加载PDF文档
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Sample.pdf");
//保存为SVG格式
doc.SaveToFile("ToSVG-result.svg", FileFormat.SVG);
doc.Close();
}
}
}
【VB.NET】
Imports System
Imports Spire.Pdf
Namespace PdfToSVG
Class Program
Private Shared Sub Main(ByVal args() As String)
'新建并加载PDF文档
Dim doc As PdfDocument = New PdfDocument
doc.LoadFromFile("Sample.pdf")
'保存为SVG格式
doc.SaveToFile("ToSVG-result.svg", FileFormat.SVG)
doc.Close
End Sub
End Class
End Namespace
效果图:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RE7FnCwX-1655355097026)(https://img2022.cnblogs.com/blog/2859233/202206/2859233-20220616093937809-970468124.png)]
将SVG转为PDF格式
具体步骤:
- 创建一个 PdfDocument 对象。
- 用PdfDocument.LoadFromSvg()方法加载SVG文档
- 用PdfDocument.SaveToFile()方法保存为PDF文档
完整代码:
【C#】
using Spire.Pdf;
using System;
namespace SVGToPdf
{
class Program
{
static void Main(string[] args)
{
//创建一个 PdfDocument 对象.
PdfDocument doc = new PdfDocument();
//加载文档.
doc.LoadFromSvg("ToSVG-result.svg");
//保存文档.
String result = "SVgToPDF\_out.pdf";
doc.SaveToFile(result);
}
}
}
【VB.NET】
Imports Spire.Pdf
Imports System
Namespace SVGToPdf
Class Program
Private Shared Sub Main(ByVal args() As String)
'创建一个 PdfDocument 对象.
Dim doc As PdfDocument = New PdfDocument
'加载文档.
doc.LoadFromSvg("ToSVG-result.svg")
'保存文档
Dim result As String = "SVgToPDF\_out.pdf"
doc.SaveToFile(result)
End Sub
End Class
End Namespace
效果图:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Zh3d1UUe-1655355097030)(https://img2022.cnblogs.com/blog/2859233/202206/2859233-20220616094234762-389225572.png)]
将PDF转为图像
具体步骤:
- 添加PDF文件
- 用PdfDocument.LoadFromFile()方法加载PDF文件
- 用Image.Save()方法保存为图片
完整代码:
【C#】
using System;
using System.Drawing;
using Spire.Pdf;
namespace ToImage
{
class Program
{
static void Main(string[] args)
{
//新建并加载PDF文档
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Sample1.pdf");
//保存为图片
for (int i = 0; i < doc.Pages.Count; i++)
{
String fileName = String.Format("ToImage-img-{0}.png", i);
using (Image image = doc.SaveAsImage(i, 300, 300))
{
image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
}
}
doc.Close();
}
}
}
【VB.NET】
Imports System
Imports System.Drawing
Imports Spire.Pdf
Namespace ToImage
Class Program
Private Shared Sub Main(ByVal args() As String)
'新建并加载PDF文档
Dim doc As PdfDocument = New PdfDocument
doc.LoadFromFile("Sample1.pdf")
'保存为图片
Dim i As Integer = 0
Do While (i < doc.Pages.Count)
Dim fileName As String = String.Format("ToImage-img-{0}.png", i)
Dim image As Image = doc.SaveAsImage(i, 300, 300)
image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png)
i = (i + 1)
Loop
doc.Close
End Sub
End Class
End Namespace
效果图:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8ZNDZRhQ-1655355097031)(https://img2022.cnblogs.com/blog/2859233/202206/2859233-20220616095013266-1072419986.png)]
将图像转为PDF格式
具体步骤:
- 创建一个添加了section和页面的 pdf 文档。
- 用PdfImage.FromFile()方法加载图片
- 在 PDF 中设置图像的显示位置和大小
- 通过PdfImage.PhysicalDimension属性获取图片大小
- 使用 PdfPage.Canvas.DrawImage() 方法在第一页 (0, 30) 处绘制 PdfImage 对象
- 使用 PdfDocument.SaveToFile() 方法将文档保存为 PDF 文件
完整代码:
【C#】
using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace ImageToPdf
{
class Program
{
static void Main(string[] args)
{
//创建一个添加了section和页面的 pdf 文档.
PdfDocument pdf = new PdfDocument();
PdfSection section = pdf.Sections.Add();
PdfPageBase page = pdf.Pages.Add();
//加载图片
PdfImage image = PdfImage.FromFile("image5.png");
//在 PDF 中设置图像的显示位置和大小
//调整图片大小以适合页面宽度
float widthFitRate = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width;
float heightFitRate = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height;
float fitRate = Math.Max(widthFitRate, heightFitRate);
//获取图片大小
float fitWidth = image.PhysicalDimension.Width / fitRate;
float fitHeight = image.PhysicalDimension.Height / fitRate;
//绘图
page.Canvas.DrawImage(image, 0, 30, fitWidth, fitHeight);
string output = "image.pdf";
pdf.SaveToFile(output);
}
}
}
【VB.NET】
Imports System
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Namespace ImageToPdf
Class Program
Private Shared Sub Main(ByVal args() As String)
' 创建一个添加了section和页面的 pdf 文档.
Dim pdf As PdfDocument = New PdfDocument
Dim section As PdfSection = pdf.Sections.Add
Dim page As PdfPageBase = pdf.Pages.Add
'加载图片
Dim image As PdfImage = PdfImage.FromFile("image5.png")
'在 PDF 中设置图像的显示位置和大小
'调整图片大小以适合页面宽度
Dim widthFitRate As Single = (image.PhysicalDimension.Width / page.Canvas.ClientSize.Width)
Dim heightFitRate As Single = (image.PhysicalDimension.Height / page.Canvas.ClientSize.Height)
Dim fitRate As Single = Math.Max(widthFitRate, heightFitRate)
' 获取图片大小
Dim fitWidth As Single = (image.PhysicalDimension.Width / fitRate)
Dim fitHeight As Single = (image.PhysicalDimension.Height / fitRate)
'绘图
page.Canvas.DrawImage(image, 0, 30, fitWidth, fitHeight)
Dim output As String = "image.pdf"
pdf.SaveToFile(output)
End Sub
End Class
End Namespace
效果图:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8kVwhqf5-1655355097032)(https://img2022.cnblogs.com/blog/2859233/202206/2859233-20220616095305046-2077878358.png)]
**注意:**测试代码中的文件路径为程序Debug路径,仅供参考,文件路径可自定义为其他路径。
边栏推荐
- Disruptor learning notes: basic use, core concepts and principles
- Oracle database synonym creation
- Bernoulli distribution, binomial distribution and Poisson distribution, and the relationship between maximum likelihood (incomplete)
- It is said that the operation and maintenance of shell scripts are paid tens of thousands of yuan a month!!!
- 【系统设计】邻近服务
- [teacher Zhao Yuqiang] kubernetes' probe
- BeanDefinitionRegistryPostProcessor
- ODL framework project construction trial -demo
- Kubernetes notes (III) controller
- Project summary --2 (basic use of jsup)
猜你喜欢
Cesium 点击获取模型表面经纬度高程坐标(三维坐标)
Interesting research on mouse pointer interaction
Redis cluster creation, capacity expansion and capacity reduction
Use abp Zero builds a third-party login module (I): Principles
Kubesphere - set up redis cluster
[teacher Zhao Yuqiang] Alibaba cloud big data ACP certified Alibaba big data product system
Cesium 点击获三维坐标(经纬度高程)
[teacher Zhao Yuqiang] index in mongodb (Part 2)
Simple solution of small up main lottery in station B
技术管理进阶——你了解成长的全貌吗?
随机推荐
Understand the first prediction stage of yolov1
Beandefinitionregistrypostprocessor
[teacher Zhao Yuqiang] index in mongodb (Part 1)
Decision tree of machine learning
Leetcode problem solving summary, constantly updating!
Leetcode solution - 02 Add Two Numbers
23 design models
[teacher Zhao Yuqiang] Cassandra foundation of NoSQL database
Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)
Understand expectations (mean / estimate) and variances
Advanced technology management - do you know the whole picture of growth?
Simple handwritten ORM framework
Difference between shortest path and minimum spanning tree
[video of Teacher Zhao Yuqiang's speech on wot] redis high performance cache and persistence
理解 期望(均值/估计值)和方差
The server data is all gone! Thinking caused by a RAID5 crash
1. 两数之和
Luogu problem list: [mathematics 1] basic mathematics problems
What's the difference between using the Service Worker Cache API and regular browser cache?
表达式的动态解析和计算,Flee用起来真香