Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions DocXToPdfConverter/DocXToPdfConverter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<ApplicationIcon />
<StartupObject />
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand All @@ -19,7 +19,8 @@

<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="2.10.0-beta0002" />
<PackageReference Include="System.Drawing.Common" Version="4.5.1" />
<PackageReference Include="IronSoftware.System.Drawing" Version="2023.7.1" />
<PackageReference Include="SkiaSharp" Version="2.88.3" />
</ItemGroup>

</Project>
33 changes: 16 additions & 17 deletions DocXToPdfConverter/DocXToPdfHandlers/ImageHandler.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,46 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using DocumentFormat.OpenXml.Packaging;
using IronSoftware.Drawing;
using static IronSoftware.Drawing.AnyBitmap;

namespace DocXToPdfConverter.DocXToPdfHandlers
{
public static class ImageHandler
{

public static Image GetImage(this MemoryStream ms)
public static AnyBitmap GetImage(this MemoryStream ms)
{
ms.Position = 0;
var image = Image.FromStream(ms);
var image = AnyBitmap.FromStream(ms);
ms.Position = 0;
return image;
}

public static ImagePartType GetImagePartType(this MemoryStream stream)
{
stream.Position = 0;
using (var image = Image.FromStream(stream))
using (var image = AnyBitmap.FromStream(stream))
{
stream.Position = 0;


if (ImageFormat.Jpeg.Equals(image.RawFormat))
if (ImageFormat.Jpeg.Equals(image.GetImageFormat()))
{
return ImagePartType.Jpeg;
}
else if (ImageFormat.Png.Equals(image.RawFormat))
else if (ImageFormat.Png.Equals(image.GetImageFormat()))
{
return ImagePartType.Png;
}
else if (ImageFormat.Gif.Equals(image.RawFormat))
else if (ImageFormat.Gif.Equals(image.GetImageFormat()))
{
return ImagePartType.Gif;
}
else if (ImageFormat.Bmp.Equals(image.RawFormat))
else if (ImageFormat.Bmp.Equals(image.GetImageFormat()))
{
return ImagePartType.Bmp;
}
else if (ImageFormat.Tiff.Equals(image.RawFormat))
else if (ImageFormat.Tiff.Equals(image.GetImageFormat()))
{
return ImagePartType.Tiff;
}
Expand All @@ -53,27 +52,27 @@ public static ImagePartType GetImagePartType(this MemoryStream stream)
public static string GetImageType(this MemoryStream stream)
{
stream.Position = 0;
using (var image = Image.FromStream(stream))
using (var image = AnyBitmap.FromStream(stream))
{
stream.Position = 0;

if (ImageFormat.Jpeg.Equals(image.RawFormat))
if (ImageFormat.Jpeg.Equals(image.GetImageFormat()))
{
return "jpeg";
}
else if (ImageFormat.Png.Equals(image.RawFormat))
else if (ImageFormat.Png.Equals(image.GetImageFormat()))
{
return "png";
}
else if (ImageFormat.Gif.Equals(image.RawFormat))
else if (ImageFormat.Gif.Equals(image.GetImageFormat()))
{
return "gif";
}
else if (ImageFormat.Bmp.Equals(image.RawFormat))
else if (ImageFormat.Bmp.Equals(image.GetImageFormat()))
{
return "bmp";
}
else if (ImageFormat.Tiff.Equals(image.RawFormat))
else if (ImageFormat.Tiff.Equals(image.GetImageFormat()))
{
return "tiff";
}
Expand Down
8 changes: 4 additions & 4 deletions DocXToPdfConverter/Documentation/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ Report from DOCX / HTML to PDF Converter can parse the source document and intro
Don't get scared away that I use LibreOffice, it is easier than you may think!
1. LibreOffice - just get the PORTABLE EDITION as you don't screw up your webserver with an installation. The portable version just runs without any installation. We need LibreOffice for converting from DOCX or from HTML to PDF and DOCX, etc

2. Nuget:
* Microsoft.NetCore.App
2. Nuget:
* Document.Format.OpenXml
* System.Drawing.Common
* IronSoftware.System.Drawing;
* SkiaSharp

3. The OpenXml PowerTools (thanks to Eric White for this great work), which I already included into the project, the whole code!

## Get started here!

1. Add dependencies to a) Document.Format.OpenXml (by Microsoft). For the OpenXml Powertools by Eric White (which are already included in the project), add System.Drawing.Common (for .NET Core). The regular System.Drawing won't work!
1. Add dependencies to a) Document.Format.OpenXml (by Microsoft). For the OpenXml Powertools by Eric White (which are already included in the project).

2. Download LibreOffice (https://www.libreoffice.org/download/portable-versions/). I recommend the portable edition as it does not install anything in your server. It is like unzipping files onto your harddrive. Note the path to "soffice.exe" (I don't know what the file is called in Linux / MacOS, probably just soffice. It is an executable to run a headless, mute version of LibreOffice for conversion processes). On my Windows machine, it is under: C:\PortableApps\LibreOfficePortable\App\libreoffice\program\soffice.exe.

Expand Down
5 changes: 2 additions & 3 deletions DocXToPdfConverter/OpenXmlPowerTools/ColorParser.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Drawing;
using IronSoftware.Drawing;

namespace OpenXmlPowerTools
{
Expand All @@ -18,7 +17,7 @@ public static bool TryFromName(string name, out Color color)
{
color = Color.FromName(name);

return color.IsNamedColor;
return color != null;
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Drawing;
using IronSoftware.Drawing;
using System.Xml.Linq;

namespace OpenXmlPowerTools
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Drawing;
using IronSoftware.Drawing;

namespace OpenXmlPowerTools
{
Expand Down
18 changes: 18 additions & 0 deletions DocXToPdfConverter/OpenXmlPowerTools/FontFamily.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;
using SkiaSharp;

namespace DocXToPdfConverter
{
public static class FontFamily
{
public static IEnumerable<string> GetFontFamilies()
{
using (var fontManager = SKFontManager.Default)
{
return fontManager.FontFamilies;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Text;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;
using System.Drawing;
using IronSoftware.Drawing;

namespace OpenXmlPowerTools
{
Expand Down
6 changes: 0 additions & 6 deletions DocXToPdfConverter/OpenXmlPowerTools/HtmlToWmlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;
using OpenXmlPowerTools;
using OpenXmlPowerTools.HtmlToWml;
using OpenXmlPowerTools.HtmlToWml.CSS;
using System.Text.RegularExpressions;

namespace OpenXmlPowerTools
Expand Down
41 changes: 20 additions & 21 deletions DocXToPdfConverter/OpenXmlPowerTools/HtmlToWmlConverterCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
Expand All @@ -109,6 +107,8 @@
using OpenXmlPowerTools.HtmlToWml;
using OpenXmlPowerTools.HtmlToWml.CSS;
using System.Text.RegularExpressions;
using IronSoftware.Drawing;
using DocXToPdfConverter;

namespace OpenXmlPowerTools.HtmlToWml
{
Expand Down Expand Up @@ -1159,10 +1159,10 @@ private static XElement FontMerge(XElement higherPriorityFont, XElement lowerPri
return 0;

// in theory, all unknown fonts are found by the above test, but if not...
FontFamily ff;
string ff;
try
{
ff = new FontFamily(fontName);
ff = (fontName);
}
catch (ArgumentException)
{
Expand Down Expand Up @@ -1885,9 +1885,9 @@ private static HashSet<string> KnownFamilies
if (_knownFamilies == null)
{
_knownFamilies = new HashSet<string>();
var families = FontFamily.Families;
var families = FontFamily.GetFontFamilies();
foreach (var fam in families)
_knownFamilies.Add(fam.Name);
_knownFamilies.Add(fam);
}
return _knownFamilies;
}
Expand Down Expand Up @@ -2279,7 +2279,7 @@ private static XElement TransformImageToWml(XElement element, HtmlToWmlConverter
{
string srcAttribute = (string)element.Attribute(XhtmlNoNamespace.src);
byte[] ba = null;
Bitmap bmp = null;
AnyBitmap bmp = null;

if (srcAttribute.StartsWith("data:"))
{
Expand All @@ -2289,14 +2289,14 @@ private static XElement TransformImageToWml(XElement element, HtmlToWmlConverter
ba = Convert.FromBase64String(base64);
using (MemoryStream ms = new MemoryStream(ba))
{
bmp = new Bitmap(ms);
bmp = new AnyBitmap(ms);
}
}
else
{
try
{
bmp = new Bitmap(settings.BaseUriForImages + "/" + srcAttribute);
bmp = new AnyBitmap(settings.BaseUriForImages + "/" + srcAttribute);
}
catch (ArgumentException)
{
Expand All @@ -2307,13 +2307,13 @@ private static XElement TransformImageToWml(XElement element, HtmlToWmlConverter
return null;
}
MemoryStream ms = new MemoryStream();
bmp.Save(ms, bmp.RawFormat);
bmp.ExportStream(ms, bmp.GetImageFormat());
ba = ms.ToArray();
}

MainDocumentPart mdp = wDoc.MainDocumentPart;
string rId = "R" + Guid.NewGuid().ToString().Replace("-", "");
ImagePartType ipt = ImagePartType.Png;
var ipt = ImagePartType.Png;
ImagePart newPart = mdp.AddImagePart(ipt, rId);
using (Stream s = newPart.GetStream(FileMode.Create, FileAccess.ReadWrite))
s.Write(ba, 0, ba.GetUpperBound(0) + 1);
Expand Down Expand Up @@ -2352,7 +2352,7 @@ private static XElement TransformImageToWml(XElement element, HtmlToWmlConverter
return null;
}

private static XElement GetImageAsInline(XElement element, HtmlToWmlConverterSettings settings, WordprocessingDocument wDoc, Bitmap bmp,
private static XElement GetImageAsInline(XElement element, HtmlToWmlConverterSettings settings, WordprocessingDocument wDoc, AnyBitmap bmp,
string rId, int pictureId, string pictureDescription)
{
XElement inline = new XElement(WP.inline, // 20.4.2.8
Expand All @@ -2369,7 +2369,7 @@ private static XElement GetImageAsInline(XElement element, HtmlToWmlConverterSet
return inline;
}

private static XElement GetImageAsAnchor(XElement element, HtmlToWmlConverterSettings settings, WordprocessingDocument wDoc, Bitmap bmp,
private static XElement GetImageAsAnchor(XElement element, HtmlToWmlConverterSettings settings, WordprocessingDocument wDoc, AnyBitmap bmp,
string rId, string floatValue, int pictureId, string pictureDescription)
{
Emu minDistFromEdge = (long)(0.125 * Emu.s_EmusPerInch);
Expand Down Expand Up @@ -2550,13 +2550,12 @@ private static XElement GetRunPropertiesForImage()
new XElement(W.noProof));
}

private static SizeEmu GetImageSizeInEmus(XElement img, Bitmap bmp)
private static SizeEmu GetImageSizeInEmus(XElement img, AnyBitmap bmp)
{
double hres = bmp.HorizontalResolution;
double vres = bmp.VerticalResolution;
Size s = bmp.Size;
Emu cx = (long)((double)(s.Width / hres) * (double)Emu.s_EmusPerInch);
Emu cy = (long)((double)(s.Height / vres) * (double)Emu.s_EmusPerInch);
double? hres = bmp.HorizontalResolution;
double? vres = bmp.VerticalResolution;
Emu cx = (long)((double)(bmp.Width / hres) * (double)Emu.s_EmusPerInch);
Emu cy = (long)((double)(bmp.Height / vres) * (double)Emu.s_EmusPerInch);

CssExpression width = img.GetProp("width");
CssExpression height = img.GetProp("height");
Expand All @@ -2583,7 +2582,7 @@ private static SizeEmu GetImageSizeInEmus(XElement img, Bitmap bmp)
return new SizeEmu(cx, cy);
}

private static XElement GetImageExtent(XElement img, Bitmap bmp)
private static XElement GetImageExtent(XElement img, AnyBitmap bmp)
{
SizeEmu szEmu = GetImageSizeInEmus(img, bmp);
return new XElement(WP.extent,
Expand Down Expand Up @@ -2616,7 +2615,7 @@ private static XElement GetCNvGraphicFramePr()
new XAttribute(NoNamespace.noChangeAspect, 1)));
}

private static XElement GetGraphicForImage(XElement element, string rId, Bitmap bmp, int pictureId, string pictureDescription)
private static XElement GetGraphicForImage(XElement element, string rId, AnyBitmap bmp, int pictureId, string pictureDescription)
{
SizeEmu szEmu = GetImageSizeInEmus(element, bmp);
XElement graphic = new XElement(A.graphic,
Expand Down
2 changes: 1 addition & 1 deletion DocXToPdfConverter/OpenXmlPowerTools/HtmlToWmlCssParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using IronSoftware.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
Expand Down
Loading