I'm finally back at this project again. I'm actually creating pdfs with Apprentice and for a while I thought it might be the PDF printer that's causing the issues but I just switched to a different PDF Printer and it didn't clear it up so I'm assuming now that it is an Apprentice issue for sure.
The worst thing about this issue is that it'll work fine for a few days, and then just at random it'll act up again. And it's not repeatable. A drawing that has an issue now might print fine the next time I try it. In fact I have never been able to duplicate the problem manually. My code is part of a Job Processor extension that automatically prints pdfs from the Vault, and it always acts up when it's running unattended. I am attaching my one routine for reference, but I'm not sure how to provide a complete example that will definitely produce the issue I'm having. Any suggestions for doing this are welcome.
public Boolean printToPDF(string idw, string outputFolder)
{
try
{
using (StreamWriter logFile = new StreamWriter(outputFolder + "PrintPDFlog.txt", true))
{
//Boolean fileExists = false;
Inventor.ApprenticeServerComponent oApprentice = new ApprenticeServerComponent();
Inventor.ApprenticeServerDrawingDocument drgDoc;
drgDoc = (Inventor.ApprenticeServerDrawingDocument)oApprentice.Document;
oApprentice.Open(idw);
drgDoc = (Inventor.ApprenticeServerDrawingDocument)oApprentice.Document;
Inventor.ApprenticeDrawingPrintManager pMgr;
pMgr = (Inventor.ApprenticeDrawingPrintManager)drgDoc.PrintManager;
pMgr.Printer = "Bullzip PDF Printer";
int pageCount = 1;
List<string> assemblyFileNameList = new List<string>();
foreach (Sheet sh in drgDoc.Sheets)
{
if (sh.DrawingViews.Count > 0)
{
string modelName;
string modelExtension;
modelName = sh.DrawingViews[1].ReferencedDocumentDescriptor.DisplayName;
modelExtension = System.IO.Path.GetExtension(modelName);
modelName = System.IO.Path.GetFileNameWithoutExtension(modelName);
switch (sh.Orientation)
{
case PageOrientationTypeEnum.kLandscapePageOrientation:
pMgr.Orientation = PrintOrientationEnum.kLandscapeOrientation;
break;
case PageOrientationTypeEnum.kDefaultPageOrientation:
pMgr.Orientation = PrintOrientationEnum.kDefaultOrientation;
break;
case PageOrientationTypeEnum.kPortraitPageOrientation:
pMgr.Orientation = PrintOrientationEnum.kPortraitOrientation;
break;
}
pMgr.SetSheetRange(pageCount, pageCount);
pMgr.PrintRange = PrintRangeEnum.kPrintSheetRange;
pMgr.ScaleMode = Inventor.PrintScaleModeEnum.kPrintBestFitScale;
string newName = "";
if (modelExtension == ".iam")
{
newName = outputFolder + modelName + ".pdf";
if (System.IO.File.Exists(outputFolder + modelName + ".pdf"))
{
assemblyFileNameList.Add(newName);
newName = outputFolder + modelName + "~" + 1 + ".pdf";
System.IO.File.Move(outputFolder + modelName + ".pdf", newName);
assemblyFileNameList.Add(newName);
}
}
else
{
System.IO.File.Delete(outputFolder + modelName + ".pdf");
}
string psFileName = outputFolder + modelName + ".ps";
string pdfFileName = outputFolder + modelName + ".pdf";
// for some reason if a ps filename contains a comma it doesn't want to print.
// we'll replace it with a tilde.
if (psFileName.Contains(","))
{
psFileName = psFileName.Replace(',', '~');
}
pMgr.PrintToFile(psFileName);
pageCount++;
Process oProc = new Process();
// need the full path to the program if we want to set UseShellExecute to false
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Program Files\gs\gs9.14\lib\ps2pdf.bat");
startInfo.WorkingDirectory = @"C:\Program Files\gs\gs9.14\bin\";
startInfo.Arguments = @"""" + psFileName + @"""" + " " + @"""" + pdfFileName + @"""";
startInfo.CreateNoWindow = true;
oProc.StartInfo = startInfo;
oProc.StartInfo.UseShellExecute = false;
oProc.Start();
oProc.WaitForExit();
System.IO.File.Delete(psFileName);
if (assemblyFileNameList != null)
{
if (assemblyFileNameList.Count > 1) // combine multiple assembly drawings into one pdf file
{
// Open the input files
PdfDocument inputDocument1 = PdfReader.Open(assemblyFileNameList[0], PdfDocumentOpenMode.Import);
PdfDocument inputDocument2 = PdfReader.Open(assemblyFileNameList[1], PdfDocumentOpenMode.Import);
// Create the output document
PdfDocument outputDocument = new PdfDocument();
// Show consecutive pages facing. Requires Acrobat 5 or higher.
outputDocument.PageLayout = inputDocument1.PageLayout;
int count = Math.Max(inputDocument1.PageCount, inputDocument2.PageCount);
for (int idx = 0; idx < count; idx++)
{
PdfPage page1 = new PdfPage();
PdfPage page2 = new PdfPage();
if (inputDocument1.PageCount > idx)
{
page1 = inputDocument1.Pages[idx];
page1 = outputDocument.AddPage(page1);
}
if (inputDocument2.PageCount > idx)
{
page2 = inputDocument2.Pages[idx];
page2 = outputDocument.AddPage(page2);
}
}
// Save the document...
string filename = assemblyFileNameList[0];
outputDocument.Save(filename);
// delete the temp file and clear the list
System.IO.File.Delete(assemblyFileNameList[1]);
assemblyFileNameList.Clear();
}
}
}
}
logFile.WriteLine("Done with File " + System.IO.Path.GetFileName(idw) + DateTime.Now + "\n");
logFile.WriteLine("");
logFile.Close();
return true;
}
}
catch (Exception)
{
return false;
}
}