Image is showing in Word file but not in PDF file

Hi,
I am using Aspose.Words version “23.7.0” to insert image with some text at the end of the word document then convert it to pdf file.
the word file is showing image and text with no issue but the pdf file is showing text only and not showing the image at all.
but if i remove last empty paragraph manual the image start showing in the pdf.
Appreciated your help to get why this happens to cover such scenario if the user use word file like this file i shared.
below is the block of my code.
sample word file:
test3.docx (33.8 KB)

var dummySignaurePath = @"D:\auth\Signature.jpg";
var doc = new WordDocument(coverLetterPath);
doc.LayoutOptions.TextShaperFactory = Aspose.Words.Shaping.HarfBuzz.HarfBuzzTextShaperFactory.Instance;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
builder.ParagraphFormat.LineSpacingRule = LineSpacingRule.AtLeast;
builder.InsertBreak(BreakType.ParagraphBreak);
builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Underline = Underline.None;
var img = builder.InsertImage(dummySignaturePath, 110, 45);
img.RelativeVerticalPosition = RelativeVerticalPosition.Page;
img.VerticalAlignment = VerticalAlignment.Center;
builder.InsertBreak(BreakType.ParagraphBreak);
var shape = builder.InsertShape(ShapeType.TextBox, 290, 88);
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
shape.VerticalAlignment = VerticalAlignment.Center;
shape.Stroked = false;
shape.TextBox.FitShapeToText = true;
shape.AllowOverlap = false;
shape.LastParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.MoveTo(shape.LastParagraph);
builder.ParagraphFormat.LineSpacingRule = LineSpacingRule.AtLeast;
builder.ParagraphFormat.SpaceAfter = 0;
builder.Font.Name = "Calibri (Body)";
builder.Font.Size = 11;
builder.Font.Bold = true;
builder.Font.SizeBi = 11;
builder.Font.BoldBi = true;
builder.Font.Bidi = true;
builder.Writeln("الأسم الأول و الأسم الأخير ");
builder.ParagraphFormat.SpaceAfter = 8;
builder.Writeln("المسمى الوظيفي ");
builder.ParagraphFormat.SpaceAfter = 0;
builder.Writeln("Manager's First name and Last Name");
builder.Writeln("Manager's Job Title");
doc.AutomaticallyUpdateStyles = true;
doc.UpdatePageLayout();
doc.UpdateFields();
PdfSaveOptions saveOptions = new PdfSaveOptions { Compliance = PdfCompliance.Pdf17 };
var pdfpath = coverLetterPath.Replace(".docx", ".pdf");
doc.Save(pdfpath, saveOptions);

@atalal The problem occurs because the last paragraph in your document is hidden so the inserted signature image is also hidden.

Please modify code like this to get the expected result:

string dummySignaurePath = @"C:\Temp\test.jpg";
Document doc = new Document(@"C:\Temp\in.docx");
doc.LayoutOptions.TextShaperFactory = Aspose.Words.Shaping.HarfBuzz.HarfBuzzTextShaperFactory.Instance;
DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToDocumentEnd();
builder.Font.Hidden = false; // <--------------------- Reset hidden flag.
builder.ParagraphFormat.LineSpacingRule = LineSpacingRule.AtLeast;
builder.InsertBreak(BreakType.ParagraphBreak);
builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Underline = Underline.None;
Shape img = builder.InsertImage(dummySignaurePath, 110, 45);
img.RelativeVerticalPosition = RelativeVerticalPosition.Page;
img.VerticalAlignment = VerticalAlignment.Center;
builder.InsertBreak(BreakType.ParagraphBreak);

Shape shape = builder.InsertShape(ShapeType.TextBox, 290, 88);
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
shape.VerticalAlignment = VerticalAlignment.Center;
shape.Stroked = false;
shape.TextBox.FitShapeToText = true;
shape.AllowOverlap = false;
shape.LastParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.MoveTo(shape.LastParagraph);
builder.ParagraphFormat.LineSpacingRule = LineSpacingRule.AtLeast;
builder.ParagraphFormat.SpaceAfter = 0;
builder.Font.Name = "Calibri (Body)";
builder.Font.Size = 11;
builder.Font.Bold = true;
builder.Font.SizeBi = 11;
builder.Font.BoldBi = true;
builder.Font.Bidi = true;
builder.Writeln("الأسم الأول و الأسم الأخير ");
builder.ParagraphFormat.SpaceAfter = 8;
builder.Writeln("المسمى الوظيفي ");
builder.ParagraphFormat.SpaceAfter = 0;
builder.Writeln("Manager's First name and Last Name");
builder.Writeln("Manager's Job Title");
PdfSaveOptions saveOptions = new PdfSaveOptions { Compliance = PdfCompliance.Pdf17 };
doc.Save(@"C:\Temp\out.pdf", saveOptions);

Thanks! it is working fine

1 Like