-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganized stuff and finished declaring and using XObject resources.
- Loading branch information
1 parent
acb7720
commit 9d4d948
Showing
10 changed files
with
188 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace BlastPDF.Builder.Graphics.Drawing; | ||
|
||
public static class PdfImageExtensions { | ||
public static PdfGraphicsObject Image(this PdfGraphicsObject graphics, string resource) { | ||
graphics.SubObjects.Add(new PdfXObject { Resource = resource }); | ||
return graphics; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...tPDF/Builder/Resources/PdfFontResource.cs → ...Builder/Resources/Font/PdfFontResource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
BlastPDF/Builder/Resources/PdfFontType1.cs → ...DF/Builder/Resources/Font/PdfFontType1.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System; | ||
using BlastPDF.Builder.Graphics; | ||
using BlastPDF.Filter; | ||
using SharperImage; | ||
using SharperImage.Formats; | ||
|
||
namespace BlastPDF.Builder.Resources.Image; | ||
|
||
public class PdfImageResource : PdfObject | ||
{ | ||
public uint Width { get; set; } | ||
public uint Height { get; set; } | ||
public PdfColorSpace ColorSpace { get; set; } | ||
public int BitsPerComponent { get; set; } | ||
public PdfFilter[] Filters { get; set; } | ||
|
||
public IImage ImageData { get; set; } | ||
|
||
public static PdfImageResource FromFile(string filename, FileFormat format, PdfColorSpace colorSpace, PdfFilter[] filters) | ||
{ | ||
var image = IImage.Decode(filename, format); | ||
return FromImage(image, colorSpace, filters); | ||
} | ||
|
||
public static PdfImageResource FromImage(IImage image, PdfColorSpace colorSpace, PdfFilter[] filters) | ||
{ | ||
var result = new PdfImageResource { | ||
Width = image.Width(), | ||
Height = image.Height(), | ||
ColorSpace = colorSpace, | ||
BitsPerComponent = 8, // TODO I would like this to be figured out from the pixel format in the image | ||
ImageData = image, | ||
Filters = filters, | ||
}; | ||
|
||
if (filters is null) | ||
{ | ||
result.Filters = new[] {PdfFilter.AsciiHex}; | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
public static class PdfImageResourceExtensions | ||
{ | ||
public static PdfPage UseImage(this PdfPage page, string resourceName, string filename, FileFormat format, PdfColorSpace colorSpace = PdfColorSpace.DeviceRGB, PdfFilter[] filters = null) | ||
{ | ||
if (string.IsNullOrEmpty(resourceName)) throw new ArgumentNullException(nameof(resourceName)); | ||
if (page.Resources.ContainsKey(resourceName)) throw new ArgumentException($"Resource '{resourceName}' already exists as a resource for this page :("); | ||
page.Resources.Add(resourceName, PdfImageResource.FromFile(filename, format, colorSpace, filters)); | ||
return page; | ||
} | ||
|
||
public static PdfDocument UseImage(this PdfDocument doc, string resourceName, string filename, FileFormat format, PdfColorSpace colorSpace = PdfColorSpace.DeviceRGB, PdfFilter[] filters = null) | ||
{ | ||
if (string.IsNullOrEmpty(resourceName)) throw new ArgumentNullException(nameof(resourceName)); | ||
if (doc.Resources.ContainsKey(resourceName)) throw new ArgumentException($"Resource '{resourceName}' already exists as a resource for this document :("); | ||
doc.Resources.Add(resourceName, PdfImageResource.FromFile(filename, format, colorSpace, filters)); | ||
return doc; | ||
} | ||
|
||
public static PdfPage UseImage(this PdfPage page, string resourceName, IImage image, PdfColorSpace colorSpace = PdfColorSpace.DeviceRGB, PdfFilter[] filters = null) | ||
{ | ||
if (string.IsNullOrEmpty(resourceName)) throw new ArgumentNullException(nameof(resourceName)); | ||
if (page.Resources.ContainsKey(resourceName)) throw new ArgumentException($"Resource '{resourceName}' already exists as a resource for this page :("); | ||
page.Resources.Add(resourceName, PdfImageResource.FromImage(image, colorSpace, filters)); | ||
return page; | ||
} | ||
|
||
public static PdfDocument UseImage(this PdfDocument doc, string resourceName, IImage image, PdfColorSpace colorSpace = PdfColorSpace.DeviceRGB, PdfFilter[] filters = null) | ||
{ | ||
if (string.IsNullOrEmpty(resourceName)) throw new ArgumentNullException(nameof(resourceName)); | ||
if (doc.Resources.ContainsKey(resourceName)) throw new ArgumentException($"Resource '{resourceName}' already exists as a resource for this document :("); | ||
doc.Resources.Add(resourceName, PdfImageResource.FromImage(image, colorSpace, filters)); | ||
return doc; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
|
||
namespace BlastPDF.Builder.Resources; | ||
|
||
public static class PdfResource | ||
{ | ||
public static PdfPage UseObject(this PdfPage page, string resourceName, PdfObject resource) | ||
{ | ||
if (string.IsNullOrEmpty(resourceName)) throw new ArgumentNullException(nameof(resourceName)); | ||
if (page.Resources.ContainsKey(resourceName)) throw new ArgumentException($"Resource '{resourceName}' already exists as a resource for this page :("); | ||
page.Resources.Add(resourceName, resource); | ||
return page; | ||
} | ||
|
||
public static PdfDocument UseObject(this PdfDocument doc, string resourceName, PdfObject resource) | ||
{ | ||
if (string.IsNullOrEmpty(resourceName)) throw new ArgumentNullException(nameof(resourceName)); | ||
if (doc.Resources.ContainsKey(resourceName)) throw new ArgumentException($"Resource '{resourceName}' already exists as a resource for this document :("); | ||
doc.Resources.Add(resourceName, resource); | ||
return doc; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters