如果页面大小相同,那么没问题,但我需要的页面大小应该是Image的大小(可变),
void DgJpgToPdf::convertToPdf( const QStringList& theImagePathList, const QString& theOutputFileName )
{
QPrinter printer;
printer.setOrientation(QPrinter::Portrait);
printer.setOutputFormat( QPrinter::PdfFormat );
printer.setOutputFileName( theOutputFileName );
QPainter painter;
painter.begin( &printer );
//printer.newPage();
foreach( QString fileName, theImagePathList ){
QImage imgObj( fileName );
printer.setPaperSize( QSizeF(imgObj.width(), imgObj.height()), QPrinter::DevicePixel );
painter.drawImage( 0, 0, imgObj );
printer.newPage();
}
painter.end();
}
答案 0 :(得分:3)
您可能希望使用QPdfWriter
而不是QPrinter
。您必须为图像选择PPI,但如果您想以96 PPI保存,则可以执行类似
QPdfWriter pdfWriter(theOutputFileName);
pdfWriter.setTitle(theDocumentTitle);
pdfWriter.setCreater(yourApplicationName);
const qreal horizontalMarginMM = 2.0; // 2mm margin on each side
const qreal verticalMarginMM = 2.0;
QPagedDevice::Margins margins;
margins.left = margins.right = horizontalMarginMM;
margins.top = margins.bottom = verticalMarginMM;
pdfWriter.setMargins(margins);
bool newPage = false;
QPainter painter;
painter.begin(&pdfWriter);
foreach (const QString& fileName, theImagePathList) {
QImage imageObj(fileName);
if (newPage)
pdfWriter.newPage();
newPage = true;
qreal pageWidthMM = imgObj.width() / 96.0 * 25.4 + 2*horizontalMarginMM;
qreal pageHeightMM = imgObj.height() / 96.0 * 25.4 + 2*verticalMarginMM;
imgObj.setDotsPerMeterX(96.0 / 25.4 * 1000.0);
imgObj.setDotsPerMeterY(96.0 / 25.4 * 1000.0);
pdfWriter.setPageSizeMM(QSizeF(pageWidthMM, pageHeightMM));
painter.drawImage(0, 0, imageObj);
}
painter.end();