当前位置:网站首页>【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路径,仅供参考,文件路径可自定义为其他路径。
边栏推荐
- Leetcode problem solving summary, constantly updating!
- Jedis source code analysis (II): jediscluster module source code analysis
- The most responsible command line beautification tutorial
- Apple submitted the new MAC model to the regulatory database before the spring conference
- Cesium entity(entities) 实体删除方法
- pytorch DataLoader实现miniBatch(未完成)
- Simple understanding of ThreadLocal
- 1. 两数之和
- PMP笔记记录
- MySQL帶二進制的庫錶導出導入
猜你喜欢

Code generator - single table query crud - generator

Solve the 1251 client does not support authentication protocol error of Navicat for MySQL connection MySQL 8.0.11

Core principles and source code analysis of disruptor

Cesium Click to obtain the longitude and latitude elevation coordinates (3D coordinates) of the model surface
![[teacher Zhao Yuqiang] use Oracle's tracking file](/img/0e/698478876d0dbfb37904d7b9ff9aca.jpg)
[teacher Zhao Yuqiang] use Oracle's tracking file

The most responsible command line beautification tutorial

项目总结--2(Jsoup的基本使用)

pytorch 搭建神经网络最简版

Kubernetes notes (VIII) kubernetes security

Advanced technology management - do you know the whole picture of growth?
随机推荐
Pytorch builds the simplest version of neural network
Interesting research on mouse pointer interaction
Oauth2.0 - Introduction and use and explanation of authorization code mode
.NET程序配置文件操作(ini,cfg,config)
Intel's new GPU patent shows that its graphics card products will use MCM Packaging Technology
Oauth2.0 - user defined mode authorization - SMS verification code login
智牛股--03
Click cesium to obtain three-dimensional coordinates (longitude, latitude and elevation)
从 Amazon Aurora 迁移数据到 TiDB
BeanDefinitionRegistryPostProcessor
Maximum likelihood estimation, divergence, cross entropy
What's the difference between using the Service Worker Cache API and regular browser cache?
88. Merge two ordered arrays
ODL framework project construction trial -demo
In depth analysis of kubernetes controller runtime
Cesium 点击获取模型表面经纬度高程坐标(三维坐标)
Solve the 1251 client does not support authentication protocol error of Navicat for MySQL connection MySQL 8.0.11
Redis cluster creation, capacity expansion and capacity reduction
SVN分支管理
Decision tree of machine learning