/////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2002-2025, Open Design Alliance (the "Alliance"). // All rights reserved. // // This software and its documentation and related materials are owned by // the Alliance. The software may only be incorporated into application // programs owned by members of the Alliance, subject to a signed // Membership Agreement and Supplemental Software License Agreement with the // Alliance. The structure and organization of this software are the valuable // trade secrets of the Alliance and its suppliers. The software is also // protected by copyright law and international treaty provisions. Application // programs incorporating this software must include the following statement // with their copyright notices: // // This application incorporates Open Design Alliance software pursuant to a license // agreement with Open Design Alliance. // Open Design Alliance Copyright (C) 2002-2025 by Open Design Alliance. // All rights reserved. // // By use of this software, its documentation or related materials, you // acknowledge and accept the above terms. /////////////////////////////////////////////////////////////////////////////// #include "OdaCommon.h" #include "DynamicLinker.h" #include "PdfPublish/PdfPublish.h" namespace { OdString correctPath(const OdString& filePath) { OdRxSystemServices* pSs = odrxSystemServices(); if (pSs) { OdString validPath = filePath; if (pSs->accessFile(validPath, Oda::kFileRead)) return validPath; // Found path as is. #if defined(ODA_WINDOWS) validPath.insert(0, L"CommonApplications\\Drawing\\Examples\\OdPdfPublishEx\\"); #else validPath.insert(0, L"CommonApplications/Drawing/Examples/OdPdfPublishEx/"); #endif if (pSs->accessFile(validPath, Oda::kFileRead)) return validPath; for (OdUInt32 i = 0; i < 6; i++) { #if defined(ODA_WINDOWS) validPath.insert(0, "..\\"); #else validPath.insert(0, "../"); #endif if (pSs->accessFile(validPath, Oda::kFileRead)) return validPath; } } return OdString::kEmpty; // not found } } using namespace OdPdfPublish; OdResult OdPdfPublishCADEx(OdDbBaseHostAppServices* pHostApp, OdDbBaseDatabase* pDatabase, const OdString& sServiceCaption, OdStreamBufPtr& sOutPdfStream, const OdString& sInputFileName); OdResult OdPdfPublishCADEx(OdDbBaseHostAppServices* pHostApp, OdDbBaseDatabase* pDatabase, const OdString& sServiceCaption, const OdString& sOutPdf, const OdString& sInputFileName) { if (sOutPdf.isEmpty()) { return eInvalidInput; } OdStreamBufPtr output_stream = odrxSystemServices()->createFile(sOutPdf, Oda::kFileWrite, Oda::kShareDenyNo, Oda::kCreateAlways); OdPdfPublishCADEx(pHostApp, pDatabase, sServiceCaption, output_stream, sInputFileName); return ::eOk; } OdResult OdPdfPublishCADEx(OdDbBaseHostAppServices* pHostApp, OdDbBaseDatabase *pDatabase, const OdString& sServiceCaption, OdStreamBufPtr& sOutPdfStream, const OdString& sInputFileName) { OdFilePtr pPublisher = OdFile::createObject(); OdDocumentPtr pDoc = OdDocument::createObject(); pDoc->setInformation(L"Pdf Publish CAD* Sample Document", L"Author", L"CAD* Sample", L"Oda Pdf Publish", L"ODA, PDF", L"Oda Pdf Publish"); pDoc->setHostServices(pHostApp); OdCADDefinitionPtr pCADDef = OdCADDefinition::createObject(); pCADDef->setDatabase(pDatabase); pCADDef->setMediaSize(Page::kMillimeters, 840., 594.); OdWatermarkPtr pWatermark = OdWatermark::createObject(); OdTextPtr pWaterText1 = OdText::createObject(); pWaterText1->setText("ODA sample watermark"); pWaterText1->setSize(20); pWaterText1->setFont(Text::kTimesBold); pWatermark->addText(pWaterText1, OdRect(0, 300, 0, 300), OdaPI4); OdTextPtr pWaterText2 = OdText::createObject(); pWaterText2->setText(L"watermark red text"); pWaterText2->setSize(10); pWaterText2->setFont(Text::kTimesBold); pWaterText2->setColor(ODRGB(255, 0, 0)); pWatermark->addText(pWaterText2, OdRect(60, 200, 25, 300), OdaPI4); pWatermark->setSize(170, 170); pWatermark->setOpacity(30); // Page 1 OdPagePtr pPage1 = OdPage::createObject(); pPage1->setFormat(Page::kA3); pPage1->setOrientation(Page::kLandscape); pDoc->addPage(pPage1); OdTextPtr pCaptionPage1 = OdText::createObject(); pCaptionPage1->setText(sServiceCaption + L" sample"); pCaptionPage1->setSize(20); pCaptionPage1->setFont(Text::kTimesBold); pPage1->addText(pCaptionPage1, OdRect(550, 1186, 820, 842)); OdCADReferencePtr pCADRef1 = OdCADReference::createObject(); pCADRef1->setCADDefinition(pCADDef); pPage1->addCADReference(pCADRef1, OdRect(5, 1180, 5, 800)); pCADRef1->setCADReferenceName(L"CADRef1"); pCADRef1->setEnableBookmarks(true); pPage1->addWatermark(pWatermark, OdRect(5, 1180, 5, 800)); // Page 2. Rotation OdPagePtr pPage2 = OdPage::createObject(); pPage2->setFormat(Page::kA3); pPage2->setOrientation(Page::kLandscape); pDoc->addPage(pPage2); OdTextPtr pCaptionPage2 = OdText::createObject(); pCaptionPage2->setText(sServiceCaption + L" rotation sample"); pCaptionPage2->setSize(20); pCaptionPage2->setFont(Text::kTimesBold); pPage2->addText(pCaptionPage2, OdRect(550, 1186, 820, 842)); OdCADReferencePtr pCADRef2 = OdCADReference::createObject(); pCADRef2->setCADDefinition(pCADDef); pCADRef2->setRotation(OdaPI4); pCADRef2->setBorder(true); pPage2->addCADReference(pCADRef2, OdRect(5, 1180, 10, 800)); pCADRef2->setCADReferenceName(L"CADRef2"); pCADRef2->setEnableBookmarks(true); pPage2->addWatermark(pWatermark, OdRect(5, 1180, 5, 800)); // Page 3. Clip OdPagePtr pPage3 = OdPage::createObject(); pPage3->setFormat(Page::kA3); pPage3->setOrientation(Page::kLandscape); pDoc->addPage(pPage3); OdTextPtr pCaptionPage3 = OdText::createObject(); pCaptionPage3->setText(sServiceCaption + L" clip sample"); pCaptionPage3->setSize(20); pCaptionPage3->setFont(Text::kTimesBold); pPage3->addText(pCaptionPage3, OdRect(550, 1186, 820, 842)); OdCADReferencePtr pCADRef3 = OdCADReference::createObject(); pCADRef3->setCADDefinition(pCADDef); OdGePoint2dArray clip_box; clip_box.append(OdGePoint2d(360, 100)); clip_box.append(OdGePoint2d(320, 225)); clip_box.append(OdGePoint2d(360, 350)); clip_box.append(OdGePoint2d(455, 400)); clip_box.append(OdGePoint2d(550, 350)); clip_box.append(OdGePoint2d(590, 225)); clip_box.append(OdGePoint2d(550, 100)); pCADRef3->setClipBoundary(Page::kMillimeters, clip_box); pCADRef3->setClipBoundaryBorder(true); pCADRef3->setClipBoundaryBorderColor(ODRGB(255,0,0)); pCADRef3->setClipBoundaryBorderWidth(2); pPage3->addCADReference(pCADRef3, OdRect(600, 1180, 50, 800)); OdTableCreatorPtr table_creator = OdTableCreator::createObject(); table_creator->setRowCount(30); table_creator->setColumnCount(3); table_creator->setParticularColumnWidth(1, 245); table_creator->setCellFillColor(0, 0, ODRGB(127, 255, 68)); table_creator->setCellFillColor(0, 1, ODRGB(127, 255, 68)); table_creator->setCellFillColor(0, 2, ODRGB(127, 255, 68)); OdImagePtr background_image1 = OdImage::createObject(); OdString img_file_name = pHostApp->findFile(L"hatching.jpg"); if (img_file_name.isEmpty()) img_file_name = correctPath(L"hatching.jpg"); if (!img_file_name.isEmpty()) { background_image1->setFile(img_file_name); table_creator->setCellBackgroundImage(5, 1, background_image1); table_creator->setCellBackgroundImage(6, 2, background_image1); } Od2dGeometryBlockPtr pHatching1 = Od2dGeometryBlock::createObject(); OdTextPtr h_text1 = OdText::createObject(); h_text1->setFont(Text::kZapfDingbats); h_text1->setColor(ODRGB(255, 0, 0)); char text_val_char = (char)169; OdString text_val1(&text_val_char, 1, CP_ASCII); h_text1->setText(text_val1); h_text1->setSize(40); OdTextPtr h_text2 = OdText::createObject(); h_text2->setFont(Text::kZapfDingbats); h_text2->setColor(ODRGB(0, 0, 0)); text_val_char = (char)168; OdString text_val2(&text_val_char, 1, CP_ASCII); h_text2->setText(text_val2); h_text2->setSize(40); OdTextPtr h_text3 = OdText::createObject(); h_text3->setFont(Text::kZapfDingbats); h_text3->setColor(ODRGB(0, 0, 0)); text_val_char = (char)171; OdString text_val3(&text_val_char, 1, CP_ASCII); h_text3->setText(text_val3); h_text3->setSize(40); OdTextPtr h_text4 = OdText::createObject(); h_text4->setFont(Text::kZapfDingbats); h_text4->setColor(ODRGB(255, 0, 0)); text_val_char = (char)170; OdString text_val4(&text_val_char, 1, CP_ASCII); h_text4->setText(text_val4); h_text4->setSize(40); pHatching1->addText(h_text1, OdRect(0, 5, 5, 10)); pHatching1->addText(h_text2, OdRect(5, 10, 5, 10)); pHatching1->addText(h_text3, OdRect(0, 5, 0, 5)); pHatching1->addText(h_text4, OdRect(5, 10, 0, 5)); Od2dGeometryBlockPtr pHatching2 = Od2dGeometryBlock::createObject(); for (int i = 0; i < 21; i += 2) { pHatching2->addLine(OdGePoint2d(i < 11 ? 0 : i - 10, i < 11 ? i : 10), OdGePoint2d(i < 11 ? i : 10, i < 11 ? 0 : i - 10)); } OdTilingPatternPtr pattern1 = OdTilingPattern::createObject(); pattern1->setGeometryBlock(pHatching1); OdTilingPatternPtr pattern2 = OdTilingPattern::createObject(); pattern2->setGeometryBlock(pHatching2); table_creator->setCellTilingPattern(9, 0, pattern1); table_creator->setCellTilingPattern(11, 1, pattern2); Od2dGeometryBlockPtr pGeom = Od2dGeometryBlock::createObject(); OdGePoint2dArray hatching_contour; hatching_contour.append(OdGePoint2d(0, 0)); hatching_contour.append(OdGePoint2d(0, 100)); hatching_contour.append(OdGePoint2d(200, 100)); hatching_contour.append(OdGePoint2d(200, 0)); pGeom->putTilingPattern(pattern2); pGeom->startContour(); pGeom->addLine(hatching_contour); pGeom->finishContour(Geometry::PDFFinishRule(Geometry::kClose | Geometry::kStroke | Geometry::kFillEvenOdd)); Od2dGeometryReferencePtr pGeomRef1 = Od2dGeometryReference::createObject(); pGeomRef1->setGeometryBlock(pGeom); pPage3->add2dGeometry(pGeomRef1, OdRect(600, 1100, 5, 100)); table_creator->setParticularRowHeight(0, 70); OdTablePtr table1 = table_creator->createTable(); OdTextPtr text1 = OdText::createObject(); text1->setText(L"NB"); text1->setSize(20); table1->setText(0, 0, text1); OdTextPtr text2 = OdText::createObject(); text2->setText(L"Part Name"); text2->setSize(20); table1->setText(0, 1, text2); OdTextPtr text3 = OdText::createObject(); text3->setText(L"QTY"); text3->setSize(20); table1->setText(0, 2, text3); Table::TextAlignment alignments_array[] = { Table::kTopLeft, Table::kTopCenter, Table::kTopRight, Table::kCenterLeft, Table::kCenter, Table::kCenterRight, Table::kBottomLeft, Table::kBottomCenter, Table::kBottomRight }; for (OdUInt8 i = 1; i < 30; ++i) { OdTextPtr text_c1 = OdText::createObject(); text_c1->setText(OdString().format(L"%i", i)); text_c1->setSize(15); table1->setText(i, 0, text_c1); double rotation = 0.0; Table::TextAlignment aligment = Table::kCenter; if (i > 20) aligment = alignments_array[i - 21]; OdTextPtr text_c2 = OdText::createObject(); text_c2->setText(OdString().format(L"part_name_%i", i)); text_c2->setSize(12); table1->setText(i, 1, text_c2, rotation, aligment); OdTextPtr text_c3 = OdText::createObject(); text_c3->setText(OdString().format(L"%i", rand() % 20 + 1)); text_c3->setSize(12); table1->setText(i, 2, text_c3); } pPage3->addTable(table1, OdRect(10, 550, 10, 800)); OdBookmarkPtr pBookMark1 = OdBookmark::createObject(); pBookMark1->setTitle(L"pBookMark1"); pBookMark1->setPage(pPage3); pBookMark1->setTextColor(ODRGB(255, 0, 0)); pBookMark1->setTextStyle(Text::kBold); pDoc->appendRootBookmark(pBookMark1); OdBookmarkPtr pBookMark2 = OdBookmark::createObject(); pBookMark2->setTitle(L"pBookMark2"); pBookMark2->setPage(pPage3); pBookMark2->setBookmarkType(Bookmarks::kXYZ); OdArray params; params.append(0); params.append(400); params.append(2); pBookMark2->setParametrs(params); pBookMark1->appendChild(pBookMark2); // Page 4. Matrix transform OdPagePtr pPage4 = OdPage::createObject(); pPage4->setFormat(Page::kA3); pPage4->setOrientation(Page::kLandscape); pDoc->addPage(pPage4); OdTextPtr pCaptionPage4 = OdText::createObject(); pCaptionPage4->setText(sServiceCaption + L" Matrix transform sample"); pCaptionPage4->setSize(20); pCaptionPage4->setFont(Text::kTimesBold); pPage4->addText(pCaptionPage4, OdRect(550, 1186, 820, 842)); OdCADReferencePtr pCADRef4 = OdCADReference::createObject(); pCADRef4->setCADDefinition(pCADDef); OdGeMatrix2d transform; transform.setCoordSystem(OdGePoint2d(350, 50), OdGeVector2d::kXAxis, OdGeVector2d::kYAxis); transform *= OdGeMatrix2d::scaling(0.035); transform *= OdGeMatrix2d::rotation(OdaPI/6.0); pCADRef4->setTransform(transform); pPage4->addCADReference(pCADRef4); pCADRef4->setCADReferenceName(L"CADRef4"); pCADRef4->setEnableBookmarks(true); pPage4->addWatermark(pWatermark, OdRect(5, 590, 5, 400)); pPage4->addWatermark(pWatermark, OdRect(590, 1180, 400, 800)); //Add source file to attachment OdString sDescription(L"source"); OdAttachedFilePtr file_attachement = OdAttachedFile::createObject(); file_attachement->setFile(sInputFileName); file_attachement->setDescription(sDescription); pDoc->addAttachment(file_attachement); pPublisher->exportPdf(pDoc, sOutPdfStream); return ::eOk; }