我在标题中使用了标签,但我认为在这种情况下需要它;)
我正在尝试使用iTextSharp使用Windows商店应用程序压缩pdf文件的所有图像。
通过Google我没有找到任何解决方案,但我找到了尝试合并的不同方法。
代码运行没有错误,但似乎没有做任何事情。
public async Task startCompressing()
{
var stream = await input.OpenAsync(FileAccessMode.ReadWrite);
PdfReader reader = new PdfReader(stream.AsStream());
Document doc = new Document();
doc.Open();
//Create our output PDF
using (MemoryStream ms = new MemoryStream())
{
//Bind a stamper to the file and our reader
using (PdfStamper stamper = new PdfStamper(reader, ms))
{
for (int i = 1; i < reader.NumberOfPages; i++)
{
PdfDictionary page = reader.GetPageN(i);
//Get the xobject structure
PdfDictionary resources = (PdfDictionary)PdfReader.GetPdfObject(page.Get(PdfName.RESOURCES));
PdfDictionary xobject = (PdfDictionary)PdfReader.GetPdfObject(resources.Get(PdfName.XOBJECT));
if (xobject != null)
{
PdfObject obj;
//Loop through each key
foreach (PdfName name in xobject.Keys)
{
obj = xobject.Get(name);
if (obj.IsIndirect())
{
//Get the current key as a PDF object
PdfDictionary imgObject = (PdfDictionary)PdfReader.GetPdfObject(obj);
//See if its an image
if (imgObject.Get(PdfName.SUBTYPE).Equals(PdfName.IMAGE))
{
//NOTE: There's a bunch of different types of filters, I'm only handing the simplest one here which is basically raw JPG, you'll have to research others
if (imgObject.Get(PdfName.FILTER).Equals(PdfName.DCTDECODE))
{
//Get the raw bytes of the current image
byte[] oldBytes = PdfReader.GetStreamBytesRaw((PRStream)imgObject);
//convert ByteStream to InMemoryRandomAccessStream to be able to create a Bitmap
var newStream = new InMemoryRandomAccessStream();
await (newStream.AsStreamForWrite()).WriteAsync(oldBytes, 0, oldBytes.Length);
await newStream.FlushAsync();
var decoder = await BitmapDecoder.CreateAsync(newStream);
var memStream = new InMemoryRandomAccessStream();
var encoder = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder);
encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant;
try
{
await encoder.FlushAsync();
}
catch (Exception err)
{
throw err;
}
//Create a new iTextSharp image from our bytes
iTextSharp.text.Image compressedImage = iTextSharp.text.Image.GetInstance(memStream.AsStream());
//Kill off the old image
PdfReader.KillIndirect(obj);
//Add our image in its place
//stamper.Writer.AddDirectImageSimple(compressedImage, (PRIndirectReference)obj);
}
}
}
}
}
}
}
}
}
如果我注释掉这一行:
stamper.Writer.AddDirectImageSimple(compressedImage, (PRIndirectReference)obj);
在我的理解中,我应该丢失所有页面上的图像。不幸的是,这不会发生。似乎没有任何反应。忽略我对流做的任何事情,如果那些正确,至少这条线应该足以杀死图像。
PdfReader.KillIndirect(obj);
由于这种情况没有发生,似乎我的代码中存在一些问题,我无法弄明白。 代码运行没有错误,执行所有行(检查jpeg也是如此)
我在Windows应用商店应用中找不到有关使用iTextSharp进行压缩的任何内容。也许你可以帮助我?
感谢您的帮助!