質問編集履歴
1
情報の追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,7 +16,6 @@
|
|
16
16
|
```
|
17
17
|
|
18
18
|
### 該当のソースコード
|
19
|
-
|
20
19
|
```C#
|
21
20
|
// バーコード生成の設定
|
22
21
|
var writer = new BarcodeWriter
|
@@ -75,7 +74,179 @@
|
|
75
74
|
}
|
76
75
|
}
|
77
76
|
```
|
78
|
-
|
77
|
+
### 追加のソースコード
|
78
|
+
```C#
|
79
|
+
public partial class MainWindow : Window
|
80
|
+
{
|
81
|
+
public MainWindow()
|
82
|
+
{
|
83
|
+
InitializeComponent();
|
84
|
+
}
|
85
|
+
|
86
|
+
private void button1_Click(object sender, RoutedEventArgs e)
|
87
|
+
{
|
88
|
+
FixedDocument documents = new FixedDocument();
|
89
|
+
|
90
|
+
Canvas.SetTop(BarcodeImage, 10);
|
91
|
+
Canvas.SetLeft(BarcodeImage, 10);
|
92
|
+
documents = CreatePageModel.CreatePages(BarcodeImage);
|
93
|
+
|
94
|
+
// プレビュー表示
|
95
|
+
PreviewControl.DoPrint(documents);
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
class PrintPageModelBase
|
102
|
+
{
|
103
|
+
public object Header { get; set; }
|
104
|
+
public object Body { get; set; }
|
105
|
+
public int PageNumber { get; set; }
|
106
|
+
}
|
107
|
+
class CreatePageModel : PrintPageModelBase
|
108
|
+
{
|
109
|
+
public static FixedDocument CreatePages(System.Windows.Controls.Image img)
|
110
|
+
{
|
111
|
+
FixedDocument doc = new FixedDocument();
|
112
|
+
|
113
|
+
PageContent pc = new PageContent();
|
114
|
+
pc.Child = PrintDocPrintPage(img);
|
115
|
+
doc.Pages.Add(pc);
|
116
|
+
|
117
|
+
return doc;
|
118
|
+
}
|
119
|
+
public static FixedPage PrintDocPrintPage(System.Windows.Controls.Image img)
|
120
|
+
{
|
121
|
+
Canvas canvas = new Canvas();
|
122
|
+
FixedPage fPage = new FixedPage();
|
123
|
+
System.Windows.Controls.Page page = new Page();
|
124
|
+
|
125
|
+
System.Windows.Controls.Image image = new System.Windows.Controls.Image();
|
126
|
+
image.Source = img.Source;
|
127
|
+
|
128
|
+
canvas.Children.Add(image);
|
129
|
+
|
130
|
+
fPage = CreateFixedPage(page);
|
131
|
+
fPage.Children.Add(canvas);
|
132
|
+
|
133
|
+
return fPage;
|
134
|
+
}
|
135
|
+
private static FixedPage CreateFixedPage(Page page)
|
136
|
+
{
|
137
|
+
FixedPage fixpage = new FixedPage();
|
138
|
+
{
|
139
|
+
try
|
140
|
+
{
|
141
|
+
Frame fm = new Frame();
|
142
|
+
fm.Content = page;
|
143
|
+
|
144
|
+
FixedPage.SetLeft(fm, 0);
|
145
|
+
FixedPage.SetTop(fm, 0);
|
146
|
+
fixpage.Children.Add(fm);
|
147
|
+
|
148
|
+
// ピクセル値を求める計算式は、 用紙の一辺の長さ[mm] × 解像度[dpi] ÷ 25.4 です。
|
149
|
+
page.Height = 1118; // 8.27 * 96; // 793.92
|
150
|
+
page.Width = 794; // 使用する用紙の幅をセットする
|
151
|
+
|
152
|
+
System.Windows.Size sz = new System.Windows.Size(page.Width, page.Height);
|
153
|
+
|
154
|
+
fixpage.Width = page.Width;
|
155
|
+
fixpage.Height = page.Height;
|
156
|
+
fixpage.Measure(sz);
|
157
|
+
fixpage.Arrange(new Rect(new System.Windows.Point(), sz));
|
158
|
+
fixpage.UpdateLayout();
|
159
|
+
}
|
160
|
+
catch (Exception ex)
|
161
|
+
{
|
162
|
+
MessageBox.Show(ex.Message);
|
163
|
+
}
|
164
|
+
|
165
|
+
return fixpage;
|
166
|
+
}
|
167
|
+
}
|
168
|
+
}
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
internal class PreviewControl
|
173
|
+
{
|
174
|
+
static Window preView;
|
175
|
+
public static string printerMei;
|
176
|
+
/// <summary>
|
177
|
+
/// 用紙サイズ
|
178
|
+
/// </summary>
|
179
|
+
public static PageMediaSize PageMediaSize { get; set; } // = new PageMediaSize(PageMediaSizeName.ISOA4);
|
180
|
+
/// <summary>
|
181
|
+
/// カラーモード
|
182
|
+
/// </summary>
|
183
|
+
public static OutputColor? OutputColor { get; set; } = System.Printing.OutputColor.Color; //.;Monochrome
|
184
|
+
/// <summary>
|
185
|
+
/// 印刷部数
|
186
|
+
/// </summary>
|
187
|
+
public static int? CopyCount { get; set; } = 1;
|
188
|
+
// public static System.Windows.Size PageSize { get; set; }
|
189
|
+
|
190
|
+
|
191
|
+
|
192
|
+
public static void DoPrint(FixedDocument doc)
|
193
|
+
{
|
194
|
+
try
|
195
|
+
{
|
196
|
+
//https://stackoverflow.com/questions/23785720/pdfsharp-missing-the-xps-namespace/23786446#23786446
|
197
|
+
//https://nathanpjones.com/2013/03/output-to-pdf-in-wpf-for-free/
|
198
|
+
|
199
|
+
// プレビュー表示すると MemoryStream?のエラーで回避できないので直接印刷
|
200
|
+
DocumentViewer viewer = new DocumentViewer { Document = doc };
|
201
|
+
viewer.CommandBindings.Add(new CommandBinding(ApplicationCommands.Print, PrintExecute));
|
202
|
+
preView = new Window()
|
203
|
+
{
|
204
|
+
Title = "印刷",
|
205
|
+
WindowStartupLocation = WindowStartupLocation.CenterScreen,
|
206
|
+
Content = viewer,
|
207
|
+
};
|
208
|
+
preView.ShowDialog();
|
209
|
+
}
|
210
|
+
catch (Exception ex)
|
211
|
+
{
|
212
|
+
MessageBox.Show(ex.Message);
|
213
|
+
}
|
214
|
+
}
|
215
|
+
|
216
|
+
private static void PrintExecute(object sender, ExecutedRoutedEventArgs e)
|
217
|
+
{
|
218
|
+
e.Handled = true;
|
219
|
+
PrintDialog dlg = new PrintDialog();
|
220
|
+
|
221
|
+
// 用紙向き(縦)
|
222
|
+
dlg.PrintTicket.PageOrientation = System.Printing.PageOrientation.Portrait; // Landscape
|
223
|
+
|
224
|
+
// 以下でも変わらない
|
225
|
+
// dlg.PrintTicket.PageResolution = new PageResolution(9600, 9600);
|
226
|
+
|
227
|
+
PrintTicket pt = dlg.PrintTicket;
|
228
|
+
// 用紙サイズ
|
229
|
+
pt.PageMediaSize = PageMediaSize;
|
230
|
+
|
231
|
+
// トレイの設定
|
232
|
+
pt.InputBin = InputBin.Manual; // ここで InputBin を設定してください。例えば、InputBin = InputBin.Custom など
|
233
|
+
|
234
|
+
// カラーモード
|
235
|
+
pt.OutputColor = OutputColor;
|
236
|
+
// 印刷部数(同じものがもう一部必要)
|
237
|
+
pt.CopyCount = CopyCount;
|
238
|
+
// Create a local print server
|
239
|
+
LocalPrintServer ps = new LocalPrintServer();
|
240
|
+
|
241
|
+
System.Windows.Xps.XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter(dlg.PrintQueue);
|
242
|
+
FixedDocument doc = (FixedDocument)((DocumentViewer)sender).Document;
|
243
|
+
doc.PrintTicket = pt; // dlg.PrintTicket;
|
244
|
+
xpsdw.Write(doc, dlg.PrintTicket);
|
245
|
+
|
246
|
+
preView.Close();
|
247
|
+
}
|
248
|
+
}
|
249
|
+
```
|
79
250
|
### 試したこと・調べたこと
|
80
251
|
- [x] teratailやGoogle等で検索した
|
81
252
|
- [x] ソースコードを自分なりに変更した
|