前言
之前有透過 Ghostscript 及 .NET 封裝它的.NET套件 Ghostscript.NET 來將 PDF 檔案轉成 Tiff 檔。
最近有朋友詢問當字型為 標楷體 時,轉出的圖檔卻會破碎掉,是否有什麼解法呢?
解法
原本程式是透過 GhostscriptRasterizer 去轉換,而我原本的 Ghostscript 為 9.54 版本,PDF 中有 標楷體 轉出來的 Tiff 的確會有破碎掉的狀況。
於是先將 Ghostscript 有區分 32/64 位元的版本,
目前 Windows 大多應該為 64 位元,更新到最新版本。
再測試還是會有破碎掉的狀況。
於是透過 Ghostscript 的 Command 來測試gswin64c -q -dNOPAUSE -dBATCH -sDEVICE=tiffg4 -r300x300 -sOutputFile=abc.tif abc.pdf
出來的結果是正常的。
而 Ghostscript.NET 中透過 GhostscriptProcessor 似乎透過 Command 執行,所以 改用 ProcessorSample1.cs 提供的方式來轉換, 標楷體就正常了。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
30private static void pdf2PNG()
{
string inputFile = @"c:\temp\abc.pdf";
string outputFile = @"c:\temp\abc-page-%03d.png";
int pageFrom = 1;
int pageTo = 50;
using (GhostscriptProcessor ghostscript = new GhostscriptProcessor())
{
ghostscript.Processing += new GhostscriptProcessorProcessingEventHandler(ghostscript_Processing);
List<string> switches = new List<string>();
switches.Add("-empty");
switches.Add("-dSAFER");
switches.Add("-dBATCH");
switches.Add("-dNOPAUSE");
switches.Add("-dNOPROMPT");
switches.Add("-dFirstPage=" + pageFrom.ToString());
switches.Add("-dLastPage=" + pageTo.ToString());
switches.Add("-sDEVICE=png16m");
switches.Add("-r96");
switches.Add("-dTextAlphaBits=4");
switches.Add("-dGraphicsAlphaBits=4");
switches.Add(@"-sOutputFile=" + outputFile);
switches.Add(@"-f");
switches.Add(inputFile);
ghostscript.Process(switches.ToArray());
}
}
- 註1: 如果要轉換成 Tiff ,請將 DEVICE 從 png16m 改成 tiffg4 or tiff24nc
- 註2: 如果要解析度好一點,請將 r 從 96 改成 300
