Hello,
its been a while but I'm glad to see that this project is still running strong! Thank you for your hard work!
I'm currently updating depencencies in one of my projects in which the migradoc and pdfsharp libs are used.
Currently using version 1.50.4740.0 and updating to 6.2.3.7625 (non GDI and non WPF)
The files I used were:
MigraDoc.DocumentObjectModel
MigraDoc.Rendering
PdfSharp.Charting
PdfSharp
I've already replaced security part:
//old
//pdfSharpDoc.SecuritySettings.DocumentSecurityLevel = PdfSharp.Pdf.Security.PdfDocumentSecurityLevel.Encrypted128Bit;
//new
pdfSharpDoc.SecurityHandler.SetEncryptionToV2With128Bits();
and removed
//pdfSharpDoc.SecuritySettings.PermitAccessibilityExtractContent = PDFP.PermitAccessibilityExtractContent;
Also added the PDFSharpSystem.dll to the project after debugger told me that PdfSharp lib needed it.
Hopefully the last step to finish updating the project is the fontresolver.
Previously I was defining styles and add them to the document like this:
Code:
private void DefineStyles(Document Doc)
{
Style style = Doc.Styles["Normal"];
style.Font.Name = "Arial Unicode MS";
style.Font.Size = 9;
//My styles and fonts
style = Doc.Styles.AddStyle("Title", "Normal");
style.Font.Name = "Arial Unicode MS";
style.Font.Size = 32;
style.Font.Color = Colors.Black;
style.ParagraphFormat.PageBreakBefore = false;
style.ParagraphFormat.SpaceBefore = 6;
style.ParagraphFormat.SpaceAfter = 6;
style = Doc.Styles.AddStyle("SummaryTitle", "Normal");
style.Font.Name = "Arial Unicode MS";
style.Font.Size = 26;
style.Font.Color = Colors.Black;
style.ParagraphFormat.PageBreakBefore = false;
style.ParagraphFormat.SpaceBefore = 6;
style.ParagraphFormat.SpaceAfter = 6;
style = Doc.Styles.AddStyle("SmallBodyFont", "Normal");
style.Font.Name = "Arial Unicode MS";
style.Font.Size = 6;
style.Font.Bold = false;
style.Font.Color = Colors.Black;
style.ParagraphFormat.PageBreakBefore = false;
style.ParagraphFormat.SpaceBefore = 6;
style.ParagraphFormat.SpaceAfter = 6;
style = Doc.Styles.AddStyle("Heading2", "Normal");
style.Font.Name = "Arial Unicode MS";
style.Font.Size = 24;
style.Font.Color = Colors.Black;
style.ParagraphFormat.PageBreakBefore = false;
style.ParagraphFormat.SpaceBefore = 6;
style.ParagraphFormat.SpaceAfter = 6;
//Bookmark font Workaround for OutlineBookmarks Adobe
style = Doc.Styles.AddStyle("Bookmark", "Normal");
style.Font.Name = "Arial Unicode MS";
style.Font.Size = 0.0001;
style.Font.Color = Colors.White;
style.ParagraphFormat.PageBreakBefore = false;
style.ParagraphFormat.SpaceBefore = 6;
style.ParagraphFormat.SpaceAfter = 6;
//Preset styles and Fonts
style = Doc.Styles["Heading1"];
style.Font.Name = "Tahoma";
style.Font.Size = 14;
style.Font.Bold = true;
style.Font.Color = Colors.DarkBlue;
style.ParagraphFormat.PageBreakBefore = true;
style.ParagraphFormat.SpaceAfter = 6;
style = Doc.Styles["Heading2"];
style.Font.Size = 12;
style.Font.Bold = true;
style.ParagraphFormat.PageBreakBefore = false;
style.ParagraphFormat.SpaceBefore = 6;
style.ParagraphFormat.SpaceAfter = 6;
style = Doc.Styles["Heading3"];
style.Font.Size = 10;
style.Font.Bold = true;
style.Font.Italic = true;
style.ParagraphFormat.SpaceBefore = 6;
style.ParagraphFormat.SpaceAfter = 3;
style = Doc.Styles[StyleNames.Header];
style.ParagraphFormat.AddTabStop("16cm", MigraDoc.DocumentObjectModel.TabAlignment.Right);
style = Doc.Styles[StyleNames.Footer];
style.ParagraphFormat.AddTabStop("8cm", MigraDoc.DocumentObjectModel.TabAlignment.Center);
// Create a new style called TextBox based on style Normal
style = Doc.Styles.AddStyle("TextBox", "Normal");
style.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
style.ParagraphFormat.Borders.Width = 2.5;
style.ParagraphFormat.Borders.Distance = "3pt";
style.ParagraphFormat.Shading.Color = Colors.SkyBlue;
// Create a new style called TOC based on style Normal
style = Doc.Styles.AddStyle("TOC", "Normal");
style.ParagraphFormat.AddTabStop("16cm", MigraDoc.DocumentObjectModel.TabAlignment.Right, TabLeader.Dots);
style.ParagraphFormat.Font.Color = Colors.Blue;
}
Add this functionality to measure a text to my program (see
viewtopic.php?p=9390)
Code:
public sealed class TextMeasurement
{
/// <summary>
/// Initializes a new instance of the TextMeasurement class with the specified font.
/// </summary>
public TextMeasurement(Font font)
{
if (font == null)
throw new ArgumentNullException("font");
_font = font;
}
/// <summary>
/// Returns the size of the bounding box of the specified text.
/// </summary>
public XSize MeasureString(string text, UnitType unitType)
{
if (text == null)
throw new ArgumentNullException("text");
if (!Enum.IsDefined(typeof(UnitType), unitType))
throw new InvalidEnumArgumentException();
XGraphics graphics = Realize();
XSize size = graphics.MeasureString(text, _gdiFont/*, new XPoint(0, 0), StringFormat.GenericTypographic*/);
switch (unitType)
{
case UnitType.Point:
break;
case UnitType.Centimeter:
size.Width = (float)(size.Width * 2.54 / 72);
size.Height = (float)(size.Height * 2.54 / 72);
break;
case UnitType.Inch:
size.Width = size.Width / 72;
size.Height = size.Height / 72;
break;
case UnitType.Millimeter:
size.Width = (float)(size.Width * 25.4 / 72);
size.Height = (float)(size.Height * 25.4 / 72);
break;
case UnitType.Pica:
size.Width = size.Width / 12;
size.Height = size.Height / 12;
break;
default:
Debug.Assert(false, "Missing unit type");
break;
}
return size;
}
/// <summary>
/// Returns the size of the bounding box of the specified text in point.
/// </summary>
public XSize MeasureString(string text)
{
return MeasureString(text, UnitType.Point);
}
/// <summary>
/// Gets or sets the font used for measurement.
/// </summary>
public Font Font
{
get { return _font; }
set
{
if (value == null)
throw new ArgumentNullException("value");
if (_font != value)
{
_font = value;
_gdiFont = null;
}
}
}
Font _font;
/// <summary>
/// Initializes appropriate GDI+ objects.
/// </summary>
XGraphics Realize()
{
if (_graphics == null)
_graphics = XGraphics.CreateMeasureContext(new XSize(2000, 2000), XGraphicsUnit.Point, XPageDirection.Downwards);
//this.graphics.PageUnit = GraphicsUnit.Point;
if (_gdiFont == null)
{
XFontStyleEx style = XFontStyleEx.Regular;
if (_font.Bold)
style |= XFontStyleEx.Bold;
if (_font.Italic)
style |= XFontStyleEx.Italic;
_gdiFont = new XFont(_font.Name, _font.Size, style/*, System.Drawing.GraphicsUnit.Point*/);
}
return _graphics;
}
XFont _gdiFont;
XGraphics _graphics;
}
However with the 6.2.3. version an exception is thrown at this line:
_gdiFont = new XFont(_font.Name, _font.Size, style/*, System.Drawing.GraphicsUnit.Point*/);
telling me:
System.InvalidOperationException: "No appropriate font found for family name 'Arial Unicode MS'. Implement IFontResolver and assign to 'GlobalFontSettings.FontResolver' to use fonts. See
https://docs.pdfsharp.net/link/font-resolving.html"
I've check the Windows system and was baffled that "Arial Unicode MS" was not installed on the system however worked with 1.50 version.
So I checked the PDFs created with 1.50. and saw that "Arial", "Arial Bold" and "Microsoft Sans Serif" was embedded in the document.
I already tried this:
GlobalFontSettings.UseWindowsFontsUnderWindows = true;
but still the same exception is thrown.
So my questions are:
1. May I assume that 1.50 encounted the same problem but did a fallback to "Arial" without me noticing it ?
2. How can I resolve this problem in 6.2.3 ?
If further information is needed please let me know.
Best regards!