PDF内のページには、Powerpointのプレゼンテーション、Webページ、またはワードプロセッシング文書に使用できる重要な情報が含まれることがよくあります。DioDocs for PDFライブラリを使用すると、PDFを画像に変換するオンラインツールを使用せずに、PDFドキュメントを高画質な画像ファイルとして保存できます。
DioDocs for PDFでは、PDFドキュメントを画像として保存する2つの方法が用意されています。
DioDocs for PDFライブラリでは、PDFドキュメント全体や特定の選択範囲を画像として保存するメソッドが提供されています。 PDFドキュメントを画像として保存するには、ユーザーがファイル名を指定して、 GcPdfDocument クラスの SaveAsBmp、 SaveAsPng、 SaveAsGif、 SaveAsJpeg、 および SaveAsTiff メソッドを呼び出すことができます。
PDFドキュメントを画像として保存するには
C# |
コードのコピー
|
---|---|
GcPdfDocument doc = new GcPdfDocument(); var fs = new FileStream(Path.Combine("Wetlands.pdf"), FileMode.Open, FileAccess.Read); doc.Load(fs); //ドキュメントをロードします //ドキュメントのどのページを保存するかを定義する出力範囲オブジェクトを作成します //出力範囲が定義されていない場合は、ドキュメントのすべてのページが保存されます OutputRange pageRange = new OutputRange(1, 2); //ドキュメントのページを画像に保存するときに使用するオプションを指定します SaveAsImageOptions op = new SaveAsImageOptions(); SaveAsImageOptions saveOptions = new SaveAsImageOptions() { BackColor = Color.LightCyan, DrawAnnotations = false, DrawFormFields = false, Resolution = 100 }; doc.SaveAsJpeg("WetlandsImage{0}.jpeg", pageRange, saveOptions); //ドキュメントページをJPEG形式の画像として保存します doc.SaveAsBmp("WetlandsImage{0}.bmp", pageRange, saveOptions); //ドキュメントページをBMP形式の画像として保存します doc.SaveAsGif("WetlandsImage{0}.gif", pageRange, saveOptions); //ドキュメントページをGIF形式の画像として保存します doc.SaveAsPng("WetlandsImage{0}.png", pageRange, saveOptions); //ドキュメントページをPNG形式の画像として保存します |
DioDocs for PDFでは、 Page クラスの SaveAsBmp, SaveAsPng, SaveAsGif, SaveAsTiff, SaveAsJpeg などのメソッドを呼び出すことで、PDFページを画像として保存することもできます。
PDFページを直接画像として保存するには
C# |
コードのコピー
|
---|---|
GcPdfDocument doc = new GcPdfDocument(); var fs = new FileStream(Path.Combine("Wetlands.pdf"), FileMode.Open, FileAccess.Read); doc.Load(fs); //ドキュメントをロードします //ページを画像に保存するときに使用する必要があるオプションを指定します SaveAsImageOptions saveOptions = new SaveAsImageOptions() { BackColor = Color.LightCyan, DrawAnnotations = false, DrawFormFields = false, Resolution = 100 }; //ドキュメントの最初ページを画像としてJPEG形式のファイルに保存します doc.Pages[0].SaveAsJpeg("WetlandsImage.jpeg",saveOptions); //ドキュメントの最初ページを画像としてJPEG形式のストリームに保存します MemoryStream stream = new MemoryStream(); doc.Pages[0].SaveAsJpeg(stream,saveOptions); |
本製品のライブラリでは、SaveAsImageOptions クラスの InterpolationMode プロパティを使用して、PDF ファイルにビットマップ画像を描画したり、PDF ファイルを画像として保存したりする際に補間モードを設定できます。 このプロパティのデフォルト値は NearestNeighbor です。
次のサンプルコードは、PDF ファイルを画像として保存する際に InterpolationMode を設定する方法を示しています。
C# |
コードのコピー
|
---|---|
class Program { GrapeCity.Documents.Pdf.GcPdfDocument Document; GrapeCity.Documents.Pdf.GcPdfGraphics Graphics; GrapeCity.Documents.Pdf.Page Page; public const float Resolution = 25.4f; public const float MM = 1; public const float CM = MM * 10; System.Drawing.SizeF PageSize; System.Drawing.RectangleF TextRect; static void Main(string[] args) { // GcPdfDocument を初期化します var pdf = new GrapeCity.Documents.Pdf.GcPdfDocument(); // PDF ドキュメントにページを描画します new Program(pdf).DrawPage(); // SaveAsImageOptions をインスタンス化します SaveAsImageOptions options = new SaveAsImageOptions(); // InterpolationMode を NearestNeighbor に設定します options.InterpolationMode = GrapeCity.Documents.Drawing.InterpolationMode.NearestNeighbor; // PDF ドキュメントを保存します pdf.Save("test.pdf"); // SaveAsImageOptions を使用して PDF ドキュメントを BMP 形式として保存します pdf.SaveAsBmp("test.bmp", null, options); // SaveAsImageOptions を使用して PDF ドキュメントを PNG 形式として保存します pdf.SaveAsPng("test.png", null, options); // SaveAsImageOptions を使用して PDF ドキュメントを JPEG 形式として保存します pdf.SaveAsJpeg("test.jpg", null, options); } Program(GrapeCity.Documents.Pdf.GcPdfDocument doc) { Document = doc; } // PDF ドキュメントのページ設定を構成します void AddPage() { Page = Document.NewPage(); Page.PaperKind = GrapeCity.Documents.Common.PaperKind.A4; Page.Landscape = false; PageSize = Page.GetRenderSize(Resolution, Resolution); Graphics = Page.Graphics; Graphics.Resolution = Resolution; TextRect = new System.Drawing.RectangleF(0, 0, PageSize.Width, PageSize.Height); TextRect.Inflate(-2.5f * CM, -2.7f * CM); Graphics.DrawRectangle(TextRect, new GrapeCity.Documents.Drawing.Pen(System.Drawing.Color.Green, 2f * MM)); } // ビットマップ上に画像を描画します void DrawBitmap(System.Drawing.RectangleF rect) { var pngImage = GrapeCity.Documents.Drawing.Image.FromFile(@"..\..\..\qrcode.png"); Graphics.DrawImage(pngImage, rect, rect, GrapeCity.Documents.Drawing.ImageAlign.StretchImage); } // PDF ドキュメントにページを描画します void DrawPage() { AddPage(); DrawBitmap(new System.Drawing.RectangleF(5 * CM, 5 * CM, 10 * CM, 10 * CM)); } } |
メモ:補間モードは、ビットマップ画像がグラフィック上に描画される方法、つまり DrawImage メソッドとビットマップ画像のサイズ変更の結果にのみ影響します。補間モードは、他のグラフィック操作には影響しません。 特に、PDF が画像形式で保存される場合、元の PDF に埋め込まれたビットマップ画像のみが補間モードの影響を受けます。
上記の一般的な画像形式に加えて、DioDocs for PDFではPDF内のページをSVGまたはその圧縮形式SVGZとして保存することもできます。PDFページのインスタンスをSVGファイル、ストリーム(.svg)またはバイト配列(.svgz)にエクスポートするには、GrapeCity.Documents.Pdf.PageクラスのSaveAsSvgおよびToSvgzメソッドを使用できます。
C# |
コードのコピー
|
---|---|
var pdfDoc = new GcPdfDocument(); using (var fs = new FileStream("Test.pdf", FileMode.Open, FileAccess.Read, FileShare.Read)) { pdfDoc.Load(fs); var page = pdfDoc.Pages[0]; // PDFのページを.svgファイルに描画します page.SaveAsSvg("GcPDFRenderToSVG.svg", null, new SaveAsImageOptions() { Zoom = 2f }, new XmlWriterSettings() { Indent = true }); // PDFのページをSVGZ形式の圧縮データを含むバイト配列に描画します var svgzData = page.ToSvgz(new SaveAsImageOptions() { Zoom = 1f }); File.WriteAllBytes("GcPDFRenderToSVGZ.svgz", svgzData); } |
制限
テキストは常にパスを使用してグラフィックとして描画されます。そのため、テキストを含んだページの.svgファイルは大きく、ブラウザで開いたSVG画像のテキストを選択またはコピーすることはできません。
DioDocs for PDFでは、GcPdfGraphicsによってテキストを描画して画像として保存する際、TrueTypeヒンティング命令を有効にすることがサポートされています。
ヒンティング命令は一部のTrueTypeフォントに含まれ、フォントサイズに関係なく、いくつかのグリフ部分を別のグリフで再利用することによって外観を改善できます。DioDocs for PDFのTrueTypeヒンティング命令では、グリフの小さな部分の組み合わせとしてCJK文字の描画をサポートされ、最終的な外観を向上させます。
TrueTypeグリフのヒンティング命令を含むフォントの場合、FontクラスのEnableHintingプロパティがtrueに設定され、それ以外のフォントの場合はFalseに設定されます。さらに、フォントのヒンティング命令を適用するには、SaveAsImageOptionsクラスのEnableFontHintingプロパティをtrue(デフォルト値)に設定する必要があります。
ただし、EnableHintingプロパティが明示的にfalseに設定される場合、フォントヒンティング命令は有効にできません。
従いまして、EnableFontHintingプロパティおよびEnableHintingプロパティのデフォルト値がtrueに設定されるため、それらのプロパティを含んだすべてのTrueTypeフォントでヒンティング命令がサポートされます。
無効化されたヒンティング命令 |
![]() |
![]() |
有効化されたヒンティング命令 |
![]() |
![]() |
中国語文字列のTrueTypeヒンティング命令を有効にするには
C# |
コードのコピー
|
---|---|
var font = Font.FromFile("kaiu.ttf"); GcPdfDocument doc = new GcPdfDocument(); { GcPdfGraphics g = doc.NewPage().Graphics; { //ヒンティング命令をtrueに設定して文字列を描画します string s1 = @"入秋空?警報!這幾招遠離PM2.5學起來"; //テキストの書式設定する属性を定義します var tf1 = new TextFormat() { Font = font, FontSize = 20, }; g.DrawString(s1, tf1, new PointF(10, 110)); SaveAsImageOptions imgOptions = new SaveAsImageOptions(); imgOptions.EnableFontHinting = true; doc.SaveAsPng("ChineseFontwithHintingInstructions1.png", null, imgOptions); } } |
DioDocs for PDF では、SaveAsImageOptionsクラスの DrawAnnotationFilter プロパティを使用して、PDFを画像として保存する際にどの注釈をレンダリングするかを制御することができます。このプロパティは、どの注釈を描画するかを決定するかを DrawAnnotationFilterCallback メソッドに割り当てます。
C# |
コードのコピー
|
---|---|
// GcPdfDocumentのインスタンスを生成します var doc = new GcPdfDocument(); // 新規ページを追加します var page = doc.NewPage(); // GcPdfGraphicsのインスタンスを生成します var g = page.Graphics; // 注釈用のユーザー名を定義します var user1 = "Jaime Smith"; var user2 = "Jane Donahue"; // 各文字列の幅と間隔を定義します var noteWidth = 72 * 4; var gap = 8; // ドキュメントに文字列を追加します var rc = Common.Util.AddNote( "This sample demonstrates some types of annotations that can be created with DsPdf.\n" + "Note that some annotation types may not display in certain viewers (such as built-in browser viewers)." + "To see all annotations on this page, open it in Acrobat Reader or other full-featured PDF viewer.", page); // テキスト注釈を追加します var ip = new PointF(rc.X, rc.Bottom + gap); rc = Common.Util.AddNote( "A red text annotation is placed to the right of this note.", page, new RectangleF(ip.X, ip.Y, noteWidth, 100)); var textAnnot = new TextAnnotation() { UserName = user1, Contents = "This is annotation 1, a red one.", Rect = new RectangleF(rc.Right, rc.Top, 72 * 2, 72), Color = Color.Red, }; page.Annotations.Add(textAnnot); // 直前の注釈に対して返信を追加します var textAnnotReply = new TextAnnotation() { UserName = user2, Contents = "This is a reply to the first annotation.", Rect = new RectangleF(rc.Right, rc.Top, 72 * 2, 72), ReferenceAnnotation = textAnnot, ReferenceType = AnnotationReferenceType.Reply, }; page.Annotations.Add(textAnnotReply); // 最初に開くテキスト注釈を追加します ip = new PointF(rc.X, rc.Bottom + gap); rc = Common.Util.AddNote( "A green text annotation that is initially open is placed to the right of this note.", page, new RectangleF(ip.X, ip.Y, noteWidth, 100)); var textAnnotOpen = new TextAnnotation() { Open = true, UserName = user1, Contents = "This is an initially open annotation (green).", Rect = new RectangleF(rc.Right, rc.Top, 72 * 2, 72), Color = Color.Green, }; page.Annotations.Add(textAnnotOpen); // フリーテキスト注釈を追加します ip = new PointF(rc.X, rc.Bottom + gap); rc = Common.Util.AddNote( "A blue free text annotation is placed below and to the right, with a callout going from it to this note.", page, new RectangleF(ip.X, ip.Y, noteWidth, 100)); var freeAnnot = new FreeTextAnnotation() { Rect = new RectangleF(rc.Right + 18, rc.Bottom + 9, 72 * 2, 72), CalloutLine = new PointF[] { new PointF(rc.Left + rc.Width / 2, rc.Bottom), new PointF(rc.Left + rc.Width / 2, rc.Bottom + 9 + 36), new PointF(rc.Right + 18, rc.Bottom + 9 + 36), }, LineWidth = 1, LineEndStyle = LineEndingStyle.OpenArrow, LineDashPattern = new float[] { 8, 4 }, Contents = "This is a free text annotation with a callout line going to the note on the left.", Color = Color.LightSkyBlue, }; page.Annotations.Add(freeAnnot); // リッチテキストが設定されたフリーテキスト注釈を追加します ip = new PointF(rc.X, freeAnnot.Rect.Bottom + gap); var freeRichAnnot = new FreeTextAnnotation() { Rect = new RectangleF(ip.X - 144, ip.Y, 72 * 5, 72), LineWidth = 1, Color = Color.LightSalmon, RichText = "<body><p>This is another <i>free text annotation</i>, with <b><i>Rich Text</i></b> content.</p>" + "<p>Even though a <b>free text</b> annotation displays text directly on a page, " + "as other annotations it can be placed outside the page's bounds.</p></body>" }; page.Annotations.Add(freeRichAnnot); // 四角形注釈を文字列上に追加します ip = new PointF(rc.X, freeRichAnnot.Rect.Bottom + gap * 2); rc = Common.Util.AddNote( "A square annotation drawn with a 3pt wide orange line around this note has a rich text associated with it.", page, new RectangleF(ip.X, ip.Y, noteWidth, 100)); rc.Inflate(8, 8); var squareAnnot = new SquareAnnotation() { UserName = user2, Rect = rc, LineWidth = 3, Color = Color.Orange, RichText = "<body><p>This <b><i>rich text</i></b> is associated with the square annotation around a text note.</p></body>" }; page.Annotations.Add(squareAnnot); // 画像出力時、テキスト注釈のみ描画されるように設定します SaveAsImageOptions options = new SaveAsImageOptions(); options.DrawAnnotationFilter = (GcPdfDocument d, Page p, AnnotationBase a, ref bool drawAnnotation) => { drawAnnotation = a is TextAnnotation; }; // ドキュメントをPNG形式で保存します doc.SaveAsPng("TextAnnotationOnly.png", null, options); |