I added header docx file but unable to set height of Header. I have to reduce height of header.
Screen short :
Code :
package com.techior.word;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import com.aspose.words.BreakType;
import com.aspose.words.ConvertUtil;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.HeaderFooterType;
import com.aspose.words.ImportFormatMode;
import com.aspose.words.ImportFormatOptions;
import com.aspose.words.License;
import com.aspose.words.Orientation;
import com.aspose.words.PageSetup;
import com.aspose.words.PaperSize;
import com.aspose.words.ParagraphAlignment;
import com.aspose.words.PdfImageCompression;
import com.aspose.words.PdfSaveOptions;
import com.aspose.words.SaveFormat;
import com.aspose.words.Shape;
import com.aspose.words.TextColumn;
import com.aspose.words.TextColumnCollection;
import com.aspose.words.WrapType;
public class AsposeWordConverter {
public AsposeWordConverter(String licenseFile) {
License license = new License();
FileInputStream licInputStream;
try {
licInputStream = new FileInputStream(licenseFile);
license.setLicense(licInputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void convertHTMLToDocx( File docxfile,String instruction,String questionPaper){
try {
DocumentBuilder builder = new DocumentBuilder();
PageSetup ps = builder.getPageSetup();
ps.setPaperSize(PaperSize.CUSTOM);
ps.setOrientation(Orientation.PORTRAIT);
ps.setPageHeight(ConvertUtil.inchToPoint(11));
ps.setPageWidth(ConvertUtil.inchToPoint(10));
ps.setTopMargin(ConvertUtil.inchToPoint(0.3));
ps.setBottomMargin(ConvertUtil.inchToPoint(0.3));
ps.setLeftMargin(ConvertUtil.inchToPoint(0.3));
ps.setRightMargin(ConvertUtil.inchToPoint(0.3));
//header and footer section
setHeaderFooter(builder);
builder.moveToSection(0);
//set instruction
builder.insertDocument(new Document(new ByteArrayInputStream(instruction.toString().getBytes(StandardCharsets.UTF_8) )), ImportFormatMode.USE_DESTINATION_STYLES);
//divide page into two columns
//builder.write("Question Paper");
builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
TextColumnCollection columns = builder.getPageSetup().getTextColumns();
columns.setLineBetween(true);
columns.setEvenlySpaced(false);
columns.setCount(2);
columns.setSpacing(ConvertUtil.inchToPoint(0.3));
double contentWidth = ps.getPageWidth() - ps.getLeftMargin() - ps.getRightMargin();
TextColumn c1 = columns.get(0);
c1 = columns.get(0);
c1.setWidth(contentWidth/2);
TextColumn c2 = columns.get(1);
c2.setWidth(contentWidth/2);
builder.getParagraphFormat().clearFormatting();
builder.write("Colunm 1");
builder.insertDocument(new Document(new ByteArrayInputStream(questionPaper.toString().getBytes(StandardCharsets.UTF_8) )), ImportFormatMode.USE_DESTINATION_STYLES);
builder.write("Colunm 1");
builder.insertDocument(new Document(new ByteArrayInputStream(questionPaper.toString().getBytes(StandardCharsets.UTF_8) )), ImportFormatMode.USE_DESTINATION_STYLES);
builder.write("Colunm 1");
builder.insertDocument(new Document(new ByteArrayInputStream(questionPaper.toString().getBytes(StandardCharsets.UTF_8) )), ImportFormatMode.USE_DESTINATION_STYLES);
builder.write("Colunm 1");
builder.insertDocument(new Document(new ByteArrayInputStream(questionPaper.toString().getBytes(StandardCharsets.UTF_8) )), ImportFormatMode.USE_DESTINATION_STYLES);
builder.write("Colunm 1");
builder.insertDocument(new Document(new ByteArrayInputStream(questionPaper.toString().getBytes(StandardCharsets.UTF_8) )), ImportFormatMode.USE_DESTINATION_STYLES);
builder.write("Colunm 1");
builder.insertDocument(new Document(new ByteArrayInputStream(questionPaper.toString().getBytes(StandardCharsets.UTF_8) )), ImportFormatMode.USE_DESTINATION_STYLES);
builder.write("Colunm 1");
builder.insertDocument(new Document(new ByteArrayInputStream(questionPaper.toString().getBytes(StandardCharsets.UTF_8) )), ImportFormatMode.USE_DESTINATION_STYLES);
builder.write("Colunm 1");
builder.insertDocument(new Document(new ByteArrayInputStream(questionPaper.toString().getBytes(StandardCharsets.UTF_8) )), ImportFormatMode.USE_DESTINATION_STYLES);
/*
* Document questionContentDoc = new Document(htmlFile.getAbsolutePath());
* builder.insertDocument(questionContentDoc,
* ImportFormatMode.USE_DESTINATION_STYLES);
*/
builder.getDocument().save( new FileOutputStream(docxfile), SaveFormat.DOCX);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void convertDocxToPDF(File docxFile, File pdfFile){
Document convDoc;
try {
convDoc = new Document(docxFile.getAbsolutePath());
convDoc.getLayoutOptions().setTextShaperFactory(com.aspose.words.shaping.harfbuzz.HarfBuzzTextShaperFactory.getInstance());
PdfSaveOptions opt = new PdfSaveOptions();
opt.getDownsampleOptions().setDownsampleImages(false);
opt.setUseAntiAliasing(true);
opt.setImageCompression(PdfImageCompression.JPEG);
convDoc.save( new FileOutputStream(pdfFile), opt);
} catch (Exception e) {
e.printStackTrace();
}
}
public void convertDocxToHtml( File docxfile,File htmlFile){
try {
Document doc = new Document(docxfile.getAbsolutePath());
doc.save(htmlFile.getAbsolutePath(),SaveFormat.HTML);
} catch (Exception e) {
e.printStackTrace();
}
}
public void setHeaderFooter(DocumentBuilder builder ) {
StringBuilder header = new StringBuilder();
header.append("<HTML><HEAD></HEAD><BODY> ");
header.append("<table style='width:100%;margin:0px;padding:0px;'>");
header.append(" <tr> ");
header.append(" <td style='text-align: center;width:auto;'><b>Techior Solutions Pvt. Ltd.</b></td> ");
header.append(" </tr> ");
header.append("<tr><td style='text-align: center;margin:0px;padding:0px;'><b>Test-1</b></td></tr>");
header.append("<tr> <td style='border-bottom: 0.5pt dotted black;' >");
header.append("<table style='width:100%;margin:0px;padding:0px;'>");
header.append("<tr>");
header.append(" <td style='text-align: left;width:50%;margin:0px;padding:0px;'>Total Time: 01:20:00</td> ");
header.append(" <td style='text-align: right;width:50%;margin:0px;padding:0px;'>Total Marks: 200</td> ");
header.append(" </tr> ");
header.append(" </table> ");
header.append(" </td></tr> ");
header.append(" </table> ");
header.append("</BODY></HTML>");
String headerLogo = "https://learnmagica.com/learnmagica/onlineTG/uploadedFile/header/main-logo_export.png";
//Set flags for different headers and footers for first, even and odd pages
builder.getPageSetup().setDifferentFirstPageHeaderFooter(true);
builder.getPageSetup().setOddAndEvenPagesHeaderFooter(true);
// Insert some text after moving the cursor to the beginning of the header
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
Document headerDocument;
try {
headerDocument = new Document(new ByteArrayInputStream(header.toString().getBytes(StandardCharsets.UTF_8)));
PageSetup ps = builder.getPageSetup();
ps.setHeaderDistance(10);
ps.setFooterDistance(10);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
builder.insertDocument(headerDocument, ImportFormatMode.USE_DESTINATION_STYLES);
Shape shape = builder.insertImage(headerLogo);
shape.setWrapType(WrapType.NONE);
shape.setLeft(4);
shape.setTop(-ConvertUtil.inchToPoint(1));
// builder.write("<<<<<<< HeaderFirst >>>>>>>");
/*
* builder.moveToHeaderFooter(HeaderFooterType.HEADER_EVEN);
* builder.write("<<<<<<< HeaderEven >>>>>>>");
*/
// Insert some text after moving the cursor to the beginning of the footer
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.insertField("PAGE", null);
builder.write(" of ");
builder.insertField("NUMPAGES", null);
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_FIRST);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.insertField("PAGE", null);
builder.write(" of ");
builder.insertField("NUMPAGES", null);
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_EVEN);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.insertField("PAGE", null);
builder.write(" of ");
builder.insertField("NUMPAGES", null);
// Move the cursor to the beginning of body in first section to add some page breaks to see the headers and footers
/*
* builder.moveToSection(0); builder.writeln("Page1");
* builder.insertBreak(BreakType.PAGE_BREAK); builder.writeln("Page2");
* builder.insertBreak(BreakType.PAGE_BREAK); builder.writeln("Page3");
*/
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String arr[]){
//set instruction.
StringBuilder instruction = new StringBuilder();
instruction.append("<div style='width:100%;'>");
instruction.append("<p style='text-align: center;'><b> Instruction </b></p>");
instruction.append("<p>");
instruction.append("ONLYOFFICE Document Builder allows you to build a document easily"
+ "without the need to actually run a document processing editor. It also "
+ "allows the developers to automate document building using the "
+ "templates you create or inserting the data from some database of yours "
+ "into a document or a set of documents. It has all the features of a "
+ "common document editor but is lightweight and can be easily integrated "
+ "into your document management system, CRM system, etc. using its "
+ "JavaScript API."
+ " As any document is a composite object consisting of a tree of larger "
+ "nodes (paragraphs and tables), where it is possible to insert smaller "
+ "document elements (text runs, images, charts, shapes), ONLYOFFICE "
+ "Document Builder allows to insert the content and format it to your "
+ "liking quickly and easily.");
instruction.append("</p>");
instruction.append("</div>");
StringBuilder questionQuestion = new StringBuilder();
questionQuestion.append("<div style='width:100%; padding:1em;'>");
questionQuestion.append("<p style='text-align: center;'><b> Questions </b></p>");
questionQuestion.append("<p>");
questionQuestion.append("ONLYOFFICE Document Builder allows you to build a document easily"
+ "without the need to actually run a document processing editor. It also "
+ "allows the developers to automate document building using the "
+ "templates you create or inserting the data from some database of yours "
+ "into a document or a set of documents. It has all the features of a "
+ "common document editor but is lightweight and can be easily integrated "
+ "into your document management system, CRM system, etc. using its "
+ "JavaScript API."
+ " As any document is a composite object consisting of a tree of larger "
+ "nodes (paragraphs and tables), where it is possible to insert smaller "
+ "document elements (text runs, images, charts, shapes), ONLYOFFICE "
+ "Document Builder allows to insert the content and format it to your "
+ "liking quickly and easily.");
questionQuestion.append("</p>");
questionQuestion.append("</div>");
AsposeWordConverter asposeWordConverter = new AsposeWordConverter("E:\\NitinData\\Nitin\\Third Part Software\\Aspose\\word\\Aspose.Words.Product.Family_temporary.lic");
asposeWordConverter.convertHTMLToDocx(new File("E:\\NitinData\\temp\\LM\\html\\Math_Type_solution_1674280083197.docx"),
instruction.toString(),questionQuestion.toString());
/*
* asposeWordConverter.convertHTMLToDocx(new
* File("E:\\NitinData\\temp\\LM\\html\\Math_Type_solution_1674280083197.html"),
* new
* File("E:\\NitinData\\temp\\LM\\html\\Math_Type_solution_1674280083197.docx"),
* instruction.toString(),questionQuestion.toString());
*/
System.out.println("conversion done.");
}
}
Please guide us how to reduce header height.