Print page truncated - java version

I print my document from a java application using PdfViewer.
The document is truncated on the left.
I tried to set page margins (eg. 1,0,0,0) but if I do that the page is truncated on the right and on bottom.

public void print(byte[] xpDocument, String xpPrinterName, int xpCopies) throws IOException {
Document doc = new Document(new ByteArrayInputStream(xpDocument));
PdfViewer viewer = new PdfViewer();
viewer.setResolution(1200);
viewer.bindPdf(doc);
viewer.setPrintPageDialog(false);
if(xpPrinterName == null) {
viewer.printDocument();
} else {
PdfPrinterSettings settings = viewer.getDefaultPrinterSettings();
settings.setCopies((short)xpCopies);
settings.setPrinterName(xpPrinterName);
PrintPageSettings pageSetting = settings.getDefaultPageSettings();
viewer.printDocumentWithSettings(pageSetting, settings);
}
}

What’s wrong ?
Tks
Tullio

@AxiomaBo

Can you please share input and output files so that we may try to reproduce the same on our end.

Stampa.pdf (616.3 KB)
The required input file.
I’ve no output file, I print directly.
Tks

The test code

package axioma.web.mwfep.services.signature;

import java.io.*;
import java.util.ArrayList;
import org.apache.commons.io.IOUtils;
import com.aspose.pdf.*;
import com.aspose.pdf.facades.*;
import com.aspose.pdf.operators.*;
import com.aspose.pdf.printing.*;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class PdfManager {
	
	public static final int STAMP_ID = 885546;
	
	public byte[] addStamp(byte[] xpDocument, byte[] xpLayer, DocumentoFirmabile xpCoordinate)  {
		Document pdfDocument = new Document(new ByteArrayInputStream(xpDocument));
		PageCollection collection = pdfDocument.getPages();
		int size = collection.size();
		Page page = collection.get_Item(size);  //ATTENZIONE base 1
		ImageStamp imageStamp = new ImageStamp(new ByteArrayInputStream(xpLayer));
		imageStamp.setBackground(false);
		imageStamp.setXIndent(xpCoordinate.getLowerLeftX());
		imageStamp.setYIndent(xpCoordinate.getLowerLeftY());
		imageStamp.setHeight(xpCoordinate.getUpperRightY() - xpCoordinate.getLowerLeftY());
		imageStamp.setWidth(xpCoordinate.getUpperRightX() - xpCoordinate.getLowerLeftX());
		imageStamp.setOpacity(2);
		imageStamp.setStampId(STAMP_ID);
		page.addStamp(imageStamp);
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		pdfDocument.save(output);
		return output.toByteArray();
	}
	
	public byte[] removeStamp(byte[] xpDocument) {
		PdfContentEditor contentEditor = new PdfContentEditor();
		contentEditor.bindPdf(new ByteArrayInputStream(xpDocument));
		contentEditor.deleteStampById(STAMP_ID);
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		contentEditor.save(output);
		return output.toByteArray();
	}
	
	public byte[] addLayer(byte[] xpDocument, byte[] xpLayer, DocumentoFirmabile xpCoordinate) throws IOException {
		Document doc = new Document(new ByteArrayInputStream(xpDocument));
		PageCollection collection = doc.getPages();
		int size = collection.size();
		Page page = collection.get_Item(size);  //ATTENZIONE base 1
		if(xpLayer != null) {
			InputStream imageStream = new ByteArrayInputStream(xpLayer);
			String imageName = page.getResources().getImages().add(imageStream);
			page.getContents().add(new GSave());
			int lowerLeftX = xpCoordinate.getLowerLeftX();
			int lowerLeftY = xpCoordinate.getLowerLeftY();
			int upperRightX = xpCoordinate.getUpperRightX();
			int upperRightY = xpCoordinate.getUpperRightY();
			Rectangle rectangle = new Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
			Matrix matrix = new Matrix(new double[] { rectangle.getURX() - rectangle.getLLX(), 0, 0, rectangle.getURY() - rectangle.getLLY(), rectangle.getLLX(), rectangle.getLLY() });
			Layer layer = new Layer("signature", "Signature");
			layer.getContents().add(new ConcatenateMatrix(matrix));
			layer.getContents().add(new Do(imageName));
			page.getContents().add(new GRestore());
			page.setLayers(new ArrayList());
			page.getLayers().add(layer);
			imageStream.close();
		}
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		doc.save(output);
		return output.toByteArray();
	}	
	
	public byte[] removeLayers(byte[] xpDocument) throws IOException {
		Document doc = new Document(new ByteArrayInputStream(xpDocument));
		PageCollection collection = doc.getPages();
		for(int i = 1; i <= collection.size(); i++) {
			Page page = collection.get_Item(i);
			page.setLayers(new ArrayList());
		}
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		doc.save(output);
		return output.toByteArray();
	}
	
	public void print(byte[] xpDocument, short xpCopies) throws IOException {
		print(xpDocument, null, xpCopies);
	}
		
	public void print(byte[] xpDocument, String xpPrinterName, int xpCopies) throws IOException {
		Document doc = new Document(new ByteArrayInputStream(xpDocument));
		PdfViewer viewer = new PdfViewer();
		viewer.setResolution(1200);
		viewer.bindPdf(doc);
		viewer.setPrintPageDialog(false);
		if(xpPrinterName == null) {
			viewer.printDocument();
		} else {
			PdfPrinterSettings settings = viewer.getDefaultPrinterSettings();
			settings.setCopies((short)xpCopies);
//			settings.setPrinterName(xpPrinterName);
			PrintPageSettings pageSetting = settings.getDefaultPageSettings();
			viewer.printDocumentWithSettings(pageSetting, settings);
		}
	}
	
	public byte[] merge(Iterable<byte[]> xpDocuments) throws IOException {
		Document documentoFinale = null;
		for(byte[] bytes :  xpDocuments) {
			Document doc = new Document(new ByteArrayInputStream(bytes));
			documentoFinale = merge2(documentoFinale, doc);
		}
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		documentoFinale.save(output);
		return output.toByteArray();
	}
	
	private Document merge2(Document xpDocument1, Document xpDocument2) {
		if(xpDocument1 == null) {
			return xpDocument2;
		}
		if(xpDocument2 == null) {
			return xpDocument1;
		}
		xpDocument1.getPages().add(xpDocument2.getPages());
		return xpDocument1;
	}
	
	public static void main(String[] args) {
		Test2(args);
	}
	
	protected static void Test1() {
		try {
			License license = new License();
			FileInputStream fis = new FileInputStream("I:/MwFep/MwFep/mwfep-app/src/main/resources/Aspose.PDF.Java.lic");
			license.setLicense(fis);
			PdfManager manager = new PdfManager();
			FileInputStream signatureFile = new FileInputStream("C:/Tmp2/Signature.png");
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			IOUtils.copy(signatureFile, bos);
			byte[] signature = bos.toByteArray();
			FileInputStream documentFile = new FileInputStream("C:/Tmp2/Input.pdf");
			bos = new ByteArrayOutputStream();
			IOUtils.copy(documentFile, bos);
			byte[] documento = bos.toByteArray();
			DocumentoFirmabile df = new DocumentoFirmabile("TITOLO");
			documento = manager.addStamp(documento, signature, df);
			ByteArrayInputStream bis = new ByteArrayInputStream(documento);
			FileOutputStream fos = new FileOutputStream("C:/Tmp2/Output.pdf");
			IOUtils.copy(bis, fos);
			fos.flush();
			fos.close();
			documento = manager.removeStamp(documento);
			signatureFile = new FileInputStream("C:/Tmp2/Signature_ori.jpg");
			bos = new ByteArrayOutputStream();
			IOUtils.copy(signatureFile, bos);
			signature = bos.toByteArray();
			documento = manager.addStamp(documento, signature, df);
			bis = new ByteArrayInputStream(documento);
			fos = new FileOutputStream("C:/Tmp2/Output2.pdf");
			IOUtils.copy(bis, fos);
			fos.flush();
			fos.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	protected static void Test2(String[] xpArgs) {
		try {
			License license = new License();
			FileInputStream fis = new FileInputStream("C:/Tmp2/Aspose.PDF.Java.lic");
			license.setLicense(fis);
			PdfManager manager = new PdfManager();
			FileInputStream documentFile = new FileInputStream("C:/Tmp2/Input.pdf");
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			IOUtils.copy(documentFile, bos);
			byte[] content = bos.toByteArray();
			short copies = (short)2;
			manager.print(content, xpArgs[0], copies);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

@AxiomaBo

I have printed the file to a PDF file with your print method, but could not notice any issue. Can you please check attached output and let us know the feedback. Stampa-print.pdf (1.5 MB)

20210513_092724.jpg (1.3 MB)
Tks for Your help but my problem is not about a pdf output but a print output.
As You can see in the uploaded image in the page directly printed, the left hand side vertical line is missing and the printed page appears truncated on the left.
This is my problem.
Tks
Tullio

@AxiomaBo

A ticket with ID PDFJAVA-40494 has been created to further investigate and resolve the issue. This thread has been linked with the issue so that you may be notified once the issue will be addressed.

No further news about the ticket 40494?
Tks
Tullio

@AxiomaBo

Please note that it was recently logged in free support model and will be investigated and resolved on a first come first serve basis. We will surely inform you as soon as we make some definite progress towards its resolution. Please be patient and spare us some time.