提取 PDF 的一部分
Extracting portion of a PDF
我正在尝试使用 PDF Sharp 提取 pdf 的一部分(该部分的坐标将始终保持不变)。然后,我将把该部分的大小调整为 4" x 6",以便打印到背面粘性标签上。我将如何提取PDF的一部分?这是在控制台应用程序中,C#。
没有简单的方法可以从 PDF 文件中提取部分内容。
可能的解决方法:创建一个标签大小的新页面,然后将现有页面绘制到新页面上,以便在新页面上可以看到所需的矩形。
如果需要,绘制白色矩形以隐藏不属于您需要的部分但在新页面上可见的信息。
这就是我设法做到这一点的方法,不是一个完美的解决方案(你确实失去了一些质量)。这使用 Spire.PDF,而不是我最初计划的 PDF Sharp。我相当幸运,因为输出尺寸接近 4" X 6"。所以我只是使用收缩来适应打印选项。
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");
}
我正在尝试使用 PDF Sharp 提取 pdf 的一部分(该部分的坐标将始终保持不变)。然后,我将把该部分的大小调整为 4" x 6",以便打印到背面粘性标签上。我将如何提取PDF的一部分?这是在控制台应用程序中,C#。
没有简单的方法可以从 PDF 文件中提取部分内容。
可能的解决方法:创建一个标签大小的新页面,然后将现有页面绘制到新页面上,以便在新页面上可以看到所需的矩形。
如果需要,绘制白色矩形以隐藏不属于您需要的部分但在新页面上可见的信息。
这就是我设法做到这一点的方法,不是一个完美的解决方案(你确实失去了一些质量)。这使用 Spire.PDF,而不是我最初计划的 PDF Sharp。我相当幸运,因为输出尺寸接近 4" X 6"。所以我只是使用收缩来适应打印选项。
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");
}