iTextSharp 合并PDF后删除合并文件

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/hebbers/article/details/70332015

问题:之前开发的一个服务中,利用iTextSharp合并PDF文件后删除合并文件,提示合并文件被占用无法删除。

解决:合并时记录PdfReader到一个集合中,合并后释放PdfReader集合中的全部资源。

代码:

	/// <summary> 合并PDF </summary>
        /// <param name="fileList">PDF文件集合</param>
        /// <param name="outMergeFile">合并文件名</param>
        private void MergePDFFiles(string[] fileList, string outMergeFile)
        {
            List<PdfReader> readerList = new List<PdfReader>();//记录合并PDF集合
            iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4.Rotate());
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));
            document.Open();

            PdfContentByte cb = writer.DirectContent;
            PdfImportedPage newPage;
            for (int i = 0; i < fileList.Length; i++)
            {
                if (!string.IsNullOrEmpty(fileList[i]))
                {
                    PdfReader reader = new PdfReader(fileList[i]);
                    int iPageNum = reader.NumberOfPages;
                    for (int j = 1; j <= iPageNum; j++)
                    {
                        document.NewPage();
                        newPage = writer.GetImportedPage(reader, j);
                        cb.AddTemplate(newPage, 0, 0);
                    }
                    readerList.Add(reader);
                }
            }
            document.Close();

	    //释放集合资源
            foreach (var rd in readerList)
            {
                rd.Dispose();
            }
        }


展开阅读全文

Java合并文件,并限制合并后文件大小

11-24

合并一个文件夹下的n个小文件,合并后的大文件最大10MB,超出后继续合并其余小文件到,还是最大10MB,以此类推。rn麻烦帮我看看那儿错了,我只能限制第一个大文件10MB,后面的就不行了!rn多谢!rnimport java.io.*; rnrnpublic class FilesIn rn rn public static void main(String[] args) rn rn File dir = new File("E://testdata"); rn File out=new File("E://Separator");rn try rn SaveFile(dir,out);rn catch (IOException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn rn rn static void SaveFile(File dir, File dst) throws IOException rn rn String[] children = dir.list();rn FilenameFilter filter = new FilenameFilter()rn rn public boolean accept(File dir, String name) rn return true; rn rn rn ;rnrn rn File filePath = new File("E://Separator");rn String fileIndex="0";rn File FileBig = new File(filePath + "/" + "Files"+"_"+fileIndex); rn BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(FileBig));rn rn children = dir.list(filter);rn rn for (int i = 0; i < children.length; i++) rn rn String inhalt = children[i];rn FileInputStream in=new FileInputStream(dir+"/" + inhalt); rn rn byte[] buffer = new byte[1024];rn int len;rn if(FileBig.length()/1024/1024<10)rn while ( (len = in.read(buffer)) > 0) rn out.write(buffer, 0, len);rn rn //out.close();rn rn elsern rn int fileIndexInt = Integer.parseInt(fileIndex)+1;rn File FileBig1 = new File(filePath + "/" + "Files"+"_"+fileIndexInt);rn BufferedOutputStream out1=new BufferedOutputStream(new FileOutputStream(FileBig1));rnrn while ( (len = in.read(buffer)) > 0) rn out1.write(buffer, 0, len);rn out.close();rn out1.close();rn rn fileIndex = String.valueOf(fileIndexInt); rn rn rn out.close();rnrn 问答

没有更多推荐了,返回首页