今回はSpire.PDFという無料で使いやすいライブラリを利用して、PDFでフォントを置換える方法を紹介します。
下準備
1.E-iceblueの公式サイトからFree Spire.PDF無料版をダウンロードしてください。
2.Visual Studioを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire. PDF.dllを参照に追加してください。
(Net 4.0を例としたら、デフォルトパスは“Bin→NET4.0→PDF.dll”というようです。)
元のファイル
```c# using Spire.Pdf; using Spire.Pdf.Graphics; using Spire.Pdf.Graphics.Fonts;namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//Pdf ファイルをロードします。
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(PDF file);
//UsedFontsでフォントを取得します。
PdfUsedFont[] fonts = doc.UsedFonts;
//新規フォントを作成します。
Font newfont = new PdfFont(PdfFontFamily.TimesRoman, 18f, PdfFontStyle.Italic | PdfFontStyle.Bold);
cjkFont = new PdfCjkStandardFont
//ファイルのフォントをループして、新規フォントを適用します。
foreach (PdfUsedFont font in fonts)
{
font.Replace(newfont);
}
//保存します。
doc.SaveToFile(Result file);
}
}
}
<h4><strong>実行結果</strong></h4>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210630/20210630145816.png" alt="f:id:lendoris:20210630145816p:plain" width="467" height="327" loading="lazy" title="" class="hatena-fotolife" itemprop="image" /></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>