简体   繁体   English

从 Excel 创建 PDF 文件:图像问题

[英]Create a PDF file from Excel: problem with images

I have an Excel template that I use to create PDF specification sheets for our vendors.我有一个Excel模板,用于为我们的供应商创建 PDF 规格表。 I can open, manipulate and export to a PDF file fine except when I insert a picture.除了插入图片时,我可以正常打开、操作和导出到 PDF 文件。 This is the code I am using to add one picture这是我用来添加一张图片的代码

workSheet.Shapes.AddPicture(imagePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 120, 700, image.Width, image.Height);

and this is the code to save the PDF file:这是保存 PDF 文件的代码:

private void createPDF()
{
     if (File.Exists(path + basePart.Number + ".pdf"))
         File.Delete(path + basePart.Number + ".pdf");
     workBook.Saved = true;
     workSheet.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, path + basePart.Number, Excel.XlFixedFormatQuality.xlQualityStandard, true,
                false, Type.Missing, Type.Missing, true, Type.Missing);
     DisposeExcelInstance();
}

Without any images the PDF file saves fine.没有任何图像,PDF 文件保存良好。 With images, the PDF file saves but cannot be opened.有图片,PDF文件保存但打不开。 I have tried multiple PDF file readers just in case but sure enough none of them work.为了以防万一,我尝试了多个 PDF 文件阅读器,但肯定它们都不起作用。

EDIT:编辑:

I have tried various Excel formats and none have worked.我尝试了各种 Excel 格式,但都没有奏效。 It seems that ExportAsFixedFormat does not work with images, no matter how they get inserted into the document.似乎ExportAsFixedFormat不适用于图像,无论它们如何插入到文档中。 Is there an alternative to this?有没有替代方案?

Microsoft is to thank for this one.微软要感谢这一点。 6 hours of more or less banging my head against a wall testing on multiple computers with multiple file formats and PDF file tools I find out it is a compatibility issue with Excel 2010.在具有多种文件格式和 PDF 文件工具的多台计算机上或多或少地撞墙测试我发现这是与 Excel 2010 的兼容性问题。

I finally got a spare laptop so I could stop bugging other people to test things on their PCs and the laptop had Excel 2007 on it and it worked like a charm.我终于得到了一台备用笔记本电脑,这样我就可以停止让其他人在他们的 PC 上测试东西了,笔记本电脑上有 Excel 2007,它就像一个魅力。 The actual issue, believe it or not, is that when working with Excel 2007 (the version that the template had originally been built in) someone had put it into the "Page Layout" view, which worked on Excel 2007 and older when creating a PDF file with a picture in it.信不信由你,实际问题是在使用 Excel 2007(最初内置模板的版本)时,有人将其放入“页面布局”视图中,该视图在 Excel 2007 及更早版本创建PDF 文件,其中包含一张图片。

With Excel 2010 it will sometimes allow me to get away with the page layout view, but never with pictures.使用 Excel 2010 有时它可以让我摆脱页面布局视图,但永远不会使用图片。 I wish I were making it up.我希望我在弥补。 I didn't think the view really had anything to do with the the functionality of the spreadsheet.我认为视图与电子表格的功能没有任何关系。 This is a very specific problem, but hopefully someday it saves someone a headache.这是一个非常具体的问题,但希望有一天它可以让人们头疼。

https://stackoverflow.com/a/6207214/713819 https://stackoverflow.com/a/6207214/713819

That's true.确实如此。 You are right.你说的对。 I also encountered this issue.我也遇到了这个问题。 I had an MS Excel template created using MS Excel 2007 in past but I was still using it.我过去有一个使用 MS Excel 2007 创建的 MS Excel 模板,但我仍在使用它。 However, when I thought to automate report generation using the same template (that I was updating manually before) and inserted an image into the sheet, It never showed up in Print Preview (of MS Excel 2016).但是,当我想使用相同的模板(我之前手动更新)自动生成报告并将图像插入工作表时,它从未出现在打印预览中(MS Excel 2016)。 I tried saving the excel report to PDF but image was also not shown in the PDF.我尝试将 excel 报告保存到 PDF 但图像也未显示在 PDF 中。

Finally, I had to re-create the template in MS Excel 2016 and then the issue was resolved.最后,我不得不在 MS Excel 2016 中重新创建模板,然后问题就解决了。 In short, it was a bug in MS Excel for compatibility which is still not fixed by Microsoft.简而言之,这是 MS Excel 中的一个错误,用于兼容性,微软仍未修复。

Anyways, thanks to you!无论如何,谢谢你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM