├── PdfFileWriter
├── PdfPage.cs
├── PdfFileWriter.csproj.user
├── PdfFileWriter.sln
├── Properties
│ └── AssemblyInfo.cs
├── PdfLayer.cs
├── PdfExtGState.cs
├── PdfImageControl.cs
├── PdfBinaryWriter.cs
├── PdfXObject.cs
├── PdfWebLink.cs
├── PdfShadingFunction.cs
├── PdfRectangle.cs
├── CreateMetafile.cs
├── PdfFileWriter.csproj
├── PdfInfo.cs
├── ImageSizePos.cs
├── PdfLayers.cs
├── PdfAnnotation.cs
├── PdfEmbeddedFile.cs
├── AnnotAction.cs
├── LocationMarker.cs
├── PdfAxialShading.cs
├── ArcToBezier.cs
├── PdfPrintDocument.cs
├── PdfRadialShading.cs
├── PdfChart.cs
├── PdfTableStyle.cs
├── PdfBookmark.cs
├── PdfTilingPattern.cs
├── PdfDictionary.cs
├── PdfDisplayMedia.cs
└── PdfFontFileClasses.cs
└── README.md
/PdfFileWriter/PdfPage.cs:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tossnet/PdfFileWriter/master/PdfFileWriter/PdfPage.cs
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PdfFileWriter
2 | # Version 1.25.0 2019/07/15
3 | The PDF File Writer C# class library PdfFileWriter allows you to create PDF files directly from your .net application. The library shields you from the details of the PDF file structure.
4 |
5 | Version 1.25.0 enhancements: Support for font collections and for non-ASCII font names. This mainly applies to Japanese, Chinese and Korean fonts.
6 |
7 | For documentation and further information visit PDF File Writer C# Class Library (Version 1.25.0)
8 |
--------------------------------------------------------------------------------
/PdfFileWriter/PdfFileWriter.csproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | ShowAllFiles
13 |
14 |
--------------------------------------------------------------------------------
/PdfFileWriter/PdfFileWriter.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 11.00
3 | # Visual C# Express 2010
4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PdfFileWriter", "PdfFileWriter.csproj", "{7B531568-9C87-4CC5-917D-28C43BCF5B38}"
5 | EndProject
6 | Global
7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
8 | Debug|Any CPU = Debug|Any CPU
9 | Release|Any CPU = Release|Any CPU
10 | EndGlobalSection
11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
12 | {7B531568-9C87-4CC5-917D-28C43BCF5B38}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13 | {7B531568-9C87-4CC5-917D-28C43BCF5B38}.Debug|Any CPU.Build.0 = Debug|Any CPU
14 | {7B531568-9C87-4CC5-917D-28C43BCF5B38}.Release|Any CPU.ActiveCfg = Release|Any CPU
15 | {7B531568-9C87-4CC5-917D-28C43BCF5B38}.Release|Any CPU.Build.0 = Release|Any CPU
16 | EndGlobalSection
17 | GlobalSection(SolutionProperties) = preSolution
18 | HideSolutionNode = FALSE
19 | EndGlobalSection
20 | EndGlobal
21 |
--------------------------------------------------------------------------------
/PdfFileWriter/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("PdfFileWriter")]
9 | [assembly: AssemblyDescription("PDF File Writer C# Class Library")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("Uzi Granot")]
12 | [assembly: AssemblyProduct("PdfFileWriter Version 1.25.0")]
13 | [assembly: AssemblyCopyright("Copyright © 2013-2019 Uzi Granot. All rights reserved.")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("27b9fc16-eb56-4ccf-aa84-723735483d25")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.1.25.0")]
36 | [assembly: AssemblyFileVersion("1.1.25.0")]
37 |
--------------------------------------------------------------------------------
/PdfFileWriter/PdfLayer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace PdfFileWriter
8 | {
9 | ///
10 | /// Lock/unlock layer enumeration
11 | ///
12 | public enum LockLayer
13 | {
14 | ///
15 | /// Unlock layer (default)
16 | ///
17 | Unlocked,
18 |
19 | ///
20 | /// Lock layer
21 | ///
22 | Locked,
23 | }
24 |
25 | ///
26 | /// Layer state
27 | ///
28 | public enum LayerState
29 | {
30 | ///
31 | /// Layer state is ON
32 | ///
33 | On,
34 |
35 | ///
36 | /// Layer state is OFF
37 | ///
38 | Off,
39 | }
40 |
41 | ///
42 | /// PdfLayer class
43 | ///
44 | public class PdfLayer : PdfObject, IComparable
45 | {
46 | ///
47 | /// Layer name
48 | ///
49 | public string Name {get; private set;}
50 |
51 | ///
52 | /// Layer locked or unlocked
53 | ///
54 | public LockLayer Locked {get; set;}
55 |
56 | ///
57 | /// Initial layer state (on or off)
58 | ///
59 | public LayerState State {get; set;}
60 |
61 | ///
62 | /// Layer is a radio button
63 | ///
64 | public string RadioButton {get; set;}
65 |
66 | internal PdfLayers LayersParent;
67 |
68 | ///
69 | /// Layer constructor
70 | ///
71 | /// Layers parent
72 | /// Layer's name
73 | public PdfLayer
74 | (
75 | PdfLayers LayersParent,
76 | string Name
77 | ) : base(LayersParent.Document, ObjectType.Dictionary, "/OCG")
78 | {
79 | // save arguments
80 | this.Name = Name;
81 |
82 | // save layers parent
83 | this.LayersParent = LayersParent;
84 |
85 | // create resource code
86 | ResourceCode = Document.GenerateResourceNumber('O');
87 |
88 | // add layer name to the dictionary
89 | Dictionary.AddPdfString("/Name", Name);
90 |
91 | // add to the list of all layers
92 | LayersParent.LayerList.Add(this);
93 |
94 | // exit
95 | return;
96 | }
97 |
98 | ///
99 | /// CompareTo for IComparabler
100 | ///
101 | /// Other layer
102 | /// Compare result
103 | public int CompareTo
104 | (
105 | PdfLayer Other
106 | )
107 | {
108 | int Cmp = string.Compare(RadioButton, Other.RadioButton);
109 | if(Cmp != 0) return Cmp;
110 | return ObjectNumber - Other.ObjectNumber;
111 | }
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/PdfFileWriter/PdfExtGState.cs:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////////
2 | //
3 | // PdfFileWriter
4 | // PDF File Write C# Class Library.
5 | //
6 | // PdfExtGState
7 | // External graphics state dictionary.
8 | //
9 | // Uzi Granot
10 | // Version: 1.0
11 | // Date: April 1, 2013
12 | // Copyright (C) 2013-2019 Uzi Granot. All Rights Reserved
13 | //
14 | // PdfFileWriter C# class library and TestPdfFileWriter test/demo
15 | // application are free software.
16 | // They is distributed under the Code Project Open License (CPOL).
17 | // The document PdfFileWriterReadmeAndLicense.pdf contained within
18 | // the distribution specify the license agreement and other
19 | // conditions and notes. You must read this document and agree
20 | // with the conditions specified in order to use this software.
21 | //
22 | // For version history please refer to PdfDocument.cs
23 | //
24 | /////////////////////////////////////////////////////////////////////
25 |
26 | using System;
27 | using System.Collections.Generic;
28 |
29 | namespace PdfFileWriter
30 | {
31 | internal class PdfExtGState : PdfObject, IComparable
32 | {
33 | internal string Key;
34 | internal string Value;
35 |
36 | // search constructor
37 | internal PdfExtGState
38 | (
39 | string Key,
40 | string Value
41 | )
42 | {
43 | // save value
44 | this.Key = Key;
45 | this.Value = Value;
46 |
47 | // exit
48 | return;
49 | }
50 |
51 | // object constructor
52 | internal PdfExtGState
53 | (
54 | PdfDocument Document,
55 | string Key,
56 | string Value
57 | ) : base(Document, ObjectType.Dictionary, "/ExtGState")
58 | {
59 | // save value
60 | this.Key = Key;
61 | this.Value = Value;
62 |
63 | // create resource code
64 | ResourceCode = Document.GenerateResourceNumber('G');
65 | return;
66 | }
67 |
68 | internal static PdfExtGState CreateExtGState
69 | (
70 | PdfDocument Document,
71 | string Key,
72 | string Value
73 | )
74 | {
75 | if(Document.ExtGStateArray == null) Document.ExtGStateArray = new List();
76 |
77 | // search list for a duplicate
78 | int Index = Document.ExtGStateArray.BinarySearch(new PdfExtGState(Key, Value));
79 |
80 | // this value is a duplicate
81 | if(Index >= 0) return Document.ExtGStateArray[Index];
82 |
83 | // new blend object
84 | PdfExtGState ExtGState = new PdfExtGState(Document, Key, Value);
85 |
86 | // save new string in array
87 | Document.ExtGStateArray.Insert(~Index, ExtGState);
88 |
89 | // update dictionary
90 | ExtGState.Dictionary.Add(Key, Value);
91 |
92 | // exit
93 | return ExtGState;
94 | }
95 |
96 | ///
97 | /// Compare two PdfExtGState objects.
98 | ///
99 | /// Other object.
100 | /// Compare result.
101 | public int CompareTo
102 | (
103 | PdfExtGState Other
104 | )
105 | {
106 | int Cmp = string.Compare(this.Key, Other.Key);
107 | if(Cmp != 0) return Cmp;
108 | return string.Compare(this.Value, Other.Value);
109 | }
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/PdfFileWriter/PdfImageControl.cs:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////////
2 | //
3 | // PdfFileWriter
4 | // PDF File Write C# Class Library.
5 | //
6 | // PdfImageControl
7 | // PDF Image control.
8 | //
9 | // Uzi Granot
10 | // Version: 1.0
11 | // Date: April 1, 2013
12 | // Copyright (C) 2013-2019 Uzi Granot. All Rights Reserved
13 | //
14 | // PdfFileWriter C# class library and TestPdfFileWriter test/demo
15 | // application are free software.
16 | // They is distributed under the Code Project Open License (CPOL).
17 | // The document PdfFileWriterReadmeAndLicense.pdf contained within
18 | // the distribution specify the license agreement and other
19 | // conditions and notes. You must read this document and agree
20 | // with the conditions specified in order to use this software.
21 | //
22 | // For version history please refer to PdfDocument.cs
23 | //
24 | /////////////////////////////////////////////////////////////////////
25 |
26 | using System;
27 | using System.Drawing;
28 | using System.Drawing.Imaging;
29 | using System.IO;
30 | using System.Text;
31 |
32 | namespace PdfFileWriter
33 | {
34 | ///
35 | /// PdfImageControl is obolete. See latest documentation
36 | ///
37 | public class PdfImageControl
38 | {
39 | #pragma warning disable 1591
40 | private const bool ObsoleteError = false;
41 | private const string ObsoleteMsg = "This PdfImageControl class is obsolete. See latest documentation.";
42 |
43 | [Obsolete(ObsoleteMsg, ObsoleteError)]
44 | public Rectangle CropRect;
45 |
46 | [Obsolete(ObsoleteMsg, ObsoleteError)]
47 | public RectangleF CropPercent;
48 |
49 | [Obsolete(ObsoleteMsg, ObsoleteError)]
50 | public bool ReverseBW;
51 |
52 | [Obsolete(ObsoleteMsg, ObsoleteError)]
53 | public double Resolution;
54 |
55 | [Obsolete(ObsoleteMsg, ObsoleteError)]
56 | public SaveImageAs SaveAs;
57 |
58 | [Obsolete(ObsoleteMsg, ObsoleteError)]
59 | public const int DefaultQuality = -1;
60 |
61 | [Obsolete(ObsoleteMsg, ObsoleteError)]
62 | public PdfImageControl()
63 | {
64 | CropRect = Rectangle.Empty;
65 | CropPercent = RectangleF.Empty;
66 | ReverseBW = false;
67 | _GrayToBWCutoff = 50;
68 | Resolution = 0.0;
69 | _ImageQuality = DefaultQuality;
70 | SaveAs = SaveImageAs.Jpeg;
71 | return;
72 | }
73 |
74 | [Obsolete(ObsoleteMsg, ObsoleteError)]
75 | public int ImageQuality
76 | {
77 | get
78 | {
79 | return _ImageQuality;
80 | }
81 | set
82 | {
83 | // set image quality
84 | if(value != DefaultQuality && (value < 0 || value > 100)) throw new ApplicationException("PdfImageControl.ImageQuality must be DefaultQuality or 0 to 100");
85 | _ImageQuality = value;
86 | return;
87 | }
88 | }
89 | internal int _ImageQuality;
90 |
91 | [Obsolete(ObsoleteMsg, ObsoleteError)]
92 | public int GrayToBWCutoff
93 | {
94 | get
95 | {
96 | return _GrayToBWCutoff;
97 | }
98 | set
99 | {
100 | if(value < 1 || value > 99) throw new ApplicationException("PdfImageControl.GrayToBWCutoff must be 1 to 99");
101 | _GrayToBWCutoff = value;
102 | }
103 | }
104 | internal int _GrayToBWCutoff;
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/PdfFileWriter/PdfBinaryWriter.cs:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////////
2 | //
3 | // PdfFileWriter
4 | // PDF File Write C# Class Library.
5 | //
6 | // PdfBinaryWriter
7 | // Extension to standard C# BinaryWriter class.
8 | //
9 | // Uzi Granot
10 | // Version: 1.0
11 | // Date: April 1, 2013
12 | // Copyright (C) 2013-2019 Uzi Granot. All Rights Reserved
13 | //
14 | // PdfFileWriter C# class library and TestPdfFileWriter test/demo
15 | // application are free software.
16 | // They is distributed under the Code Project Open License (CPOL).
17 | // The document PdfFileWriterReadmeAndLicense.pdf contained within
18 | // the distribution specify the license agreement and other
19 | // conditions and notes. You must read this document and agree
20 | // with the conditions specified in order to use this software.
21 | //
22 | // For version history please refer to PdfDocument.cs
23 | //
24 | /////////////////////////////////////////////////////////////////////
25 |
26 | using System;
27 | using System.IO;
28 | using System.Text;
29 |
30 | namespace PdfFileWriter
31 | {
32 | ///
33 | /// PDF binary writer class
34 | ///
35 | ///
36 | /// Extends .NET BinaryWriter class.
37 | ///
38 | public class PdfBinaryWriter : BinaryWriter
39 | {
40 | ///
41 | /// PDF binary writer constructor
42 | ///
43 | /// File or memory stream
44 | public PdfBinaryWriter
45 | (
46 | Stream Stream
47 | ) : base(Stream, Encoding.UTF8) {}
48 |
49 | ///
50 | /// Write String.
51 | ///
52 | /// Input string
53 | ///
54 | /// Convert each character from two bytes to one byte.
55 | ///
56 | public void WriteString
57 | (
58 | string Str
59 | )
60 | {
61 | // byte array
62 | byte[] ByteArray = new byte[Str.Length];
63 |
64 | // convert content from string to binary
65 | // do not use Encoding.ASCII.GetBytes(...)
66 | for(int Index = 0; Index < ByteArray.Length; Index++) ByteArray[Index] = (byte) Str[Index];
67 |
68 | // write to pdf file
69 | Write(ByteArray);
70 | return;
71 | }
72 |
73 | ///
74 | /// Write StringBuilder.
75 | ///
76 | /// String builder input
77 | ///
78 | /// Convert each character from two bytes to one byte.
79 | ///
80 | public void WriteString
81 | (
82 | StringBuilder Str
83 | )
84 | {
85 | // byte array
86 | byte[] ByteArray = new byte[Str.Length];
87 |
88 | // convert content from string to binary
89 | // do not use Encoding.ASCII.GetBytes(...)
90 | for(int Index = 0; Index < ByteArray.Length; Index++) ByteArray[Index] = (byte) Str[Index];
91 |
92 | // write to pdf file
93 | Write(ByteArray);
94 | return;
95 | }
96 |
97 | ///
98 | /// Combine format string with write string.
99 | ///
100 | /// Standard format string
101 | /// Array of objects
102 | public void WriteFormat
103 | (
104 | string FormatStr,
105 | params object[] List
106 | )
107 | {
108 | string Str = string.Format(FormatStr, List);
109 |
110 | // byte array
111 | byte[] ByteArray = new byte[Str.Length];
112 |
113 | // convert content from string to binary
114 | // do not use Encoding.ASCII.GetBytes(...)
115 | for(int Index = 0; Index < ByteArray.Length; Index++) ByteArray[Index] = (byte) Str[Index];
116 |
117 | // write to pdf file
118 | Write(ByteArray);
119 | return;
120 | }
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/PdfFileWriter/PdfXObject.cs:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////////
2 | //
3 | // PdfFileWriter
4 | // PDF File Write C# Class Library.
5 | //
6 | // PdfXObject
7 | // PDF X Object resource class.
8 | //
9 | // Uzi Granot
10 | // Version: 1.0
11 | // Date: April 1, 2013
12 | // Copyright (C) 2013-2019 Uzi Granot. All Rights Reserved
13 | //
14 | // PdfFileWriter C# class library and TestPdfFileWriter test/demo
15 | // application are free software.
16 | // They is distributed under the Code Project Open License (CPOL).
17 | // The document PdfFileWriterReadmeAndLicense.pdf contained within
18 | // the distribution specify the license agreement and other
19 | // conditions and notes. You must read this document and agree
20 | // with the conditions specified in order to use this software.
21 | //
22 | // For version history please refer to PdfDocument.cs
23 | //
24 | /////////////////////////////////////////////////////////////////////
25 |
26 | using System;
27 | using System.Drawing;
28 |
29 | namespace PdfFileWriter
30 | {
31 | ///
32 | /// PDF X object resource class
33 | ///
34 | public class PdfXObject : PdfContents
35 | {
36 | ///
37 | /// Bounding box rectangle
38 | ///
39 | public PdfRectangle Rect
40 | {
41 | get
42 | {
43 | return new PdfRectangle(BBox);
44 | }
45 | set
46 | {
47 | BBox = value;
48 | Dictionary.AddRectangle("/BBox", BBox);
49 | }
50 | }
51 |
52 | ///
53 | /// Bounding box left side
54 | ///
55 | public double Left
56 | {
57 | get
58 | {
59 | return BBox.Left;
60 | }
61 | set
62 | {
63 | BBox.Left = value;
64 | Dictionary.AddRectangle("/BBox", BBox);
65 | }
66 | }
67 |
68 | ///
69 | /// Bounding box bottom side
70 | ///
71 | public double Bottom
72 | {
73 | get
74 | {
75 | return BBox.Bottom;
76 | }
77 | set
78 | {
79 | BBox.Bottom = value;
80 | Dictionary.AddRectangle("/BBox", BBox);
81 | }
82 | }
83 |
84 | ///
85 | /// Bounding box right side
86 | ///
87 | public double Right
88 | {
89 | get
90 | {
91 | return BBox.Right;
92 | }
93 | set
94 | {
95 | BBox.Right = value;
96 | Dictionary.AddRectangle("/BBox", BBox);
97 | }
98 | }
99 |
100 | ///
101 | /// Bounding box top side
102 | ///
103 | public double Top
104 | {
105 | get
106 | {
107 | return BBox.Top;
108 | }
109 | set
110 | {
111 | BBox.Top = value;
112 | Dictionary.AddRectangle("/BBox", BBox);
113 | }
114 | }
115 |
116 | // bounding rectangle
117 | internal PdfRectangle BBox;
118 |
119 | ///
120 | /// PDF X Object constructor
121 | ///
122 | /// PDF document
123 | /// X Object width
124 | /// X Object height
125 | public PdfXObject
126 | (
127 | PdfDocument Document,
128 | double Width = 1.0,
129 | double Height = 1.0
130 | ) : base(Document, "/XObject")
131 | {
132 | // create resource code
133 | ResourceCode = Document.GenerateResourceNumber('X');
134 |
135 | // add subtype to dictionary
136 | Dictionary.Add("/Subtype", "/Form");
137 |
138 | // set boundig box rectangle
139 | BBox = new PdfRectangle(0.0, 0.0, Width, Height);
140 |
141 | // bounding box
142 | Dictionary.AddRectangle("/BBox", BBox);
143 | return;
144 | }
145 |
146 | ///
147 | /// Layer control
148 | ///
149 | /// PdfLayer object
150 | public void LayerControl
151 | (
152 | PdfObject Layer
153 | )
154 | {
155 | Dictionary.AddIndirectReference("/OC", Layer);
156 | return;
157 | }
158 | }
159 | }
160 |
--------------------------------------------------------------------------------
/PdfFileWriter/PdfWebLink.cs:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////////
2 | //
3 | // PdfFileWriter
4 | // PDF File Write C# Class Library.
5 | //
6 | // PdfWebLink
7 | // PDF weblink class.
8 | //
9 | // Uzi Granot
10 | // Version: 1.0
11 | // Date: April 1, 2013
12 | // Copyright (C) 2013-2019 Uzi Granot. All Rights Reserved
13 | //
14 | // PdfFileWriter C# class library and TestPdfFileWriter test/demo
15 | // application are free software.
16 | // They is distributed under the Code Project Open License (CPOL).
17 | // The document PdfFileWriterReadmeAndLicense.pdf contained within
18 | // the distribution specify the license agreement and other
19 | // conditions and notes. You must read this document and agree
20 | // with the conditions specified in order to use this software.
21 | //
22 | // For version history please refer to PdfDocument.cs
23 | //
24 | /////////////////////////////////////////////////////////////////////
25 |
26 | using System;
27 | using System.Collections.Generic;
28 |
29 | namespace PdfFileWriter
30 | {
31 | ///
32 | /// PDF Weblink class
33 | ///
34 | ///
35 | ///
36 | /// The library will make sure that all weblinks in the PDF file are unique.
37 | /// To create a weblink class you must use a static menthod. This method will
38 | /// create a new object for a new weblink. The mothod will return an
39 | /// existing object if it is a duplicate.
40 | ///
41 | ///
42 | public class PdfWebLink : PdfObject, IComparable
43 | {
44 | internal string WebLinkStr;
45 |
46 | // for search only
47 | private PdfWebLink
48 | (
49 | string WebLinkStr
50 | )
51 | {
52 | // save string
53 | this.WebLinkStr = WebLinkStr;
54 |
55 | // exit
56 | return;
57 | }
58 |
59 | // create new web link
60 | private PdfWebLink
61 | (
62 | PdfDocument Document,
63 | string WebLinkStr
64 | ) : base(Document)
65 | {
66 | // save string
67 | this.WebLinkStr = WebLinkStr;
68 |
69 | // type of action uniform resource identifier
70 | Dictionary.Add("/S", "/URI");
71 |
72 | // uniform resource identifier
73 | Dictionary.AddPdfString("/URI", WebLinkStr);
74 |
75 | // exit
76 | return;
77 | }
78 |
79 | ///
80 | /// Add a weblink
81 | ///
82 | /// PDF document
83 | /// Weblink text
84 | /// Weblink object
85 | ///
86 | ///
87 | /// The library will make sure that all weblinks in the PDF file are unique.
88 | /// To create a weblink class you must use a static menthod. This method will
89 | /// create a new object for a new weblink. The mothod will return an
90 | /// existing object if it is a duplicate.
91 | ///
92 | ///
93 | public static PdfWebLink AddWebLink
94 | (
95 | PdfDocument Document,
96 | string WebLinkStr
97 | )
98 | {
99 | // first time
100 | if(Document.WebLinkArray == null) Document.WebLinkArray = new List();
101 |
102 | // search list for a duplicate
103 | int Index = Document.WebLinkArray.BinarySearch(new PdfWebLink(WebLinkStr));
104 |
105 | // this string is a duplicate
106 | if(Index >= 0) return Document.WebLinkArray[Index];
107 |
108 | // new link
109 | PdfWebLink WebLink = new PdfWebLink(Document, WebLinkStr);
110 |
111 | // save new string in array
112 | Document.WebLinkArray.Insert(~Index, WebLink);
113 |
114 | // exit
115 | return WebLink;
116 | }
117 |
118 | ///
119 | /// Compare two WebLinkStr objects.
120 | ///
121 | /// Other object.
122 | /// Compare result.
123 | public int CompareTo
124 | (
125 | PdfWebLink Other
126 | )
127 | {
128 | return string.Compare(WebLinkStr, Other.WebLinkStr);
129 | }
130 | }
131 | }
132 |
--------------------------------------------------------------------------------
/PdfFileWriter/PdfShadingFunction.cs:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////////
2 | //
3 | // PdfFileWriter
4 | // PDF File Write C# Class Library.
5 | //
6 | // PdfShadingFunction
7 | // Support class for both axial and radial shading resources.
8 | //
9 | // Uzi Granot
10 | // Version: 1.0
11 | // Date: April 1, 2013
12 | // Copyright (C) 2013-2019 Uzi Granot. All Rights Reserved
13 | //
14 | // PdfFileWriter C# class library and TestPdfFileWriter test/demo
15 | // application are free software.
16 | // They is distributed under the Code Project Open License (CPOL).
17 | // The document PdfFileWriterReadmeAndLicense.pdf contained within
18 | // the distribution specify the license agreement and other
19 | // conditions and notes. You must read this document and agree
20 | // with the conditions specified in order to use this software.
21 | //
22 | // For version history please refer to PdfDocument.cs
23 | //
24 | /////////////////////////////////////////////////////////////////////
25 |
26 | using System;
27 | using System.Drawing;
28 | using SysMedia = System.Windows.Media;
29 |
30 | namespace PdfFileWriter
31 | {
32 | ////////////////////////////////////////////////////////////////////
33 | ///
34 | /// PDF shading function class
35 | ///
36 | ///
37 | /// PDF function to convert a number between 0 and 1 into a
38 | /// color red green and blue based on the sample color array.
39 | ///
40 | ////////////////////////////////////////////////////////////////////
41 | public class PdfShadingFunction : PdfObject
42 | {
43 | ////////////////////////////////////////////////////////////////////
44 | ///
45 | /// PDF Shading function constructor
46 | ///
47 | /// Document object parent of this function.
48 | /// Array of colors.
49 | ////////////////////////////////////////////////////////////////////
50 | public PdfShadingFunction
51 | (
52 | PdfDocument Document, // PDF document object
53 | Color[] ColorArray // Array of colors. Minimum 2.
54 | ) : base(Document, ObjectType.Stream)
55 | {
56 | // build dictionary
57 | Constructorhelper(ColorArray.Length);
58 |
59 | // add color array to contents stream
60 | foreach(Color Color in ColorArray)
61 | {
62 | ObjectValueList.Add(Color.R); // red
63 | ObjectValueList.Add(Color.G); // green
64 | ObjectValueList.Add(Color.B); // blue
65 | }
66 | return;
67 | }
68 |
69 | ///
70 | /// PDF Shading function constructor
71 | ///
72 | /// Document object parent of this function.
73 | /// System.Windows.Media gradient brush
74 | public PdfShadingFunction
75 | (
76 | PdfDocument Document,
77 | SysMedia.GradientBrush Brush
78 | ) : base(Document, ObjectType.Stream)
79 | {
80 | // build dictionary
81 | Constructorhelper(Brush.GradientStops.Count);
82 |
83 | // add color array to contents stream
84 | foreach(System.Windows.Media.GradientStop Stop in Brush.GradientStops)
85 | {
86 | ObjectValueList.Add(Stop.Color.R); // red
87 | ObjectValueList.Add(Stop.Color.G); // green
88 | ObjectValueList.Add(Stop.Color.B); // blue
89 | }
90 | return;
91 | }
92 |
93 | private void Constructorhelper
94 | (
95 | int Length
96 | )
97 | {
98 | // test for error
99 | if(Length < 2) throw new ApplicationException("Shading function color array must have two or more items");
100 |
101 | // the shading function is a sampled function
102 | Dictionary.Add("/FunctionType", "0");
103 |
104 | // input variable is between 0 and 1
105 | Dictionary.Add("/Domain", "[0 1]");
106 |
107 | // output variables are red, green and blue color components between 0 and 1
108 | Dictionary.Add("/Range", "[0 1 0 1 0 1]");
109 |
110 | // each color components in the stream is 8 bits
111 | Dictionary.Add("/BitsPerSample", "8");
112 |
113 | // number of colors in the stream must be two or more
114 | Dictionary.AddFormat("/Size", "[{0}]", Length);
115 | return;
116 | }
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/PdfFileWriter/PdfRectangle.cs:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////////
2 | //
3 | // PdfFileWriter
4 | // PDF File Write C# Class Library.
5 | //
6 | // PdfRectangle
7 | // PDF rectangle class.
8 | //
9 | // Uzi Granot
10 | // Version: 1.0
11 | // Date: April 1, 2013
12 | // Copyright (C) 2013-2019 Uzi Granot. All Rights Reserved
13 | //
14 | // PdfFileWriter C# class library and TestPdfFileWriter test/demo
15 | // application are free software.
16 | // They is distributed under the Code Project Open License (CPOL).
17 | // The document PdfFileWriterReadmeAndLicense.pdf contained within
18 | // the distribution specify the license agreement and other
19 | // conditions and notes. You must read this document and agree
20 | // with the conditions specified in order to use this software.
21 | //
22 | // For version history please refer to PdfDocument.cs
23 | //
24 | /////////////////////////////////////////////////////////////////////
25 |
26 | using System;
27 |
28 | namespace PdfFileWriter
29 | {
30 | ////////////////////////////////////////////////////////////////////
31 | ///
32 | /// PDF rectangle in double precision class
33 | ///
34 | ///
35 | /// Note: Microsoft rectangle is left, top, width and height.
36 | /// PDF rectangle is left, bottom, right and top.
37 | /// PDF numeric precision is double and Microsoft is Single.
38 | ///
39 | ////////////////////////////////////////////////////////////////////
40 | public class PdfRectangle
41 | {
42 | ///
43 | /// Gets or sets Left side.
44 | ///
45 | public double Left {get; set;}
46 |
47 | ///
48 | /// Gets or sets bottom side.
49 | ///
50 | public double Bottom {get; set;}
51 |
52 | ///
53 | /// Gets or sets right side.
54 | ///
55 | public double Right {get; set;}
56 |
57 | ///
58 | /// Gets or sets top side.
59 | ///
60 | public double Top {get; set;}
61 |
62 | ///
63 | /// Default constructor.
64 | ///
65 | public PdfRectangle() {}
66 |
67 | ///
68 | /// Constructor
69 | ///
70 | /// Left side
71 | /// Bottom side
72 | /// Right side
73 | /// Top side
74 | public PdfRectangle
75 | (
76 | double Left,
77 | double Bottom,
78 | double Right,
79 | double Top
80 | )
81 | {
82 | this.Left = Left;
83 | this.Bottom = Bottom;
84 | this.Right = Right;
85 | this.Top = Top;
86 | return;
87 | }
88 |
89 | ///
90 | /// Copy constructor
91 | ///
92 | /// Source rectangle
93 | public PdfRectangle
94 | (
95 | PdfRectangle Rect
96 | )
97 | {
98 | this.Left = Rect.Left;
99 | this.Bottom = Rect.Bottom;
100 | this.Right = Rect.Right;
101 | this.Top = Rect.Top;
102 | return;
103 | }
104 |
105 | ///
106 | /// Constructor for margin
107 | ///
108 | /// Single value for all sides
109 | public PdfRectangle
110 | (
111 | double AllTheSame
112 | )
113 | {
114 | Left = AllTheSame;
115 | Bottom = AllTheSame;
116 | Right = AllTheSame;
117 | Top = AllTheSame;
118 | return;
119 | }
120 |
121 | ///
122 | /// Constructor for margin
123 | ///
124 | /// Left and right value
125 | /// Top and bottom value
126 | public PdfRectangle
127 | (
128 | double Hor,
129 | double Vert
130 | )
131 | {
132 | Left = Hor;
133 | Bottom = Vert;
134 | Right = Hor;
135 | Top = Vert;
136 | return;
137 | }
138 |
139 | ///
140 | /// Gets width
141 | ///
142 | public double Width
143 | {
144 | get
145 | {
146 | return Right - Left;
147 | }
148 | }
149 |
150 | ///
151 | /// Gets height
152 | ///
153 | public double Height
154 | {
155 | get
156 | {
157 | return Top - Bottom;
158 | }
159 | }
160 | }
161 | }
162 |
--------------------------------------------------------------------------------
/PdfFileWriter/CreateMetafile.cs:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////////
2 | //
3 | // PdfFileWriter
4 | // PDF File Write C# Class Library.
5 | //
6 | // CreateMetafile
7 | // Create Metafile with graphics object.
8 | // It was used to test the PdfObject class with Metafile image.
9 | //
10 | // Uzi Granot
11 | // Version: 1.0
12 | // Date: April 1, 2013
13 | // Copyright (C) 2013-2019 Uzi Granot. All Rights Reserved
14 | //
15 | // PdfFileWriter C# class library and TestPdfFileWriter test/demo
16 | // application are free software.
17 | // They is distributed under the Code Project Open License (CPOL).
18 | // The document PdfFileWriterReadmeAndLicense.pdf contained within
19 | // the distribution specify the license agreement and other
20 | // conditions and notes. You must read this document and agree
21 | // with the conditions specified in order to use this software.
22 | //
23 | // For version history please refer to PdfDocument.cs
24 | //
25 | /////////////////////////////////////////////////////////////////////
26 |
27 | using System;
28 | using System.Drawing;
29 | using System.Drawing.Drawing2D;
30 | using System.Drawing.Imaging;
31 | using System.IO;
32 | using System.Runtime.InteropServices;
33 |
34 | namespace PdfFileWriter
35 | {
36 | ///
37 | /// Create image metafile class
38 | ///
39 | public class CreateMetafile : IDisposable
40 | {
41 | ///
42 | /// Gets image metafile.
43 | ///
44 | public Metafile Metafile {get; protected set;}
45 |
46 | ///
47 | /// Gets graphics object form image metafile.
48 | ///
49 | public Graphics Graphics {get; protected set;}
50 |
51 | ///
52 | /// Create image metafile constructor
53 | ///
54 | /// Image width in pixels.
55 | /// Image height in pixels.
56 | public CreateMetafile
57 | (
58 | int Width,
59 | int Height
60 | )
61 | {
62 | using (MemoryStream Stream = new MemoryStream())
63 | {
64 | using (Graphics MemoryGraphics = Graphics.FromHwndInternal(IntPtr.Zero))
65 | {
66 | IntPtr deviceContextHandle = MemoryGraphics.GetHdc();
67 | Metafile = new Metafile(Stream, deviceContextHandle, new RectangleF(0, 0, Width, Height), MetafileFrameUnit.Pixel, EmfType.EmfPlusOnly);
68 | MemoryGraphics.ReleaseHdc();
69 | }
70 | }
71 |
72 | Graphics = Graphics.FromImage(Metafile);
73 |
74 | // Set everything to high quality
75 | Graphics.SmoothingMode = SmoothingMode.HighQuality;
76 | Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
77 | Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
78 | Graphics.CompositingQuality = CompositingQuality.HighQuality;
79 | Graphics.PageUnit = GraphicsUnit.Pixel;
80 | return;
81 | }
82 |
83 | [DllImport("gdi32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
84 | private static extern IntPtr CopyEnhMetaFile(IntPtr MetaFileHandle, IntPtr FileName);
85 |
86 | ///
87 | /// Save image metafile
88 | ///
89 | /// File name
90 | public void SaveMetafile
91 | (
92 | string FileName
93 | )
94 | {
95 | // Get a handle to the metafile
96 | IntPtr MetafileHandle = Metafile.GetHenhmetafile();
97 |
98 | // allocate character table buffer in global memory (two bytes per char)
99 | IntPtr CharBuffer = Marshal.AllocHGlobal(2 * FileName.Length + 2);
100 |
101 | // move file name inclusing terminating zer0 to the buffer
102 | for(int Index = 0; Index < FileName.Length; Index++) Marshal.WriteInt16(CharBuffer, 2 * Index, (short) FileName[Index]);
103 | Marshal.WriteInt16(CharBuffer, 2 * FileName.Length, 0);
104 |
105 | // Export metafile to an image file
106 | CopyEnhMetaFile(MetafileHandle, CharBuffer);
107 |
108 | // free local buffer
109 | Marshal.FreeHGlobal(CharBuffer);
110 | return;
111 | }
112 |
113 | [DllImport("gdi32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
114 | private static extern IntPtr DeleteEnhMetaFile(IntPtr MetaFileHandle);
115 |
116 | ///
117 | /// Delete image metafile.
118 | ///
119 | public void DeleteMetafile()
120 | {
121 | // Get a handle to the metafile
122 | IntPtr MetafileHandle = Metafile.GetHenhmetafile();
123 |
124 | // Delete the metafile from memory
125 | DeleteEnhMetaFile(MetafileHandle);
126 | return;
127 | }
128 |
129 | ///
130 | /// Dispose object
131 | ///
132 | public void Dispose()
133 | {
134 | if(Graphics != null)
135 | {
136 | Graphics.Dispose();
137 | Graphics = null;
138 | }
139 | if(Metafile != null)
140 | {
141 | Metafile.Dispose();
142 | Metafile = null;
143 | }
144 | return;
145 | }
146 | }
147 | }
148 |
--------------------------------------------------------------------------------
/PdfFileWriter/PdfFileWriter.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 | 8.0.30703
7 | 2.0
8 | {7B531568-9C87-4CC5-917D-28C43BCF5B38}
9 | Library
10 | Properties
11 | PdfFileWriter
12 | PdfFileWriter
13 | v4.6.2
14 | 512
15 |
16 |
17 |
18 | true
19 | full
20 | false
21 | bin\Debug\
22 | DEBUG;TRACE
23 | prompt
24 | 4
25 | bin\Debug\PdfFileWriter.XML
26 | false
27 |
28 |
29 | pdbonly
30 | true
31 | bin\Release\
32 | TRACE
33 | prompt
34 | 4
35 | bin\Release\PdfFileWriter.XML
36 | false
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 | Component
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
108 |
--------------------------------------------------------------------------------
/PdfFileWriter/PdfInfo.cs:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////////
2 | //
3 | // PdfFileWriter
4 | // PDF File Write C# Class Library.
5 | //
6 | // PdfInfo
7 | // PDF document information dictionary.
8 | //
9 | // Uzi Granot
10 | // Version: 1.0
11 | // Date: April 1, 2013
12 | // Copyright (C) 2013-2019 Uzi Granot. All Rights Reserved
13 | //
14 | // PdfFileWriter C# class library and TestPdfFileWriter test/demo
15 | // application are free software.
16 | // They is distributed under the Code Project Open License (CPOL).
17 | // The document PdfFileWriterReadmeAndLicense.pdf contained within
18 | // the distribution specify the license agreement and other
19 | // conditions and notes. You must read this document and agree
20 | // with the conditions specified in order to use this software.
21 | //
22 | // For version history please refer to PdfDocument.cs
23 | //
24 | /////////////////////////////////////////////////////////////////////
25 |
26 | using System;
27 |
28 | namespace PdfFileWriter
29 | {
30 | ///
31 | /// PDF document information dictionary
32 | ///
33 | public class PdfInfo : PdfObject
34 | {
35 | ///
36 | /// Constructor for PdfInfo class
37 | ///
38 | /// Main document class
39 | /// PdfInfo object
40 | ///
41 | /// The constructor initialize the /Info dictionary with 4 key value pairs.
42 | ///
43 | /// - Creation date set to current local system date
44 | /// - Modification date set to current local system date
45 | /// - Creator is PdfFileWriter C# Class Library Version No
46 | /// - Producer is PdfFileWriter C# Class Library Version No
47 | ///
48 | ///
49 | public static PdfInfo CreatePdfInfo
50 | (
51 | PdfDocument Document
52 | )
53 | {
54 | // create a new default info object
55 | if(Document.InfoObject == null)
56 | {
57 | // create and add info object to trailer dictionary
58 | Document.InfoObject = new PdfInfo(Document);
59 | Document.TrailerDict.AddIndirectReference("/Info", Document.InfoObject);
60 | }
61 |
62 | // exit with either existing object or a new one
63 | return Document.InfoObject;
64 | }
65 |
66 | ///
67 | /// Protected constructor
68 | ///
69 | /// Main document object
70 | protected PdfInfo
71 | (
72 | PdfDocument Document
73 | ) : base(Document, ObjectType.Dictionary)
74 | {
75 | // set creation and modify dates
76 | DateTime LocalTime = DateTime.Now;
77 | CreationDate(LocalTime);
78 | ModDate(LocalTime);
79 |
80 | // set creator and producer
81 | Creator("PdfFileWriter C# Class Library Version " + PdfDocument.RevisionNumber);
82 | Producer("PdfFileWriter C# Class Library Version " + PdfDocument.RevisionNumber);
83 | return;
84 | }
85 |
86 | ///
87 | /// Sets document creation date and time
88 | ///
89 | /// Creation date and time
90 | public void CreationDate
91 | (
92 | DateTime Date
93 | )
94 | {
95 | Dictionary.AddPdfString("/CreationDate", string.Format("D:{0}", Date.ToString("yyyyMMddHHmmss")));
96 | return;
97 | }
98 |
99 | ///
100 | /// Sets document last modify date and time
101 | ///
102 | /// Modify date and time
103 | public void ModDate
104 | (
105 | DateTime Date
106 | )
107 | {
108 | Dictionary.AddPdfString("/ModDate", string.Format("D:{0}", Date.ToString("yyyyMMddHHmmss")));
109 | return;
110 | }
111 |
112 | ///
113 | /// Sets document title
114 | ///
115 | /// Title
116 | public void Title
117 | (
118 | string Title
119 | )
120 | {
121 | Dictionary.AddPdfString("/Title", Title);
122 | return;
123 | }
124 |
125 | ///
126 | /// Sets document author
127 | ///
128 | /// Author
129 | public void Author
130 | (
131 | string Author
132 | )
133 | {
134 | Dictionary.AddPdfString("/Author", Author);
135 | return;
136 | }
137 |
138 | ///
139 | /// Sets document subject
140 | ///
141 | /// Subject
142 | public void Subject
143 | (
144 | string Subject
145 | )
146 | {
147 | Dictionary.AddPdfString("/Subject", Subject);
148 | return;
149 | }
150 |
151 | ///
152 | /// Sets keywords associated with the document
153 | ///
154 | /// Keywords list
155 | public void Keywords
156 | (
157 | string Keywords
158 | )
159 | {
160 | Dictionary.AddPdfString("/Keywords", Keywords);
161 | return;
162 | }
163 |
164 | ///
165 | /// Sets the name of the application that created the document
166 | ///
167 | /// Creator
168 | public void Creator
169 | (
170 | string Creator
171 | )
172 | {
173 | Dictionary.AddPdfString("/Creator", Creator);
174 | return;
175 | }
176 |
177 | ///
178 | /// Sets the name of the application that produced the document
179 | ///
180 | /// Producer
181 | public void Producer
182 | (
183 | string Producer
184 | )
185 | {
186 | Dictionary.AddPdfString("/Producer", Producer);
187 | return;
188 | }
189 | }
190 | }
191 |
--------------------------------------------------------------------------------
/PdfFileWriter/ImageSizePos.cs:
--------------------------------------------------------------------------------
1 | /////////////////////////////////////////////////////////////////////
2 | //
3 | // PdfFileWriter
4 | // PDF File Write C# Class Library.
5 | //
6 | // ImageSizePoos
7 | // Support class for image aspect ratio calculations.
8 | //
9 | // Uzi Granot
10 | // Version: 1.0
11 | // Date: April 1, 2013
12 | // Copyright (C) 2013-2019 Uzi Granot. All Rights Reserved
13 | //
14 | // PdfFileWriter C# class library and TestPdfFileWriter test/demo
15 | // application are free software.
16 | // They is distributed under the Code Project Open License (CPOL).
17 | // The document PdfFileWriterReadmeAndLicense.pdf contained within
18 | // the distribution specify the license agreement and other
19 | // conditions and notes. You must read this document and agree
20 | // with the conditions specified in order to use this software.
21 | //
22 | // For version history please refer to PdfDocument.cs
23 | //
24 | /////////////////////////////////////////////////////////////////////
25 |
26 | using System;
27 | using System.Drawing;
28 |
29 | namespace PdfFileWriter
30 | {
31 | /////////////////////////////////////////////////////////////////////
32 | ///
33 | /// Image size and position class
34 | ///
35 | ///
36 | /// Delta X and Y are the adjustments to image position to
37 | /// meet the content alignment request.
38 | ///
39 | /////////////////////////////////////////////////////////////////////
40 | public static class ImageSizePos
41 | {
42 | ///
43 | /// Adjust image drawing area for both aspect ratio and content alignment
44 | ///
45 | /// Image width in pixels.
46 | /// Image height in pixels.
47 | /// Drawing area rectangle
48 | /// Content alignment.
49 | /// Adjusted drawing area rectangle
50 | public static PdfRectangle ImageArea
51 | (
52 | int ImageWidthPix,
53 | int ImageHeightPix,
54 | PdfRectangle DrawArea,
55 | ContentAlignment Alignment
56 | )
57 | {
58 | return ImageArea(ImageWidthPix, ImageHeightPix, DrawArea.Left, DrawArea.Bottom, DrawArea.Width, DrawArea.Height, Alignment);
59 | }
60 |
61 | ////////////////////////////////////////////////////////////////////
62 | ///
63 | /// Adjust image drawing area for both aspect ratio and content alignment
64 | ///
65 | /// Image width in pixels.
66 | /// Image height in pixels.
67 | /// Drawing area left side.
68 | /// Drawing area bottom side.
69 | /// Drawing area width.
70 | /// Drawing area height.
71 | /// Content alignment.
72 | /// Adjusted drawing area rectangle
73 | ////////////////////////////////////////////////////////////////////
74 | public static PdfRectangle ImageArea
75 | (
76 | int ImageWidthPix,
77 | int ImageHeightPix,
78 | double DrawAreaLeft,
79 | double DrawAreaBottom,
80 | double DrawAreaWidth,
81 | double DrawAreaHeight,
82 | ContentAlignment Alignment
83 | )
84 | {
85 | double DeltaX = 0;
86 | double DeltaY = 0;
87 | double Width;
88 | double Height;
89 |
90 | // calculate height to fit aspect ratio
91 | Height = DrawAreaWidth * ImageHeightPix / ImageWidthPix;
92 | if(Height <= DrawAreaHeight)
93 | {
94 | Width = DrawAreaWidth;
95 | if(Height < DrawAreaHeight)
96 | {
97 | if(Alignment == ContentAlignment.MiddleLeft || Alignment == ContentAlignment.MiddleCenter || Alignment == ContentAlignment.MiddleRight)
98 | {
99 | DeltaY = 0.5 * (DrawAreaHeight - Height);
100 | }
101 | else if(Alignment == ContentAlignment.TopLeft || Alignment == ContentAlignment.TopCenter || Alignment == ContentAlignment.TopRight)
102 | {
103 | DeltaY = DrawAreaHeight - Height;
104 | }
105 | }
106 | }
107 | // calculate width to fit aspect ratio
108 | else
109 | {
110 | Width = DrawAreaHeight * ImageWidthPix / ImageHeightPix;
111 | Height = DrawAreaHeight;
112 | if(Width < DrawAreaWidth)
113 | {
114 | if(Alignment == ContentAlignment.TopCenter || Alignment == ContentAlignment.MiddleCenter || Alignment == ContentAlignment.BottomCenter)
115 | {
116 | DeltaX = 0.5 * (DrawAreaWidth - Width);
117 | }
118 | else if(Alignment == ContentAlignment.TopRight || Alignment == ContentAlignment.MiddleRight || Alignment == ContentAlignment.BottomRight)
119 | {
120 | DeltaX = DrawAreaWidth - Width;
121 | }
122 | }
123 | }
124 |
125 | // position rectangle
126 | return new PdfRectangle(DrawAreaLeft + DeltaX, DrawAreaBottom + DeltaY, DrawAreaLeft + DeltaX + Width, DrawAreaBottom + DeltaY + Height);
127 | }
128 |
129 | ////////////////////////////////////////////////////////////////////
130 | ///
131 | /// Calculate best fit to preserve aspect ratio
132 | ///
133 | /// Image width in pixels.
134 | /// Image height in pixels.
135 | /// Drawing area width.
136 | /// Drawing area height.
137 | /// Image size in user units.
138 | ////////////////////////////////////////////////////////////////////
139 | public static SizeD ImageSize
140 | (
141 | int ImageWidthPix,
142 | int ImageHeightPix,
143 | double DrawAreaWidth,
144 | double DrawAreaHeight
145 | )
146 | {
147 | SizeD OutputSize = new SizeD();
148 | OutputSize.Height = DrawAreaWidth * ImageHeightPix / ImageWidthPix;
149 | if(OutputSize.Height <= DrawAreaHeight)
150 | {
151 | OutputSize.Width = DrawAreaWidth;
152 | }
153 | else
154 | {
155 | OutputSize.Width = DrawAreaHeight * ImageWidthPix / ImageHeightPix;
156 | OutputSize.Height = DrawAreaHeight;
157 | }
158 | return OutputSize;
159 | }
160 | }
161 | }
162 |
--------------------------------------------------------------------------------
/PdfFileWriter/PdfLayers.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace PdfFileWriter
8 | {
9 | ///
10 | /// Layers list mode enumeration
11 | ///
12 | public enum ListMode
13 | {
14 | ///
15 | /// List all layers
16 | ///
17 | AllPages,
18 |
19 | ///
20 | /// List layers for visible pages
21 | ///
22 | VisiblePages,
23 | }
24 |
25 | ///
26 | /// PdfLayers control class
27 | ///
28 | public class PdfLayers : PdfObject
29 | {
30 | ///
31 | /// Layers name
32 | ///
33 | public string Name {get; set;}
34 |
35 | ///
36 | /// Layers list mode
37 | ///
38 | public ListMode ListMode {get; set;}
39 |
40 | internal List LayerList;
41 | internal List