Skip to content

Commit

Permalink
Merge pull request #56 from egonl/2.4-beta
Browse files Browse the repository at this point in the history
Merged v2.4 beta
  • Loading branch information
egonl authored Jan 30, 2023
2 parents 95996e2 + 9f7f818 commit 0ffcdc2
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 83 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,4 @@ FodyWeavers.xsd

# SharpDocx
/SharpDocx/nupkg/push.bat
/Samples/Documents
Binary file modified Samples/Documents/Tutorial.docx
Binary file not shown.
Binary file modified Samples/Views/Tutorial.cs.docx
Binary file not shown.
101 changes: 90 additions & 11 deletions SharpDocx/DocumentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using SharpDocx.CodeBlocks;
using SharpDocx.Extensions;
using System.Diagnostics;
using SharpImage;

namespace SharpDocx
{
Expand Down Expand Up @@ -211,22 +212,94 @@ protected void Image(string filePath, int percentage = 100)

if (!File.Exists(filePath))
{
#if DEBUG
CurrentCodeBlock.Placeholder.Text = $"Image '{filePath}' not found.";
#endif
HandleError($"Image '{filePath}' not found.");
return;
}

var imageTypePart = ImageHelper.GetImagePartType(filePath);

const long emusPerTwip = 635;
var maxWidthInEmus = GetPageContentWidthInTwips() * emusPerTwip;
var extension = Path.GetExtension(filePath);
var imagePartType = ImageHelper.GetImagePartType(extension);

Drawing drawing;
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
drawing = ImageHelper.CreateDrawing(Package, fs, imageTypePart, percentage, maxWidthInEmus);
ImageInternal(fs, imagePartType, percentage);
}
}

protected void ImageFromBase64(string base64, int percentage = 100, string extension = null)
{
if (string.IsNullOrEmpty(base64))
{
HandleError($"{nameof(base64)} can't be null or empty.");
return;
}

byte[] bytes;
try
{
bytes = Convert.FromBase64String(base64);
}
catch
{
HandleError("Image string is not a properly base64 encoded string.");
return;
}

using (var ms = new MemoryStream(bytes))
{
ImageFromStream(ms, percentage, extension);
}
}

protected void ImageFromUrl(string url, int percentage = 100, string extension = null)
{
try
{
byte[] imageData = null;

using (var wc = new System.Net.WebClient())
imageData = wc.DownloadData(url);

using (var stream = new MemoryStream(imageData))
{
ImageFromStream(stream, percentage, extension);
}
}
catch
{
HandleError($"Failed to load image from {url}.");
return;
}
}

protected void ImageFromStream(Stream stream, int percentage = 100, string extension = null)
{
ImagePartType imagePartType;

if (extension == null)
{
var info = ImageInfo.GetInfo(stream);
if (info == null)
{
HandleError("Image could not be loaded.");
return;
}

imagePartType = ImageHelper.GetImagePartType(info.Type);
}
else
{
imagePartType = ImageHelper.GetImagePartType(extension);
}

ImageInternal(stream, imagePartType, percentage);
}

protected void ImageInternal(Stream stream, ImagePartType type, int percentage = 100)
{
const long emusPerTwip = 635;
var maxWidthInEmus = GetPageContentWidthInTwips() * emusPerTwip;

Drawing drawing = ImageHelper.CreateDrawing(Package, stream, type, percentage, maxWidthInEmus);

CurrentCodeBlock.Placeholder.InsertAfterSelf(drawing);

Expand All @@ -237,6 +310,14 @@ protected void Image(string filePath, int percentage = 100)
}
}

protected void HandleError(string errorMessage)
{
#if DEBUG
Console.WriteLine($"ERROR: {errorMessage}");
CurrentCodeBlock.Placeholder.Text = errorMessage;
#endif
}

