我必须清除/删除pdf文件的左边距,以便左边距(12mm)只有透明背景。
我会尝试画一个白色的矩形,但左边距必须是透明的。
我目前的做法是,以12毫米的偏移(矩形裁剪框)开始剪切所有内容。结果是所有内容都左对齐。 有人知道如何在右边对齐吗?
感谢您的帮助。
using (FileStream stream = new FileStream(targetFile, FileMode.Create))
{
using (Document doc = new Document())
{
using (PdfSmartCopy pdfSmartCopy = new PdfSmartCopy(doc, stream))
{
doc.Open();
PdfContentByte cb = pdfSmartCopy.DirectContent;
PdfImportedPage importedPage = null;
for (int i = 0; i < m_MergeFileList.Count; i++)
{
MergeFileList mfl = m_MergeFileList[i];
PdfReader.unethicalreading = true;
using (PdfReader reader = new PdfReader(mfl.FilePath))
{
if (reader.NumberOfPages > 0)
{
List<int> lPages = CRangeParser.ParseRange(mfl.CopyPages, 1, reader.NumberOfPages);
if (lPages.Count != 0)
{
foreach (int iPage in lPages)
{
importedPage = pdfSmartCopy.GetImportedPage(reader, iPage);
if (true)
{
PdfDictionary pdfDictionary = reader.GetPageN(iPage);
PdfArray cropArray = new PdfArray();
PdfArray cropArrayOrg = new PdfArray();
Rectangle cropbox = reader.GetCropBox(iPage);
cropArray.Add(new PdfNumber(cropbox.GetLeft((72f / 25.4f) * 12.5f)));
cropArray.Add(new PdfNumber(cropbox.GetBottom(0f)));
cropArray.Add(new PdfNumber(cropbox.GetLeft((72f / 25.4f) * 12.5f) + cropbox.Width));
cropArray.Add(new PdfNumber(cropbox.GetBottom(0f) + cropbox.Height));
pdfDictionary.Put(PdfName.CROPBOX, cropArray);
pdfDictionary.Put(PdfName.MEDIABOX, cropArray); //cropArray);
//pdfDictionary.Put(PdfName.TRIMBOX, cropArray);
//pdfDictionary.Put(PdfName.BLEEDBOX, cropArray);
/*
PdfSmartCopy.PageStamp stamp = pdfSmartCopy.CreatePageStamp(importedPage);
PdfContentByte cb = stamp.GetOverContent();
cb.Rectangle(0, 0, (72f / 25.4f) * 12.5f, importedPage.Height);
cb.SetColorStroke(BaseColor.WHITE);
cb.SetColorFill(BaseColor.WHITE);
cb.FillStroke();
stamp.AlterContents();
*/
}
importedPage = pdfSmartCopy.GetImportedPage(reader, iPage);
pdfSmartCopy.AddPage(importedPage);
totalPages++;
}
}
else
{
bResult = false;
}
pdfSmartCopy.FreeReader(reader);
reader.Close();
}
else
{
bResult = false;
}
}
}
doc.Close();
}
}
}