-
Notifications
You must be signed in to change notification settings - Fork 220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Retrieving Current CTM When Modifying Existing PDF File #296
Comments
I can answer in more details RE CTM parsing, but the point is yes - you'd need to interpret the content stream. i'm doing that in a text parser solution of mine and you are welcome to take the relevant code. here's around where i interpret the operators. as for resetting to the base coordinate system, that's easy. go: |
Thank you for your assistance. I was able to resolve the issue by using Additionally, I tried implementing the first approach you mentioned (parsing the final CTM from the content stream), and it works as well. Here's the working implementation I created: #include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "PDFParser.h"
#include "PDFDictionary.h"
#include "PDFPage.h"
#include "PDFObjectCast.h"
#include "PDFStreamInput.h"
#include "PDFObject.h"
#include "PDFReal.h"
#include "InputFile.h"
#include "ParsedPrimitiveHelper.h"
#define private public
#include "TextExtraction/lib/graphic-content-parsing/GraphicContentInterpreter.h"
#undef private
#include "TextExtraction/lib/graphic-content-parsing/IGraphicContentInterpreterHandler.h"
using namespace PDFHummus;
class DummyContentHandler : public IGraphicContentInterpreterHandler {
public:
virtual bool OnTextElementComplete(const TextElement &inTextElement) {
return true;
}
virtual bool OnPathPainted(const PathElement &inPathElement) {
return true;
}
virtual bool OnResourcesRead(const Resources &inResources, IInterpreterContext *inContext) {
return true;
}
};
class CTMTrackingInterpreter : public GraphicContentInterpreter {
public:
CTMTrackingInterpreter() {
UnitMatrix(mFinalCTM);
}
virtual ~CTMTrackingInterpreter() {}
// Captures CTM state after each operation
virtual bool OnOperation(const std::string &inOperation,
const PDFObjectVector &inOperands,
IInterpreterContext *inContext) override {
bool continueInterpret = GraphicContentInterpreter::OnOperation(inOperation, inOperands, inContext);
for (int i = 0; i < 6; i++) {
mFinalCTM[i] = CurrentGraphicState().ctm[i];
}
return continueInterpret;
}
void GetFinalCTM(double *outCTM) const {
for (int i = 0; i < 6; i++) {
outCTM[i] = mFinalCTM[i];
}
}
private:
double mFinalCTM[6];
};
int main() {
double finalCTM[6];
InputFile pdfFile;
pdfFile.OpenFile("demo.pdf");
PDFParser parser;
parser.StartPDFParsing(pdfFile.GetInputStream());
RefCountPtr<PDFDictionary> firstPage = parser.ParsePage(0);
CTMTrackingInterpreter interpreter;
DummyContentHandler handler;
bool interpretOK = interpreter.InterpretPageContents(
&parser,
firstPage.GetPtr(),
&handler
);
interpreter.GetFinalCTM(finalCTM);
std::cout << "Final CTM [ "
<< finalCTM[0] << ", "
<< finalCTM[1] << ", "
<< finalCTM[2] << ", "
<< finalCTM[3] << ", "
<< finalCTM[4] << ", "
<< finalCTM[5] << " ]" << std::endl;
return 0;
} |
Amazing :) |
First, thank you for developing and maintaining this incredibly useful library. I truly appreciate your work.
Context:
I'm working on redacting specific PDF areas by drawing rectangles over them (coordinates derived from object detection on PDF-converted images). While using PDFHummus to append rectangles, I encountered CTM-related challenges when pages modify the default coordinate system (e.g., changing origin from bottom-left to top-left via initial
cm
operations).Issue:
When modifying existing PDFs containing pre-defined CTM transformations (e.g.,
contentContext->cm(1,0,0,-1,0,792)
), subsequentre()
operations in modification contexts appear to use the default CTM rather than the active transformation matrix. This causes misaligned rectangles.Minimal Example:
create_empty.cpp
):add_rect.cpp
):Current Workaround:
I temporarily inject special text markers (e.g., "DDDDD") via PDFHummus, then parse CTM using pypdf:
Request:
Would you be so kind as to suggest a recommended approach or best practice for:
I understand PDF content streams can be complex to parse directly. Is there an API-level method to query transformation states that I might have overlooked?
Thank you very much for your time and assistance. Any insights or suggestions you can provide would be greatly appreciated.
The text was updated successfully, but these errors were encountered: