I want to pull the text from .doc or .docx with the same font, size on Java. But so far I can’t even get the text out. I tried many options, but everywhere the answer comes out with cubes.
The following code is used:
package myconverter; import com.itextpdf.text.Anchor; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Font; import com.itextpdf.text.FontFactory; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.CMYKColor; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; public class Myconverter { public static void main(String[] args) throws FileNotFoundException, DocumentException, IOException { String result = ""; String line; String doc = "C:\\samples\\HelloWorld.doc"; String pdf = doc.substring(0, doc.lastIndexOf('.') + 1) + "pdf"; Document document = new Document(PageSize.A4, 50, 50, 50, 50); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf)); document.open(); Anchor anchorTarget = new Anchor(""); anchorTarget.setName("BackToTop"); Paragraph paragraph1 = new Paragraph(); // paragraph1.setSpacingBefore(50); paragraph1.add(anchorTarget); document.add(paragraph1); File file = new File(doc); try (FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr)) { while((line = br.readLine()) != null){ result = line; // System.out.println(result); //document.add(new Paragraph(result, FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD,new CMYKColor(0, 255, 0, 0)))); document.add(new Paragraph(result)); } } // System.out.println(result); document.close(); } }