Hi,
I am trying to convert a HTML body to a PDF using HtmlFormatProvider and PdfFormatProvider.
It works well when I try to create a pdf with "normal" characters, but when I use characters like "åäö" the characters is either missing or replaced with other character.
I have found a similiar issue but that was due to the fact that the person was using .net core I am using .net framework.
Am I missing something when I am converting it to a PDF or is there a limitation with special characters like "åäö".
private string CreateAndStorePdf(string htmlBody)
{
Telerik.Windows.Documents.Extensibility.JpegImageConverterBase jpegImageConverter = new Telerik.Documents.ImageUtils.JpegImageConverter();
Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.JpegImageConverter = jpegImageConverter;
var provider = new HtmlFormatProvider();
var document = provider.Import(htmlBody);
var exportProvider = new PdfFormatProvider();
var fixedExportProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
var fileName = "_original.pdf";
try
{
using (var outputStream = new MemoryStream())
{
exportProvider.Export(fixedExportProvider.ExportToFixedDocument(document), outputStream);
outputStream.Seek(0, SeekOrigin.Begin);
var pdfBytes = new BinaryReader(outputStream).ReadBytes((int)outputStream.Length);
return fileName;
}
}
catch (Exception ex)
{
Log.Error(ex, "The pdf could not be created.");
return null;
}
}
Some Shading elements are not property imported which leads to unexpected drawings on the exported (previewed in the PdfViewer) document.
Currently, when registering *.pfb font file with FontsRepository.RegisterFont method an exception is thrown during the font creation.
WORKAROUND: The font file may be converted to TTF format (*.ttf) which is successfully registered.
When invoking the FontsRepository.TryCreateFont() method to create Font from an installed TrueType font with font properties set (FontStyles.Italic, FontWeights.Bold) the name of the created font is not correct.
FontBase font;
bool isRegistered = FontsRepository.TryCreateFont(new FontFamily("Helvetica"), FontStyles.Italic, FontWeights.Bold, out font);
Expected:<Helvetica-BoldOblique>
Actual:<Helvetica,Italic>
A small line is drawn in the top left corner when converting a paragraph with a shading color to a PDF
Workaround:
var table = header.Blocks.AddTable();
table.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 100);
var row = table.Rows.AddTableRow();
var cell = row.Cells.AddTableCell();
cell.Shading.BackgroundColor = new ThemableColor(Colors.Red);
var head = new Run(doc);
head.Text = "Test";
var paragraph = cell.Blocks.AddParagraph();
paragraph.TextAlignment = Telerik.Windows.Documents.Flow.Model.Styles.Alignment.Center;
paragraph.Inlines.Add(head);
When Bold and Italic properties are set in the TrueType font file`s OS/2 table they are not respected and the OpenTypeFontSource`s Bold and Italic properties are not correct.
When the Font`s Widths array contains entries defined as Indirect references an exception is thrown: System.ArgumentException: 'The IndirectReference type cannot be converted to a real numeric value.'
According to the Pdf Specification: The glyph widths are measured in units in which 1000 units corresponds to 1 unit in text space.
When importing a document containing text fragment with a wrong Type3 font set the glyph cannot be obtained from the font and an exception is thrown: System.InvalidOperationException: 'Cannot create glyph with charcode: <<>>'
URI Action with invalid mailto URL scheme - mailto:***@***.**(E-mail) can be imported but trying to merge or clone the document throws UriFormatException: 'Invalid URI: The hostname could not be parsed.'
Workaround: Remove the annotations that contain invalid Uri:
PdfFormatProvider provider = new PdfFormatProvider();
this.pdfDocument = provider.Import(memory);
foreach (var page in this.pdfDocument.Pages.ToList())
{
List<Link> links = page.Annotations.Where(a => a.Type == AnnotationType.Link).Select(a => a as Link).ToList();
foreach (var link in links)
{
Uri uri;
UriAction uriAction = link.Action as UriAction;
try
{
uri = uriAction.Uri;
}
catch (UriFormatException)
{
page.Annotations.Remove(link);
}
}
}
When a document containing a SignatureField is exported with the IsEncrypted property set to true, a not set UserPassword is required to open it, which makes it impossible to be opened.
Workaround: Exporting with AES256 encryption does not have this problem:
provider.ExportSettings = new PdfExportSettings
{
IsEncrypted = true,
EncryptionType = EncryptionType.AES256
};
PdfFormatProvider: When Arial Narrow Bold fond is set in the document the font-weight is lost when converting to PDF.
Workaround:
var fontData = File.ReadAllBytes(@"C:\Downloads\arial-narrow\arialnb.ttf");
FontsRepository.RegisterFont(new System.Windows.Media.FontFamily("Arial Narrow"), FontStyles.Normal, FontWeights.Bold, fontData);
.../AP<</N<</Off null/Yes 439 0 R>>...
When a Table consisting of one TableRow that contains a TableCell with a Rowspan > 1 set, additional lines are added to the table.
Table table = new Table();
TableRow row = table.Rows.AddTableRow();
row.Cells.AddTableCell();
TableCell secondCell = row.Cells.AddTableCell();
secondCell.RowSpan = 2;
table.Measure();
table.Rows.AddTableRow();
When a row has two cells and the content of the first one is large, the width of the second cell results in too small value while measuring and its content remains invisible.
Workaround: Set PreferredWidth to the second cell.