我们计划通过传递html文本作为输入来生成横向模式的pdf。此外, pdf将以横向模式生成,但内容为纵向模式。 以下是我们用于执行上述要求的步骤。
1)从源系统接收xml内容。
2)使用xsl从xml生成html输出。
3)将html输出作为输入传递给pdf java类,进一步该类将以横向模式生成pdf。
示例xml文件:http://www.filedropper.com/resettsample
示例xsl文件:http://www.filedropper.com/resettcopy
java代码
package testproject; import java.io.File; import java.io.FileOutputStream; import java.io.StringReader; import java.io.StringWriter; import java.text.SimpleDateFormat; import java.util.Date; import javax.xml.transform.Result; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import com.itextpdf.text.Document; import com.itextpdf.text.PageSize; import com.itextpdf.text.RectangleReadOnly; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.XMLWorkerHelper; public class PDFTest { public static void main(String[] args) { try { new PDFTest().PDFCreateRequest(); } catch (Exception ex) { ex.printStackTrace(); } } public static void PDFCreateRequest() throws Exception { StringWriter stringWriter = null; Result result = null; TransformerFactory tFactory = null; Transformer transformer = null; String strXMLFile = null; String strXSLFile = null; String strHTMLString = null; String strFeedFolderPath = null; String strPDFFileName = null; String strPDFFileNameWithExtension = null; String strPDFFile = null; Document document = null; PdfWriter pdfWriter = null; strXMLFile = "C:\\RESETT_sample.xml"; String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date()); strXSLFile = "C:\\RESETT_Copy.xsl"; stringWriter = new StringWriter(); result = new StreamResult(stringWriter); tFactory = TransformerFactory.newInstance(); transformer = tFactory.newTransformer(new StreamSource(strXSLFile)); transformer.transform(new StreamSource(strXMLFile), result); strHTMLString = stringWriter.toString(); System.out.println("###### [HTML output " + strHTMLString); strPDFFileName = "RESETT_" + timeStamp; strFeedFolderPath = "C:\\pdfdocuments"; strPDFFileNameWithExtension = strPDFFileName + ".pdf"; strPDFFile = strFeedFolderPath + File.separator + strPDFFileNameWithExtension; // This is for PDF Landscape output document = new Document(new RectangleReadOnly(842, 595), 10, 10, 75, 20); // This is for PDF Portrait output document = new Document(PageSize.A4, 10, 10, 75, 20); pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(strPDFFile)); document.open(); XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document, new StringReader(strHTMLString)); document.close(); } }
对于如何实现所需输出的任何建议表示赞赏。
由于 和Sandeep
答案 0 :(得分:3)
转到XML Worker examples并采取您想要的任何示例,但更改以下行:
Document document = new Document();
这样的事情:
Document document = new Document(PageSize.A4.rotate());
这将导致文档处于横向而非纵向。
您可能需要升级您的iText / XML Worker版本(确保始终使用相应的iText和XML Worker版本。)