您的位置:首页 > 编程语言 > ASP

itextsharp、Aspose.Words、Aspose.Cells联合使用

2014-09-12 11:05 555 查看
最近做了一个系统需要把文件转换为pdf然后把转换后的pdf合成一个pdf文件,网上搜索了半天,最终决定使用itestsharp.dll配合Aspose.words和Aspose.cells来做,废话少说,上代码……

#region 文件转换pdf
public void ConvertImageToPdf(string inputFileName, string outputFileName)
{
Aspose.Words.Document doc = new Aspose.Words.Document();
DocumentBuilder builder = new DocumentBuilder(doc);
using (System.Drawing.Image image = System.Drawing.Image.FromFile(inputFileName))
{
FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);
int framesCount = image.GetFrameCount(dimension);
for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)
{
if (frameIdx != 0)
builder.InsertBreak(BreakType.SectionBreakNewPage);
image.SelectActiveFrame(dimension, frameIdx);
Aspose.Words.PageSetup ps = builder.PageSetup;
if (image.Width > 2000)
{
ps.PageWidth = ConvertUtil.PixelToPoint(image.Width / 2, image.HorizontalResolution);
ps.PageHeight = ConvertUtil.PixelToPoint(image.Height / 2, image.VerticalResolution);
}
else
{
ps.PageWidth = ConvertUtil.PixelToPoint(image.Width-300, image.HorizontalResolution);
ps.PageHeight = ConvertUtil.PixelToPoint(image.Height-100, image.VerticalResolution);
}
builder.InsertImage(image, RelativeHorizontalPosition.Page,100, RelativeVerticalPosition.Page, 0, ps.PageWidth, ps.PageHeight, WrapType.Inline);
}
}
doc.Save(outputFileName);
}

//Word转换成pdf
/// <summary>
/// 把Word文件转换成为PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public bool DOCConvertToPDF(string sourcePath, string targetPath)
{
try
{
Aspose.Words.Document doc = new Aspose.Words.Document(sourcePath);

doc.Save(targetPath, Aspose.Words.SaveFormat.Pdf);
return true;
}
catch (Exception ex)
{
return false;
}
}

/// <summary>
/// 把Excel文件转换成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public bool XLSConvertToPDF(string sourcePath, string targetPath)
{
try
{
Workbook workbook = new Workbook(sourcePath);
workbook.CreateStyle();
workbook.Save(targetPath, Aspose.Cells.SaveFormat.Pdf);
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// txt转pdf
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="targetPath"></param>
/// <returns></returns>
public bool TxtConvertToPDF(string sourcePath, string targetPath)
{
try
{
StreamReader reader = new StreamReader(sourcePath, Encoding.Default); //使用默认编码,否则转换后乱码
string text = reader.ReadToEnd();
Aspose.Words.Document doc = new Aspose.Words.Document();
Aspose.Words.DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write(text);
doc.Save(targetPath, Aspose.Words.SaveFormat.Pdf);
reader.Close();

return true;
}
catch (Exception ex)
{
return false;
}
}
//Word转换成pdf
/// <summary>
/// 把Word文件转换成为PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public bool ConvertCebToPdf(string sourcePath, string targetPath)
{
try
{
Aspose.Words.Document doc = new Aspose.Words.Document(sourcePath);

doc.Save(targetPath, Aspose.Words.SaveFormat.Pdf);
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion
<p>/// <summary>
        /// 合并pdf
        /// </summary>
        /// <param name="fileList">pdf路径集合</param>
        /// <param name="outMergeFile"></param>
        public void mergePDFFiles(List<string> fileList, string outMergeFile, string headerText)
        {
            iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25); 
           
            try
            {
                PdfWriter instance = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));
                document.Open();
                PdfContentByte directContent = instance.DirectContent;
                BaseFont baseFont = BaseFont.CreateFont(@"C:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                #region 目录
                {
                    document.Add(new iTextSharp.text.Paragraph("\r\r\r"));</p><p>                    iTextSharp.text.Paragraph p1 = new iTextSharp.text.Paragraph(new Phrase(headerText, new iTextSharp.text.Font(baseFont)));
                    p1.Alignment = iTextSharp.text.Rectangle.ALIGN_CENTER;  //居中
                    document.Add(p1);
                    document.Add(new iTextSharp.text.Paragraph("\r\r"));
                    int num = 2;
                    for (int i = 0; i < fileList.Count; i++)
                    {
                        PdfReader pdfReader = new PdfReader(fileList[i].Split('$')[0]);
                        int numberOfPages = pdfReader.NumberOfPages;
                        string text = string.Format("{0}. {1}{2}\n\n", (i + 1), fileList[i].Split('$')[2].PadRight((80 - fileList[i].Split('$')[2].Length), '.'), num);
                        iTextSharp.text.Anchor anchor = new iTextSharp.text.Anchor(text, new iTextSharp.text.Font(baseFont));
                        anchor.Reference = "#link" + i;
                        document.Add(anchor);
                        num += numberOfPages;
                    }
 
                    /*for (int i = 0; i < fileList.Count; i++)
                    {
                        PdfReader pdfReader = new PdfReader(fileList[i].Split('$')[0]);
                        string text = string.Format("{0}. {1}\n\n", (i + 1), fileList[i].Split('$')[2]);
                        iTextSharp.text.Anchor anchor = new iTextSharp.text.Anchor(text, new iTextSharp.text.Font(baseFont));
                        anchor.Reference = "#link" + i;
                        document.Add(anchor);
                    }*/</p><p>                }
                #endregion</p><p>                #region 页码
                {
                    iTextSharp.text.HeaderFooter footer = new iTextSharp.text.HeaderFooter(new iTextSharp.text.Phrase("Page:"), true);
                    footer.Border = iTextSharp.text.Rectangle.TITLE;
                    document.Footer = footer;
                }
                #endregion</p><p>                #region 文件合并
                int nn = 0;
                for (int i = 0; i < fileList.Count; i++)
                {
                    document.NewPage();
                    PdfReader pdfReader = new PdfReader(fileList[i].Split('$')[0]);
                    //添加锚点
                    Anchor anchor = new Anchor((i + 1) + "、 " + fileList[i].Split('$')[2] + "\n\n", new iTextSharp.text.Font(baseFont));
                    anchor.Name = "link" + i;
                    document.Add(anchor);</p><p>                    int numberOfPages = pdfReader.NumberOfPages;
                    for (int j = 1; j <= numberOfPages; j++)
                    {
                        if (nn == numberOfPages)
                            document.NewPage();
                        PdfImportedPage importedPage = instance.GetImportedPage(pdfReader, j);
                        directContent.AddTemplate(importedPage, 0f, 0f);
                        nn = numberOfPages;
                    }</p><p>                }
                #endregion
                document.Close();
            }
            catch (Exception ex)
            {
                document.Close();
            }
        }</p>


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息