C# 分割PDF文件的压缩
如何在c#…中压缩切片pdf文档 我有一个pdf文档。我正在切分那份文件。如果切片后原始pdf文档大小从10 mb增加到15 mb。这就是我必须压缩切片文档的原因。有没有办法压缩。。??请帮帮我C# 分割PDF文件的压缩,c#,pdf,C#,Pdf,如何在c#…中压缩切片pdf文档 我有一个pdf文档。我正在切分那份文件。如果切片后原始pdf文档大小从10 mb增加到15 mb。这就是我必须压缩切片文档的原因。有没有办法压缩。。??请帮帮我 public int ExtractPages(string sourcePdfPath, string DestinationFolder) { int p = 0, initialcount = 0; try {
public int ExtractPages(string sourcePdfPath, string DestinationFolder)
{
int p = 0, initialcount = 0;
try
{
iTextSharp.text.Document document;
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(new iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdfPath), new ASCIIEncoding().GetBytes(""));
if (!Directory.Exists(DestinationFolder))
{
Directory.CreateDirectory(DestinationFolder);
}
else
{
DirectoryInfo di = new DirectoryInfo(DestinationFolder);
initialcount = di.GetFiles("*.pdf", SearchOption.AllDirectories).Length;
}
for (p = 1; p <= reader.NumberOfPages; p++)
{
using (MemoryStream memoryStream = new MemoryStream())
{
document = new iTextSharp.text.Document();
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, memoryStream);
writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_2);
writer.CompressionLevel = iTextSharp.text.pdf.PdfStream.BEST_COMPRESSION;
writer.SetFullCompression();
document.SetPageSize(reader.GetPageSize(p));
document.NewPage();
document.Open();
document.AddDocListener(writer);
iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
iTextSharp.text.pdf.PdfImportedPage pageImport = writer.GetImportedPage(reader, p);
int rot = reader.GetPageRotation(p);
if (rot == 90 || rot == 270)
{
cb.AddTemplate(pageImport, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(p).Height);
}
else
{
cb.AddTemplate(pageImport, 1.0F, 0, 0, 1.0F, 0, 0);
}
document.Close();
document.Dispose();
File.WriteAllBytes(DestinationFolder + "/" + p + ".pdf", memoryStream.ToArray());
}
}
reader.Close();
reader.Dispose();
}
catch
{
}
finally
{
GC.Collect();
}
if (initialcount > (p - 1))
{
for (int k = (p - 1) + 1; k <= initialcount; k++)
{
try
{
File.Delete(DestinationFolder + "/" + k + ".pdf");
}
catch
{
}
}
}
return p - 1;
}
public int ExtractPages(字符串sourcePdfPath,字符串DestinationFolder)
{
int p=0,initialcount=0;
尝试
{
iTextSharp.text.Document文件;
iTextSharp.text.pdf.PdfReader=new iTextSharp.text.pdf.PdfReader(new iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdfPath),new ascienceoding().GetBytes(“”);
如果(!Directory.Exists(DestinationFolder))
{
目录.CreateDirectory(DestinationFolder);
}
其他的
{
DirectoryInfo di=新的DirectoryInfo(DestinationFolder);
initialcount=di.GetFiles(“*.pdf”,SearchOption.AllDirectories).Length;
}
对于(p=1;p(p-1))
{
对于(int k=(p-1)+1;k首先,对于手头这样的任务,您不应该将PdfWriter
与GetImportedPage
及其直接内容与AddTemplate
一起使用。相反,请查看of
在这里,您可以找到带有中心代码的示例
PdfReader reader = new PdfReader(pdf);
// loop over all the pages in the original PDF
int n = reader.NumberOfPages;
for (int i = 0; i < n; i++)
{
using (MemoryStream ms = new MemoryStream())
{
// We'll create as many new PDFs as there are pages
using (Document document = new Document())
{
using (PdfCopy copy = new PdfCopy(document, ms))
{
document.Open();
copy.AddPage(copy.GetImportedPage(reader, i + 1));
}
}
// store ms.ToArray() somewhere
}
}
这会保留PDF元信息,因此可能更适合您的要求。但是,对于每个页面导出,PdfReader
内容都会被操纵,因此,在导出下一页时必须重新读取。这就是为什么我必须压缩切片文档的原因-不,这就是为什么您需要这样做的原因你应该扔掉你的代码,使用iText(Sharp),因为它应该被使用。例如,使用Pdf[Smart]Copy
或者甚至PdfStamper
应用到PdfReader
上,你称之为selectPages
方法。我不知道如何使用iTextSharp我只能提供Java示例(iTextSharp是iText for Java的.Net端口;我在Java方面工作)。如果可以的话,我会发布一些。是的,当然..实际上我在.Net中工作,我会参考..有人知道LZW压缩吗??
for (int i = 1; i <= TEST_FILE_PAGES; i++)
{
FileOutputStream fos = new FileOutputStream(String.format("%03d.pdf", i));
PdfReader reader = new PdfReader(TEST_FILE);
reader.selectPages(Collections.singletonList(i));
PdfStamper stamper = new PdfStamper(reader, fos);
stamper.close();
fos.close();
}