将图像转换为pdf

问题描述 投票:0回答:1

我在寻找什么

我想将几个图像放在PDF文件中,带有ImageMagick

(最好是),如下所示:

    每页图像一个图像,
  • pdf中的每个页面都具有A4大小,
  • 图像大小(以像素为单位)可能会缩小,但永远不会扩大,
  • 图像的可能大小为1240x1753像素(最大DPI目标为150);这样的维度会填充整个A4页面,
  • 如果图像小于上面所述的尺寸,则不会调整大小(是否看起来像缩小图像一样大),
  • 图像必须以每页为中心,
  • 图图像周围没有添加白色像素以使其“适合页面”,
  • 图像必须是JPEG(质量80)才能限制PDF文件大小。
  • 我尝试了什么(一部分)
  • ,到目前为止,我正在使用:
  • convert small.png big1.jpeg big2.jpeg -compress jpeg -quality 80 -resize '1240x1753>' -page A4 out.pdf
  • 结果

那种作品,但是:

小图像不集中:它在页面的左下角,
我的PDF阅读器上没有相同大小的页面(首页较小),

pdfinfo out.pdf | grep -a size

    Page size: 112.441 x 159.118 pts
  • ,但带有从libreoffice生成的A4页,它说
  • Page size: 595.304 x 841.89 pts (A4)
  • 其他尝试
    我尝试使用
    -gravity
    -density
    ,但是在我的测试中产生了奇怪的结果:它们似乎适用于整个PDF,并且使图像在页面外(带有
    -gravity
  • )或我的PDF读取器的一半是一半,或者是我的PDF阅读器,当我首次使用(
-density

)打开它时,使同一文档显得很远,因此我需要Zoom。

-extent

可能被排除在外,因为它在图像中添加了像素(周围)。
我的请求
我可以使用ImageMagick还是
convert
? (或可能与一个或其他一个或其他简单命令行工具一起)
我不是在寻求建议。我有一个问题和一个复杂的工具(ImageMagick)。我很确定该工具可以解决问题,但我不知道该如何使用它。
	
我曾经使用称为

Ghostscript.net

.

的库来进行UPWork作业。
步骤One

下载

ghostscriptsdk

从其官方

website
中安装在Windows Machine中。

步出两

创建了一个新的Visual Studio项目,并安装
image-processing imagemagick pdf-generation batch-processing
1个回答
0
投票
称为

GhostScript.net,然后使用该库中内置的类,称为GhostScriptrasterizer将文档中的所有pdf页面转换为您的PDF页面,以您的首选形式将文档中的所有PDF页面转换为image Files。这是您可以用来实现这一目标的示例代码。

using System; using System.Drawing; using System.Imaging; using System.IO; //add a reference to the pdf rasterizer using GhostScript.NET.Rasterizer; class program{ //entry method for the program static void Main(string args[]){ //declare a relative path to the location of PDF var pdf = @"document.pdf"; //declare a folder to store the images var imageFolder = @"images\\"; //check if the folder exists, if not create it if (!Directory.Exists(imageFolder)) { //create the folder if it does not exist Directory.CreateDirectory(); } //create a new instance of GhostScript Rasterizer using(var rasterizer = new GhostScriptRasterizer()){ //open the pdf doucument using the rasterizer rasterizer.Open(pdf); //iterate through each page while generating //a bitmap object from each for(int i=0;i<rasterizer.PageCount;i++){ //create a new Bitmap from the current page using(Bitmap bmp = rasterizer.GetPage(300,300, i){ //create a full path for the image var outPath = Path.Combine(imageFolder $"Page-{i}.png"); //save the bitmap to the file img.Save(outPath, ImageFormat.Png); //write the console the process completed Console.WriteLine($"Saved: {outPath}"); } } } } }

IT将生成每个页面并保存到位图映像并保存到脚本运行的图像文件夹中。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.