We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose.Words PostScript saving seems to rotate image?

I am using Aspose.Words to create a PostScript document that I sent to a printer using Windows PrintAPI, and CUPS on Mac/Linux.


This is how I generate the file:

Aspose.Words.Document asposeDoc = new Aspose.Words.Document();
// image format
DocumentBuilder builder = new DocumentBuilder(asposeDoc);

// Read the image from file, ensure it is disposed.
using (Image image = Image.FromStream(pageData))
{
// Find which dimension the frames in this image represent. For example
// the frames of a BMP or TIFF are “page dimension” whereas frames of a GIF image are “time dimension”.
FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);

// Get the number of frames in the image.
int framesCount = image.GetFrameCount(dimension);

// Loop through all frames.
for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)
{
// Insert a section break before each new page, in case of a multi-frame TIFF.
if (frameIdx != 0)
builder.InsertBreak(BreakType.SectionBreakNewPage);

// Select active frame.
image.SelectActiveFrame(dimension, frameIdx);

// We want the size of the page to be the same as the size of the image.
// Convert pixels to points to size the page to the actual image size.
PageSetup ps = builder.PageSetup;
ps.PageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);
ps.PageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);

// Insert the image into the document and position it at the top left corner of the page.
builder.InsertImage(
image,
RelativeHorizontalPosition.Page,
0,
RelativeVerticalPosition.Page,
0,
ps.PageWidth,
ps.PageHeight,
WrapType.None);
}
}

SaveOptions opts = PsSaveOptions.CreateSaveOptions(SaveFormat.Ps);
asposeDoc.Save(outStream, opts);
outStream.Seek(0, SeekOrigin.Begin);
return outStream;

When I send this PS file to the print APIs, the image prints rotated 90 degrees.

However, if I instead save this exact document with SaveFormat.PNG, the image is not rotated.

Am I doing something wrong when making the PostScript file?

The PS file views correctly if I look at it with various online viewers.


Thanks!

Hi Andrew,


Thanks for your inquiry. Could you please attach your input image here for testing? We will investigate the issue on our side and provide you more information.

I have uploaded the image.


Thanks.

Hi Andrew,

Thanks for sharing the detail. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-12764. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi Andrew,

Thanks for your patience. Please use PageSetup.Orientation as Landscape to fix this issue. See the highlighted code snippet below.

Aspose.Words.Document asposeDoc = new Aspose.Words.Document();

// image format

DocumentBuilder builder = new DocumentBuilder(asposeDoc);

// Read the image from file, ensure it is disposed.

using (Image image = Image.FromFile(MyDir + "nao.png"))

{

// Find which dimension the frames in this image represent. For example

// the frames of a BMP or TIFF are "page dimension" whereas frames of a GIF image are "time dimension".

FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);

// Get the number of frames in the image.

int framesCount = image.GetFrameCount(dimension);

// Loop through all frames.

for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)

{

// Insert a section break before each new page, in case of a multi-frame TIFF.

if (frameIdx != 0)

builder.InsertBreak(BreakType.SectionBreakNewPage);

// Select active frame.

image.SelectActiveFrame(dimension, frameIdx);

// We want the size of the page to be the same as the size of the image.

// Convert pixels to points to size the page to the actual image size.

PageSetup ps = builder.PageSetup;

ps.PageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);

ps.PageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);

// Insert the image into the document and position it at the top left corner of the page.

builder.InsertImage(

image,

RelativeHorizontalPosition.Page,

0,

RelativeVerticalPosition.Page,

0,

ps.PageWidth,

ps.PageHeight,

WrapType.None);

}

}

asposeDoc.FirstSection.PageSetup.Orientation = Orientation.Landscape;

Aspose.Words.Saving.SaveOptions opts = PsSaveOptions.CreateSaveOptions(SaveFormat.Ps);

asposeDoc.Save(MyDir + "Out.ps", opts);

I am not sure if this is truly the correct solution. When saving the Aspose Document as an image, or as an XPS document, I do not need to do this. The image is correctly oriented.