protected long GetPageContentWidthInTwips()
{
long width = 10000;
Expand Down Expand Up @@ -317,7 +398,5 @@ private List<DocProperties> GetDocProperties()

return list;
}


}
}
80 changes: 53 additions & 27 deletions SharpDocx/ImageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static Drawing CreateDrawing(

using (imageStream)
{
var type = GetImageInfoType(imagePartType);
var type = GetImageType(imagePartType);
imageInfo = ImageInfo.GetInfo(type, imageStream);
if (imageInfo == null)
{
Expand Down Expand Up @@ -61,82 +61,108 @@ public static Drawing CreateDrawing(
return drawing;
}

public static ImagePartType GetImagePartType(string filePath)
internal static ImagePartType GetImagePartType(string extension)
{
var extension = Path.GetExtension(filePath);
if (extension == null)
{
throw new ArgumentException("Unknown extension", filePath);
throw new ArgumentException("Unknown extension", nameof(extension));
}

// TODO: test which types actually work.
ImagePartType? type = null;
switch (extension.ToLower())

extension = extension.Replace(".", "").ToLower();
switch (extension)
{
case ".bmp":
case "bmp":
type = ImagePartType.Bmp;
break;
case ".emf":
case "emf": // untested
type = ImagePartType.Emf;
break;
case ".gif":
case "gif":
type = ImagePartType.Gif;
break;
case ".ico":
case ".icon":
case "ico": // untested
case "icon":
type = ImagePartType.Icon;
break;
case ".jpg":
case ".jpeg":
case "jpg":
case "jpeg":
type = ImagePartType.Jpeg;
break;
case ".pcx":
case "pcx": // untested
type = ImagePartType.Pcx;
break;
case ".png":
case "png":
type = ImagePartType.Png;
break;
case ".tif":
case ".tiff":
case "tif":
case "tiff":
type = ImagePartType.Tiff;
break;
case ".wmf":
case "wmf":
type = ImagePartType.Wmf;
break;
}

if (type == null)
{
throw new ArgumentException("Unknown extension", filePath);
throw new ArgumentException("Unknown extension", nameof(extension));
}

return type.Value;
}

public static ImageInfo.Type GetImageInfoType(ImagePartType imagePartType)
internal static ImageType GetImageType(ImagePartType imagePartType)
{
switch (imagePartType)
{
case ImagePartType.Bmp:
return ImageInfo.Type.Bmp;
return ImageType.Bmp;

case ImagePartType.Gif:
return ImageInfo.Type.Gif;
return ImageType.Gif;

case ImagePartType.Jpeg:
return ImageInfo.Type.Jpeg;
return ImageType.Jpeg;

case ImagePartType.Png:
return ImageInfo.Type.Png;
return ImageType.Png;

case ImagePartType.Tiff:
return ImageInfo.Type.Tiff;
return ImageType.Tiff;

case ImagePartType.Emf:
return ImageInfo.Type.Emf;
return ImageType.Emf;
}

return ImageType.Unknown;
}

internal static ImagePartType GetImagePartType(ImageType imageType)
{
switch (imageType)
{
case ImageType.Bmp:
return ImagePartType.Bmp;

case ImageType.Gif:
return ImagePartType.Gif;

case ImageType.Jpeg:
return ImagePartType.Jpeg;

case ImageType.Png:
return ImagePartType.Png;

case ImageType.Tiff:
return ImagePartType.Tiff;

case ImageType.Emf:
return ImagePartType.Emf;
}

return ImageInfo.Type.Unknown;
return (ImagePartType) (-1);
}

private static Drawing GetDrawing(string relationshipId, long widthEmus, long heightEmus)
Expand Down
10 changes: 5 additions & 5 deletions SharpDocx/SharpDocx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
<TargetFrameworks>net48;netstandard2.0</TargetFrameworks>
<!--<TargetFrameworks>net35;net40;net45;netstandard2.0</TargetFrameworks>-->
<RootNamespace>SharpDocx</RootNamespace>
<Version>2.3.0.0</Version>
<Version>2.4.0.0</Version>
<Authors>egonl</Authors>
<Product>SharpDocx</Product>
<PackageId>SharpDocx</PackageId>
<AssemblyVersion>2.3.0.0</AssemblyVersion>
<FileVersion>2.3.0.0</FileVersion>
<AssemblyVersion>2.4.0.0</AssemblyVersion>
<FileVersion>2.4.0.0</FileVersion>
<NeutralLanguage>en</NeutralLanguage>
<Description>C# based template engine for generating Word documents</Description>
<Copyright>Copyright 2017-2022 egonl</Copyright>
<Copyright>Copyright 2017-2023 egonl</Copyright>
<PackageLicenseUrl>https://github.com/egonl/SharpDocx/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/egonl/SharpDocx</PackageProjectUrl>
<!-- <PackageIconUrl>Icon URL</PackageIconUrl> -->
Expand All @@ -37,7 +37,7 @@
<ItemGroup>
<ProjectReference Include="..\SharpImage\SharpImage.csproj" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.18.0" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.19.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
Expand Down
8 changes: 6 additions & 2 deletions SharpDocx/nupkg/SharpDocx.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>SharpDocx</id>
<version>2.3.0</version>
<version>2.4.0</version>
<authors>egonl</authors>
<owners>egonl</owners>
<license type="expression">MIT</license>
Expand All @@ -16,7 +16,11 @@
- Supports .NET Framework 3.5/4.5+, .NET Standard 2.0, .NET Core 3.1, .NET 5.0 and .NET 6.0.
- Based on Microsoft's Open XML SDK.
- Source code, tutorial and examples on GitHub.</description>
<releaseNotes>Version 2.3.0 - October 9, 2022
<releaseNotes>Version 2.4.0 - January 30, 2023
- Added ImageFromBase64, ImageFromUrl and ImageFromStream methods.
- Added ImageFromBase64 and ImageFromUrl examples to the tutorial.

Version 2.3.0 - October 9, 2022
- Added support for EMF files (issue #51).
- The IDs of DocProperties are now always unique (issue #51).
- Changed the default for TargetFrameworks to net48 and net6.0 to better support Visual Studio 2022.
Expand Down
4 changes: 4 additions & 0 deletions SharpImage/BmpInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class BmpInfo : ImageInfoBase
{
public override void Init(Stream stream)
{
Type = ImageType.Unknown;

using (var bh = new ByteHelper(stream))
{
bh.Seek(0, SeekOrigin.Begin);
Expand Down Expand Up @@ -36,6 +38,8 @@ public override void Init(Stream stream)
DpiV = dpiV;
}
}

Type = ImageType.Bmp;
}
}
}
3 changes: 3 additions & 0 deletions SharpImage/EmfInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class EmfInfo : ImageInfoBase
public override void Init(Stream stream)
{
// Inspired by https://www.codeproject.com/Articles/1307140/Parse-understand-and-demystify-Enhanced-Meta-Files
Type = ImageType.Unknown;

using (var bh = new ByteHelper(stream))
{
Expand Down Expand Up @@ -37,6 +38,8 @@ public override void Init(Stream stream)
DpiH = (int)(((long)deviceWidth * 254 / mmWidth + 5) / 10);
DpiV = (int)(((long)deviceHeight * 254 / mmHeight + 5) / 10);
}

Type = ImageType.Emf;
}
}
}
4 changes: 4 additions & 0 deletions SharpImage/GifInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class GifInfo : ImageInfoBase
{
public override void Init(Stream stream)
{
Type = ImageType.Unknown;

using (var bh = new ByteHelper(stream))
{
bh.Seek(0, SeekOrigin.Begin);
Expand All @@ -29,6 +31,8 @@ public override void Init(Stream stream)
DpiH = 72;
DpiV = 72;
}

Type = ImageType.Gif;
}
}
}
Loading

0 comments on commit 0ffcdc2

Please sign in to comment.