Converting docx to a PDF
The docx has a page that has landscape orientation. when it is converted to PDF the page becomes portrait and the text becomes distorted.
what is the proper way to convert a docx to a pdf that has landscape pages?
here is the code i am using
public Byte[] SaveAsPdf(Byte[] Document)
{
using (MemoryStream InputStream = new MemoryStream(Document))
{
FileFormatInfo info = FileFormatUtil.DetectFileFormat(InputStream);
LoadOptions loadOptions = new LoadOptions
{
LoadFormat = info.LoadFormat
};
Document doc = new Document(InputStream);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions
{
SaveFormat = SaveFormat.Pdf,
Compliance = PdfCompliance.PdfA1a
};
using (MemoryStream stream = new MemoryStream())
{
string sz = doc.Save(stream, pdfSaveOptions).ContentType;//doc.Save(stream, pdfSaveOptions).ContentType;
return stream.ToArray();
}
}
}
@DazzaBee123
Could you please attach your input Word document here for testing? We will investigate the issue on our side and provide you more information.
hi, after further investigation there is nothing wrong with converting docx to pdf.
i have though found an issue. I convert the docx to pdf , then convert the pdf to jpeg images. This is where the issue lies.
Here is the code i use to convert pdf to jpg
public void ConvertPDFToJPEG(Byte[] PDFBlob , int resolution , string dataDir)
{
// Open document
using (MemoryStream InputStream = new MemoryStream(PDFBlob))
{
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(InputStream);
for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
{
using (FileStream imageStream = new FileStream(dataDir + "image" + pageCount + "_out" + ".jpg", FileMode.Create))
{
// Create JPEG device with specified attributes
// Width, Height, Resolution, Quality
// Quality [0-100], 100 is Maximum
// Create Resolution object
Aspose.Pdf.Devices.Resolution res = new Aspose.Pdf.Devices.Resolution(resolution);
// JpegDevice jpegDevice = new JpegDevice(500, 700, resolution, 100);
// added the following to determine if landscape or not
Int32 height, width = 0;
if (pdfDocument.Pages[pageCount].PageInfo.IsLandscape)
{
height = Convert.ToInt32(pdfDocument.Pages[pageCount].PageInfo.Width);
width = Convert.ToInt32(pdfDocument.Pages[pageCount].PageInfo.Height);
}
else
{
width = Convert.ToInt32(pdfDocument.Pages[pageCount].PageInfo.Width);
height = Convert.ToInt32(pdfDocument.Pages[pageCount].PageInfo.Height);
}
// it seems every page is saved as portrait. Why is this so?
Aspose.Pdf.Devices.JpegDevice jpegDevice =
//new Aspose.Pdf.Devices.JpegDevice(Aspose.Pdf.PageSize.A4, res, 100);
new Aspose.Pdf.Devices.JpegDevice(width,height , res, 100);
// Convert a particular page and save the image to stream
//Aspose.Pdf.PageSize.A4.IsLandscape = true;
jpegDevice.Process(pdfDocument.Pages[pageCount], imageStream);
// Close stream
imageStream.Close();
}
}
}
}
ive added in the IsLandscape part , but it always returns portrait.
ive attached the pdf that i converted from docx and also the original docx (in the zip file) testpdf.pdf (22.2 KB)
LandscapePortrait.zip (8.9 KB)
@DazzaBee123
Your issue is related to Aspose.PDF. We are moving this forum thread to Aspose.PDF forum where you will be guided appropriately.
@DazzaBee123
Please note that PageInfo
class contains default values which can be changed while creating new pages. Therefore, you may retrieve the values using below code snippet so that there is not any need of swapping the height with width or otherwise. The images are generated fine with default orientation of the page:
public void ConvertPDFToJPEG(Byte[] PDFBlob, int resolution, string dataDir)
{
// Open document
using (MemoryStream InputStream = new MemoryStream(PDFBlob))
{
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(InputStream);
for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
{
using (FileStream imageStream = new FileStream(dataDir + "image" + pageCount + "_out" + ".jpg", FileMode.Create))
{
// Create JPEG device with specified attributes
// Width, Height, Resolution, Quality
// Quality [0-100], 100 is Maximum
// Create Resolution object
Aspose.Pdf.Devices.Resolution res = new Aspose.Pdf.Devices.Resolution(resolution);
// JpegDevice jpegDevice = new JpegDevice(500, 700, resolution, 100);
// added the following to determine if landscape or not
Int32 height, width = 0;
PdfFileInfo info = new PdfFileInfo(pdfDocument);
width = Convert.ToInt32(info.GetPageWidth(pdfDocument.Pages[pageCount].Number));
height = Convert.ToInt32(info.GetPageHeight(pdfDocument.Pages[pageCount].Number));
Aspose.Pdf.Devices.JpegDevice jpegDevice =
//new Aspose.Pdf.Devices.JpegDevice(Aspose.Pdf.PageSize.A4, res, 100);
new Aspose.Pdf.Devices.JpegDevice(width, height, res, 100);
// Convert a particular page and save the image to stream
//Aspose.Pdf.PageSize.A4.IsLandscape = true;
jpegDevice.Process(pdfDocument.Pages[pageCount], imageStream);
// Close stream
imageStream.Close();
}
}
}
}
We hope this will be helpful. Please feel free to contact us if you need any further assistance.
Thank you. Works fine now.