質問するログイン新規登録

回答編集履歴

3

見直しキャンペーン中

2023/07/17 05:55

投稿

TN8001
TN8001

スコア10118

answer CHANGED
@@ -1,68 +1,69 @@
1
- あきらめていたのですができてしまいました。
2
- パスワード付きや暗号化?されているとPDFsharpが開けずエラーになります(web上にあるものって結構パスワード付いてるんですね)
3
-
4
- PDFsharpで作ったもの以外にもいくつかやってみましたが、ミラー化できました。
5
- Google Chromeで見たときにページ送り?(元は横に矢印が出てるのに縦スクロールになる)が違ってしまうpdfがありましたがよくわかりません。
6
-
7
- [参考](http://www.pdfsharp.net/wiki/TwoPagesOnOne-sample.ashx)
8
-
9
- ```C#
10
- using System.Diagnostics;
11
- using PdfSharp.Drawing;
12
- using PdfSharp.Pdf;
13
-
14
- namespace Questions226967
15
- {
16
- class Program
17
- {
18
- /// <summary>PDFを鏡像化して保存します</summary>
19
- /// <param name="inputFilePath">元になるファイルパス</param>
20
- /// <param name="outputFilePath">鏡像化したファイルパス</param>
21
- static void MirrorPdf(string inputFilePath, string outputFilePath)
22
- {
23
- XPdfForm form = XPdfForm.FromFile(inputFilePath); // XPdfFormがポイント
24
- PdfDocument output = new PdfDocument();
25
-
26
- for(int i = 0; i < form.PageCount; i++)
27
- {
28
- form.PageNumber = i + 1;
29
- PdfPage page = output.AddPage();
30
- page.Width = form.Page.Width;
31
- page.Height = form.Page.Height;
32
-
33
- XRect box = new XRect(0, 0, page.Width, page.Height);
34
- XGraphics gfx = XGraphics.FromPdfPage(page);
35
-
36
- gfx.TranslateTransform(page.Width, 0);
37
- gfx.ScaleTransform(-1, 1);
38
- gfx.DrawImage(form, box);
39
- }
40
-
41
- output.Save(outputFilePath);
42
- }
43
- // 単に入力用のpdfを作っているだけ
44
- static void SamplePdf(string outputFilePath)
45
- {
46
- PdfDocument document = new PdfDocument();
47
- document.Info.Title = "Created with PDFsharp";
48
- XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
49
- XImage image = XImage.FromFile(@"file_icon_text_pdf.png");
50
- PdfPage page = document.AddPage();
51
- XGraphics gfx = XGraphics.FromPdfPage(page);
52
- gfx.DrawString("Hello, World!", font, XBrushes.Black,
53
- new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
54
- gfx.DrawImage(image, 0, 0);
55
- document.Save(outputFilePath);
56
- }
57
- static void Main()
58
- {
59
- string input = @"HelloWorld.pdf";
60
- string output = "HelloWorld_Mirror.pdf";
61
- SamplePdf(input);
62
-
63
- MirrorPdf(input, output);
64
- Process.Start(output);
65
- }
66
- }
67
- }
1
+ あきらめていたのですができてしまいました。
2
+ パスワード付きや暗号化?されているとPDFsharpが開けずエラーになります(web上にあるものって結構パスワード付いてるんですね)
3
+
4
+ PDFsharpで作ったもの以外にもいくつかやってみましたが、ミラー化できました。
5
+ Google Chromeで見たときにページ送り?(元は横に矢印が出てるのに縦スクロールになる)が違ってしまうpdfがありましたがよくわかりません。
6
+
7
+ 参考
8
+ [PDFsharp Sample: Two Pages on One - PDFsharp and MigraDoc Wiki](http://www.pdfsharp.net/wiki/TwoPagesOnOne-sample.ashx?AspxAutoDetectCookieSupport=1)
9
+
10
+ ```cs
11
+ using System.Diagnostics;
12
+ using PdfSharp.Drawing;
13
+ using PdfSharp.Pdf;
14
+
15
+ namespace Questions226967
16
+ {
17
+ class Program
18
+ {
19
+ /// <summary>PDFを鏡像化して保存します</summary>
20
+ /// <param name="inputFilePath">元になるファイルパス</param>
21
+ /// <param name="outputFilePath">鏡像化したファイルパス</param>
22
+ static void MirrorPdf(string inputFilePath, string outputFilePath)
23
+ {
24
+ XPdfForm form = XPdfForm.FromFile(inputFilePath); // XPdfFormがポイント
25
+ PdfDocument output = new PdfDocument();
26
+
27
+ for(int i = 0; i < form.PageCount; i++)
28
+ {
29
+ form.PageNumber = i + 1;
30
+ PdfPage page = output.AddPage();
31
+ page.Width = form.Page.Width;
32
+ page.Height = form.Page.Height;
33
+
34
+ XRect box = new XRect(0, 0, page.Width, page.Height);
35
+ XGraphics gfx = XGraphics.FromPdfPage(page);
36
+
37
+ gfx.TranslateTransform(page.Width, 0);
38
+ gfx.ScaleTransform(-1, 1);
39
+ gfx.DrawImage(form, box);
40
+ }
41
+
42
+ output.Save(outputFilePath);
43
+ }
44
+ // 単に入力用のpdfを作っているだけ
45
+ static void SamplePdf(string outputFilePath)
46
+ {
47
+ PdfDocument document = new PdfDocument();
48
+ document.Info.Title = "Created with PDFsharp";
49
+ XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
50
+ XImage image = XImage.FromFile(@"file_icon_text_pdf.png");
51
+ PdfPage page = document.AddPage();
52
+ XGraphics gfx = XGraphics.FromPdfPage(page);
53
+ gfx.DrawString("Hello, World!", font, XBrushes.Black,
54
+ new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
55
+ gfx.DrawImage(image, 0, 0);
56
+ document.Save(outputFilePath);
57
+ }
58
+ static void Main()
59
+ {
60
+ string input = @"HelloWorld.pdf";
61
+ string output = "HelloWorld_Mirror.pdf";
62
+ SamplePdf(input);
63
+
64
+ MirrorPdf(input, output);
65
+ Process.Start(output);
66
+ }
67
+ }
68
+ }
68
69
  ```

2

あきらめていたのですができてしまいました。

2019/12/05 08:32

投稿

TN8001
TN8001

スコア10118

answer CHANGED
@@ -1,5 +1,11 @@
1
+ あきらめていたのですができてしまいました。
1
- PDFsharpのサンプルのつぎはぎです、こううこといいのでしょうか?
2
+ パスワード付きや暗号化?されているとPDFsharpが開けずエラーになります(web上にあるものって結構パスワード付てるんすね)
2
3
 
4
+ PDFsharpで作ったもの以外にもいくつかやってみましたが、ミラー化できました。
5
+ Google Chromeで見たときにページ送り?(元は横に矢印が出てるのに縦スクロールになる)が違ってしまうpdfがありましたがよくわかりません。
6
+
7
+ [参考](http://www.pdfsharp.net/wiki/TwoPagesOnOne-sample.ashx)
8
+
3
9
  ```C#
4
10
  using System.Diagnostics;
5
11
  using PdfSharp.Drawing;
@@ -9,44 +15,54 @@
9
15
  {
10
16
  class Program
11
17
  {
18
+ /// <summary>PDFを鏡像化して保存します</summary>
19
+ /// <param name="inputFilePath">元になるファイルパス</param>
20
+ /// <param name="outputFilePath">鏡像化したファイルパス</param>
12
- static void Main()
21
+ static void MirrorPdf(string inputFilePath, string outputFilePath)
13
22
  {
23
+ XPdfForm form = XPdfForm.FromFile(inputFilePath); // XPdfFormがポイント
24
+ PdfDocument output = new PdfDocument();
25
+
26
+ for(int i = 0; i < form.PageCount; i++)
27
+ {
28
+ form.PageNumber = i + 1;
29
+ PdfPage page = output.AddPage();
30
+ page.Width = form.Page.Width;
31
+ page.Height = form.Page.Height;
32
+
33
+ XRect box = new XRect(0, 0, page.Width, page.Height);
34
+ XGraphics gfx = XGraphics.FromPdfPage(page);
35
+
36
+ gfx.TranslateTransform(page.Width, 0);
37
+ gfx.ScaleTransform(-1, 1);
38
+ gfx.DrawImage(form, box);
39
+ }
40
+
41
+ output.Save(outputFilePath);
42
+ }
43
+ // 単に入力用のpdfを作っているだけ
44
+ static void SamplePdf(string outputFilePath)
45
+ {
14
46
  PdfDocument document = new PdfDocument();
15
47
  document.Info.Title = "Created with PDFsharp";
16
48
  XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
17
49
  XImage image = XImage.FromFile(@"file_icon_text_pdf.png");
18
-
19
-
20
- // 1ページ目 通常
21
50
  PdfPage page = document.AddPage();
22
51
  XGraphics gfx = XGraphics.FromPdfPage(page);
23
-
24
52
  gfx.DrawString("Hello, World!", font, XBrushes.Black,
25
- new XRect(0, 0, page.Width, page.Height),
53
+ new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
26
- XStringFormats.Center);
27
54
  gfx.DrawImage(image, 0, 0);
55
+ document.Save(outputFilePath);
56
+ }
57
+ static void Main()
58
+ {
59
+ string input = @"HelloWorld.pdf";
60
+ string output = "HelloWorld_Mirror.pdf";
61
+ SamplePdf(input);
28
62
 
29
-
30
- // 2ページ目 ミラー
31
- page = document.AddPage();
32
- gfx = XGraphics.FromPdfPage(page);
33
-
34
- gfx.TranslateTransform(page.Width, 0);
35
- gfx.ScaleTransform(-1, 1);
36
-
37
- gfx.DrawString("Hello, World!", font, XBrushes.Black,
38
- new XRect(0, 0, page.Width, page.Height),
39
- XStringFormats.Center);
40
- gfx.DrawImage(image, 0, 0);
63
+ MirrorPdf(input, output);
41
-
42
-
43
- const string filename = "HelloWorld.pdf";
44
- document.Save(filename);
45
- Process.Start(filename);
64
+ Process.Start(output);
46
65
  }
47
66
  }
48
67
  }
49
- ```
68
+ ```
50
- ![pdf](90609e8687bb967ce9d3a7284da6ddc2.png)
51
- 初めて使ったので何か頓珍漢なことをしているかもしれません。
52
- 画像は「いらすとや」さんからお借りしました。

1

PDFsharp

2019/12/05 08:32

投稿

TN8001
TN8001

スコア10118

answer CHANGED
@@ -1,4 +1,4 @@
1
- PdfSharpのサンプルのつぎはぎですが、こういうことでいいのでしょうか?
1
+ PDFsharpのサンプルのつぎはぎですが、こういうことでいいのでしょうか?
2
2
 
3
3
  ```C#
4
4
  using System.Diagnostics;