業務で利用するPDFを取り込んだらどうだろうと思い実験。とりあえず、ページ移動とブックマークのようなものだけ。
NuGetから、
・PdfiumViewer
・PdfiumViewer.Native.x86.v8-xfa
の2つをインストール。
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
using PdfiumViewer; using System; using System.Drawing; using System.Windows.Forms; namespace PDFViewer { public partial class Form1 : Form { PdfDocument _Document; int _PageNum = 1; int _PageW; int _PageH; public Form1() { InitializeComponent(); _Document = PdfDocument.Load(@"G:\Download\i.pdf"); var size = _Document.PageSizes[0]; _PageW = PointToPixel(size.Width); _PageH = PointToPixel(size.Height); HeaderEventAttach(); SideEventAttach(); RenderDocument(); int maxPage = _Document.PageCount - 1; hScrollBar1.Maximum = maxPage + hScrollBar1.LargeChange - 1; hScrollBar1.Scroll += (s, e) => { _PageNum = e.NewValue; RenderDocument(); }; } private void SideEventAttach() { MemoText.KeyDown += (s, e) => { if (e.KeyCode != Keys.Enter) return; var sender = (TextBox)s; if (sender.Text == "") return; listBox1.ValueMember = "Page"; listBox1.DisplayMember = "Title"; listBox1.Items.Add(new { Page = _PageNum.ToString(), Title = sender.Text + "#" + _PageNum.ToString() }); MemoText.Text = ""; }; listBox1.MouseDoubleClick += (s, e) => { if (listBox1.IndexFromPoint(new Point(e.X, e.Y)) == -1) return; if ((ModifierKeys & Keys.Control) == Keys.Control) { listBox1.Items.RemoveAt(listBox1.SelectedIndex); } else { dynamic items = listBox1.SelectedItem; int.TryParse(items.Page, out int result); _PageNum = result; RenderDocument(); } }; } private void HeaderEventAttach() { NextButton.Click += (s, e) => { if (_PageNum > _Document.PageCount - 1) return; _PageNum++; RenderDocument(); hScrollBar1.Value = _PageNum; }; PrevButton.Click += (s, e) => { if (_PageNum <= 0) return; _PageNum--; RenderDocument(); hScrollBar1.Value = _PageNum; }; PageNumText.KeyDown += (s, e) => { if (e.KeyCode == Keys.Enter) { var sender = (TextBox)s; int.TryParse(sender.Text, out int result); _PageNum = result; RenderDocument(); } if (e.KeyCode == Keys.Right) { NextButton.PerformClick(); RenderDocument(); } if (e.KeyCode == Keys.Left) { PrevButton.PerformClick(); RenderDocument(); } }; } private void RenderDocument() { pictureBox1.Image = _Document.Render(_PageNum - 1, _PageW, _PageH, DeviceDpi, DeviceDpi, false); PageNumText.Text = Convert.ToString(_PageNum); } private int PointToPixel(float point) { return (int)(DeviceDpi * point) / 72; } } } |