I need to convert from word to pdf in c# by using ITextSharp
Loading
I need to convert from word to pdf in c# by using ITextSharp
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Leon DPosted Jul 19, 2024, 9:01 AM
You can try Spire.Doc to convert Word with Arabic text to PDF. Make sure your system has already installed the fonts used in your Word file.
Here is a basic code example:
Spire.Doc supports various advanced conversion options when converting Word to PDF, you can check this article for more details:
https://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/How-to-Convert-Word-to-PDF.html
Vishal JoshiPosted Jul 16, 2024, 6:23 AM
Hello Rasha,
To convert a Word file to a PDF in C#, you can use the Microsoft Office Interop library if you have Microsoft Office installed on your machine, or you can use the Open XML SDK along with a third-party library like Open XML PowerTools. Below are examples of both methods:
Method 1: Using Microsoft Office Interop
Add a reference to
Microsoft.Office.Interop.Word:Microsoft Word XX.0 Object Library(where XX is the version number).Method 2: Using Open XML SDK and Open XML PowerTools
Thanks
Vishal Joshi
Adarsh NigamPosted Jul 15, 2024, 4:49 PM
To convert an Arabic Word file to PDF and ensure that the Arabic text is displayed correctly, you need to embed a font that supports Arabic characters in the PDF document. The default fonts used by iTextSharp might not support Arabic, leading to the text not appearing correctly. Here's an updated version of your code that uses a font supporting Arabic characters.
First, ensure you have an Arabic-supporting font file (e.g., `arialuni.ttf`) in your project directory.
--------------------------------------<><><><><><><><><>---------------------------------------------
### Updated Code
using System;
using System.IO;
using Xceed.Words.NET;
using iTextSharp.text;
using iTextSharp.text.pdf;
class Program
{
static void Main(string[] args)
{
string wordFilePath = @"path\to\your\wordfile.docx";
string pdfFilePath = @"path\to\your\pdffile.pdf";
string fontPath = @"path\to\your\arialuni.ttf"; // Path to a font that supports Arabic characters
var doc = DocX.Load(wordFilePath);
using (FileStream fs = new FileStream(pdfFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
Document pdfDoc = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, fs);
pdfDoc.Open();
// Load the font that supports Arabic characters
BaseFont baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(baseFont, 12);
foreach (var paragraph in doc.Paragraphs)
{
pdfDoc.Add(new Paragraph(paragraph.Text, font));
}
pdfDoc.Close();
}
Console.WriteLine("Conversion completed successfully!");
}
}
### Steps to Follow
1. **Download and Install the Required Packages**
Ensure you have the necessary NuGet packages installed:
Install-Package itextsharp
Install-Package DocX
2. **Ensure a Font Supporting Arabic Characters**
Download a font that supports Arabic characters, such as `arialuni.ttf`. Place this font file in your project directory.
3. **Update the Paths**
Update the `wordFilePath`, `pdfFilePath`, and `fontPath` variables with the correct paths to your Word file, output PDF file, and the font file respectively.
4. **Run the Code**
Execute the program. The Arabic text in the Word document should now be correctly converted and displayed in the PDF file.
### Explanation
- **BaseFont and Font Initialization**:
- `BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED)`: This initializes a font that supports Unicode characters, ensuring that Arabic text is rendered correctly.
- `Font font = new Font(baseFont, 12)`: This creates a font instance with a specified size using the base font.
--------------------------------------<><><><><><><><><>---------------------------------------------
- **Adding Paragraphs with the Specified Font**:
- The `new Paragraph(paragraph.Text, font)` ensures that each paragraph from the Word document is added to the PDF using the specified font, which supports Arabic characters.
By following these steps and using the updated code, you should be able to convert an Arabic Word file to a PDF correctly.
--------------------------------------<><><><><><><><><>---------------------------------------------
rasha galalPosted Jul 15, 2024, 1:33 PM
I excuted this code, but it didn't convert arabic word file to pdf correctly, the arabic text doesnot appear, how can i convert arabic word file to pdf ??
Amit MohantyPosted Jul 15, 2024, 7:31 AM
Install iTextSharp and DocX via NuGet Package Manager.