I am converting a docx file to pdf with the following code using Aspose Words.
using System;
using Aspose.Words;
using Aspose.Words.Saving;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("DOCX to PDF Converter using Aspose.Words");
// Specify the path to the input DOCX file
string inputDocxPath = "docs/textfield.docx"; // Replace with the actual path to your DOCX file
// Specify the path to the output PDF file
string outputPdfPath = "pdfs/textfield.pdf"; // Replace with the desired path for the PDF file
try
{
var licenseAspose = new License();
licenseAspose.SetLicense("licenses/Aspose.Words.lic");
// Load the DOCX file
Document doc = new Document(inputDocxPath);
var warningCollection = new WarningInfoCollection();
doc.WarningCallback = warningCollection;
PdfSaveOptions saveOptions = new PdfSaveOptions
{
ImageCompression = PdfImageCompression.Jpeg,
PreserveFormFields = true,
UpdateFields = false,
EmbedFullFonts = true,
SaveFormat = SaveFormat.Pdf,
};
// Save the document as a PDF
doc.Save(outputPdfPath, saveOptions);
Console.WriteLine($"DOCX file successfully converted to PDF: {outputPdfPath}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
The docx file contains a rich text control where I have used tab characters for white space.
In the resulting pdf file, square blocks are shown instead of tabs.
It shows tab spaces properly only when I click on that field of the pdf.
Befoe clicking on the field:
After clicking on the field:
Input docx file:
textfield.docx (19.0 KB)
Output pdf file:
textfield.pdf (567.2 KB)
I am using Aspose Words version 24.8.0 with Dot NET 6.
@SadmanRiz1
It seems that you are experiencing an issue with tab characters not displaying correctly in a PDF generated from a DOCX file using Aspose.Words. Specifically, the tab spaces appear as square blocks until the field is clicked.
This behavior can occur due to how the PDF rendering engine interprets tab characters in form fields. Here are a few suggestions to address this issue:
-
Check Font Embedding: Ensure that the fonts used in your DOCX file are properly embedded in the PDF. You are already using the EmbedFullFonts
option in your PdfSaveOptions
, which is good. This helps maintain the appearance of the text in the PDF.
-
Adjust Tab Settings: If the tab settings in your DOCX file are not standard, consider adjusting them. You can set specific tab stops in the document to ensure that the spacing is consistent when converted to PDF.
-
Use Additional Text Positioning: You might want to enable additional text positioning in your PDF save options. This can help with the layout of text fields and may resolve issues with tab characters. You can do this by setting AdditionalTextPositioning
to true
in your PdfSaveOptions
.
Here is an example of how to modify your existing code to include this option:
PdfSaveOptions saveOptions = new PdfSaveOptions
{
ImageCompression = PdfImageCompression.Jpeg,
PreserveFormFields = true,
UpdateFields = false,
EmbedFullFonts = true,
AdditionalTextPositioning = true, // Enable additional text positioning
SaveFormat = SaveFormat.Pdf,
};
-
Update Aspose.Words: Ensure that you are using the latest version of Aspose.Words. Sometimes, issues like these are resolved in newer releases, so updating to the latest version may help.
-
Testing with Different Fonts: If the issue persists, try using different fonts in your DOCX file to see if the problem is related to a specific font.
If these suggestions do not resolve the issue, consider reaching out to Aspose support with your specific DOCX file and the generated PDF for further assistance.
Feel free to ask if you have any more questions or need further clarification!
Sources:
@SadmanRiz1
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSNET-27698
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.