docx4java';s SectionWrapper.getHeaderFooterPolicy——我可以用它删除标题吗&;页脚 重写后看起来更像一个编程问题
好的,我做了更多的研究,看起来我需要使用的java包是docx4j。不幸的是,我对该包以及PDF格式的基础不熟悉,这使得我很难准确地了解如何使用返回的页眉和页脚docx4java';s SectionWrapper.getHeaderFooterPolicy——我可以用它删除标题吗&;页脚 重写后看起来更像一个编程问题,java,pdf,pdf-generation,docx,docx4j,Java,Pdf,Pdf Generation,Docx,Docx4j,好的,我做了更多的研究,看起来我需要使用的java包是docx4j。不幸的是,我对该包以及PDF格式的基础不熟悉,这使得我很难准确地了解如何使用返回的页眉和页脚SectionWrapper.getHeaderFooterPolicy()。返回的HeaderPart和FooterPart对象是否可写或者如何修改它们还不完全清楚 它提供了一个如何创建标题部分的示例,但它创建了一个新的HeaderPart,并将其添加到文档中 我想找到现有的页眉/页脚部分,如果可能的话将其删除或清空。理想情况下,它们将
SectionWrapper.getHeaderFooterPolicy()
。返回的HeaderPart
和FooterPart
对象是否可写或者如何修改它们还不完全清楚
它提供了一个如何创建标题部分的示例,但它创建了一个新的HeaderPart
,并将其添加到文档中
我想找到现有的页眉/页脚部分,如果可能的话将其删除或清空。理想情况下,它们将完全从文件中消失
与此类似,允许您使用setJaxbElement
设置headerpart的文本,但很多术语都不熟悉,我担心最终的结果是在每个文档中创建标题(尽管是空标题),而不是删除标题
下面是原始问题
我正在处理一组变化很大的MS Word文档。我正在将它们编译成单个PDF,并希望在这样做之前确保它们没有页眉或页脚
理想情况下,如果不是Times New Roman,我也希望覆盖它们的默认字体
有没有办法通过编程或使用某种批处理来实现这一点
我将在当前没有安装Office或Word的Windows服务器上运行它(尽管我认为它可能安装了OpenOffice,当然也很容易添加安装)
现在我正在使用一些版本的iText(java)将文件转换为PDF。我知道iText显然不能做删除页眉/页脚之类的事情,但由于现代.doc文件的底层结构是XML,我想知道是否有API(甚至是XML解析/编辑API,或者,如果所有其他方法都失败了,是否有RegEx[horrors])用于删除页眉和页脚以及设置一些默认样式。要删除页眉/页脚,有一个非常简单的解决方案: 以Zip格式打开docx,并删除名为header*.xml/footer*.xml的文件(位于word文件夹中) 解压docx的结构: 要真正删除链接(如果您不这样做,它可能会损坏): 您需要编辑document.xml.rels文件,并删除所有包含页脚/页眉的关系。这是您应该删除的关系:
更一般地说,所有包含type='footer'或type='header'的内容要删除页眉/页脚,有一个非常简单的解决方案: 以Zip格式打开docx,并删除名为header*.xml/footer*.xml的文件(位于word文件夹中) 解压docx的结构: 要真正删除链接(如果您不这样做,它可能会损坏): 您需要编辑document.xml.rels文件,并删除所有包含页脚/页眉的关系。这是您应该删除的关系:
更一般地说,所有包含type='footer'或type='header'的内容,这里有一些您想要的代码:
public class HeaderFooterRemove {
public static void main(String[] args) throws Exception {
// A docx or a dir containing docx files
String inputpath = System.getProperty("user.dir") + "/testHF.docx";
StringBuilder sb = new StringBuilder();
File dir = new File(inputpath);
if (dir.isDirectory()) {
String[] files = dir.list();
for (int i = 0; i<files.length; i++ ) {
if (files[i].endsWith("docx")) {
sb.append("\n\n" + files[i] + "\n");
removeHFFromFile(new java.io.File(inputpath + "/" + files[i]));
}
}
} else if (inputpath.endsWith("docx")) {
sb.append("\n\n" + inputpath + "\n");
removeHFFromFile(new java.io.File(inputpath ));
}
System.out.println(sb.toString());
}
public static void removeHFFromFile(File f) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(f);
MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
// Remove from sectPr
SectPrFinder finder = new SectPrFinder(mdp);
new TraversalUtil(mdp.getContent(), finder);
for (SectPr sectPr : finder.getSectPrList()) {
sectPr.getEGHdrFtrReferences().clear();
}
// Remove rels
List<Relationship> hfRels = new ArrayList<Relationship>();
for (Relationship rel : mdp.getRelationshipsPart().getRelationships().getRelationship() ) {
if (rel.getType().equals(Namespaces.HEADER)
|| rel.getType().equals(Namespaces.FOOTER)) {
hfRels.add(rel);
}
}
for (Relationship rel : hfRels ) {
mdp.getRelationshipsPart().removeRelationship(rel);
}
wordMLPackage.save(f);
}
}
public class headerfooteremove{
公共静态void main(字符串[]args)引发异常{
//包含docx文件的docx或dir
字符串inputpath=System.getProperty(“user.dir”)+“/testHF.docx”;
StringBuilder sb=新的StringBuilder();
文件目录=新文件(inputpath);
if(dir.isDirectory()){
String[]files=dir.list();
对于(int i=0;i,这里有一些热压代码,可用于执行您想要的操作:
public class HeaderFooterRemove {
public static void main(String[] args) throws Exception {
// A docx or a dir containing docx files
String inputpath = System.getProperty("user.dir") + "/testHF.docx";
StringBuilder sb = new StringBuilder();
File dir = new File(inputpath);
if (dir.isDirectory()) {
String[] files = dir.list();
for (int i = 0; i<files.length; i++ ) {
if (files[i].endsWith("docx")) {
sb.append("\n\n" + files[i] + "\n");
removeHFFromFile(new java.io.File(inputpath + "/" + files[i]));
}
}
} else if (inputpath.endsWith("docx")) {
sb.append("\n\n" + inputpath + "\n");
removeHFFromFile(new java.io.File(inputpath ));
}
System.out.println(sb.toString());
}
public static void removeHFFromFile(File f) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(f);
MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
// Remove from sectPr
SectPrFinder finder = new SectPrFinder(mdp);
new TraversalUtil(mdp.getContent(), finder);
for (SectPr sectPr : finder.getSectPrList()) {
sectPr.getEGHdrFtrReferences().clear();
}
// Remove rels
List<Relationship> hfRels = new ArrayList<Relationship>();
for (Relationship rel : mdp.getRelationshipsPart().getRelationships().getRelationship() ) {
if (rel.getType().equals(Namespaces.HEADER)
|| rel.getType().equals(Namespaces.FOOTER)) {
hfRels.add(rel);
}
}
for (Relationship rel : hfRels ) {
mdp.getRelationshipsPart().removeRelationship(rel);
}
wordMLPackage.save(f);
}
}
public class headerfooteremove{
公共静态void main(字符串[]args)引发异常{
//包含docx文件的docx或dir
字符串inputpath=System.getProperty(“user.dir”)+“/testHF.docx”;
StringBuilder sb=新的StringBuilder();
文件目录=新文件(inputpath);
if(dir.isDirectory()){
String[]files=dir.list();
对于(int i=0;iIt)这是一个有趣的问题,但我认为这不是一个好的提问方式。那么让我更明确地说程序员-y。问题是你需要一个工具。对不起,我将对它进行改写以明确我的意思是更像API的东西。好的,我删除了“工具”这个词并在docx4j Java包中添加了我的一些初步发现,这可能是但不一定是我需要使用的。这是一个有趣的问题,但我认为这不是一个好问题。让我更明确地说,程序员-y。问题是你需要一个工具。对不起,我会重新编写它,以明确我的意思更像是一个API。好吧,我去掉了“工具”这个词并在docx4j Java包中添加了我的一些初步发现,这可能是但不一定是我需要使用的。您还需要从每个sectPr元素中删除页眉-页脚引用。在哪里可以找到它们?您还需要从每个sectPr元素中删除页眉-页脚引用在哪里可以找到它们?Github已停止使用m目前正在维护,但我一进去我就看一看!谢谢!Github正在进行维护,但我一进去我就看一看!谢谢!