We're sorry Aspose doesn't work properply without JavaScript enabled.

# Filename Field in header or footer of word document lost after converting docx to pdf usding .NET Aspose.Word

Hi Support,
I have created a word document and insert the field “file name” from the Word field selection into the header or footer.
The field is automatically filled with the file name of the document.
If you convert this document into PDF using .NET Aspose.Word, the result PDF document doen’t show the file name in header or footer.
You get a example word document in attachment.
Dateinamen.docx (17.6 KB)

Thanks
Zhentao

@zhentaosong Aspose.Words properly updates file name field, this value is taken from file name specified while loading or while saving. But if your document is loaded from stream and saved to stream there is not file name specified and the field value is empty string.

Hi,
how can I load documet from file?.

I load document fro, stream like this.

using (var targetStream = targetFile.Create())
{
using (var contentStream = streamEntry.GetContentStream())
{
var saveOptions = new PdfSaveOptions();
}
}


Thanks
Zhentao

@zhentaosong You can pass file name as a parameter of Document constructor:

Document doc = new Document(@"C:\Temp\in.docx");
doc.Save(@"C:\Temp\out.pdf");


We have stream from server, if I save stream in file and load from, than I have more performance problem. My request, can I set filename in property?

Hi
If I put the ‘File Name’ field in the content area, then it works. But it doesn’t work in header or footer.

@zhentaosong In your case, you can lock the field to prevent it’s updating. For example see the following code:

foreach (Field f in doc.Range.Fields)
{
if (f.Start.FieldType == FieldType.FieldFileName)
{
// Uncomment the below line to set file name you need. Otherwise value read from the document will be used.
//f.Result = "This is my file.pdf";
f.IsLocked = true;
}
}


Hi Support,

Word document doesn’t update automatically the field ‘File Name’ in header or footer . If I change the ‘File Name’ in my PDF conversion code, then my PDF document has a different content of the field ‘File Name’ than the original Word document. My question: is it possible to transfer the ‘File Name’ field as it is in the Word document to the PDF document? Here I have an example Word document, the field ‘File Name’ is different the physical file name.

Thanks
Zhentao

Here I have an example Word document, the field ‘File Name’ is different the physical file name. Can I convert the content of the field to PDF like Word document? Then I have the same content in PDF and Word.
10147076487_5_Dateinamen (1) Version 2.docx (18.7 KB)

@zhentaosong Sure, the code I have provided does exactly this. Here is full code I used for testing:

// Load document from stream.
MemoryStream inputStream = new MemoryStream(File.ReadAllBytes(@"C:\Temp\10147076487_5_Dateinamen (1) Version 2.docx"));
Document doc = new Document(inputStream);

foreach (Field f in doc.Range.Fields)
{
if (f.Start.FieldType == FieldType.FieldFileName)
{
// Uncomment the below line to set file name you need. Otherwise value read from the document will be used.
//f.Result = "This is my file.pdf";
f.IsLocked = true;
}
}

// Save document to stream
using (FileStream outFile = File.Create(@"C:\Temp\out.pdf"))
{
doc.Save(outFile, SaveFormat.Pdf);
}


Attached is output PDF. out.pdf (27.6 KB)
As you can see FILENAME was not updated and the output PDF has the same value as you see when open document in MS Word.