Font issue when converting word to PDF format


We always find that that we cannot pick up the non-standard font in the result PDF page that displays well in the word document when we save word documents to PDF file format. In this article, I will use a few lines of codes to render word documents to PDF file with a non-standard font barcode font for example.

Background


When we save word documents to PDF file format, of course we want to get the result exactly the same as our original word document. Unfortunately, we always find that that we cannot pick up the non-standard font in the result PDF page that displays well in the word document. We've tried various things: Installing the TrueType font on the server where we are running our tests, embedded the font into the DOCX file. Whatever we do, we can't pick up the font and the font is not applied on the PDF file.

Solution


I searched a lot and finally found Spire.Doc, which can help us solve the font issue no matter I install the non-standard font or not in my server. It is very simple and I can use only few lines of codes to render my word documents to PDF file exactly what I want. In this article, I will use a non-standard font barcode font for example. It is easy to distinguish the difference and check the results. Wish it helps.

Please check my word document with the non-standard font Bar-Code 39:




Without the help of third party library, the barcode font will be replaced by text in the result PDF file. View the results as below:



Now I will show you the code of how to render word document to PDF, where the font won't be changed.

The first step we need to do is getting the Spire.Doc.dll and add it as the reference for my application in the Visual Studio.

If we can install the font on our server, then we only need to set the property IsEmbeddedAllFonts as true when converting the word document to PDF file.
{
Document doc = new Document();
doc.LoadFromFile("sample.docx");

ToPdfParameterList parms = new ToPdfParameterList()
{
IsEmbeddedAllFonts = true
};

doc.SaveToFile("result.pdf", parms);

}

If there is some inconvenience for us to install the font on our server machine, that doesn't matter. We can use the following method to apply for the fonts and save it to PDF successfully.
{
Document doc = new Document();
doc.LoadFromFile("sample.docx");


ToPdfParameterList parms = new ToPdfParameterList()
{
PrivateFontPaths = new List()
{

new PrivateFontPath("Bar-Code 39","..\\..\\Code39.ttf")
}

};

doc.SaveToFile("result.pdf", parms);

}

Now we can view the result PDF file with non-standard barcode font. It is exactly the same with the original word document with an extra warning message list above. After we apply for the license file of Spire.Doc, the warning message will be removed.



Summary


Spire.Doc is a proven reliable MS Word API for .NET which enables to perform many Word document processing tasks.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: