0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C# PDFでフォントを置換

Last updated at Posted at 2021-06-30

今回はSpire.PDFという無料で使いやすいライブラリを利用して、PDFでフォントを置換える方法を紹介します。

下準備

1.E-iceblueの公式サイトからFree Spire.PDF無料版をダウンロードしてください。

f:id:lendoris:20210630145648p:plain
2.Visual Studioを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire. PDF.dllを参照に追加してください。

(Net 4.0を例としたら、デフォルトパスは“Bin→NET4.0→PDF.dll”というようです。)

f:id:lendoris:20210630145700p:plain

元のファイル

f:id:lendoris:20210630145829p:plain

```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>
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?