Skip to content

Commit 62e18db

Browse files
committed
Save image as PDF using PDFsharp
1 parent f58eec0 commit 62e18db

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

PasteIntoFile/ClipboardContents.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using System.Text.RegularExpressions;
1212
using System.Windows.Forms;
1313
using PasteIntoFile.Properties;
14+
using PdfSharp.Drawing;
15+
using PdfSharp.Pdf;
1416

1517
namespace PasteIntoFile {
1618

@@ -66,9 +68,28 @@ public ImageContent(Image image) {
6668
Data = image;
6769
}
6870
public Image Image => Data as Image;
69-
public override string[] Extensions => new[] { "png", "bmp", "emf", "gif", "ico", "jpg", "tif", "wmf" };
71+
public override string[] Extensions => new[] { "png", "bmp", "emf", "gif", "ico", "jpg", "pdf", "tif", "wmf" };
7072
public override string Description => string.Format(Resources.str_preview_image, Image.Width, Image.Height);
7173
public override void SaveAs(string path, string extension) {
74+
if (extension == "pdf") {
75+
// convert image
76+
var stream = new MemoryStream();
77+
Image.Save(stream, ImageFormat.Png);
78+
stream.Position = 0;
79+
XImage img = XImage.FromStream(stream);
80+
// create pdf document
81+
PdfDocument document = new PdfDocument();
82+
document.Info.Creator = Resources.str_main_window_title;
83+
PdfPage page = document.AddPage();
84+
page.Width = XUnit.FromPoint(img.PointWidth);
85+
page.Height = XUnit.FromPoint(img.PointHeight);
86+
// insert image and save
87+
XGraphics gfx = XGraphics.FromPdfPage(page);
88+
gfx.DrawImage(img, 0, 0);
89+
document.Save(path);
90+
return;
91+
}
92+
// natively supported formats
7293
ImageFormat imageFormat;
7394
switch (extension) {
7495
case "bmp": imageFormat = ImageFormat.Bmp; break;

PasteIntoFile/PasteIntoFile.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
<PackageReference Include="BetterFolderBrowser" Version="1.0.0" />
184184
<PackageReference Include="CommandLineParser" Version="2.8.0" />
185185
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.2" />
186+
<PackageReference Include="PDFsharp" Version="1.50.5147" />
186187
<PackageReference Include="SharpClipboard" Version="3.5.2" />
187188
</ItemGroup>
188189
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

0 commit comments

Comments
 (0)