Hi, i'm trying to use document processing to convert HTML template to PDF, but fonts are missing from resulting file, maybe even from the imported html.
How can I find out if the fonts are already missing after importing HTML?
What is the best way to convert HTML to PDF keeping images, fonts, etc. using document processing?
This is one of my attempts to get it to work.
static void Main(string[] args)
{
string templateFileName = @"html template.htm";
string pdfPath = @"test html template to pdf.pdf";
FileStream templateAsStream = new FileStream(templateFileName, FileMode.Open, FileAccess.Read);
///////////
//telerik
///////////
try
{
// Register the font
byte[] fontDataR = File.ReadAllBytes("Raleway-VariableFont_wght.ttf");
System.Windows.Media.FontFamily fontFamilyR = new System.Windows.Media.FontFamily("Releway");
byte[] fontDataRL = File.ReadAllBytes("Raleway-Light.ttf");
System.Windows.Media.FontFamily fontFamilyRL = new System.Windows.Media.FontFamily("Releway Light");
//Telerik.Windows.Documents.Fixed.Model.Fonts.
FontsRepository.RegisterFont(fontFamilyR, System.Windows.FontStyles.Normal, System.Windows.FontWeights.Normal, fontDataR);
FontsRepository.RegisterFont(fontFamilyRL, System.Windows.FontStyles.Normal, System.Windows.FontWeights.Normal, fontDataRL);
byte[] fontDataRb = File.ReadAllBytes("Raleway-Bold.ttf");
System.Windows.Media.FontFamily fontFamilyRb = new System.Windows.Media.FontFamily("Releway");
//Telerik.Windows.Documents.Fixed.Model.Fonts.
FontsRepository.RegisterFont(fontFamilyR, System.Windows.FontStyles.Normal, System.Windows.FontWeights.Bold, fontDataRb);
//import AFTER setting fonts
//Telerik.Windows.Documents.Flow.FormatProviders.Html.
HtmlFormatProvider htmlProvider = new HtmlFormatProvider();
HtmlImportSettings importSettings = new HtmlImportSettings();
HtmlExportSettings exportSettings = new HtmlExportSettings();
var document = htmlProvider.Import(templateAsStream);
PdfFormatProvider pdfProvider = new PdfFormatProvider();
//Telerik.Windows.Documents.Extensibility.FontsProviderBase fontsProvider = new FontsProvider();
//Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.FontsProvider = fontsProvider;
PdfExportSettings pdfExportSettings = new PdfExportSettings();
pdfExportSettings.ShouldEmbedFonts = true;
pdfProvider.ExportSettings = pdfExportSettings;
FileStream stream = File.Create("fixed pdf.pdf");
var radFixedDocument = (new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider()).ExportToFixedDocument(document);
var fixedFormatProvider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider();
fixedFormatProvider.Export(radFixedDocument, stream);
stream.Close();
//var resultBytes = pdfProvider.Export(document);
//File.WriteAllBytes(pdfPath, resultBytes);
}
catch(Exception e)
{
throw e;
}
I have also attached html file that is result of importing html and then export html
also I get this in pdf reader both if I use embed fonts option or not
https://www.telerik.com/forums/radflowdocument-to-pdf---arialnarrow-font-issues
Hello,
This post is not a question. I simply wanted to share my partial solution for those who may need it.
Below is a manual method for creating a Block with justified text in the PDFProcessing API. This makes use of the Block object's Measure method and the WordSpacing property to adjust each line until the indicated width is filled.
PLEASE NOTE:
Best Regards,
Aidan D.
TAGS FOR GOOGLE: Justify, Alignment
using System;
using System.Collections.Generic;
using Telerik.Windows.Documents.Fixed.Model.Editing;
using Telerik.Windows.Documents.Fixed.Model.Fonts;
classTextJustify
{
public static void InsertJustifiedBlock(FixedContentEditor PageEditor, string Text, int TextBoxWidth)
{
Block justifiedBlock = CreateJustifiedBlock(Text, TextBoxWidth, PageEditor.TextProperties.Font, PageEditor.TextProperties.FontSize);
PageEditor.DrawBlock(justifiedBlock);
}
public static Block CreateJustifiedBlock(string Text, int TextBoxWidth, FontBase Font, double FontSize)
{
Block justifiedBlock = new Block();
double currentSpacing = 0;
double spacingStep = 1;
string currentLine;
if (Text.Length > 0)
{
{
var withBlock = justifiedBlock.TextProperties;
// Default word spacing. Must be initialized to 0 before use. This defaults to Nothing/Null// and will not function correctly if not initialized.
withBlock.WordSpacing = 0;
withBlock.Font = Font;
withBlock.FontSize = FontSize;
}
// We need a seperate block to use as a measuring tool using the same text properties as the justified block.
Block measuringBlock = new Block(justifiedBlock);
// Make sure the WordSpacing property is initialized to zero. // Break the text string into lines, adding as many full words as we can to each line.// This does not support hyphenation.
List<string> lines = breakIntoLines(Text, TextBoxWidth, ref measuringBlock);
for (int i = 0; i <= lines.Count - 1; i++)
{
currentLine = lines[i];
// For all except the last line, space out the words to fill the allowed space.if (i < (lines.Count - 1))
{
do
{
// Set the word spacing before inserting the text.
measuringBlock.TextProperties.WordSpacing = currentSpacing;
// We need to reinsert the text each time the spacing changes since the spacing is only applied on insert
measuringBlock.Clear();
measuringBlock.InsertText(currentLine);
measuringBlock.Measure();
if (measuringBlock.DesiredSize.Width > TextBoxWidth && spacingStep != 0.01)
{
// Step back by one then narrow in at finer increments
currentSpacing -= spacingStep;
spacingStep /= 10;
currentSpacing += spacingStep;
continue;
}
currentSpacing += spacingStep;
}
while (measuringBlock.DesiredSize.Width > TextBoxWidth && spacingStep == 0.01);
// Reduce by one step to bring it back into allowed width
currentSpacing -= spacingStep;
}
// Add the line of text with the appropriate spacing.using (justifiedBlock.SaveTextProperties())
{
justifiedBlock.TextProperties.WordSpacing = currentSpacing;
justifiedBlock.InsertText(currentLine);
justifiedBlock.InsertLineBreak();
}
// Reset for next line
currentSpacing = 0; spacingStep = 1;
}
}
return justifiedBlock;
}
private static List<string> breakIntoLines(string Text, int maxLineWidth, ref Block measuringBlock)
{
List<string> sResults = new List<string>();
string[] lines = Text.Split(newstring[] { "\n", "\r\n" }, StringSplitOptions.None);
foreach (string line in lines)
{
string[] words = line.Split(newstring[] { " " }, StringSplitOptions.RemoveEmptyEntries);
sResults.Add(""); // Initialize the line. This may remain empty if the line is blank.for (int i = 0; i <= words.Length - 1; i++)
{
if (sResults[sResults.Count - 1].Length == 0)
sResults[sResults.Count - 1] = words[i];
else
{
string testLine = string.Concat(sResults[sResults.Count - 1], " ", words[i]);
measuringBlock.InsertText(testLine);
measuringBlock.Measure();
if (measuringBlock.DesiredSize.Width <= maxLineWidth)
// Replace with new, longer line
sResults[sResults.Count - 1] = testLine;
else// Start new line
sResults.Add(words[i]);
// Remove text from measuring block to reset for next word
measuringBlock.Clear();
}
}
}
return sResults;
}
}
Hi,
there seems to be a bug in the PdfFormatProvider Import code.
When using swedish culture, the read PDF data has incorrect position matrices, causing all elements to disappear.
Example ("en"):
Example ("sv"):
Once found, it's easy for me to work around this big.
But it looks like it should be easy to fix!
Thanks,
Anton
I am trying to import HTML but when I try to export the radflowdocument it is blank . (i've exported to both HTML and PDF)
this is in blazor WASM.
Thanks
Phil
I'm using Telerik.Documents.Spreadsheet (2022.3.1108) .Net C# 6.0. I have my excel spreadsheet formated as I want (Excel.png). The text in the cells are clearly visible. Once the same spreadsheet is export using PdfFormatProvider. The last half a character is trimmed (not visible). (Pdf.png). While creating the excel file I do have "AutoFitWidth()" set for all the columns.
Export Code here
public void CreatePdf(Workbook wb, string fileName)
{
var fixedProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.PdfFormatProvider();
RadFixedDocument fixedDocument = fixedProvider.ExportToFixedDocument(wb);
var pdfFormatProvider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider();
pdfFormatProvider.ExportSettings.ShouldEmbedFonts = true;
using (Stream output = File.OpenWrite($"c:\\temp\\{fileName}.pdf"))
{
pdfFormatProvider.Export(fixedDocument, output);
}
}
When I convert a document from .docx to .PDF, the footer disappears. There are a couple checkboxes in the docx that disappear as well. Does this library support these things? Is there something specific I have to do to make them work?
My code:
RadFlowDocument document; Telerik.Documents.ImageUtils.ImagePropertiesResolver defaultImagePropertiesResolver = new Telerik.Documents.ImageUtils.ImagePropertiesResolver(); Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.ImagePropertiesResolver = defaultImagePropertiesResolver; using (FileStream input = new FileStream("file.bin", FileMode.Create, System.IO.FileAccess.ReadWrite)) { input.Write(doc.Content, 0, doc.Content.Count()); DocxFormatProvider provider = new DocxFormatProvider(); document = provider.Import(input); //insert the data RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document); foreach (var item in templateValues) { editor.ReplaceText(item.Key, item.Value); } //change the value in the footer editor.ReplaceText("[Document Revised Date]", doc.lastModified.ToShortDateString()); PdfFormatProvider pdfProvider = new PdfFormatProvider(); var result = pdfProvider.Export(document); return result;
Hi Telerik.
Attempting to create a Header with your documentation. Link: Headers and Footers
The example code writes:
RadFlowDocument document = new RadFlowDocument();
document.Sections.AddSection();
document.Sections.First().Headers.Add(); // Creates the default Header.
document.Sections.First().Headers.Add(HeaderFooterType.First);
document.Sections.First().Headers.Add(HeaderFooterType.Even);
However I get the errorcode:
'object' does not contain a definition for'Headers'and no accessible extension method 'Headers' accepting a first argument of type
'object'could be found (are you missing a using directive or an assembly reference?)
Am I doing something wrong?
Hello,
we are currently in need for a docx to pdf converter.
The docx file contains placeholders created in word. When we want to read it in with the Telerik documents package it gives us an error.
The Import() function returns:
Error: System.ArgumentException: 'An item with the same key has already been added. Key: 0'
Here is the code we are using:
static void ConverDocxToPdf(string path, string resultPath)
{
var docxPRovider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
var pdfProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
byte[] docBytes = File.ReadAllBytes(path);
var document = docxPRovider.Import(docBytes);
var resultBytes = pdfProvider.Export(document);
File.WriteAllBytes(resultPath, resultBytes);
}
I attached one of the templates we are using. When you try to read it in, it will return the mentioned error.
Thank you in advance!