I knew can set the margin by pagesetup, but it would cause the page numbers change,
so if there’s a way to change margin when print.
To set margins for printing using the Aspose.Words library in Java, you typically adjust the margins through the PageSetup
class. However, changing the margins this way can affect the layout of the document, including page numbers. Unfortunately, there isn’t a direct method to set margins specifically for printing without altering the document’s layout.
If you want to maintain the original layout while printing, you might consider creating a temporary copy of the document with adjusted margins for printing purposes. This way, the original document remains unchanged, and you can set the margins as needed for the print job.
Here’s a basic example of how you might implement this in Java:
import com.aspose.words.*;
public class PrintDocumentWithMargins {
public static void main(String[] args) throws Exception {
// Load the document
Document doc = new Document("input.docx");
// Create a new document for printing
Document printDoc = doc.deepClone();
// Set margins for the print document
PageSetup pageSetup = printDoc.getFirstSection().getPageSetup();
pageSetup.setTopMargin(50); // Set top margin
pageSetup.setBottomMargin(50); // Set bottom margin
pageSetup.setLeftMargin(50); // Set left margin
pageSetup.setRightMargin(50); // Set right margin
// Print the document
PrinterSettings printerSettings = new PrinterSettings();
printDoc.print(printerSettings);
}
}
In this example, we load the original document, create a deep clone for printing, set the desired margins on the cloned document, and then print it. This approach allows you to keep the original document intact while adjusting the margins for the print job.
If you have further questions or need more specific examples, feel free to ask!
@GusGus Aspose.Words renders the document according to setting set in the document. There is no direct way to change margins of the document upon printing. You can render the document to image and scale it as it is required. For example you can use RenderToSize or RenderToScale methods. Then you can print the resulting images.
Thanks,
But I met a problem,when I using office print it’s ok.
I useing the aspose print function, the part of bottom had been cut off, and the barcode is wrong like this:
Here is the word:
1.docx (59.0 KB)
Here is my code:
Document doc = new Document("C:\\Users\\z_jia\\Desktop\\a\\1.docx");
String printerName = "Brother DCP-1618W Printer";
String printPages = "";
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setJobName("1.docx");
PrintService[] services = PrinterJob.lookupPrintServices();
for (PrintService service : services)
{
if (service.getName().equalsIgnoreCase(printerName))
{
try
{
pj.setPrintService(service);
}
catch (PrinterException e)
{
e.printStackTrace();
}
}
}
// Initialize the Print Dialog with the number of pages in the document.
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
if (printPages == null || printPages.isEmpty())
{
attributes.add(new PageRanges(1, doc.getPageCount()));
}
else
{
String[] pages = printPages.split(",");
List<int[]> ranges = new ArrayList<>();
for (int j = 0; j < pages.length; j++)
{
String temp = pages[j];
if (temp != null && !temp.isEmpty())
{
if (temp.contains("-"))
{
String[] tempAttr = temp.split("-");
ranges.add(new int[]{Integer.parseInt(tempAttr[0]),
Integer.parseInt(tempAttr[1])});
}
else
{
int pageIndex = Integer.parseInt(temp);
ranges.add(new int[] { pageIndex, pageIndex });
}
}
}
// Convert to 2d array
int[][] rangeArrays = new int[ranges.size()][2];
for (int j = 0; j < ranges.size(); j++)
{
rangeArrays[j] = ranges.get(j);
}
attributes.add(new PageRanges(rangeArrays));
}
// 添加A4纸张属性
attributes.add(MediaSizeName.ISO_A4);
// 创建AsposeWordsPrintDocument对象
AsposeWordsPrintDocument awPrintDoc = new AsposeWordsPrintDocument(doc);
// 将AwPrintDocument对象设置为要打印的内容
pj.setPageable(awPrintDoc);
pj.print(attributes);
this issue is belog to bug?
@GusGus As I can see Document is rendered properly by Aspose.Words. You can check this by converting the document to PDF. Which version of Aspose.Words do you use?
Also I checked printing on my side using the following simple code:
Document doc = new Document("C:\\Temp\\in.docx");
doc.print();
Yes,pdf is ok,but when I use the code from Printing a Document|Aspose.Words for Java , the result is not correct.
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(new PageRanges(1, doc.getPageCount()));
AsposeWordsPrintDocument awPrintDoc = new AsposeWordsPrintDocument(doc);
// 将AwPrintDocument对象设置为要打印的内容
pj.setPageable(awPrintDoc);
pj.print(attributes);
I am using openJDK, it’s that problem. because I user doc.print() nothing happened.
@GusGus Please try setting the required printer as a default printer on your PC and try using the following code for testing:
Document doc = new Document("C:\\Temp\\in.docx");
doc.print();
it throws exception:
java.lang.IllegalAccessException: class com.aspose.words.internal.zzVSn cannot access class sun.print.Win32MediaTray (in module java.desktop) because module java.desktop does not export sun.print to unnamed module @79d8407f
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:392)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:674)
at java.base/java.lang.reflect.Field.checkAccess(Field.java:1102)
at java.base/java.lang.reflect.Field.getInt(Field.java:599)
at com.aspose.words.internal.zzVSn.zzXsO(Unknown Source)
@GusGus I have rechecked printing on my side with your code and the problem is still not reproducible. I use the latest 24.8 version of Aspose.Words for Java on my side.
@GusGus We have checked the scenario on another machine with another printer and still the problem is not reproducible on our side.
Ok, I had find another solution to print by converting to pdf, then print the pdf, it seems ok.
But I am afraid there’s other unkonw layout issue when printing.
Could you please give me some advises what is the best way to print as same with Microsoft office word, As I Know there’s a lot of way to print word by aspose.
@GusGus Actually, there is only one way to print the document using Aspose.Words and the same layout engine is used for rendering document to PDF and for printing. It is not clear why the document is printed improperly on your side.
Please try setting UsePrinterMerics
compatibility option before printing the document:
Document doc = new Document("C:\\Temp\\in.docx");
doc.getCompatibilityOptions().setUsePrinterMetrics(true);
doc.print();
Does this make any difference on your side?