我试图提取一部分pdf (该部分的坐标将始终保持不变)使用PDF夏普。然后,我将调整该部分的大小为4“x6”,以打印在一个粘性的背标签上。我如何提取PDF的部分?这是在控制台应用程序C#中完成的。
发布于 2017-05-15 09:42:57
所以,这就是我如何做到这一点,不是一个完美的解决方案(你做了一些质量)。它使用的是Spire.PDF,而不是我最初计划的PDF。我很幸运,因为输出大小接近4“X6”。所以我就用心理医生来适应打印选项。
static void Main(string[] args)
{
ConvertPDFToBmp("FilePathOfPDF");
CropAtRect("FilePathOfConvertedImage");
ConvertToPDF("FilePathOfCroppedImage");
}
public static void ConvertPDFToBmp(string filePath)
{
PdfDocument document = new PdfDocument();
document.LoadFromFile(filePath);
Image emf = document.SaveAsImage(0, Spire.Pdf.Graphics.PdfImageType.Bitmap, 400, 400);
emf.Save("FilePath", ImageFormat.Jpeg);
}
public static void CropAtRect(string filePath)
{
Bitmap b = (Bitmap)Bitmap.FromFile(filePath);
Rectangle r = new Rectangle(new /*Where the rectangle starts*/Point(/*Width*/, /*Height*/), (new /*How big is the rectangle*/Size(/*Width*/, /*Height*/)));
Bitmap nb = new Bitmap(r.Width, r.Height);
nb.SetResolution(400, 400); //Scale to keep quality
Graphics g = Graphics.FromImage(nb);
g.DrawImage(b, -r.X, -r.Y);
nb.Save("FilePath", ImageFormat.Jpeg);
}
public static void ConvertToPDF(string filePath)
{
Bitmap b = (Bitmap)Bitmap.FromFile(filePath);
PdfDocument doc = new PdfDocument();
PdfImage pdfImage = PdfImage.FromImage(b);
PdfUnitConvertor uinit = new PdfUnitConvertor();
PdfPageBase page = doc.Pages.Add(new /*Size of PDF Page*/SizeF(585, 365), new PdfMargins(0f));
page.Canvas.DrawImage(pdfImage, new /*Where the image starts*/PointF(0, 0));
doc.SaveToFile("FilePath");
}发布于 2017-05-11 14:29:48
从PDF文件中提取部件没有简单的方法。
一个可能的解决办法:创建一个标签大小的新页面,然后将现有页绘制到新页上,以便在新页上可见所需的矩形。
如果需要,绘制白色矩形以隐藏不属于所需部分但在新页上可见的信息。
https://stackoverflow.com/questions/43917780
复制相似问题