The image in question should fit within the page layout of “Portrait”. It’s not big enough to require landscape.

The rotation only seems to occur with PostScript output.


Hi Andrew,

Thanks for your feedback. We have logged the shared detail in our issue tracking system. We will let you know once there is any update available about this issue.

Hi Andrew,


Thank you for shared details. I am the developer who are working on this issue. As I can see orientation of the image is correct in all output formats including PostScript. I suppose the problem occurs only when you print PostScript on paper. Am I right? If so, I think there are some settings in print API you can play with, like fit to page or auto detect orientation.
In your code you change dimensions of page to fit the image, but maybe in your case (I mean printing) you should use paper size the same as you have in your printer and scale the image accordingly.

Best regards,

I tried changing the orientation of the print using Windows PrintAPI, and CUPS on Linux. Changing these settings did not seem to impact the printer.


I suspect this is because printing raw Post Script causes the printer to ignore settings in favor of the Post Script file.


I do not understand why XPS document format using the same image handling code would print the document out correctly.

I also note that when the document prints, it is not top-left oriented like I’ve set the picture up to be.

It is rotated 90 degrees counter clockwise, and prints in the center of the top of the page.

Hi Andrew,


Thank you for additional information. Could you please also share the code you use to send PostScript file to a printer.

Best regards,

This is the Windows code I was using to test the file.


The CUPS code is shorter, but requires Linux or OSX.

HANDLE ph;

char name[255];
int size = 255;
GetDefaultPrinter(name, (LPDWORD)&size);
OpenPrinter(name, &ph, NULL);

// Get size of needed buffer for the devmode
int devmodesize = DocumentProperties(NULL, ph, name, NULL, NULL, 0);
if (devmodesize <= 0)
{

}

LPDEVMODE pDevMode = (DEVMODE *) new char[devmodesize];
memset(pDevMode, 0, devmodesize);
// Get Printer settings to Devmode
DocumentProperties(NULL, ph, name, pDevMode, NULL, DM_OUT_BUFFER);

// Modify Printer Settings
pDevMode->dmDefaultSource = DMBIN_ONLYONE;
pDevMode->dmOrientation = DMORIENT_PORTRAIT;
// Set Printer settings from Devmode
pDevMode->dmFields |= DM_DEFAULTSOURCE | DM_ORIENTATION;
if (DocumentProperties(NULL, ph, name, pDevMode, pDevMode, DM_IN_BUFFER | DM_OUT_BUFFER) < 0)
{
cout << “Error”;
}


DOC_INFO_1 di1;
di1.pDatatype = “RAW”;
di1.pDocName = “Raw print document”;
di1.pOutputFile = NULL;

StartDocPrinter(ph, 1, (LPBYTE)&di1);

StartPagePrinter(ph);

//open a file
fstream fs;
char buffer[65535];
unsigned int read;
unsigned int written;

fs.open(“insert post script path here”, std::fstream::in);
while (!fs.eof())
{
fs.read(buffer, 65535);
read = fs.gcount();
WritePrinter(ph, buffer, read, (LPDWORD)&written);
}
EndPagePrinter(ph);
EndDocPrinter(ph);
fs.close();



Hi Andew,

Thank you for additional information. I checked PostScript specification and here what it says:

To allow pages of the same dimensions in portrait (height greater than width) and landscape (width greater than height) orientations to be interspersed on the same physical medium, the setpagedevice operator rotates the default user space for landscape orientation 90 degrees counterclockwise with respect to that for portrait orientation.

In your case width of page is greater than height and smart PostScript device rotates it.

As a possible solution, I would suggest you use normal page size when you generate document (do not adjust page dimensions to image size). In this case page orientation will be portrait and output will be printed correctly. For example see the following code:

DocumentBuilder builder = new DocumentBuilder();

builder.InsertImage(@"C:\Temp\nao.png");

builder.Document.Save(@"C:\Temp\out.ps");

You can also adjust page margins and shape size if required.

Hope this helps,

The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan