微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我如何使用Apache POI在Word文档中添加图像作为我的标题

我正在尝试使用Apache POI创build一个Word文档,它将包含一个图像作为其页眉和一些信息,即作为其页脚的段落。我能够创build只有段落的页眉页脚。但是我需要将图像添加到页眉我无法pipe理,我张贴我的代码在这里正确地给结果作为页眉页脚与段落。有人请帮助我达到这一点,

public class CreateWordDoc { public static void main (String[] args) throws Exception { //XWPFDocument document = new XWPFDocument(); CustomXWPFDocument document = new CustomXWPFDocument(new FileInputStream(new File("D:\test.docx"))); CTP ctp = CTP.Factory.newInstance(); CTR ctr = ctp.addNewR(); CTRPr rpr = ctr.addNewRPr(); CTText textt = ctr.addNewT(); textt.setStringValue( " Client Service Contact:[email protected]" ); XWPFParagraph codePara = new XWPFParagraph( ctp,document ); XWPFParagraph imagePara = new XWPFParagraph(ctp,document); XWPFParagraph[] newparagraphs = new XWPFParagraph[1]; newparagraphs[0] = codePara; XWPFParagraph[] imaheparagraphs = new XWPFParagraph[1]; imaheparagraphs[0]=imagePara; String blipId = document.addPictureData(new FileInputStream(new File("D:\msh.jpg")),Document.PICTURE_TYPE_JPEG); CTSectPr sectPr = document.getDocument().getBody().addNewSectPr(); XWPFheaderfooterPolicy headerfooterPolicy = new XWPFheaderfooterPolicy( document,sectPr ); document.createPicture(blipId,document.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG),400,129); headerfooterPolicy.createHeader( STHdrftr.FirsT,imaheparagraphs); //headerfooterPolicy.createFooter( STHdrftr.DEFAULT,newparagraphs ); FileOutputStream out = new FileOutputStream("D:\test.docx"); document.write(out); System.out.println("Doc Created"); } }

我的modofications

XWPFParagraph[] imaheparagraphs = new XWPFParagraph[1]; r.addPicture(new FileInputStream(new File("D:\msh.jpg")),Document.PICTURE_TYPE_JPEG,"D:\msh.jpg",21,32); r=imagePara.createRun(); imaheparagraphs[0]=imagePara; String blipId = document.addPictureData(new FileInputStream(new File("D:\msh.jpg")),129); headerfooterPolicy.createHeader( STHdrftr.DEFAULT,imaheparagraphs);

………………………………………….. ……………………………..

public class CustomXWPFDocument extends XWPFDocument{ public CustomXWPFDocument(FileInputStream in) throws IOException { super(in); } public void createPicture(String blipId,int id,int width,int height) { final int EMU = 9525; width *= EMU; height *= EMU; //String blipId = getAllPictures().get(id).getPackageRelationship().getId(); CTInline inline = createParagraph().createRun().getCTR().addNewDrawing().addNewInline(); String picXml = "" + "<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">" + " <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">" + " <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">" + " <pic:nvPicPr>" + " <pic:cNvPr id="" + id + "" name="Generated"/>" + " <pic:cNvPicPr/>" + " </pic:nvPicPr>" + " <pic:blipFill>" + " <a:blip r:embed="" + blipId + "" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"/>" + " <a:stretch>" + " <a:fillRect/>" + " </a:stretch>" + " </pic:blipFill>" + " <pic:spPr>" + " <a:xfrm>" + " <a:off x="0" y="0"/>" + " <a:ext cx="" + width + "" cy="" + height + ""/>" + " </a:xfrm>" + " <a:prstGeom prst="rect">" + " <a:avLst/>" + " </a:prstGeom>" + " </pic:spPr>" + " </pic:pic>" + " </a:graphicData>" + "</a:graphic>"; //CTGraphicalObjectData graphicData = inline.addNewGraphic().addNewGraphicData(); XmlToken xmlToken = null; try { xmlToken = XmlToken.Factory.parse(picXml); } catch(XmlException xe) { xe.printstacktrace(); } inline.set(xmlToken); //graphicData.set(xmlToken); inline.setdistT(0); inline.setdistB(0); inline.setdistL(0); inline.setdistR(0); CTPositiveSize2D extent = inline.addNewExtent(); extent.setCx(width); extent.setCy(height); CTNonVisualDrawingProps docPr = inline.addNewDocPr(); docPr.setId(id); docPr.setName("Picture " + id); docPr.setDescr("Generated"); } }

截至目前头文件正在进入文档的正文,请帮助,谢谢你的提前。

Rails 3.1的间歇性“脚本头文件过早结束”

PHP的mkdir()权限

wamp服务器无法启动:Windows 7,64Bit

PHP内容长度标题被覆盖!

SELinux阻止httpd(usr / sbin / httpd)写入访问/ var / www / html / bookings / templates_c

Mod_rewrite – 创build干净的URL

检测哪个脚本导致服务器重载 – apache + PHP

.htaccess重写:子域名作为GET参数和文件path完好无损后

任何替代CachingHttpClient的Android?

.htaccess – RewriteCond:坏标志分隔符

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