简体   繁体   中英

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. I can open, manipulate and export to a PDF file fine except when I insert a picture. 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:

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. With images, the PDF file saves but cannot be opened. I have tried multiple PDF file readers just in case but sure enough none of them work.

EDIT:

I have tried various Excel formats and none have worked. It seems that ExportAsFixedFormat does not work with images, no matter how they get inserted into the document. 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.

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. 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.

With Excel 2010 it will sometimes allow me to get away with the page layout view, but never with pictures. 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

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. 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). I tried saving the excel report to PDF but image was also not shown in the PDF.

Finally, I had to re-create the template in MS Excel 2016 and then the issue was resolved. In short, it was a bug in MS Excel for compatibility which is still not fixed by Microsoft.

Anyways, thanks to you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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