使用Itextsharp裁剪pdf的左侧 [英] crop Left side of pdf using Itextsharp
问题描述
我正在尝试将pdf的左侧裁剪为10毫米。我使用下面的代码
public void TrimLeft(string sourceFilePath,string outputFilePath)
{
I am trying to crop left side of pdf to 10 mm. i used below code public void TrimLeft(string sourceFilePath, string outputFilePath) {
PdfReader pdfReader = new PdfReader(sourceFilePath);
float width =(float) GetPDFwidth(sourceFilePath);
float height = (float)GetPDFHeight(sourceFilePath);
float widthTo_Trim = iTextSharp.text.Utilities.MillimetersToPoints(10);
// Set which part of the source document will be copied.
// PdfRectangel(bottom-left-x, bottom-left-y, upper-right-x, upper-right-y)
PdfRectangle rect = new PdfRectangle(0, 0, width - widthTo_Trim, height);
PdfRectangle rectLeftside = new PdfRectangle(0,0,width - widthTo_Trim, height);
using (var output = new FileStream(outputFilePath, FileMode.CreateNew, FileAccess.Write))
{
// Create a new document
Document doc = new Document();
// Make a copy of the document
PdfSmartCopy smartCopy = new PdfSmartCopy(doc, output);
// Open the newly created document
doc.Open();
// Loop through all pages of the source document
for (int i = 1; i <= pdfReader.NumberOfPages; i++)
{
// Get a page
var page = pdfReader.GetPageN(i);
// Apply the rectangle filter we created
page.Put(PdfName.CROPBOX, rectLeftside);
page.Put(PdfName.MEDIABOX, rectLeftside);
// Copy the content and insert into the new document
var copiedPage = smartCopy.GetImportedPage(pdfReader, i);
smartCopy.AddPage(copiedPage);
}
// Close the output document
doc.Close();
}
}
其裁剪的RHS为pdf。我尝试更改坐标
PdfRectangle rectLeftside = new PdfRectangle(0,0,width - widthTo_Trim,height);
但无法获得理想的结果。
我如何裁剪X mm左侧
Its croping RHS of pdf. i tried with changing the coordinates PdfRectangle rectLeftside = new PdfRectangle(0,0,width - widthTo_Trim, height); but unable to get desired result. How can i crop X mm left side
推荐答案
使评论中的提示成为实际答案。 ..
您可以像这样创建新的裁剪框矩形:
You create the new crop box rectangle like this:
PdfRectangle rectLeftside = new PdfRectangle(0,0,width - widthTo_Trim, height);
有问题的构造函数是:
/**
* Constructs a <CODE>PdfRectangle</CODE>-object.
*
* @param llx lower left x
* @param lly lower left y
* @param urx upper right x
* @param ury upper right y
*/
...
public PdfRectangle(float llx, float lly, float urx, float ury)
因此,假设原始PDF具有左下坐标(0,0)的裁剪框,则代码操纵右上角x,即框的右侧。另一方面,你实际上想要操纵左侧。因此,你应该使用类似的东西:
Thus, assuming your original PDF has a crop box with lower left coordinates (0,0), your code manipulates the upper right x, i.e. the right side of the box. You, on the other hand, actually want to manipulate the left side. Thus, you should use something like:
PdfRectangle rectLeftside = new PdfRectangle(widthTo_Trim, 0, width, height);
在评论中提示后,这也是OP的解决方案。
OP使用 PdfSmartCopy
实例及其方法 GetImportedPage
到裁剪pdf的左侧。 while对于此任务,这已经比使用普通的 PdfWriter
更好,操作单个PDF的最佳选择通常是 PdfStamper
:您不必再复制任何内容,只需应用更改即可。此外,结果内部更像是原始结果。
The OP uses a PdfSmartCopy
instance and its method GetImportedPage
to crop left side of pdf. While this already is better than the use of a plain PdfWriter
for this task, the best choice for manipulating a single PDF usually is a PdfStamper
: You don't have to copy anything anymore, you merely apply the changes. Furthermore the result internally is more like the original.
代码中的OP假设
- PDF中所有页面的常量大小(他在方法中确定
GetPDFwidth
和GetPDFHeight
)和 - 所有页面上当前裁剪框的常左下坐标(0,0)。
- a constant size of all pages in the PDF (which he determines in his methods
GetPDFwidth
andGetPDFHeight
) and - constant lower left coordinates (0,0) of the current crop box on all pages.
对于所有PDF,这些假设都不适用。因此,应该分别检索和操作每个页面的裁剪框。
Neither of these assumptions is true for all PDFs. Thus, one should retrieve and manipulate the crop box of each page separately.
public void TrimLeftImproved(string sourceFilePath, string outputFilePath)
{
PdfReader pdfReader = new PdfReader(sourceFilePath);
float widthTo_Trim = iTextSharp.text.Utilities.MillimetersToPoints(10);
using (FileStream output = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, output))
{
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
Rectangle cropBox = pdfReader.GetCropBox(page);
cropBox.Left += widthTo_Trim;
pdfReader.GetPageN(page).Put(PdfName.CROPBOX, new PdfRectangle(cropBox));
}
}
}
这篇关于使用Itextsharp裁剪pdf的左侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!