📄 dtpdfconvert.cpp
字号:
/****************************************************************************/
/* */
/* CDTPDFConvert::tiff_writePDFPagesObject */
/* */
/****************************************************************************/
int CDTPDFConvert::tiff_writePDFPagesObject( FILE * pf, long nObj, QTiff & qt )
{
try {
long obj_per_image = 5; // just required entries. page, contents, procset , ximage, length
CString czbuf;
//
//Write Pages object.
//
czbuf = "<<\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
czbuf = "/Type /Pages\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
czbuf.Format( "/Kids [%lu 0 R ", nObj +1 );
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
long count = (long) (nObj + obj_per_image +1);
//
// determine what objects the pages will be.
//
if (qt.pagecount() > 1) {
for (int i = 1 ; i < qt.pagecount() ; i ++, count += obj_per_image ) {
czbuf.Format( "%ld 0 R ", count );
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
}
}
czbuf = "]\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
czbuf.Format( "/Count %lu\r", (count-nObj) / obj_per_image );
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
czbuf.Format( "/MediaBox [0 0 %ld %ld]\r",0,0);
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
czbuf = ">>\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
czbuf = "endobj\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
} catch(...)
{
throw -15;
}
return 1;
}
/****************************************************************************/
/* */
/* CDTPDFConvert::tiff_writePDFCatalogObject */
/* */
/****************************************************************************/
int CDTPDFConvert::tiff_writePDFCatalogObject( FILE * pf, long nObj )
{
try {
CString czbuf;
czbuf = "<<\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
czbuf = "/Type /Catalog\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
czbuf.Format("/Pages %lu 0 R\r",nObj+1);
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
czbuf = ">>\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
czbuf = "endobj\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
} catch(...)
{
throw -16;
}
return 1;
}
/****************************************************************************/
/* */
/* CDTPDFConvert::tiff_writePDFInfoObject */
/* */
/****************************************************************************/
int CDTPDFConvert::tiff_writePDFInfoObject( FILE * pf, char* a_szCreator, char* a_szCreationDate,
char * a_szAuthor, char * a_szProducer, char * a_szTitle, char * a_szSubject,
char * a_szKeywords )
{
try {
CString czbuf;
CString czdate;
czbuf = "<<\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
CTime ctime = CTime::GetCurrentTime();
czbuf.Format("/Creator (%s)\r", a_szCreator );
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
if (a_szCreationDate == NULL)
czbuf.Format("/CreationDate (%s)\r", ctime.Format( "%A, %B %d, %Y" ) );
else
czbuf.Format("/CreationDate (%s)\r", a_szCreationDate );
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
czbuf.Format( "/Author (%s)\r", a_szAuthor );
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
czbuf.Format( "/Producer (%s)\r", a_szProducer );
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
czbuf.Format( "/Title (%s)\r", a_szTitle );
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
czbuf.Format("/Subject (%s)\r", a_szSubject );
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
czbuf.Format("/Keywords (%s)\r", a_szKeywords);
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
czbuf = ">>\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
czbuf = "endobj\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1, pf );
} catch(...)
{
throw -17;
}
return 1;
}
/****************************************************************************/
/* */
/* CDTPDFConvert::TiffToPdf */
/* */
/****************************************************************************/
int CDTPDFConvert::TiffToPdf( char* a_tiff, char* a_pdf, char* a_szCreator, char* a_szCreationDate,
char * a_szAuthor, char * a_szProducer, char * a_szTitle, char * a_szSubject,
char * a_szKeywords )
{
char szTiff[ 255 ];
char szPdf[ 255 ];
CString czbuf;
long lCount = 0;
long lLen = lstrlen( a_tiff );
long nObj = 0;
fpos_t start, end;
memcpy( szTiff, a_tiff, lLen );
szTiff[ lLen ] = '\0';
if (a_pdf == NULL) {
memcpy( szPdf, a_tiff, lLen );
szPdf[ lLen - 3 ] = 'p';
szPdf[ lLen - 2 ] = 'd';
szPdf[ lLen - 1 ] = 'f';
szPdf[ lLen ] = '\0';
} else
memcpy( szPdf, a_pdf, lstrlen( a_pdf ) );
unsigned long info_id,pages_id, root_id;
QTiff qt;
try {
if (qt.open( szTiff )) {
//
// Convert the image to a fax format.
//
if (!qt.isFax()) {
strcpy( m_szErrorCode, "Image is not in fax format." );
return 0; // General failure.
}
FILE * pf = fopen( szPdf, "wb" );
//
// Make sure we have enough space in the array.
//
fpos_t * xref_arr = new fpos_t[ 255 + ( qt.pagecount() * 30) ];
memset( xref_arr, 0, sizeof(fpos_t) * (255 + ( qt.pagecount() * 30 )) );
if (pf) {
//
// Need to track the size of the obj every write add the offset
//
czbuf = "%PDF-1.2\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
//
// Write out info object
//
fgetpos( pf, &xref_arr[ nObj++ ] );
info_id = nObj;
czbuf.Format("%lu 0 obj\r",nObj);
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
tiff_writePDFInfoObject( pf, a_szCreator, a_szCreationDate,a_szAuthor,a_szProducer,a_szTitle,a_szSubject,a_szKeywords );
//
// Write out catalog object
//
fgetpos( pf, &xref_arr[ nObj++ ] );
root_id = nObj;
czbuf.Format("%lu 0 obj\r",nObj);
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
tiff_writePDFCatalogObject( pf, nObj );
//
// Write out pages object
//
fgetpos( pf, &xref_arr[ nObj++ ] );
pages_id = nObj;
czbuf.Format("%lu 0 obj\r",nObj);
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
tiff_writePDFPagesObject( pf, nObj, qt);
for ( int i = 0 ; i < qt.pagecount() ; i++ ) {
if (qt.page() != i)
qt.setpage( i );
//
// Write out page object
//
fgetpos( pf, &xref_arr[ nObj++ ] );
czbuf.Format("%lu 0 obj\r",nObj);
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
tiff_writePDFPageObject( pf, nObj, pages_id, qt );
//
// Write out contents object nobj + 1
//
fgetpos( pf, &xref_arr[ nObj++ ] );
czbuf.Format("%lu 0 obj\r",nObj);
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
tiff_writePDFContentsObject( pf, nObj, qt, start, end );
//
// Write out procset
//
fgetpos( pf, &xref_arr[ nObj++ ] );
czbuf.Format("%lu 0 obj\r",nObj);
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
tiff_writePDFProcsetObject( pf, nObj, qt, start, end );
//
// Write out XObject
//
fgetpos( pf, &xref_arr[ nObj++ ] );
czbuf.Format("%lu 0 obj\r",nObj);
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
tiff_writePDFXObject( pf, nObj, qt, start, end );
//
// Write out XObject Length
//
fgetpos( pf, &xref_arr[ nObj++ ] );
czbuf.Format("%lu 0 obj\r",nObj);
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
tiff_writePDFXObjectLength( pf, nObj, qt, start, end );
}
//
// Write Xref object.
//
fgetpos( pf, &end );
czbuf = "xref\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
czbuf.Format( "0 %lu\r", nObj + 1 );
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
czbuf = "0000000000 65535 f \r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
for (i = 0; i < (long) nObj; i++) {
czbuf.Format( "%010lu 00000 n \r",(unsigned long) xref_arr[ i ] );
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
fgetpos( pf, &start );
}
czbuf = "trailer\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
czbuf = "<<\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
czbuf.Format( "/Size %lu\r", nObj+1 );
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
czbuf.Format( "/Root %lu 0 R\r",root_id);
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
czbuf.Format( "/Info %lu 0 R\r",info_id);
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
czbuf = ">>\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
czbuf = "startxref\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
czbuf.Format( "%lu\r",(unsigned long)end );
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
czbuf = "%%EOF\r";
fwrite( (LPCSTR)czbuf, czbuf.GetLength(), 1 , pf );
fclose( pf );
}
delete[] xref_arr;
} else {
strcpy( m_szErrorCode, "Unable to open file." );
return -1; // Unable to open file.
}
} catch ( int err ) {
char szFunction[ 50 ];
switch( err )
{
case -10:
strcpy( szFunction, "tiff_writePDFXObjectLength" );
break;
case -11:
strcpy( szFunction, "tiff_writePDFXObject" );
break;
case -12:
strcpy( szFunction, "tiff_writePDFProcsetObject" );
break;
case -13:
strcpy( szFunction, "tiff_writePDFContentsObject" );
break;
case -14:
strcpy( szFunction, "tiff_writePDFPageObject" );
break;
case -15:
strcpy( szFunction, "tiff_writePDFPagesObject" );
break;
case -16:
strcpy( szFunction, "tiff_writePDFCatalogObject" );
break;
case -17:
strcpy( szFunction, "tiff_writePDFInfoObject" );
break;
}
sprintf( m_szErrorCode, "Error in function %s", szFunction );
return err;
} catch (...) {
// error in this function
strcpy( m_szErrorCode, "Error in convertTiffToPdf" );
return 0;
}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -