Java - Convert a docx to a pdf document - java

I am trying to convert a docx document containing a logo to a pdf document.
I have tried this :
FileInputStream in=new FileInputStream(fileInput);
XWPFDocument document=new XWPFDocument(in);
File outFile=new File(fileOutput);
OutputStream out=new FileOutputStream(outFile);
PdfOptions options=null;
PdfConverter.getInstance().convert(document,out,options);
But in the pdf document the logo is not at the right place.
Is there a way to create a PDF that is exactly the same as the docx document?

Could be document4j an option? It delegates the convertion to the native application.
This is achieved by delegating the conversion to any native application which understands the conversion of the given file into the desired target format.
File wordFile = new File( ... );
File target = new File( ... );
IConverter converter = ... ;
Future<Boolean> conversion = converter
.convert(wordFile).as(DocumentType.MS_WORD)
.to(target).as(DocumentType.PDF)
.prioritizeWith(1000) // optional
.schedule();
You can quickly test if the convertion fit your requirement with the "Local demo" on a Windows machine with Word and Excel installed:
git clone https://github.com/documents4j/documents4j.git
cd documents4j
cd documents4j-local-demo
mvn jetty:run
Then go to http://localhost:8080
See the full documentation here :
http://documents4j.com/#/

Related

PDF with forms to simple image PDF

How can I transform a PDF with forms made in Adobe Livecycle to a simple image PDF using Java?
I tried using Apache PDFBox but it can't save as image a PDF with forms.
This is what I tried(from this question: Convert PDF files to images with PDFBox)
String pdfFilename = "PDForm_1601661791_587488.pdf";
try (PDDocument document = PDDocument.load(new File(pdfFilename))) {
PDFRenderer pdfRenderer = new PDFRenderer(document);
for (int page = 0; page < document.getNumberOfPages(); ++page) {
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
ImageIOUtil.writeImage(bim, pdfFilename + "-" + (page+1) + ".png", 300);
}
} catch (IOException ex) {
Logger.getLogger(StartClass.class.getName()).log(Level.SEVERE, null, ex);
}
But is not working, the result is an image where it writes that "The document you are trying to load requires Adobe Reader 8 or higher.
I guess is not possible, I tried many libraries and none worked.
This is how I solved the problem:
I used an external tool - PDFCreator.
In PDFCreator I created a special printer that prints and saves the PDF without asking any questions(you have these options in PDFCreator).
This is simple to reproduce in PDFCreator because in the Debug section you have an option to load a config file, so I have this file prepared, I just install PDFCreator and load the config file.
If you will use my INI file in the link above you should know that the resulted PDF is automatically saved in the folder: "current user folder/Desktop/temporary".
The rest of the job is done from Java using Adobe Reader, the code is in my case:
ProcessBuilder pb = new ProcessBuilder(adobePath, "/t", path+"/"+filename, printerName);
Process p = pb.start();
This code opens my PDF in AdobeReader, prints the PDF to the specified virtual printer, and exists automatically.
"adobePath" is the path to the adobe executable
path+"/"+filename is the path to my PDF.
"printerName" is the name of the virtual printer created in PDFCreator
So this is not a pure Java solution and in the future, I intend to use Apache PDFBox to generate my PDF's in a format that is compatible with browsers and all readers...but this works also.

how to generate the POI excel file in a specific location?

I have created a java maven programme that generate an excel file using Apache POI API, all is working correctly but when i try to put the report to a specific location doing like this
: FileInputStream inputStream = new FileInputStream(new File("C:\\Users\\C5292600\\Desktop\\report1.xlsx"));
i get the following error :
Exception in thread "main" java.lang.IllegalArgumentException: Invalid char (:) found at index (1) in sheet name 'C:\Users\C5292600\Desktop\repor'
at org.apache.poi.ss.util.WorkbookUtil.validateSheetName(WorkbookUtil.java:151)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.createSheet(XSSFWorkbook.java:873)
at com.occCheckNoData.report.Create.newSheet(Create.java:25)
at com.occCheckNoData.report.mainReport.main(mainReport.java:34)
Is it an existing path? If not use something like File file = new File(yourpath); file.mkdirs(); to create a new path. And to generate the file FileOutputStream fileOut = new FileOutputStream(new File(yourpath + filetype));

Fop - streaming PDF

I want to make pdf stream. Without trying to add special characters it use to works, but with this part it doesn't streaming my pdf from third line
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
File file1 = new File("fonts.xsl");
Configuration cfg = cfgBuilder.buildFromFile(file1);
fopFactory.setUserConfig(cfg);
Here is link to this fonts.xsl
And class with that code FOPdpf

How to create PS file from PDF file using Java?

I wrote an application to create PDF file to PDDocument file it work fine. i use the pdfbox library
PDDocument pdfDoc = PDDocument.load(pdfFile);
Now i want to create PS(Post script) file from PDF file. Is there are any way in java. I can use any free API.
Many thanks.
Adobe seems to have a library. Here are some instructions. Please note, I have not tried this myself: http://help.adobe.com/en_US/livecycle/9.0/programLC/help/index.htm?content=000761.html
This link has a more detailed solution:
http://help.adobe.com/en_US/livecycle/9.0/programLC/help/index.htm?content=000074.html
You can use PDFDocument to load your PDF then use PSConverter to convert the PDF document into an OutputStream.
The library I'm using is called ghost4j:
import org.ghost4j.converter.PSConverter;
import org.ghost4j.document.PDFDocument;
Here's a small snippet:
private ByteArrayOutputStream convertPDFtoPS(){
ByteArrayOutputStream outstreamFile = new ByteArrayOutputStream();
try{
PDFDocument document = new PDFDocument();
//getPDFFile just returns an InputStream of the PDF file
document.load(getPDFFile());
PSConverter converter = new PSConverter();
converter.convert(document, outstreamFile);
outstreamFile.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return outstreamFile;
}

Uploading a PDF file to Google Docs generated by pdfjet on GAE/J

I need to upload a PDF file to users google docs which is generated by pdfjet on google app engine. I figure out to generate pdf using pdfjet for gae/j. pdfjet uses streams to create the pdf. Is there anyway to convert stream to file so I can upload to users google docs. I tried gaevfs but couldn't make it work. I can use another pdf generation solution if needed or another virtual file system etc.
PDF generation code
ByteArrayOutputStream os = new ByteArrayOutputStream();
PDF pdf = new PDF(os);
Google Docs API code
DocumentListEntry newEntry = new PdfEntry();
newEntry.setTitle(new PlainTextConstruct("Some Report"));
The line I couldn't get make it work : setFile(File, String)
newEntry.setFile(pdf, "application/pdf");
Thanks.
Couldn't you simply write from ByteArrayOutputStream to FileOutputStream using the ByteArrayOutputStream.writeTo(OutputStream) method? Like this:
ByteArrayOutputStream os = new ByteArrayOutputStream();
PDF pdf = new PDF(os);
FileOutputStream fos = new FileOutputStream("myPDF.pdf");
baos.writeTo(os);

Categories

Resources