Thu Nov 30, 2023 10:34 am
We still have this issue , I am sending document that you can test on an code where we convert doc to pdf and find some sentences and then replace it with sting made of "~" in the same width as the sentence that we replacing .Also I am sending the pdf product after code.
Code:
Document doc =new Document();
PdfDocument doc1 = new PdfDocument();
MemoryStream memoryStream1 =new MemoryStream();
doc.LoadFromFile(@"root...\file-sampleBefore.doc");
ToPdfParameterList pdf2 = new ToPdfParameterList()
{
IsEmbeddedAllFonts = true
};
pdf2.DisableLink = false;
pdf2.UsePSCoversion = true;
doc.SaveToStream(memoryStream1, pdf2);
doc1.LoadFromStream(memoryStream1);
List<string> dictionary = new List<string> { "dapibus", "viverra" , "In non mauris justo.Duis vehicula mi vel mi pretium, a viverra erat
efficitur.viverra Mauris id", "Mauris id ex erat.", "Maecenas" };
PdfTextFindOptions findOptions = new PdfTextFindOptions();
findOptions.Parameter = TextFindParameter.WholeWord | TextFindParameter.IgnoreCase;
dictionary = dictionary.OrderByDescending(x => x.Length).ToList();
int i = 0;
PdfFont font1;
foreach (string word in dictionary)
{
foreach (PdfPageBase page in doc1.Pages)
{
i++;
PdfTextFinder finder = new PdfTextFinder(page);
finder.Options = findOptions;
List<PdfTextFragment> result = finder.Find(word);
PdfTextReplacer replacer = new PdfTextReplacer(page);
replacer.Options.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;
if (result.Count() > 0)
{
foreach (PdfTextFragment findWord in result)
{
font1 = new PdfFont(PdfFontFamily.Helvetica, findWord.Sizes[0].Height);
PdfStringFormat format = new PdfStringFormat();
SizeF sizeOne = font1.MeasureString("~", format);
int count = (int)(findWord.Bounds[0].Width / sizeOne.Width);
if (count == 0) count = 1;
string replaseStr = String.Concat(Enumerable.Repeat("~", count));
SizeF sizereplace = font1.MeasureString(replaseStr, format);
while ((findWord.Bounds[0].Width - sizeOne.Width) > sizereplace.Width)
{
replaseStr = replaseStr + "~";
sizereplace = font1.MeasureString(replaseStr, format);
}
try
{
replacer.ReplaceText(findWord.Text, replaseStr);
}
catch (Exception ex)
{
}
}
}
}
}
doc1.SaveToFile(@"root..\file-sampleAfter.pdf");
Login to view the files attached to this post.