2010-03-17 123 views
0

我有一個excel工作簿vsto解決方案,需要生成其中一個工作表的pdf副本作爲輸出。Excel 2003 VSTO轉換爲PDF

我有一個abcdpdf .net的許可證,並嘗試輸出到html,然後使用abcpdf將html轉換爲pdf,但是excel html標記試圖模擬excel與所有4個帶有可怕標記的工作表。它也混淆了顏色(整個工作簿中的銀色背景)。

有什麼建議嗎?

這是我目前使用生成的HTML文件中的代碼:

FileInfo excelDoc = new FileInfo(Globals.ThisWorkbook.Path + @"\Document.html"); 

Globals.Sheet2.SaveAs(excelDoc.FullName, 
    Excel.XlFileFormat.xlHtml, missing, missing, false, false, 
    Excel.XlSaveAsAccessMode.xlNoChange, 
    missing, missing, missing); 

如果我手動謬以千里一些HTML標題標籤的,我可以得到abcdpf接受它,但格式是有點偏離,這個解決方案似乎是次優的。

在此先感謝。

回答

1

找到解決方案:將Excel表格存儲爲XPS打印出來。將XPS輸出導入到pdf文檔中。

MyImportOperation代碼改編自abcpdf XPS示例源代碼。

public void SaveSheetToPdf(FileInfo outputPDF) 
    { 
     FileInfo documentFile = new FileInfo(Globals.ThisWorkbook.Path + @"\tempDoc.xps"); 
     if (documentFile.Exists) 
      documentFile.Delete(); 

     Globals.Sheet2.PrintOut(1, missing, 1, false, "Microsoft XPS Document Writer", true, false, documentFile.FullName); 

     Doc theDoc = new Doc();     

     try 
     { 
      MyImportOperation importOp = new MyImportOperation(theDoc); 
      importOp.Import(documentFile);    
     } 
     catch (Exception ex) 
     { 
      throw new Exception("Error rendering pdf. PDF Source XPS Path: " + investmentPlanXPSPath, ex); 
     } 

     theDoc.Save(outputPDF.FullName); 
    } 

    public class MyImportOperation 
    { 
     private Doc _doc = null; 
     private double _margin = 10; 
     private int _pagesAdded = 0; 

     public MyImportOperation(Doc doc) 
     { 
      _doc = doc; 
     } 

    public void Import(string inPath) 
    { 
     using (XpsImportOperation op = new XpsImportOperation()) 
     { 
      op.ProcessingObject += Processing; 
      op.ProcessedObject += Processed; 
      op.Import(_doc, inPath); 
     } 
    } 

    public void Processing(object sender, ProcessingObjectEventArgs e) 
    { 

     if (e.Info.SourceType == ProcessingSourceType.PageContent) 
     {  
      _doc.Page = _doc.AddPage();  
      e.Info.Handled = true; 
      _pagesAdded++; 
     } 
    } 

    public void Processed(object sender, ProcessedObjectEventArgs e) 
    { 
     if (e.Successful) 
     { 
      PixMap pixmap = e.Object as PixMap; 
      if (pixmap != null) 
       pixmap.Compress();  
     } 
    } 

}