<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iTextSharp | ばすにっきTips</title>
	<atom:link href="https://takabus.com/tips/tag/itextsharp/feed/" rel="self" type="application/rss+xml" />
	<link>https://takabus.com/tips</link>
	<description>プログラミングやサーバー、日々のTipsをメモしています。</description>
	<lastBuildDate>Fri, 01 Oct 2021 09:14:33 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.3</generator>

<image>
	<url>https://takabus.com/tips/wp-content/uploads/2022/12/cropped-tips-1-32x32.png</url>
	<title>iTextSharp | ばすにっきTips</title>
	<link>https://takabus.com/tips</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>C#でPDFにしおりを追加するには？（iTextsharp）</title>
		<link>https://takabus.com/tips/296/</link>
					<comments>https://takabus.com/tips/296/#respond</comments>
		
		<dc:creator><![CDATA[ばすにっきTips]]></dc:creator>
		<pubDate>Fri, 01 Oct 2021 09:14:25 +0000</pubDate>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[iTextSharp]]></category>
		<guid isPermaLink="false">https://takabus.com/tips/?p=296</guid>

					<description><![CDATA[C#で既存のPDFファイルにしおりを追加したいときのサンプルを紹介します。iTextSharpを使用した方法です。]]></description>
										<content:encoded><![CDATA[
<p>C#で既存のPDFファイルにしおりを追加したいときのサンプルを紹介します。iTextSharpを使用した方法です。</p>



<h2 class="wp-block-heading">サンプルコード</h2>



<p>コードは以下のとおりです。</p>



<pre class="wp-block-code"><code>using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;

private void AddBookmarksToExistingPDF()
{
    //出力PDFファイル名
    string srcPdfFilePath = @"test.pdf";
    string makePdfFilePath = @"test_out.pdf";
    Document pdfDocument = new Document();
    pdfDocument.Open();

    //FileStreamを作成
    FileStream fileStream = new FileStream(makePdfFilePath, FileMode.Create);

    //PDFをストリームにコピーする
    PdfCopy pdfCopy = new PdfCopy(pdfDocument, fileStream);
    var pdfReader = new PdfReader(srcPdfFilePath);
    pdfCopy.AddDocument(pdfReader);
    pdfReader.Close();

    //しおりのリストを作成
    List&lt;Dictionary&lt;string, object>> bookmarks = new List&lt;Dictionary&lt;string, object>>();

    // しおりアイテムを作成<strong>（※複数のしおりを追加する場合はここを繰り返します）</strong>
    Dictionary&lt;string, object> bookmark = new Dictionary&lt;string, object>();
    bookmark.Add("Title", "Sample");
    bookmark.Add("Action", "GoTo");
    bookmark.Add("Page", "1");
    bookmarks.Add(bookmark);//しおりのリストに追加

    //リストをしおりに設定する
    pdfCopy.Outlines = bookmarks;

    //新しいPDFファイルに書き出す
    pdfDocument.Close();
}</code></pre>



<p>なお、iTextSharpのバージョンは5.5.13.1です。</p>



<h2 class="wp-block-heading">解説</h2>



<p>全体の流れは以下のとおりです。</p>



<ol><li>まず、<strong>しおり1つに対し、1つのDictionaryをつくります。</strong></li><li>そして、<strong>このDictionaryにパラメーターを追加します。</strong>しおりのタイトルや移動先ページといったパラメータを追加します。</li><li>最後にListを作成し、<strong>Dictionaryを1つのListにまとめます。</strong></li><li>PDFCopyオブジェクトのOutlinesプロパティにListをsetします。</li><li>ストリームをクローズすれば、しおりが追加されたPDFが出力されます。</li></ol>



<p><span class="red"><span class="marker-under">Dictionaryには1つのしおりに対するパラメータを束ねます。</span></span>1つのしおりに対して、1つのDictionaryができることになります。</p>



<p>最後に複数のDictionaryを1つのListにまとめて、iTextSharpに渡します。あとはiTextSharpが自動的にしおりを追加してくれます。</p>



<h2 class="wp-block-heading">しおりに設定できるパラメータについて</h2>



<p>しおりに設定できるパラメーターについては、Adobeが発行しているPDF標準仕様書に記載があります。</p>



<p><a href="https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf">https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf</a></p>



<figure class="wp-block-image size-full"><a href="https://takabus.com/tips/wp-content/uploads/2021/10/image-1.png"><img fetchpriority="high" decoding="async" width="673" height="286" src="https://takabus.com/tips/wp-content/uploads/2021/10/image-1.png" alt="Action types" class="wp-image-298" srcset="https://takabus.com/tips/wp-content/uploads/2021/10/image-1.png 673w, https://takabus.com/tips/wp-content/uploads/2021/10/image-1-300x127.png 300w" sizes="(max-width: 673px) 100vw, 673px" /></a><figcaption>Actionで指定できるプロパティ</figcaption></figure>



<figure class="wp-block-image size-full"><a href="https://takabus.com/tips/wp-content/uploads/2021/10/image.png"><img decoding="async" width="540" height="574" src="https://takabus.com/tips/wp-content/uploads/2021/10/image.png" alt="Destination Syntax" class="wp-image-297" srcset="https://takabus.com/tips/wp-content/uploads/2021/10/image.png 540w, https://takabus.com/tips/wp-content/uploads/2021/10/image-282x300.png 282w" sizes="(max-width: 540px) 100vw, 540px" /></a><figcaption>ページ番号とともに表示形式も指定できる。(Portable document format — Part 1: PDF 1.7 &#8211; Adobeより）</figcaption></figure>



<p>ページ番号のみならず、表示倍率やページ表示（幅に合わせるなど）もしおりに設定することができます。</p>



<h2 class="wp-block-heading">まとめ</h2>



<p>C#でPDFにしおりを追加する方法を紹介しました。</p>



<p>実際にこのコードを使って開発してみましたが、それほど苦労なく実装できました。DictionaryとListの関係性さえ理解できれば、かんたんでしょう。ぜひお試しあれ。</p>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://takabus.com/tips/296/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
