Hello, I am trying to replace text according to sample given in Developer Guide. First I used TT font installed on my system as default. Later I found I need to use Type 1 font. As they have the same name I had to remove TT font set first and then install Type 1 font set. So now I use ExperSans-Regular Type 1 font in my test document.
I am on Win7, JDK 7u85. I create docx file with simple text in MS Word 2007 then I convert to PDF using Acrobat 9.5 plugin from Word (and using our own custom settings for PDF converter job). I will attach both text docx and pdf file here with my font file.
My code just gets text fragment gets font and font size from it and then it tries to use the same font and font size for new text fragment so that we only change text. (Without setting font when I worked with TTF we saw missing letters due to missing letters in font subset IIRC.)
(When I installed Type 1 fonts I just drag&drop pfm files, pfb files are regenerated.)
First I get font name and font size from any fragment in segment (they should be the same for all fragments) and then I get again font instance of font by call of Font font = FontRepository.findFont(fontName); but it looks like it returns some wrong instance as its font name is “exch” instead of “ExpertSans-Regular” which I pass as argument to findFont. When I log font paths it looks ok (Font paths:[C:\Windows\Fonts/])
Then when I call textFragment.getTextState().setFont(font); with this font instance I get exception below
Code:
for (String placeholderToReplace : textReplacementMap.keySet()) {
String textReplacement = textReplacementMap.get(placeholderToReplace);
TextFragmentAbsorber textFragmentAbsorber = new com.aspose.pdf.TextFragmentAbsorber(placeholderToReplace);
TextSearchOptions textSearchOptions = new com.aspose.pdf.TextSearchOptions(true);
textFragmentAbsorber.setTextSearchOptions(textSearchOptions);
pdfDocument.getPages().accept(textFragmentAbsorber);
com.aspose.pdf.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
for (com.aspose.pdf.TextFragment textFragment : (Iterable<com.aspose.pdf.TextFragment>) textFragmentCollection) {
String foundText = textFragment.getText();
float fontSize = -1;
String fontName = “”;
TextSegmentCollection textSegmentCol = textFragment.getSegments();
for (TextSegment textSegment : textSegmentCol) {
logger.info(" textSegment text:{} fontName:{} fontSize:{}", textSegment.getText(), textSegment.getTextState().getFont().getFontName(),
textSegment.getTextState().getFontSize());
if (fontSize < 0) {
fontSize = textSegment.getTextState().getFontSize();
}
if (StringUtils.isBlank(fontName)) {
fontName = textSegment.getTextState().getFont().getFontName();
}
}
// Replace only when replacement text is different from original text
if (!foundText.equals(textReplacement)) {
result.increaseReplacementCounter(placeholderToReplace);
textFragment.setText(textReplacement);
if (StringUtils.isBlank(fontName)) {
fontName = “Arial”;
logger.error("# # No font found. Use Arial");
}
logger.info(" fontName:{}", fontName);
Font font = FontRepository.findFont(fontName);
logger.info("# # font.getFontName:{} isEmbedded:{} isSubset:{} fontSize:{}", font.getFontName(), font.isEmbedded(), font.isSubset(), fontSize);
textFragment.getTextState().setFont(font);
if (fontSize > 0) {
textFragment.getTextState().setFontSize(fontSize);
}
}
}
}
Exception:
Caused by: com.aspose.pdf.internal.ms.System.z106: Specified method is not supported.
at com.aspose.pdf.internal.p53.z3.m2(Unknown Source) ~[aspose-pdf-11.5.0.jar:11.5.0]
at com.aspose.pdf.internal.p53.z3.(Unknown Source) ~[aspose-pdf-11.5.0.jar:11.5.0]
at com.aspose.pdf.internal.p66.z2.m1(Unknown Source) ~[aspose-pdf-11.5.0.jar:11.5.0]
at com.aspose.pdf.internal.p53.z26.m1(Unknown Source) ~[aspose-pdf-11.5.0.jar:11.5.0]
at com.aspose.pdf.internal.p54.z1.m1(Unknown Source) ~[aspose-pdf-11.5.0.jar:11.5.0]
at com.aspose.pdf.internal.p55.z1.m1(Unknown Source) ~[aspose-pdf-11.5.0.jar:11.5.0]
at com.aspose.pdf.internal.p55.z1.m1(Unknown Source) ~[aspose-pdf-11.5.0.jar:11.5.0]
at com.aspose.pdf.internal.p56.z6.m1(Unknown Source) ~[aspose-pdf-11.5.0.jar:11.5.0]
at com.aspose.pdf.internal.p56.z6.m1(Unknown Source) ~[aspose-pdf-11.5.0.jar:11.5.0]
at com.aspose.pdf.internal.p56.z6.m1(Unknown Source) ~[aspose-pdf-11.5.0.jar:11.5.0]
at com.aspose.pdf.TextState.setFont(Unknown Source) ~[aspose-pdf-11.5.0.jar:11.5.0]
at com.aspose.pdf.TextFragmentState.setFont(Unknown Source) ~[aspose-pdf-11.5.0.jar:11.5.0]
at com.barclays.rp.util.pdfFixerUtil.service.PdfFixer.simpleDocumentReplaceByRegex(PdfFixer.java:152) ~[pdfFixerUtil-1.7-SNAPSHOT.jar:na]
… 33 common frames omitted
For completness I add here what params I use for replacement but I have no idea if it is relevant or not. I may try to create more trivial code sample to reproduce this issue.
Looking for: (?i)Produced: \s*\d{2}\s(January|February|March|April|May|June|July|August|September|October|November|December)\s\d{4},\s\d{2}:\d{2}\s[A-Z]{2}T
Found text: Produced: 12 February 2016, 14:30 GMT
Replacing with text: Produced: 16 May 2016, 14:21 GMT
Hi Marek,
Thanks for contacting support.
I have tested the scenario using Aspose.Pdf for Java 11.5.0 where I have used following code and I am unable to notice any issue. As per my observations, correct font name is being retrieved and resultant file also contains ExpertSans-Regular font under Fonts tab. For your reference, I have also attached the output file generated over my end.
[Java]
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("c:/pdftest/Test-ESR-T1.pdf");
// for (String placeholderToReplace : textReplacementMap.keySet()) {
String textReplacement = "10 March 2016, 14:30 GMT"; // textReplacementMap.get(placeholderToReplace);
com.aspose.pdf.TextFragmentAbsorber textFragmentAbsorber = new com.aspose.pdf.TextFragmentAbsorber("12 February 2016, 14:30 GMT");
com.aspose.pdf.TextSearchOptions textSearchOptions = new com.aspose.pdf.TextSearchOptions(true);
textFragmentAbsorber.setTextSearchOptions(textSearchOptions);
pdfDocument.getPages().accept(textFragmentAbsorber);
com.aspose.pdf.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
for (com.aspose.pdf.TextFragment textFragment : (Iterable<com.aspose.pdf.TextFragment>) textFragmentCollection) {
String foundText = textFragment.getText();
float fontSize = -1;
String fontName = "C:\\pdftest\\ESR-T1\\ExpertSans-Regular.pfm";
com.aspose.pdf.TextSegmentCollection textSegmentCol = textFragment.getSegments();
for (com.aspose.pdf.TextSegment textSegment : textSegmentCol) {
if (fontSize < 0) {
fontSize = textSegment.getTextState().getFontSize();
}
fontName = textSegment.getTextState().getFont().getFontName();
System.out.println(fontName);
}
// Replace only when replacement text is different from original text
if (!foundText.equals(textReplacement)) {
textFragment.setText(textReplacement);
com.aspose.pdf.Font font = com.aspose.pdf.FontRepository.findFont(fontName);
textFragment.getTextState().setFont(font);
if (fontSize > 0) {
textFragment.getTextState().setFontSize(fontSize);
}
}
}
pdfDocument.save("c:/pdftest/UpdatedDocument.pdf");
Hi, I created simple sample and I have the same problem when I use symbolic font name I get from existing text fragment. In sample to keep it short I simply use as font name “ExpertSans-Regular”.
When I use full path to pfm file I get:
Caused by: class com.aspose.pdf.exceptions.FontNotFoundException: Font c:\Windows\Fonts\ExpertSans-Regular.pfm was not found
But that file is present at default font location and this Type 1 font is visible in Control Panel -> Fonts and MS Word. Can this be some configuration issue? Anyway preferred way is to use symbolic font name not full path to any file on filesystem.
Code sample:
public static void main(String[] args) throws Exception {
PdfReplace3 pdfReplace = new PdfReplace3();
pdfReplace.replace();
}
public void replace() throws IOException {
String iFile = “C:\work\pdf\Test-ESR-T1.pdf”;
String oFile = “C:\work\pdf\output.pdf”;
documentReplace(iFile, oFile);
}
public void documentReplace(String iFile, String oFile) {
Document pdfDocument = new Document(iFile);
FontRepository.loadFonts();
System.out.println(“Font paths:” + Document.getLocalFontPaths());
try {
String toReplace = “GMT”;
String newText = “EST”;
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(toReplace);
TextSearchOptions textSearchOptions = new TextSearchOptions(true);
textFragmentAbsorber.setTextSearchOptions(textSearchOptions);
pdfDocument.getPages().accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
for (TextFragment textFragment : (Iterable) textFragmentCollection) {
String foundText = textFragment.getText();
String fontName = “ExpertSans-Regular”;
textFragment.setText(newText);
System.out.println(" fontName:" + fontName);
//Font font = FontRepository.findFont(fontName);
Font font = FontRepository.findFont(“c:\Windows\Fonts\ExpertSans-Regular.pfm”);
System.out.println(“font:” + font.getFontName());
textFragment.getTextState().setFont(font);
}
pdfDocument.optimize();
pdfDocument.save(oFile);
} finally {
pdfDocument.close();
}
}
Hi Marek,mslama2:
Hi, I created simple sample and I have the same problem when I use symbolic font name I get from existing text fragment. In sample to keep it short I simply use as font name "ExpertSans-Regular".When I use full path to pfm file I get:Caused by: class com.aspose.pdf.exceptions.FontNotFoundException: Font c:\Windows\Fonts\ExpertSans-Regular.pfm was not foundBut that file is present at default font location and this Type 1 font is visible in Control Panel -> Fonts and MS Word. Can this be some configuration issue? Anyway preferred way is to use symbolic font name not full path to any file on filesystem.Code sample:public static void main(String[] args) throws Exception {PdfReplace3 pdfReplace = new PdfReplace3();pdfReplace.replace();}public void replace() throws IOException {String iFile = "C:\\work\\pdf\\Test-ESR-T1.pdf";String oFile = "C:\\work\\pdf\\output.pdf";documentReplace(iFile, oFile);}public void documentReplace(String iFile, String oFile) {Document pdfDocument = new Document(iFile);FontRepository.loadFonts();System.out.println("Font paths:" + Document.getLocalFontPaths());try {String toReplace = "GMT";String newText = "EST";TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(toReplace);TextSearchOptions textSearchOptions = new TextSearchOptions(true);textFragmentAbsorber.setTextSearchOptions(textSearchOptions);pdfDocument.getPages().accept(textFragmentAbsorber);TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();for (TextFragment textFragment : (Iterable) textFragmentCollection) {String foundText = textFragment.getText();String fontName = "ExpertSans-Regular";textFragment.setText(newText);System.out.println(" fontName:" + fontName);//Font font = FontRepository.findFont(fontName);Font font = FontRepository.findFont("c:\\Windows\\Fonts\\ExpertSans-Regular.pfm");System.out.println("font:" + font.getFontName());textFragment.getTextState().setFont(font);}pdfDocument.optimize();pdfDocument.save(oFile);} finally {pdfDocument.close();}}
Thanks for sharing the details.
I have tested the scenario and have managed to reproduce same problem. For the sake of correction, I have logged it as PDFJAVA-35878 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.