Pdf文件解压缩并覆盖原文件,用于部分pdf文件加水印失败,文件被破坏
/// <summary>
/// pdf文件解压缩
/// </summary>
/// <param name="compressedPdfPath">压缩的PDF文件路径</param>
/// <returns></returns>
public static string Decompression(string compressedPdfPath)
{
string outputPdfPath = compressedPdfPath + "_copy"; // 输出的PDF文件路径
using (FileStream compressedPdfStream = new FileStream(compressedPdfPath, FileMode.Open, FileAccess.Read))
using (FileStream outputPdfStream = new FileStream(outputPdfPath, FileMode.Create, FileAccess.Write, FileShare.None))
{
iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(compressedPdfStream);
using (iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(pdfReader, outputPdfStream))
{
pdfStamper.Close();
}
pdfReader.Close();
}
if (File.Exists(compressedPdfPath))
{
File.Delete(compressedPdfPath);
}
File.Copy(outputPdfPath, compressedPdfPath);
if (File.Exists(outputPdfPath))
{
File.Delete(outputPdfPath);
}
return compressedPdfPath;
}